|
|
@@ -0,0 +1,125 @@
|
|
|
+package com.zhiqiyun.open.core.video.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.zhiqiyun.open.core.video.VideoDockingService;
|
|
|
+import com.zhiqiyun.open.exception.ServiceException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.FormBody;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.rmi.ServerException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author xin yang
|
|
|
+ * @date 2022/8/4
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class VideoDockingServiceImpl implements VideoDockingService {
|
|
|
+
|
|
|
+ private final static String USER_NAME = "";
|
|
|
+ private final static String PASSWORD = "";
|
|
|
+ private final static String URI = "";
|
|
|
+ private final static String RESULT_CODE = "resultCode";
|
|
|
+
|
|
|
+ private final static String VIDEO_JSESSIONID_TOKEN = "VIDEO_JSESSIONID_TOKEN";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OkHttpClient okHttpClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getValidToken() throws IOException {
|
|
|
+ BoundValueOperations<String, String> boundValueOperations = this.redisTemplate.boundValueOps(VIDEO_JSESSIONID_TOKEN);
|
|
|
+ String token = boundValueOperations.get();
|
|
|
+ if (StringUtils.isBlank(token)) {
|
|
|
+ Map<String, String> paramValues = new HashMap<>(2);
|
|
|
+ paramValues.put("userName", USER_NAME);
|
|
|
+ paramValues.put("password", PASSWORD);
|
|
|
+
|
|
|
+ Response response = okHttpPost("/loginInfo/login/v1.0", paramValues);
|
|
|
+ analysisRequestSuccess(response);
|
|
|
+ token = response.header("Set-Cookie");
|
|
|
+ log.info("用户登录TOKEN:{}", token);
|
|
|
+ if (StringUtils.isNotBlank(token)) {
|
|
|
+ boundValueOperations.set(token, 30, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshValidToken() throws IOException {
|
|
|
+ Map<String, String> paramValues = new HashMap<>(1);
|
|
|
+ paramValues.put("Cookie", getValidToken());
|
|
|
+ Response response = okHttpPost("/common/keepAlive", paramValues);
|
|
|
+ analysisRequestSuccess(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一POST请求方法
|
|
|
+ *
|
|
|
+ * @param uri uri
|
|
|
+ * @param paramValues paramValues
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ private Response okHttpPost(String uri, Map<String, String> paramValues) throws IOException {
|
|
|
+ String cookie = paramValues.get("Cookie");
|
|
|
+ if (StringUtils.isNotBlank(cookie)) {
|
|
|
+ paramValues.remove("Cookie");
|
|
|
+ }
|
|
|
+ FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
+ paramValues.forEach(formBuilder::add);
|
|
|
+
|
|
|
+ Request.Builder builder = new Request.Builder();
|
|
|
+ builder.url(String.format("%s" + uri, URI));
|
|
|
+ builder.header("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ if (StringUtils.isNotBlank(cookie)) {
|
|
|
+ builder.header("Cookie", cookie);
|
|
|
+ }
|
|
|
+ builder.post(formBuilder.build());
|
|
|
+ Response response = this.okHttpClient.newCall(builder.build()).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ return response;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ String result = Objects.requireNonNull(response.body()).string();
|
|
|
+ log.error(result);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+ throw new ServiceException(jsonObject.getString("error_description"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("okHttpPost error:" + e.getMessage(), e);
|
|
|
+ throw new ServiceException("网络异常" + response.message());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析请求是否成功
|
|
|
+ *
|
|
|
+ * @param response response
|
|
|
+ */
|
|
|
+ private void analysisRequestSuccess(Response response) throws IOException {
|
|
|
+ String result = Objects.requireNonNull(response.body()).string();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+ if (0 != jsonObject.getIntValue(RESULT_CODE)) {
|
|
|
+ throw new ServerException(jsonObject.getString(""));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|