Example usage for javax.servlet.http HttpServletResponse getStatus

List of usage examples for javax.servlet.http HttpServletResponse getStatus

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getStatus.

Prototype

public int getStatus();

Source Link

Document

Gets the current status code of this response.

Usage

From source file:nu.yona.server.rest.ErrorLoggingFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    try (LoggingContext loggingContext = LoggingContext.createInstance(request)) {
        chain.doFilter(request, response);
        Series responseStatus = HttpStatus.Series.valueOf(response.getStatus());
        if (responseStatus == Series.SUCCESSFUL) {
            return;
        }/* ww  w  . jav  a 2s .  c o m*/

        logResponseStatus(request, response, responseStatus);
    }
}

From source file:business.security.CustomLoggingInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView model) throws Exception {
    log.trace(String.format("%s\t%s\t%s\t%s\t%s\t%d", new Date(), request.getRemoteAddr(),
            request.getUserPrincipal() == null ? " - " : request.getUserPrincipal().getName(),
            request.getMethod(), request.getRequestURI(), response.getStatus()));
    super.postHandle(request, response, handler, model);
}

From source file:org.springframework.cloud.sleuth.instrument.web.TraceFilter.java

private boolean httpStatusSuccessful(HttpServletResponse response) {
    if (response.getStatus() == 0) {
        return false;
    }/*from w  ww .  j  av  a2 s  . c o  m*/
    HttpStatus.Series httpStatusSeries = HttpStatus.Series.valueOf(response.getStatus());
    return httpStatusSeries == HttpStatus.Series.SUCCESSFUL
            || httpStatusSeries == HttpStatus.Series.REDIRECTION;
}

From source file:org.kmnet.com.fw.web.exception.HandlerExceptionResolverLoggingInterceptor.java

/**
 * Outputs the log// www.  j  a  v  a2s  .c  o  m
 * @param ex Exception
 * @param request HTTP Servlet Request
 * @param response HTTP Servlet Response
 * @param handler handler
 */
protected void log(Exception ex, HttpServletRequest request, HttpServletResponse response, Object handler) {
    int statusCode = response.getStatus();
    if (HttpServletResponse.SC_INTERNAL_SERVER_ERROR <= statusCode) {
        // responseCode is 500 ~
        logServerError(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_BAD_REQUEST <= statusCode) {
        // 400 ~ 499
        logClientError(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_MULTIPLE_CHOICES <= statusCode) {
        // 300 ~ 399
        logRedirection(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_OK <= statusCode) {
        // 200 ~ 299
        logSuccess(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_CONTINUE <= statusCode) {
        // 100 ~ 199
        logInformational(ex, request, response, handler);
        return;
    }

}

From source file:com.ge.predix.acs.commons.web.RestErrorHandlerTest.java

@Test
public void testException() {
    RestErrorHandler errorHandler = new RestErrorHandler();

    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    Exception e = new Exception("Descriptive Error Message");

    ModelAndView errorResponse = errorHandler.createApiErrorResponse(e, request, response);

    // The default error status code is 500
    Assert.assertEquals(response.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR.value());

    // Not null error response containing an ErrorDetails model
    Assert.assertNotNull(errorResponse);
    Assert.assertNotNull(errorResponse.getModel().get("ErrorDetails"));

    // Default response payload error code and message
    assertRestApiErrorResponse(errorResponse, "FAILED", "Operation Failed");

}

From source file:org.jitsi.rest.AbstractJSONHandler.java

/**
 * Ends an {@link HttpServletResponse}.//from   www.j a  v a 2 s  . co  m
 *
 * @param target the target of the request
 * @param baseRequest the original unwrapped {@link Request} object
 * @param request the request either as the {@code Request} object or a
 * wrapper of that request
 * @param response the response either as the {@code Response} object or a
 * wrapper of that response
 */
protected void endResponse(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) {
    if (!baseRequest.isHandled()) {
        if (response.getStatus() == 0)
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        baseRequest.setHandled(true);
    }
}

From source file:nu.yona.server.rest.ErrorLoggingFilter.java

private void logResponseStatus(HttpServletRequest request, HttpServletResponse response,
        Series responseStatus) {/*from www . j a va  2  s  .com*/
    LogMethod logMethod = seriesToLoggerMap.get(responseStatus);
    if (logMethod == null) {
        throw new IllegalStateException("Status " + responseStatus + " is not supported");
    }
    logMethod.log("Status {} returned from {}", response.getStatus(),
            GlobalExceptionMapping.buildRequestInfo(request));
}

From source file:org.springframework.cloud.sleuth.instrument.web.TraceFilter.java

/** Override to add annotations not defined in {@link TraceKeys}. */
protected void addResponseTags(HttpServletResponse response, Throwable e) {
    int httpStatus = response.getStatus();
    if (httpStatus == HttpServletResponse.SC_OK && e != null) {
        // Filter chain threw exception but the response status may not have been set
        // yet, so we have to guess.
        this.tracer.addTag(this.traceKeys.getHttp().getStatusCode(),
                String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }//from  w w  w .  ja  va  2  s  .  c  o m
    // only tag valid http statuses
    else if (httpStatus >= 100 && (httpStatus < 200) || (httpStatus > 399)) {
        this.tracer.addTag(this.traceKeys.getHttp().getStatusCode(), String.valueOf(response.getStatus()));
    }
}

From source file:com.ge.predix.acs.commons.web.RestErrorHandlerTest.java

@Test
public void testIllegalArgumentException() {
    RestErrorHandler errorHandler = new RestErrorHandler();

    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    Exception e = new IllegalArgumentException("Descriptive Error Message");

    ModelAndView errorResponse = errorHandler.createApiErrorResponse(e, request, response);

    // The default error status code for IllegalArgumentException is 400
    Assert.assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST.value());

    Assert.assertNotNull(errorResponse);
    Assert.assertNotNull(errorResponse.getModel().get("ErrorDetails"));

    // Response payload with default error code and the
    // IllegalArgumentException's message
    assertRestApiErrorResponse(errorResponse, "FAILED", "Descriptive Error Message");
}

From source file:org.smigo.config.ErrorViewExceptionResolver.java

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {//from  w ww.  j a v a  2  s.  c o m
    log.info("Invalidating session and removing persistent token:" + request.getUserPrincipal());
    try {
        request.logout();
    } catch (ServletException e) {
        log.error("Could not logout user " + request.getUserPrincipal(), e);
    }
    return new ModelAndView("error.jsp", "statusCode", response.getStatus());
}