Example usage for org.springframework.web.util WebUtils INCLUDE_SERVLET_PATH_ATTRIBUTE

List of usage examples for org.springframework.web.util WebUtils INCLUDE_SERVLET_PATH_ATTRIBUTE

Introduction

In this page you can find the example usage for org.springframework.web.util WebUtils INCLUDE_SERVLET_PATH_ATTRIBUTE.

Prototype

String INCLUDE_SERVLET_PATH_ATTRIBUTE

To view the source code for org.springframework.web.util WebUtils INCLUDE_SERVLET_PATH_ATTRIBUTE.

Click Source Link

Document

Standard Servlet 2.3+ spec request attribute for include servlet path.

Usage

From source file:com.capgemini.b2bassets.storefront.web.mvc.AcceleratorUrlPathHelperTest.java

@Test
public void testGetPathWithinServletMapping() {
    final AcceleratorUrlPathHelper pathHelper = new AcceleratorUrlPathHelper();
    request.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, StringUtils.EMPTY);
    final String result = pathHelper.getPathWithinServletMapping(request);
    Assertions.assertThat(result.equals("/")).isTrue();
}

From source file:net.paoding.rose.web.RequestPath.java

public RequestPath(HttpServletRequest request) {
    // method//w  ww . ja v  a 2 s.co m
    setMethod(parseMethod(request));

    // ctxpath
    setCtxpath(request.getContextPath());
    String invocationCtxpath = null; // includeinvocationCtxPathincludectxpath
    // dispather, uri, ctxpath
    String uri;
    if (WebUtils.isIncludeRequest(request)) {
        setDispatcher(Dispatcher.INCLUDE);
        uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
        invocationCtxpath = ((String) request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
        setRosePath((String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
    } else {
        uri = request.getRequestURI();
        this.setRosePath(request.getServletPath());
        if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) == null) {
            this.setDispatcher(Dispatcher.REQUEST);
        } else {
            this.setDispatcher(Dispatcher.FORWARD);
        }
    }
    if (uri.startsWith("http://") || uri.startsWith("https://")) {
        int start = uri.indexOf('/', 9);
        if (start == -1) {
            uri = "";
        } else {
            uri = uri.substring(start);
        }
    }
    if (uri.indexOf('%') != -1) {
        try {
            String encoding = request.getCharacterEncoding();
            if (encoding == null || encoding.length() == 0) {
                encoding = "UTF-8";
            }
            uri = URLDecoder.decode(uri, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    this.setUri(uri);
    // requestPathctxpathincludeinvocationCtxpath

    if (getCtxpath().length() <= 1) {
        setRosePath(getUri());
    } else {
        setRosePath(
                getUri().substring((invocationCtxpath == null ? getCtxpath() : invocationCtxpath).length()));
    }
}

From source file:com.sinosoft.one.mvc.web.RequestPath.java

public RequestPath(HttpServletRequest request) {
    // method//  w w  w.  ja  v a  2  s  .co m
    setMethod(parseMethod(request));

    // ctxpath
    setCtxpath(request.getContextPath());
    String invocationCtxpath = null; // includeinvocationCtxPathincludectxpath
    // dispather, uri, ctxpath
    String uri;
    if (WebUtils.isIncludeRequest(request)) {
        setDispatcher(Dispatcher.INCLUDE);
        uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
        invocationCtxpath = ((String) request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
        setMvcPath((String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
    } else {
        if (request.getAttribute(MvcConstants.WINDOW_REQUEST_URI) != null) {
            uri = (String) request.getAttribute(MvcConstants.WINDOW_REQUEST_URI);
            request.removeAttribute(MvcConstants.WINDOW_REQUEST_URI);
            request.setAttribute(MvcConstants.IS_WINDOW_REQUEST, "1");
        } else {
            uri = request.getRequestURI();
            request.removeAttribute(MvcConstants.IS_WINDOW_REQUEST);
        }

        this.setMvcPath(request.getServletPath());
        if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) == null) {
            this.setDispatcher(Dispatcher.REQUEST);
        } else {
            this.setDispatcher(Dispatcher.FORWARD);
        }
    }
    if (uri.startsWith("http://") || uri.startsWith("https://")) {
        int start = uri.indexOf('/', 9);
        if (start == -1) {
            uri = "";
        } else {
            uri = uri.substring(start);
        }
    }
    if (uri.indexOf('%') != -1) {
        try {
            String encoding = request.getCharacterEncoding();
            if (encoding == null || encoding.length() == 0) {
                encoding = "UTF-8";
            }
            uri = URLDecoder.decode(uri, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    this.setUri(uri);
    // requestPathctxpathincludeinvocationCtxpath

    if (getCtxpath().length() <= 1) {
        setMvcPath(getUri());
    } else {
        setMvcPath(getUri().substring((invocationCtxpath == null ? getCtxpath() : invocationCtxpath).length()));
    }
}

From source file:org.hyperic.hq.ui.servlet.RenditServlet.java

protected void handleRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    boolean useInclude = false;

    // Since we may be processing via an internal RequestDispatcher 
    // include(), we need to investigate the subrequest URIs, etc.
    // and use those, as Tomcat won't set them up in subrequest objects
    String reqUri = (String) req.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
    if (reqUri != null)
        useInclude = true;// w w w.j a va  2s .  c om
    if (reqUri == null && !useInclude)
        reqUri = req.getRequestURI();

    String ctxPath = (String) req.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE);
    if (ctxPath != null)
        useInclude = true;
    if (ctxPath == null && !useInclude)
        ctxPath = req.getContextPath();

    String pathInfo = (String) req.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE);
    if (pathInfo != null)
        useInclude = true;
    if (pathInfo == null && !useInclude)
        pathInfo = req.getPathInfo();

    String servletPath = (String) req.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
    if (servletPath != null)
        useInclude = true;
    if (servletPath == null && !useInclude)
        servletPath = req.getServletPath();

    String queryStr = (String) req.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE);
    if (queryStr != null)
        useInclude = true;
    if (queryStr == null && !useInclude)
        queryStr = req.getQueryString();

    List fullPath = StringUtil.explode(reqUri, "/");
    int pathSize = fullPath.size();

    if (_log.isDebugEnabled()) {
        _log.debug("Request path [" + fullPath + "]");
    }

    if (((String) fullPath.get(pathSize - 1)).endsWith(".groovy")) {
        _log.warn(".groovy file requested [" + fullPath + "]");
        throw new ServletException("Illegal request path [" + fullPath + "]");
    }

    if (!requestIsValid(req)) {
        throw new ServletException("Illegal request path [" + fullPath + "]");
    }

    List subPath = fullPath.subList(pathSize - 3, fullPath.size());

    String plugin = (String) subPath.get(0);
    if (_log.isDebugEnabled()) {
        _log.debug("Request for [" + plugin + "]: " + reqUri + (queryStr == null ? "" : ("?" + queryStr)));
    }

    int sessId = RequestUtils.getSessionIdInt(req);
    WebApplicationContext springContext = WebApplicationContextUtils
            .getWebApplicationContext(getServletContext());
    AuthzBoss authzBoss = springContext.getBean(AuthzBoss.class);

    AuthzSubject user;

    try {
        user = authzBoss.getCurrentSubject(sessId);
    } catch (SessionException e) {
        // Could not get the current user.  We should default to a 'nobody'
        // user here.
        _log.error("Unable to get current user.  Bailing", e);
        throw new ServletException(e);
    }

    RequestInvocationBindings b = new RequestInvocationBindings(reqUri, ctxPath, pathInfo, servletPath,
            queryStr, user, req, resp, getServletContext());
    long start = System.currentTimeMillis();
    try {
        Bootstrap.getBean(RenditServer.class).handleRequest(plugin, b);
    } catch (Exception e) {
        throw new ServletException(e);
    } finally {
        _log.debug("Processed request for [" + plugin + "] in " + (System.currentTimeMillis() - start) + " ms");
    }
}

From source file:org.springframework.web.util.UrlPathHelper.java

/**
 * Return the servlet path for the given request, regarding an include request
 * URL if called within a RequestDispatcher include.
 * <p>As the value returned by {@code request.getServletPath()} is already
 * decoded by the servlet container, this method will not attempt to decode it.
 * @param request current HTTP request/*from   w  ww. ja v  a 2s . com*/
 * @return the servlet path
 */
public String getServletPath(HttpServletRequest request) {
    String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
    if (servletPath == null) {
        servletPath = request.getServletPath();
    }
    if (servletPath.length() > 1 && servletPath.endsWith("/")
            && shouldRemoveTrailingServletPathSlash(request)) {
        // On WebSphere, in non-compliant mode, for a "/foo/" case that would be "/foo"
        // on all other servlet containers: removing trailing slash, proceeding with
        // that remaining slash as final lookup path...
        servletPath = servletPath.substring(0, servletPath.length() - 1);
    }
    return servletPath;
}