Search in sources :

Example 21 with DispatcherType

use of javax.servlet.DispatcherType in project undertow by undertow-io.

the class ServletContextImpl method addMappingForServletNames.

void addMappingForServletNames(FilterInfo filterInfo, final EnumSet<DispatcherType> dispatcherTypes, final boolean isMatchAfter, final String... servletNames) {
    DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    for (final String servlet : servletNames) {
        if (isMatchAfter) {
            if (dispatcherTypes == null || dispatcherTypes.isEmpty()) {
                deploymentInfo.addFilterServletNameMapping(filterInfo.getName(), servlet, DispatcherType.REQUEST);
            } else {
                for (final DispatcherType dispatcher : dispatcherTypes) {
                    deploymentInfo.addFilterServletNameMapping(filterInfo.getName(), servlet, dispatcher);
                }
            }
        } else {
            if (dispatcherTypes == null || dispatcherTypes.isEmpty()) {
                deploymentInfo.insertFilterServletNameMapping(filterMappingServletNameInsertPosition++, filterInfo.getName(), servlet, DispatcherType.REQUEST);
            } else {
                for (final DispatcherType dispatcher : dispatcherTypes) {
                    deploymentInfo.insertFilterServletNameMapping(filterMappingServletNameInsertPosition++, filterInfo.getName(), servlet, dispatcher);
                }
            }
        }
    }
    deployment.getServletPaths().invalidate();
}
Also used : DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) DispatcherType(javax.servlet.DispatcherType)

Example 22 with DispatcherType

use of javax.servlet.DispatcherType in project undertow by undertow-io.

the class HttpServletRequestImpl method getHttpServletMapping.

@Override
public HttpServletMapping getHttpServletMapping() {
    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletPathMatch match = src.getOriginalServletPathMatch();
    final DispatcherType dispatcherType = getDispatcherType();
    // UNDERTOW-1899 - ERROR is essentially forward operation
    if (dispatcherType == DispatcherType.FORWARD || dispatcherType == DispatcherType.ERROR) {
        match = src.getServletPathMatch();
    }
    String matchValue;
    switch(match.getMappingMatch()) {
        case EXACT:
            matchValue = match.getMatched();
            if (matchValue.startsWith("/")) {
                matchValue = matchValue.substring(1);
            }
            break;
        case DEFAULT:
        case CONTEXT_ROOT:
            matchValue = "";
            break;
        case PATH:
            matchValue = match.getRemaining();
            if (matchValue == null) {
                matchValue = "";
            } else if (matchValue.startsWith("/")) {
                matchValue = matchValue.substring(1);
            }
            break;
        case EXTENSION:
            String matched = match.getMatched();
            String matchString = match.getMatchString();
            int startIndex = matched.startsWith("/") ? 1 : 0;
            int endIndex = matched.length() - matchString.length() + 1;
            matchValue = matched.substring(startIndex, endIndex);
            break;
        default:
            matchValue = match.getRemaining();
    }
    return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch(), match.getServletChain().getManagedServlet().getServletInfo().getName());
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) HttpString(io.undertow.util.HttpString) DispatcherType(javax.servlet.DispatcherType)

Example 23 with DispatcherType

use of javax.servlet.DispatcherType in project undertow by undertow-io.

the class RequestDispatcherImpl method includeImpl.

private void includeImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext, HttpServletRequestImpl requestImpl, HttpServletResponseImpl responseImpl) throws ServletException, IOException {
    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();
    Object requestUri = null;
    Object contextPath = null;
    Object servletPath = null;
    Object pathInfo = null;
    Object queryString = null;
    Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
    if (!named) {
        requestUri = request.getAttribute(INCLUDE_REQUEST_URI);
        contextPath = request.getAttribute(INCLUDE_CONTEXT_PATH);
        servletPath = request.getAttribute(INCLUDE_SERVLET_PATH);
        pathInfo = request.getAttribute(INCLUDE_PATH_INFO);
        queryString = request.getAttribute(INCLUDE_QUERY_STRING);
        int qsPos = path.indexOf("?");
        String newServletPath = path;
        if (qsPos != -1) {
            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.setQueryParameters(newQueryParameters);
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, newQueryString);
        } else {
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, "");
        }
        String newRequestUri = servletContext.getContextPath() + newServletPath;
        requestImpl.setAttribute(INCLUDE_REQUEST_URI, newRequestUri);
        requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, servletContext.getContextPath());
        requestImpl.setAttribute(INCLUDE_SERVLET_PATH, pathMatch.getMatched());
        requestImpl.setAttribute(INCLUDE_PATH_INFO, pathMatch.getRemaining());
    }
    boolean inInclude = responseImpl.isInsideInclude();
    responseImpl.setInsideInclude(true);
    DispatcherType oldDispatcherType = servletRequestContext.getDispatcherType();
    ServletContextImpl oldContext = requestImpl.getServletContext();
    try {
        requestImpl.setServletContext(servletContext);
        responseImpl.setServletContext(servletContext);
        try {
            servletRequestContext.setServletRequest(request);
            servletRequestContext.setServletResponse(response);
            servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.INCLUDE);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        responseImpl.setInsideInclude(inInclude);
        requestImpl.setServletContext(oldContext);
        responseImpl.setServletContext(oldContext);
        servletRequestContext.setServletRequest(oldRequest);
        servletRequestContext.setServletResponse(oldResponse);
        servletRequestContext.setDispatcherType(oldDispatcherType);
        if (!named) {
            requestImpl.setAttribute(INCLUDE_REQUEST_URI, requestUri);
            requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, contextPath);
            requestImpl.setAttribute(INCLUDE_SERVLET_PATH, servletPath);
            requestImpl.setAttribute(INCLUDE_PATH_INFO, pathInfo);
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, queryString);
            requestImpl.setQueryParameters(queryParameters);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) IOException(java.io.IOException) Deque(java.util.Deque) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) DispatcherType(javax.servlet.DispatcherType) ServletResponseWrapper(javax.servlet.ServletResponseWrapper)

