Search in sources :

Example 26 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project ddf by codice.

the class SparkServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String relativePath = getRelativePath(req, filterPath);
    HttpServletRequestWrapper requestWrapper;
    synchronized (this) {
        requestWrapper = requestSupplier.apply(req, relativePath);
    }
    // handle static resources
    boolean consumed = StaticFilesConfiguration.servletInstance.consume(req, resp);
    if (consumed) {
        return;
    }
    matcherFilter.doFilter(requestWrapper, resp, null);
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 27 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project sling by apache.

the class SlingServlet method service.

/**
     * If Sling has already been started, the request is forwarded to the
     * started Sling framework. Otherwise the Sling framework is started unless
     * there were too many startup failures.
     * <p>
     * If the request is not forwarded to Sling, this method returns a 404/NOT
     * FOUND if the startup failure counter has exceeded or 503/SERVICE
     * UNAVAILABLE if the Sling framework is starting up.
     * <p>
     * If a request causes the framework to start, it is immediately terminated
     * with said response status and framework is started in a separate thread.
     */
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    // delegate the request to the registered delegatee servlet
    Servlet delegatee = sling;
    if (delegatee != null) {
        // check for problematic application servers like WebSphere
        // where path info and servlet path is set wrong SLING-2410
        final HttpServletRequest request = (HttpServletRequest) req;
        if (request.getPathInfo() == null && request.getServletPath() != null && request.getServletPath().endsWith(".jsp")) {
            req = new HttpServletRequestWrapper(request) {

                @Override
                public String getPathInfo() {
                    return request.getServletPath();
                }

                @Override
                public String getServletPath() {
                    return "";
                }
            };
        }
        delegatee.service(req, res);
    } else if (startFailureCounter > MAX_START_FAILURES) {
        // too many startup retries, fail for ever
        ((HttpServletResponse) res).sendError(HttpServletResponse.SC_NOT_FOUND);
    } else {
        startSling(req);
        ((HttpServletResponse) res).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Apache Sling is currently starting up, please try again");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet)

Example 28 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project ddf by codice.

the class SparkServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String relativePath = getRelativePath(req, filterPath);
    HttpServletRequestWrapper requestWrapper;
    synchronized (this) {
        requestWrapper = requestSupplier.apply(req, relativePath);
    }
    // handle static resources
    boolean consumed = StaticFilesConfiguration.servletInstance.consume(req, resp);
    if (consumed) {
        return;
    }
    matcherFilter.doFilter(requestWrapper, resp, null);
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 29 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project uPortal by Jasig.

the class RemoteUserSettingFilter method doFilter.

/* (non-Javadoc)
     * @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 {
    final String remoteUser = StringUtils.trimToNull(FileUtils.readFileToString(this.remoteUserFile));
    if (remoteUser != null) {
        request = new HttpServletRequestWrapper((HttpServletRequest) request) {

            /* (non-Javadoc)
                         * @see javax.servlet.http.HttpServletRequestWrapper#getRemoteUser()
                         */
            @Override
            public String getRemoteUser() {
                return remoteUser;
            }

            /* (non-Javadoc)
                         * @see javax.servlet.http.HttpServletRequestWrapper#getHeader(java.lang.String)
                         */
            @Override
            public String getHeader(String name) {
                if ("REMOTE_USER".equals(name)) {
                    return remoteUser;
                }
                return super.getHeader(name);
            }

            /* (non-Javadoc)
                         * @see javax.servlet.http.HttpServletRequestWrapper#getHeaders(java.lang.String)
                         */
            @Override
            public Enumeration<String> getHeaders(String name) {
                if ("REMOTE_USER".equals(name)) {
                    return Iterators.asEnumeration(Collections.singleton(remoteUser).iterator());
                }
                return super.getHeaders(name);
            }

            /* (non-Javadoc)
                         * @see javax.servlet.http.HttpServletRequestWrapper#getHeaderNames()
                         */
            @Override
            public Enumeration<String> getHeaderNames() {
                final LinkedHashSet<String> headers = new LinkedHashSet<String>();
                for (final Enumeration<String> headersEnum = super.getHeaderNames(); headersEnum.hasMoreElements(); ) {
                    headers.add(headersEnum.nextElement());
                }
                headers.add("REMOTE_USER");
                return Iterators.asEnumeration(headers.iterator());
            }

            /* (non-Javadoc)
                         * @see javax.servlet.http.HttpServletRequestWrapper#getIntHeader(java.lang.String)
                         */
            @Override
            public int getIntHeader(String name) {
                if ("REMOTE_USER".equals(name)) {
                    return Integer.valueOf(remoteUser);
                }
                return super.getIntHeader(name);
            }
        };
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LinkedHashSet(java.util.LinkedHashSet) Enumeration(java.util.Enumeration) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 30 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project ORCID-Source by ORCID.

the class JsonpCallbackFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    Map<String, String[]> parms = httpRequest.getParameterMap();
    if (parms.containsKey("callback")) {
        HttpServletRequestWrapper requestWrapper = new AcceptHeaderRequestWrapper(httpRequest, "application/json");
        OutputStream out = httpResponse.getOutputStream();
        GenericResponseWrapper responseWrapper = new GenericResponseWrapper(httpResponse);
        filterChain.doFilter(requestWrapper, responseWrapper);
        out.write(new String(parms.get("callback")[0] + "(").getBytes());
        out.write(responseWrapper.getData());
        out.write(new String(");").getBytes());
        responseWrapper.setContentType("text/javascript;charset=UTF-8");
        out.close();
    } else {
        filterChain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)54 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ServletRequest (javax.servlet.ServletRequest)13 ServletResponse (javax.servlet.ServletResponse)11 IOException (java.io.IOException)10 FilterChain (javax.servlet.FilterChain)10 ServletException (javax.servlet.ServletException)9 Test (org.junit.Test)9 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)8 FilterConfig (javax.servlet.FilterConfig)7 Principal (java.security.Principal)6 Filter (javax.servlet.Filter)6 ArrayList (java.util.ArrayList)5 Provider (com.google.inject.Provider)4 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)4 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)4 OutputStream (java.io.OutputStream)4 Cookie (javax.servlet.http.Cookie)4 Injector (com.google.inject.Injector)2