org.apache.catalina.connector.RequestFacade Java Examples

The following examples show how to use org.apache.catalina.connector.RequestFacade. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
protected String determineMethodsAllowed(HttpServletRequest req) {
    StringBuilder allow = new StringBuilder();

    // Start with methods that are always allowed
    allow.append("OPTIONS, GET, HEAD, POST");

    // PUT and DELETE depend on readonly
    if (!readOnly) {
        allow.append(", PUT, DELETE");
    }

    // Trace - assume disabled unless we can prove otherwise
    if (req instanceof RequestFacade &&
            ((RequestFacade) req).getAllowTrace()) {
        allow.append(", TRACE");
    }

    return allow.toString();
}
 
Example #2
Source File: DefaultServlet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Override default implementation to ensure that TRACE is correctly
 * handled.
 *
 * @param req   the {@link HttpServletRequest} object that
 *                  contains the request the client made of
 *                  the servlet
 *
 * @param resp  the {@link HttpServletResponse} object that
 *                  contains the response the servlet returns
 *                  to the client
 *
 * @exception IOException   if an input or output error occurs
 *                              while the servlet is handling the
 *                              OPTIONS request
 *
 * @exception ServletException  if the request for the
 *                                  OPTIONS cannot be handled
 */
@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    StringBuilder allow = new StringBuilder();
    // There is a doGet method
    allow.append("GET, HEAD");
    // There is a doPost
    allow.append(", POST");
    // There is a doPut
    allow.append(", PUT");
    // There is a doDelete
    allow.append(", DELETE");
    // Trace - assume disabled unless we can prove otherwise
    if (req instanceof RequestFacade &&
            ((RequestFacade) req).getAllowTrace()) {
        allow.append(", TRACE");
    }
    // Always allow options
    allow.append(", OPTIONS");

    resp.setHeader("Allow", allow.toString());
}
 
Example #3
Source File: DefaultServlet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Override default implementation to ensure that TRACE is correctly
 * handled.
 *
 * @param req   the {@link HttpServletRequest} object that
 *                  contains the request the client made of
 *                  the servlet
 *
 * @param resp  the {@link HttpServletResponse} object that
 *                  contains the response the servlet returns
 *                  to the client
 *
 * @exception IOException   if an input or output error occurs
 *                              while the servlet is handling the
 *                              OPTIONS request
 *
 * @exception ServletException  if the request for the
 *                                  OPTIONS cannot be handled
 */
@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    StringBuilder allow = new StringBuilder();
    // There is a doGet method
    allow.append("GET, HEAD");
    // There is a doPost
    allow.append(", POST");
    // There is a doPut
    allow.append(", PUT");
    // There is a doDelete
    allow.append(", DELETE");
    // Trace - assume disabled unless we can prove otherwise
    if (req instanceof RequestFacade &&
            ((RequestFacade) req).getAllowTrace()) {
        allow.append(", TRACE");
    }
    // Always allow options
    allow.append(", OPTIONS");

    resp.setHeader("Allow", allow.toString());
}
 
Example #4
Source File: WebdavServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Determines the methods normally allowed for the resource.
 *
 * @param req The Servlet request
 *
 * @return The allowed HTTP methods
 */
@Override
protected String determineMethodsAllowed(HttpServletRequest req) {


    WebResource resource = resources.getResource(getRelativePath(req));

    // These methods are always allowed. They may return a 404 (not a 405)
    // if the resource does not exist.
    StringBuilder methodsAllowed = new StringBuilder(
            "OPTIONS, GET, POST, HEAD");

    if (!readOnly) {
        methodsAllowed.append(", DELETE");
        if (!resource.isDirectory()) {
            methodsAllowed.append(", PUT");
        }
    }

    // Trace - assume disabled unless we can prove otherwise
    if (req instanceof RequestFacade &&
            ((RequestFacade) req).getAllowTrace()) {
        methodsAllowed.append(", TRACE");
    }

    methodsAllowed.append(", LOCK, UNLOCK, PROPPATCH, COPY, MOVE");

    if (listings) {
        methodsAllowed.append(", PROPFIND");
    }

    if (!resource.exists()) {
        methodsAllowed.append(", MKCOL");
    }

    return methodsAllowed.toString();
}
 
Example #5
Source File: RemoteIpFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public PushBuilder getPushBuilder() {
    ServletRequest current = getRequest();
    while (current instanceof ServletRequestWrapper) {
        current = ((ServletRequestWrapper) current).getRequest();
    }
    if (current instanceof RequestFacade) {
        return ((RequestFacade) current).newPushBuilder(this);
    } else {
        return null;
    }
}
 
