@Test public void testDecoratedNativeRequest() { MockRenderRequest portletRequest = new MockRenderRequest(); MockRenderResponse portletResponse = new MockRenderResponse(); PortletRequest decoratedRequest = new PortletRequestWrapper(portletRequest); PortletResponse decoratedResponse = new PortletResponseWrapper(portletResponse); PortletWebRequest request = new PortletWebRequest(decoratedRequest, decoratedResponse); assertSame(decoratedRequest, request.getNativeRequest()); assertSame(decoratedRequest, request.getNativeRequest(PortletRequest.class)); assertSame(portletRequest, request.getNativeRequest(RenderRequest.class)); assertSame(portletRequest, request.getNativeRequest(MockRenderRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(decoratedResponse, request.getNativeResponse()); assertSame(decoratedResponse, request.getNativeResponse(PortletResponse.class)); assertSame(portletResponse, request.getNativeResponse(RenderResponse.class)); assertSame(portletResponse, request.getNativeResponse(MockRenderResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
/** * Return an appropriate request object of the specified type, if available, * unwrapping the given request as far as necessary. * @param request the portlet request to introspect * @param requiredType the desired type of request object * @return the matching request object, or {@code null} if none * of that type is available */ @SuppressWarnings("unchecked") public static <T> T getNativeRequest(PortletRequest request, Class<T> requiredType) { if (requiredType != null) { if (requiredType.isInstance(request)) { return (T) request; } else if (request instanceof PortletRequestWrapper) { return getNativeRequest(((PortletRequestWrapper) request).getRequest(), requiredType); } } return null; }