Example 24 with DispatcherType

use of javax.servlet.DispatcherType in project undertow by undertow-io.

the class FilterHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    DispatcherType dispatcher = servletRequestContext.getDispatcherType();
    Boolean supported = asyncSupported.get(dispatcher);
    if (supported != null && !supported) {
        servletRequestContext.setAsyncSupported(false);
    }
    final List<ManagedFilter> filters = this.filters.get(dispatcher);
    if (filters == null) {
        next.handleRequest(exchange);
    } else {
        final FilterChainImpl filterChain = new FilterChainImpl(exchange, filters, next, allowNonStandardWrappers);
        filterChain.doFilter(request, response);
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) ManagedFilter(io.undertow.servlet.core.ManagedFilter) DispatcherType(javax.servlet.DispatcherType)

Example 25 with DispatcherType

use of javax.servlet.DispatcherType in project hive by apache.

the class Main method runServer.

public Server runServer(int port) throws Exception {
    // Authenticate using keytab
    if (UserGroupInformation.isSecurityEnabled()) {
        String serverPrincipal = SecurityUtil.getServerPrincipal(conf.kerberosPrincipal(), "0.0.0.0");
        UserGroupInformation.loginUserFromKeytab(serverPrincipal, conf.kerberosKeytab());
    }
    // Create the Jetty server. If jetty conf file exists, use that to create server
    // to have more control.
    Server server = null;
    if (StringUtils.isEmpty(conf.jettyConfiguration())) {
        server = new Server(port);
    } else {
        FileInputStream jettyConf = new FileInputStream(conf.jettyConfiguration());
        XmlConfiguration configuration = new XmlConfiguration(jettyConf);
        server = (Server) configuration.configure();
    }
    ServletContextHandler root = new ServletContextHandler(server, "/");
    // Add the Auth filter
    FilterHolder fHolder = makeAuthFilter();
    EnumSet<DispatcherType> dispatches = EnumSet.of(DispatcherType.REQUEST);
    /* 
     * We add filters for each of the URIs supported by templeton.
     * If we added the entire sub-structure using '/*', the mapreduce 
     * notification cannot give the callback to templeton in secure mode.
     * This is because mapreduce does not use secure credentials for 
     * callbacks. So jetty would fail the request as unauthorized.
     */
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/ddl/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/pig/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/hive/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/sqoop/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/queue/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/jobs/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/mapreduce/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/status/*", dispatches);
    root.addFilter(fHolder, "/" + SERVLET_PATH + "/v1/version/*", dispatches);
    if (conf.getBoolean(AppConfig.XSRF_FILTER_ENABLED, false)) {
        root.addFilter(makeXSRFFilter(), "/" + SERVLET_PATH + "/*", dispatches);
        LOG.debug("XSRF filter enabled");
    } else {
        LOG.warn("XSRF filter disabled");
    }
    root.addFilter(makeFrameOptionFilter(), "/" + SERVLET_PATH + "/*", dispatches);
    // Connect Jersey
    ServletHolder h = new ServletHolder(new ServletContainer(makeJerseyConfig()));
    root.addServlet(h, "/" + SERVLET_PATH + "/*");
    // Add any redirects
    addRedirects(server);
    // Set handling for low resource conditions.
    final LowResourceMonitor low = new LowResourceMonitor(server);
    low.setLowResourcesIdleTimeout(10000);
    server.addBean(low);
    server.addConnector(createChannelConnector());
    // Start the server
    server.start();
    this.server = server;
    return server;
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(com.sun.jersey.spi.container.servlet.ServletContainer) LowResourceMonitor(org.eclipse.jetty.server.LowResourceMonitor) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType) FileInputStream(java.io.FileInputStream)

Aggregations

DispatcherType (javax.servlet.DispatcherType)45 ServletContext (javax.servlet.ServletContext)6 ServletRequest (javax.servlet.ServletRequest)6 IOException (java.io.IOException)5 FilterMap (org.apache.catalina.deploy.FilterMap)5 ServletResponse (javax.servlet.ServletResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Server (org.eclipse.jetty.server.Server)4 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)4 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 File (java.io.File)3 WebInitParam (javax.servlet.annotation.WebInitParam)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 Test (org.junit.Test)3 ServletFilterMapping (com.sun.enterprise.deployment.web.ServletFilterMapping)2 ServletContainer (com.sun.jersey.spi.container.servlet.ServletContainer)2 ManagedFilter (io.undertow.servlet.core.ManagedFilter)2 ArrayList (java.util.ArrayList)2