List of usage examples for javax.servlet.http HttpServletRequestWrapper getServletPath
@Override
public String getServletPath()
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 w w w . j a v a 2 s . c o m*/ 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); }
From source file:org.broadleafcommerce.openadmin.web.filter.BroadleafAdminTypedEntityRequestFilter.java
@SuppressWarnings("unchecked") public boolean isRequestForTypedEntity(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { String servletPath = request.getServletPath(); if (!servletPath.contains(":")) { return false; }//www .j a v a 2 s .com // Find the Admin Section for the typed entity. String sectionKey = getSectionKeyFromRequest(request); AdminSection typedEntitySection = adminNavigationService.findAdminSectionByURI(sectionKey); // If the Typed Entity Section does not exist, continue with the filter chain. if (typedEntitySection == null) { return false; } // Check if admin user has access to this section. if (!adminUserHasAccess(typedEntitySection)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access is denied"); return true; } // Add the typed entity admin section to the request. request.setAttribute("typedEntitySection", typedEntitySection); // Find the type and build the new path. String type = getEntityTypeFromRequest(request); final String forwardPath = servletPath.replace(type, ""); // Get the type field name on the Entity for the given section. String typedFieldName = getTypeFieldName(typedEntitySection); // Build out the new parameter map to be forwarded. final Map parameters = new LinkedHashMap(request.getParameterMap()); if (typedFieldName != null) { parameters.put(typedFieldName, new String[] { type.substring(1).toUpperCase() }); } // Build our request wrapper. final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) { @Override public String getParameter(String name) { Object temp = parameters.get(name); Object[] response = new Object[0]; if (temp != null) { ArrayUtils.addAll(response, temp); } if (ArrayUtils.isEmpty(response)) { return null; } else { return (String) response[0]; } } @Override public Map getParameterMap() { return parameters; } @Override public Enumeration getParameterNames() { return new IteratorEnumeration(parameters.keySet().iterator()); } @Override public String[] getParameterValues(String name) { return (String[]) parameters.get(name); } @Override public String getServletPath() { return forwardPath; } }; requestProcessor.process(new ServletWebRequest(wrapper, response)); // Forward the wrapper to the appropriate path wrapper.getRequestDispatcher(wrapper.getServletPath()).forward(wrapper, response); return true; }