Example #6
Source File: ApplicationHttpRequest.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public PushBuilder newPushBuilder() {
    ServletRequest current = getRequest();
    while (current instanceof ServletRequestWrapper) {
        current = ((ServletRequestWrapper) current).getRequest();
    }
    if (current instanceof RequestFacade) {
        return ((RequestFacade) current).newPushBuilder(this);
    } else {
        return null;
    }
}
 
Example #7
Source File: ApplicationDispatcher.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * 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();

    }

}
 
Example #8
Source File: TomcatHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static HttpHeaders createTomcatHttpHeaders(HttpServletRequest request) {
	RequestFacade requestFacade = getRequestFacade(request);
	org.apache.catalina.connector.Request connectorRequest = (org.apache.catalina.connector.Request)
			ReflectionUtils.getField(COYOTE_REQUEST_FIELD, requestFacade);
	Assert.state(connectorRequest != null, "No Tomcat connector request");
	Request tomcatRequest = connectorRequest.getCoyoteRequest();
	TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatRequest.getMimeHeaders());
	return new HttpHeaders(headers);
}
 
Example #9
Source File: TomcatHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static RequestFacade getRequestFacade(HttpServletRequest request) {
	if (request instanceof RequestFacade) {
		return (RequestFacade) request;
	}
	else if (request instanceof HttpServletRequestWrapper) {
		HttpServletRequestWrapper wrapper = (HttpServletRequestWrapper) request;
		HttpServletRequest wrappedRequest = (HttpServletRequest) wrapper.getRequest();
		return getRequestFacade(wrappedRequest);
	}
	else {
		throw new IllegalArgumentException("Cannot convert [" + request.getClass() +
				"] to org.apache.catalina.connector.RequestFacade");
	}
}
 
Example #10
Source File: TomcatHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static HttpHeaders createTomcatHttpHeaders(HttpServletRequest request) {
	RequestFacade requestFacade = getRequestFacade(request);
	org.apache.catalina.connector.Request connectorRequest = (org.apache.catalina.connector.Request)
			ReflectionUtils.getField(COYOTE_REQUEST_FIELD, requestFacade);
	Assert.state(connectorRequest != null, "No Tomcat connector request");
	Request tomcatRequest = connectorRequest.getCoyoteRequest();
	TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatRequest.getMimeHeaders());
	return new HttpHeaders(headers);
}
 
Example #11
Source File: TomcatHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static RequestFacade getRequestFacade(HttpServletRequest request) {
	if (request instanceof RequestFacade) {
		return (RequestFacade) request;
	}
	else if (request instanceof HttpServletRequestWrapper) {
		HttpServletRequestWrapper wrapper = (HttpServletRequestWrapper) request;
		HttpServletRequest wrappedRequest = (HttpServletRequest) wrapper.getRequest();
		return getRequestFacade(wrappedRequest);
	}
	else {
		throw new IllegalArgumentException("Cannot convert [" + request.getClass() +
				"] to org.apache.catalina.connector.RequestFacade");
	}
}
 
Example #12
Source File: ApplicationDispatcher.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * 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();

    }

}
 
Example #13
Source File: ApplicationDispatcher.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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();

    }

}
 
Example #14
Source File: TestPersistentManager.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug62175() throws Exception {
    final PersistentManager manager = new PersistentManager();
    final AtomicInteger sessionExpireCounter = new AtomicInteger();

    Store mockStore = EasyMock.createNiceMock(Store.class);
    EasyMock.expect(mockStore.load(EasyMock.anyString())).andAnswer(new IAnswer<Session>() {

        @Override
        public Session answer() throws Throwable {
            return timedOutSession(manager, sessionExpireCounter);
        }
    }).anyTimes();

    EasyMock.replay(mockStore);

    manager.setStore(mockStore);

    Host host = new TesterHost();

    final RequestCachingSessionListener requestCachingSessionListener = new RequestCachingSessionListener();

    final Context context = new TesterContext() {

        @Override
        public Object[] getApplicationLifecycleListeners() {
            return new Object[] { requestCachingSessionListener };
        }

        @Override
        public Manager getManager() {
            return manager;
        }
    };
    context.setParent(host);

    Request req = new Request() {
        @Override
        public Context getContext() {
            return context;
        }
    };
    req.setRequestedSessionId("invalidSession");
    HttpServletRequest request = new RequestFacade(req);
    requestCachingSessionListener.request = request;

    manager.setContext(context);

    manager.start();

    Assert.assertNull(request.getSession(false));
    Assert.assertEquals(1, sessionExpireCounter.get());

}
 
