Example usage for org.springframework.web.servlet View RESPONSE_STATUS_ATTRIBUTE

List of usage examples for org.springframework.web.servlet View RESPONSE_STATUS_ATTRIBUTE

Introduction

In this page you can find the example usage for org.springframework.web.servlet View RESPONSE_STATUS_ATTRIBUTE.

Prototype

String RESPONSE_STATUS_ATTRIBUTE

To view the source code for org.springframework.web.servlet View RESPONSE_STATUS_ATTRIBUTE.

Click Source Link

Document

Name of the HttpServletRequest attribute that contains the response status code.

Usage

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

/**
 * Set the response status according to the {@link ResponseStatus} annotation.
 *///  w  w  w  . j  ava2 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:com.epam.cme.storefront.controllers.pages.AbstractPageController.java

/**
 * Checks request URL against properly resolved URL and returns null if url is proper or
 * redirection string if not./*  w  w w . j  a v  a  2 s  . co m*/
 * 
 * @param request
 *            - request that contains current URL
 * @param response
 *            - response to write "301 Moved Permanently" status to if redirected
 * @param resolvedUrl
 *            - properly resolved URL
 * @return null if url is properly resolved or redirection string if not
 * @throws UnsupportedEncodingException
 */
protected String checkRequestUrl(final HttpServletRequest request, final HttpServletResponse response,
        final String resolvedUrl) throws UnsupportedEncodingException {
    try {
        final String requestURI = URIUtil.decode(request.getRequestURI(), "utf-8");
        final String decoded = URIUtil.decode(resolvedUrl, "utf-8");
        if (StringUtils.isNotEmpty(requestURI) && requestURI.endsWith(decoded)) {
            return null;
        } else {
            request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.MOVED_PERMANENTLY);
            final String queryString = request.getQueryString();
            if (queryString != null && !queryString.isEmpty()) {
                return "redirect:" + resolvedUrl + "?" + queryString;
            }
            return "redirect:" + resolvedUrl;
        }
    } catch (final URIException e) {
        throw new UnsupportedEncodingException();
    }
}

From source file:com.exxonmobile.ace.hybris.storefront.controllers.pages.AbstractPageController.java

/**
 * Checks request URL against properly resolved URL and returns null if url is proper or redirection string if not.
 * /*from  w  w w.  j  a  v  a 2s . co  m*/
 * @param request
 *           - request that contains current URL
 * @param response
 *           - response to write "301 Moved Permanently" status to if redirected
 * @param resolvedUrlPath
 *           - properly resolved URL
 * @return null if url is properly resolved or redirection string if not
 * @throws UnsupportedEncodingException
 */
protected String checkRequestUrl(final HttpServletRequest request, final HttpServletResponse response,
        final String resolvedUrlPath) throws UnsupportedEncodingException {
    try {
        final String resolvedUrl = response.encodeURL(request.getContextPath() + resolvedUrlPath);
        final String requestURI = URIUtil.decode(request.getRequestURI(), "utf-8");
        final String decoded = URIUtil.decode(resolvedUrl, "utf-8");
        if (StringUtils.isNotEmpty(requestURI) && requestURI.endsWith(decoded)) {
            return null;
        } else {
            request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.MOVED_PERMANENTLY);
            final String queryString = request.getQueryString();
            if (queryString != null && !queryString.isEmpty()) {
                return "redirect:" + resolvedUrlPath + "?" + queryString;
            }
            return "redirect:" + resolvedUrlPath;
        }
    } catch (final URIException e) {
        throw new UnsupportedEncodingException();
    }
}

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;//  ww  w.  ja  v a  2  s  .c  o  m
    }

    // 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);
    }
}