|
|
|
@ -19,6 +19,7 @@ import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
import org.springframework.web.servlet.view.RedirectView;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -34,7 +35,7 @@ import static com.by.api.constants.RequestInfoConstant.CUSTOMER_IP_ADDR_KEY;
|
|
|
|
|
public class ShortServerOpenApiController {
|
|
|
|
|
|
|
|
|
|
// TODO: 2021/4/14 0014 添加接口限流
|
|
|
|
|
// TODO: 2021/4/14 0014 添加接口相关的验证 丰富VO层 加入 appid secret_key 及 时间戳验证
|
|
|
|
|
// TODO: 2021/4/14 0014 添加接口相关的验证 丰富VO层 加入 appId secret_key 及 时间戳验证
|
|
|
|
|
|
|
|
|
|
private final ShortServerService shortServerService;
|
|
|
|
|
|
|
|
|
@ -48,24 +49,28 @@ public class ShortServerOpenApiController {
|
|
|
|
|
@ApiOperation("批量生成短链接")
|
|
|
|
|
@PostMapping(value = "/trans")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CommonResponse parseContentToShortChain(@Validated @RequestBody ShortChainRequestVO shortChainRequestVO, HttpServletRequest request) {
|
|
|
|
|
public CommonResponse<ShortChainResponseVO> parseContentToShortChain(@Validated @RequestBody ShortChainRequestVO shortChainRequestVO, HttpServletRequest request) {
|
|
|
|
|
log.info("=== [ShortServerOpenApiController|parseContentToShortChain, one request is coming, request vo is {} ] ===", shortChainRequestVO.toString());
|
|
|
|
|
|
|
|
|
|
ShortChainDTO shortChainDTO = ShortChainVOToDTOConvert.convertShortChainRequestVOToDTO(shortChainRequestVO);
|
|
|
|
|
|
|
|
|
|
// 拿到远程请求客户端的Ip信息
|
|
|
|
|
String remoteAddr = request.getRemoteAddr();
|
|
|
|
|
if (StringUtils.isNotBlank(remoteAddr)){
|
|
|
|
|
if (StringUtils.isNotBlank(remoteAddr)) {
|
|
|
|
|
ThreadLocalUtil.set(CUSTOMER_IP_ADDR_KEY, remoteAddr);
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
ThreadLocalUtil.set(CUSTOMER_IP_ADDR_KEY, SystemConstant.DEFAULT_IP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shortChainDTO = shortServerService.handleOriginUrlsToShortUrls(shortChainDTO);
|
|
|
|
|
// 参数不存在的时候,给一个标识,来缩短短链
|
|
|
|
|
if (CollectionUtil.isEmpty(shortChainRequestVO.getVariableList())){
|
|
|
|
|
shortChainDTO = shortServerService.handleOriginUrlsToShortUrls(shortChainDTO, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> shortChainResult = shortChainDTO.getShortChainResult();
|
|
|
|
|
ShortChainResponseVO shortChainResponseVO = new ShortChainResponseVO();
|
|
|
|
|
if (CollectionUtil.isNotEmpty(shortChainResult)){
|
|
|
|
|
if (CollectionUtil.isNotEmpty(shortChainResult)) {
|
|
|
|
|
shortChainResponseVO.setShortChainResult(shortChainResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -74,19 +79,36 @@ public class ShortServerOpenApiController {
|
|
|
|
|
|
|
|
|
|
@ApiOperation("短链接兑换长链接并进行")
|
|
|
|
|
@GetMapping(value = "/s/{redeem}")
|
|
|
|
|
public ModelAndView redeemShortChainClick(@PathVariable("redeem") String redeem){
|
|
|
|
|
@CrossOrigin
|
|
|
|
|
public ModelAndView redeemShortChainClick(@PathVariable("redeem") String redeem) {
|
|
|
|
|
log.info("=== [ShortServerOpenApiController|redeemShortChainClick, one request is coming, request param is {} ] ===", redeem);
|
|
|
|
|
|
|
|
|
|
ShortUrl shortUrl = shortServerService.handleOnceShortUrlToRedirectOriginUrlAndRecord(redeem);
|
|
|
|
|
|
|
|
|
|
String originUrl = "";
|
|
|
|
|
if (!Objects.isNull(shortUrl)){
|
|
|
|
|
String originUrl = "";
|
|
|
|
|
if (!Objects.isNull(shortUrl)) {
|
|
|
|
|
|
|
|
|
|
// 异步进行更新数据库中的点击记录及推送给之前的调用方(可以走消息)
|
|
|
|
|
shortChainTask.doRunTask(shortUrl);
|
|
|
|
|
originUrl = StringUtils.substringBefore(shortUrl.getOriginUrl(), SymbolConstant.SPLIT_VERTICAL);
|
|
|
|
|
originUrl = shortUrl.getOriginUrl();
|
|
|
|
|
|
|
|
|
|
// 增加如果是不带参数的url就不调用task进行分发
|
|
|
|
|
if (StringUtils.contains(originUrl, SymbolConstant.SPLIT_VERTICAL)){
|
|
|
|
|
shortChainTask.doRunTask(shortUrl);
|
|
|
|
|
}
|
|
|
|
|
originUrl = StringUtils.substringBefore(originUrl, SymbolConstant.SPLIT_VERTICAL);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ModelAndView("redirect:" + originUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("用于测试转发")
|
|
|
|
|
@GetMapping(value = "/t/{redeem}")
|
|
|
|
|
@CrossOrigin
|
|
|
|
|
@Deprecated
|
|
|
|
|
public ModelAndView testRedirect(@PathVariable("redeem") String redeem) {
|
|
|
|
|
System.out.println(redeem);
|
|
|
|
|
return new ModelAndView(new RedirectView("http://www.baidu.com"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|