Example #15
Source File: WebSocketServlet.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    // Information required to send the server handshake message
    String key;
    String subProtocol = null;
    List<String> extensions = Collections.emptyList();

    if (!headerContainsToken(req, "upgrade", "websocket")) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (!headerContainsToken(req, "connection", "upgrade")) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (!headerContainsToken(req, "sec-websocket-version", "13")) {
        resp.setStatus(426);
        resp.setHeader("Sec-WebSocket-Version", "13");
        return;
    }

    key = req.getHeader("Sec-WebSocket-Key");
    if (key == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    String origin = req.getHeader("Origin");
    if (!verifyOrigin(origin)) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    List<String> subProtocols = getTokensFromHeader(req,
            "Sec-WebSocket-Protocol");
    if (!subProtocols.isEmpty()) {
        subProtocol = selectSubProtocol(subProtocols);

    }

    // TODO Read client handshake - Sec-WebSocket-Extensions

    // TODO Extensions require the ability to specify something (API TBD)
    //      that can be passed to the Tomcat internals and process extension
    //      data present when the frame is fragmented.

    // If we got this far, all is good. Accept the connection.
    resp.setHeader("Upgrade", "websocket");
    resp.setHeader("Connection", "upgrade");
    resp.setHeader("Sec-WebSocket-Accept", getWebSocketAccept(key));
    if (subProtocol != null) {
        resp.setHeader("Sec-WebSocket-Protocol", subProtocol);
    }
    if (!extensions.isEmpty()) {
        // TODO
    }

    WsHttpServletRequestWrapper wrapper = new WsHttpServletRequestWrapper(req);
    StreamInbound inbound = createWebSocketInbound(subProtocol, wrapper);
    wrapper.invalidate();

    // Small hack until the Servlet API provides a way to do this.
    ServletRequest inner = req;
    // Unwrap the request
    while (inner instanceof ServletRequestWrapper) {
        inner = ((ServletRequestWrapper) inner).getRequest();
    }
    if (inner instanceof RequestFacade) {
        ((RequestFacade) inner).doUpgrade(inbound);
    } else {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                sm.getString("servlet.reqUpgradeFail"));
    }
}
 
Example #16
Source File: WebSocketServlet.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    // Information required to send the server handshake message
    String key;
    String subProtocol = null;
    List<String> extensions = Collections.emptyList();

    if (!headerContainsToken(req, "upgrade", "websocket")) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (!headerContainsToken(req, "connection", "upgrade")) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (!headerContainsToken(req, "sec-websocket-version", "13")) {
        resp.setStatus(426);
        resp.setHeader("Sec-WebSocket-Version", "13");
        return;
    }

    key = req.getHeader("Sec-WebSocket-Key");
    if (key == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    String origin = req.getHeader("Origin");
    if (!verifyOrigin(origin)) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    List<String> subProtocols = getTokensFromHeader(req,
            "Sec-WebSocket-Protocol");
    if (!subProtocols.isEmpty()) {
        subProtocol = selectSubProtocol(subProtocols);

    }

    // TODO Read client handshake - Sec-WebSocket-Extensions

    // TODO Extensions require the ability to specify something (API TBD)
    //      that can be passed to the Tomcat internals and process extension
    //      data present when the frame is fragmented.

    // If we got this far, all is good. Accept the connection.
    resp.setHeader("Upgrade", "websocket");
    resp.setHeader("Connection", "upgrade");
    resp.setHeader("Sec-WebSocket-Accept", getWebSocketAccept(key));
    if (subProtocol != null) {
        resp.setHeader("Sec-WebSocket-Protocol", subProtocol);
    }
    if (!extensions.isEmpty()) {
        // TODO
    }

    WsHttpServletRequestWrapper wrapper = new WsHttpServletRequestWrapper(req);
    StreamInbound inbound = createWebSocketInbound(subProtocol, wrapper);
    wrapper.invalidate();

    // Small hack until the Servlet API provides a way to do this.
    ServletRequest inner = req;
    // Unwrap the request
    while (inner instanceof ServletRequestWrapper) {
        inner = ((ServletRequestWrapper) inner).getRequest();
    }
    if (inner instanceof RequestFacade) {
        ((RequestFacade) inner).doUpgrade(inbound);
    } else {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                sm.getString("servlet.reqUpgradeFail"));
    }
}