@Override
  protected void doFilterInternal(
      HttpServletRequest request, HttpServletResponse response, FilterChain chain)
      throws ServletException, IOException {

    long start = System.currentTimeMillis();
    try {
      log.info(
          "Request: {} {}",
          request.getMethod(),
          request.getQueryString() == null
              ? request.getRequestURI()
              : request.getRequestURI() + "?" + request.getQueryString());
      log.info("Remote host: {} {}", request.getRemoteAddr(), request.getHeader("X-Forwarded-For"));
      log.info("User-Agent: {}", request.getHeader("User-Agent"));

      chain.doFilter(request, response);

    } finally {
      HttpStatus status = HttpStatus.valueOf(response.getStatus());
      log.info(
          "{} {}, in {}ms",
          status.value(),
          status.getReasonPhrase(),
          System.currentTimeMillis() - start);
    }
  }
 private String getExceptionMessage(Throwable throwable, Integer statusCode) {
   if (throwable != null) {
     return Throwables.getRootCause(throwable).getMessage();
   }
   logger.info("statusCode : " + statusCode);
   if (statusCode != null) {
     HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
     return httpStatus.getReasonPhrase();
   } else {
     return "";
   }
 }
 @RequestMapping
 public ResponseEntity<ApiError> handleErrorPage(
     @RequestParam("errorCode") String errorCode, WebRequest request) {
   HttpStatus httpStatus =
       HttpStatus.valueOf(
           (Integer)
               request.getAttribute(
                   RequestDispatcher.ERROR_STATUS_CODE, RequestAttributes.SCOPE_REQUEST));
   ApiError apiError =
       apiErrorCreator.createApiError(request, errorCode, httpStatus.getReasonPhrase());
   return new ResponseEntity<>(apiError, httpStatus);
 }
  public GoogleSearchResponse search(String searchString, String referer) {
    try {
      if (ValidatorUtil.isNull(referer)) {
        referer = "http://longfalcon.net";
      }

      String v = "1.0";
      String userip = "192.168.0.1";
      HttpHeaders httpHeaders = new HttpHeaders();
      httpHeaders.set("Referer", referer);

      HttpEntity<?> requestEntity = new HttpEntity(httpHeaders);

      UriComponents uriComponents =
          UriComponentsBuilder.fromUriString(_SEARCH_URL)
              .queryParam("v", v)
              .queryParam("q", searchString)
              .queryParam("userip", userip)
              .build();
      ResponseEntity<GoogleSearchResponse> responseEntity =
          restTemplate.exchange(
              uriComponents.toUri(), HttpMethod.GET, requestEntity, GoogleSearchResponse.class);
      HttpStatus statusCode = responseEntity.getStatusCode();
      if (statusCode.is2xxSuccessful() || statusCode.is3xxRedirection()) {
        return responseEntity.getBody();
      } else {
        _log.error(
            String.format(
                "Search request: \n%s\n failed with HTTP code %s : %s",
                uriComponents.toString(), statusCode.toString(), statusCode.getReasonPhrase()));
        return null;
      }
    } catch (Exception e) {
      _log.error(e);
    }

    return null;
  }
예제 #5
0
  @RequestMapping("/error")
  public String customError(HttpServletRequest request, HttpServletResponse response, Model model) {
    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

    HttpStatus httpStatus = HttpStatus.valueOf(statusCode);

    Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");

    String exceptionMessage = null;
    exceptionMessage = httpStatus.getReasonPhrase();

    String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");

    if (requestUri == null) {
      requestUri = "Unknown";
    }

    String message =
        MessageFormat.format("{0} returned for {1}: {2}", statusCode, requestUri, exceptionMessage);

    model.addAttribute("errorMessage", message);
    return "customError";
  }
예제 #6
0
 public HttpResponseMatcher(HttpStatus status) {
   this.content("HTTP/1.1 " + status.value() + " " + status.getReasonPhrase());
   this.content("");
 }
 private static void logAndSendErrorResponse(
     HttpServletResponse response, HttpStatus status, Exception ex) throws IOException {
   logAndSendErrorResponse(response, status, status.getReasonPhrase(), ex);
 }