Search in sources :

Example 1 with BaseModel

use of com.alibaba.otter.canal.admin.model.BaseModel in project canal by alibaba.

the class WebConfig method addInterceptors.

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new HandlerInterceptor() {

        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Token");
            httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpServletResponse.setHeader("Access-Control-Max-Age", String.valueOf(3600 * 24));
            if (HttpMethod.OPTIONS.toString().equals(httpServletRequest.getMethod())) {
                httpServletResponse.setStatus(HttpStatus.NO_CONTENT.value());
                return false;
            }
            return true;
        }
    }).addPathPatterns("/api/**");
    registry.addInterceptor(new HandlerInterceptor() {

        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            String token = httpServletRequest.getHeader("X-Token");
            boolean valid = false;
            if (token != null) {
                User user = UserController.loginUsers.getIfPresent(token);
                if (user != null) {
                    valid = true;
                    httpServletRequest.setAttribute("user", user);
                    httpServletRequest.setAttribute("token", token);
                }
            }
            if (!valid) {
                BaseModel baseModel = BaseModel.getInstance(null);
                baseModel.setCode(50014);
                baseModel.setMessage("Expired token");
                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(baseModel);
                try {
                    httpServletResponse.setContentType("application/json;charset=UTF-8");
                    PrintWriter out = httpServletResponse.getWriter();
                    out.print(json);
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
                return false;
            }
            return true;
        }
    }).addPathPatterns("/api/**").excludePathPatterns("/api/**/config/server_polling").excludePathPatterns("/api/**/config/instances_polling").excludePathPatterns("/api/**/config/instance_polling/**").excludePathPatterns("/api/**/user/login").excludePathPatterns("/api/**/user/logout").excludePathPatterns("/api/**/user/info");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.alibaba.otter.canal.admin.model.User) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) BaseModel(com.alibaba.otter.canal.admin.model.BaseModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter)

Example 2 with BaseModel

use of com.alibaba.otter.canal.admin.model.BaseModel in project canal by alibaba.

the class CustomExceptionHandler method commonExceptionHandle.

/**
 * 通用异常处理
 *
 * @param e 异常
 * @return
 */
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(value = Exception.class)
public BaseModel commonExceptionHandle(Exception e) {
    if (e instanceof ServiceException) {
        logger.error(e.getMessage());
    } else {
        logger.error(e.getMessage(), e);
    }
    BaseModel res = new BaseModel();
    res.setCode(50000);
    res.setMessage(e.getMessage());
    return res;
}
Also used : ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) BaseModel(com.alibaba.otter.canal.admin.model.BaseModel) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

BaseModel (com.alibaba.otter.canal.admin.model.BaseModel)2 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)1 User (com.alibaba.otter.canal.admin.model.User)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PrintWriter (java.io.PrintWriter)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)1