Search in sources :

Example 71 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project tomcat by apache.

the class WebdavFixFilter method doFilter.

/**
     * Check for the broken MS WebDAV client and if detected issue a re-direct
     * that hopefully will cause the non-broken client to be used.
     */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
        chain.doFilter(request, response);
        return;
    }
    HttpServletRequest httpRequest = ((HttpServletRequest) request);
    HttpServletResponse httpResponse = ((HttpServletResponse) response);
    String ua = httpRequest.getHeader("User-Agent");
    if (ua == null || ua.length() == 0 || !ua.startsWith(UA_MINIDIR_START)) {
        // No UA or starts with non MS value
        // Hope everything just works...
        chain.doFilter(request, response);
    } else if (ua.startsWith(UA_MINIDIR_5_1_2600)) {
        // XP 32-bit SP3 - needs redirect with explicit port
        httpResponse.sendRedirect(buildRedirect(httpRequest));
    } else if (ua.startsWith(UA_MINIDIR_5_2_3790)) {
        // XP 64-bit SP2
        if (!"".equals(httpRequest.getContextPath())) {
            log("XP-x64-SP2 clients only work with the root context");
        }
        // Namespace issue maybe
        // see http://greenbytes.de/tech/webdav/webdav-redirector-list.html
        log("XP-x64-SP2 is known not to work with WebDAV Servlet");
        chain.doFilter(request, response);
    } else {
        // Don't know which MS client it is - try the redirect with an
        // explicit port in the hope that it moves the client to a different
        // WebDAV implementation that works
        httpResponse.sendRedirect(buildRedirect(httpRequest));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 72 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project tomcat by apache.

the class ExpiresFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        if (response.isCommitted()) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("expiresFilter.responseAlreadyCommited", httpRequest.getRequestURL()));
            }
            chain.doFilter(request, response);
        } else {
            XHttpServletResponse xResponse = new XHttpServletResponse(httpRequest, httpResponse);
            chain.doFilter(request, xResponse);
            if (!xResponse.isWriteResponseBodyStarted()) {
                // Empty response, manually trigger
                // onBeforeWriteResponseBody()
                onBeforeWriteResponseBody(httpRequest, xResponse);
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 73 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project tomcat by apache.

the class SecurityUtil method execute.

/**
     * Perform work as a particular <code>Subject</code>. Here the work
     * will be granted to a <code>null</code> subject.
     *
     * @param methodName the method to apply the security restriction
     * @param targetObject the <code>Servlet</code> on which the method will
     *  be called.
     * @param targetArguments <code>Object</code> array contains the
     *  runtime parameters instance.
     * @param principal the <code>Principal</code> to which the security
     *  privilege applies
     * @throws Exception an execution error occurred
     */
private static void execute(final Method method, final Object targetObject, final Object[] targetArguments, Principal principal) throws Exception {
    try {
        Subject subject = null;
        PrivilegedExceptionAction<Void> pea = new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                method.invoke(targetObject, targetArguments);
                return null;
            }
        };
        // The first argument is always the request object
        if (targetArguments != null && targetArguments[0] instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) targetArguments[0];
            boolean hasSubject = false;
            HttpSession session = request.getSession(false);
            if (session != null) {
                subject = (Subject) session.getAttribute(Globals.SUBJECT_ATTR);
                hasSubject = (subject != null);
            }
            if (subject == null) {
                subject = new Subject();
                if (principal != null) {
                    subject.getPrincipals().add(principal);
                }
            }
            if (session != null && !hasSubject) {
                session.setAttribute(Globals.SUBJECT_ATTR, subject);
            }
        }
        Subject.doAsPrivileged(subject, pea, null);
    } catch (PrivilegedActionException pe) {
        Throwable e;
        if (pe.getException() instanceof InvocationTargetException) {
            e = pe.getException().getCause();
            ExceptionUtils.handleThrowable(e);
        } else {
            e = pe;
        }
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("SecurityUtil.doAsPrivilege"), e);
        }
        if (e instanceof UnavailableException)
            throw (UnavailableException) e;
        else if (e instanceof ServletException)
            throw (ServletException) e;
        else if (e instanceof IOException)
            throw (IOException) e;
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new ServletException(e.getMessage(), e);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) HttpSession(javax.servlet.http.HttpSession) UnavailableException(javax.servlet.UnavailableException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) Subject(javax.security.auth.Subject) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Example 74 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project tomcat by apache.

the class WsFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // This filter only needs to handle WebSocket upgrade requests
    if (!sc.areEndpointsRegistered() || !UpgradeUtil.isWebSocketUpgradeRequest(request, response)) {
        chain.doFilter(request, response);
        return;
    }
    // HTTP request with an upgrade header for WebSocket present
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    // Check to see if this WebSocket implementation has a matching mapping
    String path;
    String pathInfo = req.getPathInfo();
    if (pathInfo == null) {
        path = req.getServletPath();
    } else {
        path = req.getServletPath() + pathInfo;
    }
    WsMappingResult mappingResult = sc.findMapping(path);
    if (mappingResult == null) {
        // No endpoint registered for the requested path. Let the
        // application handle it (it might redirect or forward for example)
        chain.doFilter(request, response);
        return;
    }
    UpgradeUtil.doUpgrade(sc, req, resp, mappingResult.getConfig(), mappingResult.getPathParams());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 75 with HttpServletRequest

use of javax.servlet.http.HttpServletRequest in project tomcat by apache.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch() {
    check();
    String path;
    String pathInfo;
    ServletRequest servletRequest = getRequest();
    if (servletRequest instanceof HttpServletRequest) {
        HttpServletRequest sr = (HttpServletRequest) servletRequest;
        path = sr.getServletPath();
        pathInfo = sr.getPathInfo();
    } else {
        path = request.getServletPath();
        pathInfo = request.getPathInfo();
    }
    if (pathInfo != null) {
        path += pathInfo;
    }
    if (this.context.getDispatchersUseEncodedPaths()) {
        path = URLEncoder.DEFAULT.encode(path, "UTF-8");
    }
    dispatch(path);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest)

Aggregations

HttpServletRequest (javax.servlet.http.HttpServletRequest)2488 HttpServletResponse (javax.servlet.http.HttpServletResponse)1308 Test (org.junit.Test)987 IOException (java.io.IOException)595 ServletException (javax.servlet.ServletException)498 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 FilterChain (javax.servlet.FilterChain)200 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)196 Test (org.testng.annotations.Test)168 Request (org.eclipse.jetty.server.Request)164 CountDownLatch (java.util.concurrent.CountDownLatch)160 HttpServlet (javax.servlet.http.HttpServlet)156 HttpSession (javax.servlet.http.HttpSession)150 HashMap (java.util.HashMap)130 PrintWriter (java.io.PrintWriter)121 Map (java.util.Map)100 InterruptedIOException (java.io.InterruptedIOException)97 ServletRequest (javax.servlet.ServletRequest)95 ServletContext (javax.servlet.ServletContext)91 ServletOutputStream (javax.servlet.ServletOutputStream)90