Search in sources :

Example 31 with HttpServletRequestWrapper

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

the class JsonpCallbackFilterWeb 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")) {
        if (crossDomainWebManger.allowed(request)) {
            if (log.isDebugEnabled())
                log.debug("Wrapping response with JSONP callback '" + parms.get("callback")[0] + "'");
            String callbackParam = parms.get("callback")[0];
            // make sure callback is valid
            if (!callbackParam.equals(callbackParam.replaceAll("[^0-9a-zA-Z_$]", ""))) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().println("<html><head><title>Oops an error happened!</title></head>");
                response.getWriter().println("<body>400 Bad Request: Callback url param is not valid</body>");
                response.getWriter().println("</html>");
                return;
            }
            HttpServletRequestWrapper requestWrapper = new AcceptHeaderRequestWrapper(httpRequest, "application/json");
            OutputStream out = httpResponse.getOutputStream();
            GenericResponseWrapper responseWrapper = new GenericResponseWrapper(httpResponse);
            filterChain.doFilter(requestWrapper, responseWrapper);
            out.write(new String("/* jsonp callback */ \n" + callbackParam + "(").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)

Example 32 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project hudson-2.x by hudson.

the class ServletRegistrationFilterAdapter method doFilter.

private void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
    assert request != null;
    assert response != null;
    assert chain != null;
    String contextPath = request.getContextPath();
    if (!contextPath.endsWith("/") && !uriPrefix.startsWith("/")) {
        contextPath = contextPath + '/';
    }
    if (request.getRequestURI().startsWith(contextPath + uriPrefix)) {
        // Wrap the request to augment the servlet uriPrefix
        HttpServletRequestWrapper req = new HttpServletRequestWrapper(request) {

            @Override
            public String getServletPath() {
                return String.format("/%s", uriPrefix);
            }
        };
        servlet.service(req, response);
    } else {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 33 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper 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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper)

Example 34 with HttpServletRequestWrapper

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

the class Request method setRequest.

/**
     * Set a wrapped HttpServletRequest to pass to the application. Components
     * wishing to wrap the request should obtain the request via
     * {@link #getRequest()}, wrap it and then call this method with the
     * wrapped request.
     *
     * @param applicationRequest The wrapped request to pass to the application
     */
public void setRequest(HttpServletRequest applicationRequest) {
    // Check the wrapper wraps this request
    ServletRequest r = applicationRequest;
    while (r instanceof HttpServletRequestWrapper) {
        r = ((HttpServletRequestWrapper) r).getRequest();
    }
    if (r != facade) {
        throw new IllegalArgumentException(sm.getString("request.illegalWrap"));
    }
    this.applicationRequest = applicationRequest;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Example 35 with HttpServletRequestWrapper

use of javax.servlet.http.HttpServletRequestWrapper in project guice by google.

the class ServletDefinition method doService.

/**
   * Utility that delegates to the actual service method of the servlet wrapped with a contextual
   * request (i.e. with correctly computed path info).
   *
   * <p>We need to suppress deprecation coz we use HttpServletRequestWrapper, which implements
   * deprecated API for backwards compatibility.
   */
void doService(final ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    HttpServletRequest request = new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {

        private boolean pathComputed;

        private String path;

        private boolean pathInfoComputed;

        private String pathInfo;

        @Override
        public String getPathInfo() {
            if (!isPathInfoComputed()) {
                String servletPath = getServletPath();
                int servletPathLength = servletPath.length();
                String requestUri = getRequestURI();
                pathInfo = requestUri.substring(getContextPath().length()).replaceAll("[/]{2,}", "/");
                // See: https://github.com/google/guice/issues/372
                if (pathInfo.startsWith(servletPath)) {
                    pathInfo = pathInfo.substring(servletPathLength);
                    // then pathinfo is null.
                    if (pathInfo.isEmpty() && servletPathLength > 0) {
                        pathInfo = null;
                    } else {
                        try {
                            pathInfo = new URI(pathInfo).getPath();
                        } catch (URISyntaxException e) {
                        // ugh, just leave it alone then
                        }
                    }
                } else {
                    // we know nothing additional about the URI.
                    pathInfo = null;
                }
                pathInfoComputed = true;
            }
            return pathInfo;
        }

        // NOTE(dhanji): These two are a bit of a hack to help ensure that request dispatcher-sent
        // requests don't use the same path info that was memoized for the original request.
        // NOTE(iqshum): I don't think this is possible, since the dispatcher-sent request would
        // perform its own wrapping.
        private boolean isPathInfoComputed() {
            return pathInfoComputed && servletRequest.getAttribute(REQUEST_DISPATCHER_REQUEST) == null;
        }

        private boolean isPathComputed() {
            return pathComputed && servletRequest.getAttribute(REQUEST_DISPATCHER_REQUEST) == null;
        }

        @Override
        public String getServletPath() {
            return computePath();
        }

        @Override
        public String getPathTranslated() {
            final String info = getPathInfo();
            return (null == info) ? null : getRealPath(info);
        }

        // Memoizer pattern.
        private String computePath() {
            if (!isPathComputed()) {
                String servletPath = super.getServletPath();
                path = patternMatcher.extractPath(servletPath);
                pathComputed = true;
                if (null == path) {
                    path = servletPath;
                }
            }
            return path;
        }
    };
    doServiceImpl(request, (HttpServletResponse) servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

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