use of javax.servlet.ServletRequestWrapper in project tomcat by apache.
the class ApplicationDispatcher method wrapRequest.
/**
* Create and return a request wrapper that has been inserted in the
* appropriate spot in the request chain.
*/
private ServletRequest wrapRequest(State state) {
// Locate the request we should insert in front of
ServletRequest previous = null;
ServletRequest current = state.outerRequest;
while (current != null) {
if (state.hrequest == null && (current instanceof HttpServletRequest))
state.hrequest = (HttpServletRequest) current;
if (!(current instanceof ServletRequestWrapper))
break;
if (current instanceof ApplicationHttpRequest)
break;
if (current instanceof ApplicationRequest)
break;
previous = current;
current = ((ServletRequestWrapper) current).getRequest();
}
// Instantiate a new wrapper at this point and insert it in the chain
ServletRequest wrapper = null;
if ((current instanceof ApplicationHttpRequest) || (current instanceof Request) || (current instanceof HttpServletRequest)) {
// Compute a crossContext flag
HttpServletRequest hcurrent = (HttpServletRequest) current;
boolean crossContext = false;
if ((state.outerRequest instanceof ApplicationHttpRequest) || (state.outerRequest instanceof Request) || (state.outerRequest instanceof HttpServletRequest)) {
HttpServletRequest houterRequest = (HttpServletRequest) state.outerRequest;
Object contextPath = houterRequest.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH);
if (contextPath == null) {
// Forward
contextPath = houterRequest.getContextPath();
}
crossContext = !(context.getPath().equals(contextPath));
}
wrapper = new ApplicationHttpRequest(hcurrent, context, crossContext);
} else {
wrapper = new ApplicationRequest(current);
}
if (previous == null)
state.outerRequest = wrapper;
else
((ServletRequestWrapper) previous).setRequest(wrapper);
state.wrapRequest = wrapper;
return (wrapper);
}
use of javax.servlet.ServletRequestWrapper in project tomcat by apache.
the class ApplicationDispatcher method checkSameObjects.
private void checkSameObjects(ServletRequest appRequest, ServletResponse appResponse) throws ServletException {
ServletRequest originalRequest = ApplicationFilterChain.getLastServicedRequest();
ServletResponse originalResponse = ApplicationFilterChain.getLastServicedResponse();
// Some forwards, eg from valves will not set original values
if (originalRequest == null || originalResponse == null) {
return;
}
boolean same = false;
ServletRequest dispatchedRequest = appRequest;
//find the request that was passed into the service method
while (originalRequest instanceof ServletRequestWrapper && ((ServletRequestWrapper) originalRequest).getRequest() != null) {
originalRequest = ((ServletRequestWrapper) originalRequest).getRequest();
}
//compare with the dispatched request
while (!same) {
if (originalRequest.equals(dispatchedRequest)) {
same = true;
}
if (!same && dispatchedRequest instanceof ServletRequestWrapper) {
dispatchedRequest = ((ServletRequestWrapper) dispatchedRequest).getRequest();
} else {
break;
}
}
if (!same) {
throw new ServletException(sm.getString("applicationDispatcher.specViolation.request"));
}
same = false;
ServletResponse dispatchedResponse = appResponse;
//find the response that was passed into the service method
while (originalResponse instanceof ServletResponseWrapper && ((ServletResponseWrapper) originalResponse).getResponse() != null) {
originalResponse = ((ServletResponseWrapper) originalResponse).getResponse();
}
//compare with the dispatched response
while (!same) {
if (originalResponse.equals(dispatchedResponse)) {
same = true;
}
if (!same && dispatchedResponse instanceof ServletResponseWrapper) {
dispatchedResponse = ((ServletResponseWrapper) dispatchedResponse).getResponse();
} else {
break;
}
}
if (!same) {
throw new ServletException(sm.getString("applicationDispatcher.specViolation.response"));
}
}
use of javax.servlet.ServletRequestWrapper in project tomcat by apache.
the class ApplicationDispatcher method unwrapRequest.
/**
* Unwrap the request if we have wrapped it.
*/
private void unwrapRequest(State state) {
if (state.wrapRequest == null)
return;
if (state.outerRequest.isAsyncStarted()) {
if (!state.outerRequest.getAsyncContext().hasOriginalRequestAndResponse()) {
return;
}
}
ServletRequest previous = null;
ServletRequest current = state.outerRequest;
while (current != null) {
// If we run into the container request we are done
if ((current instanceof Request) || (current instanceof RequestFacade))
break;
// Remove the current request if it is our wrapper
if (current == state.wrapRequest) {
ServletRequest next = ((ServletRequestWrapper) current).getRequest();
if (previous == null)
state.outerRequest = next;
else
((ServletRequestWrapper) previous).setRequest(next);
break;
}
// Advance to the next request in the chain
previous = current;
current = ((ServletRequestWrapper) current).getRequest();
}
}
use of javax.servlet.ServletRequestWrapper in project jetty.project by eclipse.
the class TestFilter method doFilter.
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String from = request.getRemoteAddr();
String to = request.getLocalAddr();
String path = ((HttpServletRequest) request).getServletPath();
if (!_remote && !_allowed.contains(path) && !from.equals(to)) {
_context.getRequestDispatcher("/remote.html").forward(request, response);
return;
}
Integer old_value = null;
ServletRequest r = request;
while (r instanceof ServletRequestWrapper) r = ((ServletRequestWrapper) r).getRequest();
try {
old_value = (Integer) request.getAttribute("testFilter");
Integer value = (old_value == null) ? new Integer(1) : new Integer(old_value.intValue() + 1);
request.setAttribute("testFilter", value);
String qString = ((HttpServletRequest) request).getQueryString();
if (qString != null && qString.indexOf("wrap") >= 0) {
request = new HttpServletRequestWrapper((HttpServletRequest) request);
}
_context.setAttribute("request" + r.hashCode(), value);
chain.doFilter(request, response);
} finally {
request.setAttribute("testFilter", old_value);
_context.setAttribute("request" + r.hashCode(), old_value);
}
}
use of javax.servlet.ServletRequestWrapper in project undertow by undertow-io.
the class RequestDispatcherImpl method error.
private void error(ServletRequestContext servletRequestContext, final ServletRequest request, final ServletResponse response, final String servletName, final Throwable exception, final String message) throws ServletException, IOException {
if (request.getDispatcherType() == DispatcherType.ERROR) {
//we have already dispatched once with an error
//if we dispatch again we run the risk of a stack overflow
//so we just kill it, the user will just get the basic error page
UndertowServletLogger.REQUEST_LOGGER.errorGeneratingErrorPage(servletRequestContext.getExchange().getRequestPath(), request.getAttribute(ERROR_EXCEPTION), servletRequestContext.getExchange().getStatusCode(), exception);
servletRequestContext.getExchange().endExchange();
return;
}
final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
if (servletRequestContext.getOriginalRequest() != request) {
if (!(request instanceof ServletRequestWrapper)) {
throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
}
}
if (servletRequestContext.getOriginalResponse() != response) {
if (!(response instanceof ServletResponseWrapper)) {
throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
}
}
}
final ServletRequest oldRequest = servletRequestContext.getServletRequest();
final ServletResponse oldResponse = servletRequestContext.getServletResponse();
servletRequestContext.setDispatcherType(DispatcherType.ERROR);
//only update if this is the first forward
requestImpl.setAttribute(ERROR_REQUEST_URI, requestImpl.getRequestURI());
requestImpl.setAttribute(ERROR_SERVLET_NAME, servletName);
if (exception != null) {
requestImpl.setAttribute(ERROR_EXCEPTION, exception);
requestImpl.setAttribute(ERROR_EXCEPTION_TYPE, exception.getClass());
}
requestImpl.setAttribute(ERROR_MESSAGE, message);
requestImpl.setAttribute(ERROR_STATUS_CODE, responseImpl.getStatus());
int qsPos = path.indexOf("?");
String newServletPath = path;
if (qsPos != -1) {
Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
String newQueryString = newServletPath.substring(qsPos + 1);
newServletPath = newServletPath.substring(0, qsPos);
String encoding = QueryParameterUtils.getQueryParamEncoding(servletRequestContext.getExchange());
Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString, encoding);
requestImpl.getExchange().setQueryString(newQueryString);
requestImpl.setQueryParameters(newQueryParameters);
}
String newRequestUri = servletContext.getContextPath() + newServletPath;
requestImpl.getExchange().setRelativePath(newServletPath);
requestImpl.getExchange().setRequestPath(newRequestUri);
requestImpl.getExchange().setRequestURI(newRequestUri);
requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(pathMatch);
requestImpl.setServletContext(servletContext);
responseImpl.setServletContext(servletContext);
try {
try {
servletRequestContext.setServletRequest(request);
servletRequestContext.setServletResponse(response);
servletContext.getDeployment().getServletDispatcher().dispatchToPath(requestImpl.getExchange(), pathMatch, DispatcherType.ERROR);
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
AsyncContextImpl ac = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
if (ac != null) {
ac.complete();
}
servletRequestContext.setServletRequest(oldRequest);
servletRequestContext.setServletResponse(oldResponse);
}
}
Aggregations