panlinlin
2021-02-25 0f58218badea86a5209ae7f1ccd60b7cb4b26eee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.genersoft.iot.vmp.media.zlm;
 
import com.genersoft.iot.vmp.conf.MediaServerConfig;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
// import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@RestController
@RequestMapping("/zlm")
public class ZLMHTTPProxyController {
 
 
    // private final static Logger logger = LoggerFactory.getLogger(ZLMHTTPProxyController.class);
 
    // @Autowired
    // private IVideoManagerStorager storager;
 
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
 
    @Value("${media.port}")
    private int mediaHttpPort;
 
    @ResponseBody
    @RequestMapping(value = "/**/**/**", produces = "application/json;charset=UTF-8")
    public Object proxy(HttpServletRequest request, HttpServletResponse response){
 
        if (redisCatchStorage.getMediaInfo() == null) {
            return "未接入流媒体";
        }
        MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
        String requestURI = String.format("http://%s:%s%s?%s&%s",
                mediaInfo.getLocalIP(),
                mediaHttpPort,
                request.getRequestURI().replace("/zlm",""),
                mediaInfo.getHookAdminParams(),
                request.getQueryString()
        );
        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        //将指定的url返回的参数自动封装到自定义好的对应类对象中
        Object result = null;
        try {
            result = restTemplate.getForObject(requestURI,Object.class);
 
        }catch (HttpClientErrorException httpClientErrorException) {
            response.setStatus(httpClientErrorException.getStatusCode().value());
        }
        return result;
    }
}