Example usage for javax.servlet.http HttpServletRequestWrapper getPathInfo

List of usage examples for javax.servlet.http HttpServletRequestWrapper getPathInfo

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequestWrapper getPathInfo.

Prototype

@Override
public String getPathInfo() 

Source Link

Document

The default behavior of this method is to return getPathInfo() on the wrapped request object.

Usage

From source file:com.marvelution.hudson.plugins.apiv2.servlet.filter.HudsonAPIV2ServletFilter.java

/**
 * {@inheritDoc}/*from ww w.  j av a  2s .  c om*/
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    // The Wink RestFilter can only handle HttpServletRequests so make sure we have one
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
        // Put the original HttpServletRequest in the HttpServletRequestWrapper
        final HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(
                (HttpServletRequest) request);
        // Get the requestUri without the context path and the leading slash
        String requestUri = servletRequest.getPathInfo();
        if (StringUtils.isNotBlank(requestUri) && requestUri.startsWith("/")) {
            requestUri = requestUri.substring(1);
        }
        LOGGER.log(Level.FINE, "Got a request from URI: " + requestUri + " with Accept Header: "
                + servletRequest.getHeader("Accept"));
        // Make sure it is a REST call
        if (StringUtils.isNotBlank(requestUri) && requestUri.startsWith(BaseRestResource.BASE_REST_URI)) {
            validateRuntimeDelegate();
            LOGGER.log(Level.FINE, "Got a REST request, forwarding it to the Wink RestFilter");
            FilteredHttpServletResponse servletResponse = new FilteredHttpServletResponse(
                    (HttpServletResponse) response);
            restServlet.service(servletRequest, servletResponse);
            if ((!(servletResponse.isCommitted())) && (servletResponse.getStatusCode() == 404)) {
                LOGGER.log(Level.FINE, "Filter " + this.getClass().getName()
                        + " did not match a resource so letting request continue on FilterChain");
                servletResponse.setStatus(200);
                filterChain.doFilter(request, response);
            }
            // Otherwise forward the request to the next Filter in the chain
        } else {
            LOGGER.log(Level.FINE, "No REST request, forwarding request to the next ServletFilter");
            filterChain.doFilter(request, response);
        }
        // If we don't have a HttpServletRequest and HttpServletResponse then forward to the next Filter in the chain
    } else {
        LOGGER.log(Level.FINE,
                "No HttpServletRequest and HttpServletResponse, forwarding request to the next ServletFilter");
        filterChain.doFilter(request, response);
    }
}

From source file:info.magnolia.cms.filters.ServletDispatchingFilterTest.java

private void doTestBypassAndPathInfo(final boolean shouldBypass, final String expectedPathInfo,
        final String expectedServletPath, final String requestPath, String mapping,
        boolean shouldCheckPathInfoAndServletPath) throws Exception {
    ComponentsTestUtil.setInstance(Voting.class, new DefaultVoting());
    WebContext ctx = createStrictMock(WebContext.class);
    MgnlContext.setInstance(ctx);/*from  ww  w .j  ava2  s .  c om*/
    final AggregationState state = new MockAggregationState();
    expect(ctx.getContextPath()).andReturn("/magnoliaAuthor").anyTimes();

    final FilterChain chain = createNiceMock(FilterChain.class);
    final HttpServletResponse res = createNiceMock(HttpServletResponse.class);
    final HttpServletRequest req = createMock(HttpServletRequest.class);
    expect(req.getAttribute(EasyMock.<String>anyObject())).andReturn(null).anyTimes();
    expect(ctx.getAggregationState()).andReturn(state).anyTimes();
    expect(req.getRequestURI()).andReturn("/magnoliaAuthor" + requestPath).anyTimes();
    expect(req.getContextPath()).andReturn("/magnoliaAuthor").anyTimes();
    expect(req.getServletPath()).andReturn("/magnoliaAuthor" + requestPath).anyTimes();
    expect(req.getPathInfo()).andReturn(null).anyTimes();
    expect(req.getQueryString()).andReturn(null).anyTimes();

    final Servlet servlet = createStrictMock(Servlet.class);
    if (!shouldBypass) {
        servlet.service(isA(HttpServletRequestWrapper.class), same(res));
        expectLastCall().andAnswer(new IAnswer<Object>() {
            @Override
            public Object answer() throws Throwable {
                final HttpServletRequestWrapper requestWrapper = (HttpServletRequestWrapper) getCurrentArguments()[0];
                final String pathInfo = requestWrapper.getPathInfo();
                final String servletPath = requestWrapper.getServletPath();
                assertEquals("pathInfo does not match", expectedPathInfo, pathInfo);
                assertEquals("servletPath does not match", expectedServletPath, servletPath);
                return null;
            }
        });
    }

    replay(chain, res, req, servlet, ctx);

    state.setCurrentURI(requestPath);
    final AbstractMgnlFilter filter = new ServletDispatchingFilter();
    final Field servletField = ServletDispatchingFilter.class.getDeclaredField("servlet");
    servletField.setAccessible(true);
    servletField.set(filter, servlet);

    filter.addMapping(mapping);

    assertEquals("Should " + (shouldBypass ? "" : "not ") + "have bypassed", shouldBypass,
            !filter.matches(req));
    if (!shouldBypass) {
        filter.doFilter(req, res, chain);
    }

    verify(chain, res, req, servlet, ctx);
}