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:com.scf.web.context.DefaultWebContext.java

@Override
protected HttpServletResponse getHttpServletResponse() {
    ServletWebRequest servletWebRequest = new ServletWebRequest(getHttpServletRequest());
    HttpServletResponse response = servletWebRequest.getResponse();
    return response;
}

From source file:com.epam.ta.reportportal.commons.exception.rest.RestExceptionHandler.java

private void applyStatusIfPossible(ServletWebRequest webRequest, HttpStatus status) {
    if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
        webRequest.getResponse().setStatus(status.value());
    }/*  w w w. j av  a 2s .  c o  m*/
}

From source file:it.unitn.disi.smatch.web.server.api.handlers.ExceptionDetailsExceptionResolver.java

private void applyStatusIfPossible(ServletWebRequest webRequest, ExceptionDetails error) {
    if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
        webRequest.getResponse().setStatus(error.getStatus());
    }//from   ww w.  ja va2  s .  c  o m
}

From source file:org.ayfaar.app.spring.handler.RestExceptionHandler.java

/**
 * Actually resolve the given exception that got thrown during on handler execution, returning a ModelAndView that
 * represents a specific error page if appropriate.
 * <p/>/*from  w w  w . ja  v a 2 s . c o m*/
 * May be overridden in subclasses, in order to apply specific
 * exception checks. Note that this template method will be invoked <i>after</i> checking whether this resolved applies
 * ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.
 *
 * @param request  current HTTP request
 * @param response current HTTP response
 * @param handler  the executed handler, or <code>null</code> if none chosen at the time of the exception (for example,
 *                 if multipart resolution failed)
 * @param ex       the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to, or <code>null</code> for default processing
 */
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) {

    ServletWebRequest webRequest = new ServletWebRequest(request, response);

    //        if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
    webRequest.getResponse().setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    //        }

    RestErrorResolver resolver = getErrorResolver();

    BusinessError error = resolver.resolveError(webRequest, handler, ex);
    if (error == null) {
        return null;
    }

    ModelAndView mav = null;

    try {
        mav = handleResponseBody(new ErrorResponse(error), webRequest);
    } catch (Exception invocationEx) {
        log.error("Acquiring ModelAndView for Exception [" + ex + "] resulted in an exception.", invocationEx);
    }

    return mav;
}

From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityConnectInterceptor.java

/**
 * This callback 1)  Ensures that 2 different local users
 * cannot share the same 3rd party connection 2) Updates the current
 * user's authentication if the set of roles they are assigned
 * needs to change now that this connection has been made.
 * 3) Looks for a request previously saved by an access denied
 * handler, and if present, sets the url of this original
 * pre-authorisation request as a session attribute
 * //from   ww w  .j  av  a 2 s.c o m
 */
@Override
public void postConnect(Connection<S> connection, WebRequest webRequest) {

    super.postConnect(connection, webRequest);

    /**
     * User roles are generated according to connected
     * providers in spring-social-security
     * 
     * Now that this connection has been made,
     * doe we need to update the user roles?
     * 
     * If so, update the current user's authentication and update
     * remember-me services accordingly.
     */
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    Collection<? extends GrantedAuthority> existingAuthorities = authentication.getAuthorities();

    GrantedAuthority newAuthority = userAuthoritiesService.getProviderAuthority(connection.getKey());

    if (!existingAuthorities.contains(newAuthority)) {

        Authentication newAuthentication = authenticationFactory
                .updateAuthenticationForNewConnection(authentication, connection);
        SecurityContextHolder.getContext().setAuthentication(newAuthentication);

        if (rememberMeServices != null && webRequest instanceof ServletWebRequest) {

            ServletWebRequest servletWebRequest = ((ServletWebRequest) webRequest);
            rememberMeServices.loginSuccess(servletWebRequest.getRequest(), servletWebRequest.getResponse(),
                    newAuthentication);
        }
    }

    /**
     * This connection may have been instigated by an 
     * access denied handler which may have saved the
     * original request made by the user before their access
     * was denied.  
     * 
     * Spring Social sends the user to a particular view
     * on completion of connection.  We may wish to offer the
     * user a "continue" link on this view, allowing their
     * original request (if saved by the access denied handler)
     * to be re-attempted
     *
     */
    if (webRequest instanceof ServletWebRequest) {
        ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
        SavedRequest savedRequest = requestCache.getRequest(servletWebRequest.getRequest(),
                servletWebRequest.getResponse());
        if (savedRequest != null) {
            String redirectUrl = savedRequest.getRedirectUrl();
            if (redirectUrl != null && savedRequest.getMethod().equalsIgnoreCase("get")) {
                servletWebRequest.setAttribute(SAVED_REQUEST_URL_ATTRIBUTE_NAME, savedRequest.getRedirectUrl(),
                        RequestAttributes.SCOPE_SESSION);
            }
        }
    }
}

From source file:com.novation.eligibility.rest.spring.web.servlet.handler.RestExceptionHandler.java

private void applyStatusIfPossible(ServletWebRequest webRequest, RestError error) {
    if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
        webRequest.getResponse().setStatus(error.getStatus().value());
    }/* w  w  w.  j  ava 2  s.c o m*/
    //TODO support response.sendError ?
}

From source file:com.oolong.platform.web.error.RestExceptionHandler.java

private void applyStatusIfPossible(ServletWebRequest webRequest, RestError error) {
    if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
        webRequest.getResponse().setStatus(error.getStatus().value());
    }/*from   ww w.  j  a va2  s. c  om*/
    // TODO support response.sendError ?
}

From source file:org.jlot.web.api.error.RestExceptionHandler.java

private void applyStatusIfPossible(ServletWebRequest webRequest, RestError error) {
    if (!WebUtils.isIncludeRequest(webRequest.getRequest())) {
        webRequest.getResponse().setStatus(error.getStatusCode());
    }/*from  w ww .j av a2 s  . com*/
    // TODO support response.sendError ?
}

From source file:co.paralleluniverse.springframework.web.servlet.mvc.method.annotation.FiberServletInvocableHandlerMethod.java

/**
 * Set the response status according to the {@link ResponseStatus} annotation.
 *///w ww . ja  v a2 s  .c  om
private void setResponseStatus(ServletWebRequest webRequest) throws IOException {
    if (this.responseStatus == null) {
        return;
    }

    if (StringUtils.hasText(this.responseReason)) {
        webRequest.getResponse().sendError(this.responseStatus.value(), this.responseReason);
    } else {
        webRequest.getResponse().setStatus(this.responseStatus.value());
    }

    // to be picked up by the RedirectView
    webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, this.responseStatus);
}

From source file:gumga.framework.presentation.exceptionhandler.ErrorMessageHandlerExceptionResolver.java

/**
 * Copied from/*ww  w  .ja  v  a2 s .  c om*/
 * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver}
 */
@SuppressWarnings("unchecked")
private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }
    MediaType.sortByQualityValue(acceptedMediaTypes);
    try (ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(webRequest.getResponse())) {
        Class<?> returnValueType = returnValue.getClass();
        if (this.messageConverters != null) {
            for (MediaType acceptedMediaType : acceptedMediaTypes) {
                for (HttpMessageConverter messageConverter : this.messageConverters) {
                    if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                        messageConverter.write(returnValue, acceptedMediaType, outputMessage);
                        return new ModelAndView();
                    }
                }
            }
        }
        if (logger.isWarnEnabled()) {
            logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType
                    + "] and " + acceptedMediaTypes);
        }
    }

    return null;
}