Example usage for org.springframework.web.context.request ServletWebRequest getResponse

List of usage examples for org.springframework.web.context.request ServletWebRequest getResponse

Introduction

In this page you can find the example usage for org.springframework.web.context.request ServletWebRequest getResponse.

Prototype

@Nullable
public final HttpServletResponse getResponse() 

Source Link

Document

Exposes the native HttpServletResponse that we're wrapping (if any).

Usage

From source file:org.cloudfoundry.identity.uaa.oauth.UaaAuthorizationEndpoint.java

private ModelAndView handleException(Exception e, ServletWebRequest webRequest) throws Exception {

    ResponseEntity<OAuth2Exception> translate = getExceptionTranslator().translate(e);
    webRequest.getResponse().setStatus(translate.getStatusCode().value());

    if (e instanceof ClientAuthenticationException || e instanceof RedirectMismatchException) {
        Map<String, Object> map = new HashMap<>();
        map.put("error", translate.getBody());
        if (e instanceof UnauthorizedClientException) {
            map.put("error_message_code", "login.invalid_idp");
        }/*w  ww.  j a  va2 s. c om*/
        return new ModelAndView(errorPage, map);
    }

    AuthorizationRequest authorizationRequest = null;
    try {
        authorizationRequest = getAuthorizationRequestForError(webRequest);
        String requestedRedirectParam = authorizationRequest.getRequestParameters()
                .get(OAuth2Utils.REDIRECT_URI);
        String requestedRedirect = redirectResolver.resolveRedirect(requestedRedirectParam,
                getClientServiceExtention().loadClientByClientId(authorizationRequest.getClientId(),
                        IdentityZoneHolder.get().getId()));
        authorizationRequest.setRedirectUri(requestedRedirect);
        String redirect = getUnsuccessfulRedirect(authorizationRequest, translate.getBody(),
                authorizationRequest.getResponseTypes().contains("token"));
        return new ModelAndView(new RedirectView(redirect, false, true, false));
    } catch (OAuth2Exception ex) {
        // If an AuthorizationRequest cannot be created from the incoming parameters it must be
        // an error. OAuth2Exception can be handled this way. Other exceptions will generate a standard 500
        // response.
        return new ModelAndView(errorPage, Collections.singletonMap("error", translate.getBody()));
    }

}

From source file:com.revolsys.ui.web.rest.interceptor.WebAnnotationMethodHandlerAdapter.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public ModelAndView getModelAndView(final Method handlerMethod, final Class<?> handlerType,
        final Object returnValue, final ExtendedModelMap implicitModel, final ServletWebRequest webRequest)
        throws Exception {
    boolean responseArgumentUsed = false;
    final ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod,
            ResponseStatus.class);
    if (responseStatusAnn != null) {
        final HttpStatus responseStatus = responseStatusAnn.value();
        // to be picked up by the RedirectView
        webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, responseStatus);
        webRequest.getResponse().setStatus(responseStatus.value());
        responseArgumentUsed = true;/*from   w w w  .j a v a2  s . c om*/
    }

    // Invoke custom resolvers if present...
    if (WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers != null) {
        for (final ModelAndViewResolver mavResolver : WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers) {
            final ModelAndView mav = mavResolver.resolveModelAndView(handlerMethod, handlerType, returnValue,
                    implicitModel, webRequest);
            if (mav != ModelAndViewResolver.UNRESOLVED) {
                return mav;
            }
        }
    }

    if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) {
        final View view = handleResponseBody(returnValue, webRequest);
        return new ModelAndView(view).addAllObjects(implicitModel);
    }

    if (returnValue instanceof ModelAndView) {
        final ModelAndView mav = (ModelAndView) returnValue;
        mav.getModelMap().mergeAttributes(implicitModel);
        return mav;
    } else if (returnValue instanceof Model) {
        return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
    } else if (returnValue instanceof View) {
        return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
    } else if (AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class) != null) {
        addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel);
        return new ModelAndView().addAllObjects(implicitModel);
    } else if (returnValue instanceof Map) {
        return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
    } else if (returnValue instanceof String) {
        return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
    } else if (returnValue == null) {
        // Either returned null or was 'void' return.
        if (responseArgumentUsed || webRequest.isNotModified()) {
            return null;
        } else {
            // Assuming view name translation...
            return new ModelAndView().addAllObjects(implicitModel);
        }
    } else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
        // Assume a single model attribute...
        addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel);
        return new ModelAndView().addAllObjects(implicitModel);
    } else {
        throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
    }
}

From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java

private ModelAndView handleException(Exception e, ServletWebRequest webRequest) throws Exception {

    ResponseEntity<OAuth2Exception> translate = getExceptionTranslator().translate(e);
    webRequest.getResponse().setStatus(translate.getStatusCode().value());

    if (e instanceof ClientAuthenticationException || e instanceof RedirectMismatchException) {
        return new ModelAndView(errorPage, Collections.singletonMap("error", translate.getBody()));
    }/*from  w  ww  . j  av a2 s  .c  om*/

    AuthorizationRequest authorizationRequest = null;
    try {
        authorizationRequest = getAuthorizationRequestForError(webRequest);
        String requestedRedirectParam = authorizationRequest.getRequestParameters()
                .get(OAuth2Utils.REDIRECT_URI);
        String requestedRedirect = redirectResolver.resolveRedirect(requestedRedirectParam,
                getClientDetailsService().loadClientByClientId(authorizationRequest.getClientId()));
        authorizationRequest.setRedirectUri(requestedRedirect);
        String redirect = getUnsuccessfulRedirect(authorizationRequest, translate.getBody(),
                authorizationRequest.getResponseTypes().contains("token"));
        return new ModelAndView(new RedirectView(redirect, false, true, false));
    } catch (OAuth2Exception ex) {
        // If an AuthorizationRequest cannot be created from the incoming parameters it must be
        // an error. OAuth2Exception can be handled this way. Other exceptions will generate a standard 500
        // response.
        return new ModelAndView(errorPage, Collections.singletonMap("error", translate.getBody()));
    }

}

From source file:org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.java

/**
 * Customize the response for NoHandlerFoundException.
 * <p>This method delegates to {@link #handleExceptionInternal}.
 * @param ex the exception//from  w w w  .ja v a  2s.c o m
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param webRequest the current request
 * @return a {@code ResponseEntity} instance
 * @since 4.2.8
 */
@Nullable
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex,
        HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

    if (webRequest instanceof ServletWebRequest) {
        ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
        HttpServletRequest request = servletWebRequest.getRequest();
        HttpServletResponse response = servletWebRequest.getResponse();
        if (response != null && response.isCommitted()) {
            if (logger.isErrorEnabled()) {
                logger.error("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
            }
            return null;
        }
    }

    return handleExceptionInternal(ex, null, headers, status, webRequest);
}