Search in sources :

Example 1 with PortletRequestAttributes

use of org.springframework.web.portlet.context.PortletRequestAttributes in project uPortal by Jasig.

the class PortalWebUtils method currentRequestContextPath.

/**
 * Get the request context path from the current request. Copes with both HttpServletRequest and
 * PortletRequest and so usable when handling Spring-processed Servlet or Portlet requests.
 * Requires that Spring have bound the request, as in the case of dispatcher servlet or portlet
 * or when the binding filter or listener is active. This should be the case for all requests in
 * the uPortal framework and framework portlets.
 *
 * @return request.getContextPath() for the relevant servlet or portlet request
 * @throws IllegalStateException if the request is not Spring-bound or is neither Servlet nor
 *     Portlet flavored
 */
public static String currentRequestContextPath() {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (null == requestAttributes) {
        throw new IllegalStateException("Request attributes are not bound.  " + "Not operating in context of a Spring-processed Request?");
    }
    if (requestAttributes instanceof ServletRequestAttributes) {
        final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        final HttpServletRequest request = servletRequestAttributes.getRequest();
        return request.getContextPath();
    } else if (requestAttributes instanceof PortletRequestAttributes) {
        final PortletRequestAttributes portletRequestAttributes = (PortletRequestAttributes) requestAttributes;
        final PortletRequest request = portletRequestAttributes.getRequest();
        return request.getContextPath();
    } else {
        throw new IllegalStateException("Request attributes are an unrecognized implementation.");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PortletRequestAttributes(org.springframework.web.portlet.context.PortletRequestAttributes) PortletRequest(javax.portlet.PortletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) PortletRequestAttributes(org.springframework.web.portlet.context.PortletRequestAttributes)

Aggregations

PortletRequest (javax.portlet.PortletRequest)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1 PortletRequestAttributes (org.springframework.web.portlet.context.PortletRequestAttributes)1