Java Code Examples for javax.servlet.DispatcherType#REQUEST

The following examples show how to use javax.servlet.DispatcherType#REQUEST . 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: ServletSecurityRoleHandler.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest request = servletRequestContext.getServletRequest();
    if (request.getDispatcherType() == DispatcherType.REQUEST) {
        List<SingleConstraintMatch> constraints = servletRequestContext.getRequiredConstrains();
        SecurityContext sc = exchange.getSecurityContext();
        if (!authorizationManager.canAccessResource(constraints, sc.getAuthenticatedAccount(), servletRequestContext.getCurrentServlet().getManagedServlet().getServletInfo(), servletRequestContext.getOriginalRequest(), servletRequestContext.getDeployment())) {

            HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
            response.sendError(StatusCodes.FORBIDDEN);
            return;
        }
    }
    next.handleRequest(exchange);
}
 
Example 2
Source File: DefaultWebApplicationRequest.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructor.
 */
public DefaultWebApplicationRequest() {
    this.authType = null;
    this.asyncStarted = false;
    this.asyncSupported = false;
    this.attributeManager = new DefaultAttributeManager();
    this.characterEncoding = "ISO-8859-1";
    this.contentLength = -1;
    this.contentType = null;
    this.contextPath = "";
    this.cookies = null;
    this.dispatcherType = DispatcherType.REQUEST;
    this.headerManager = new DefaultHttpHeaderManager();
    this.headerManager.setHeader("Accept", "*/*");
    this.inputStream = new ByteArrayInputStream(new byte[0]);
    this.method = "GET";
    this.protocol = "HTTP/1.1";
    this.scheme = "http";
    this.serverName = "localhost";
    this.serverPort = 80;
    this.servletPath = "";
    this.parameters = new HashMap<>();
    this.upgraded = false;
}
 
Example 3
Source File: ServletSecurityRoleHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest request = servletRequestContext.getServletRequest();
    if (request.getDispatcherType() == DispatcherType.REQUEST) {
        List<SingleConstraintMatch> constraints = servletRequestContext.getRequiredConstrains();
        SecurityContext sc = exchange.getSecurityContext();
        if (!authorizationManager.canAccessResource(constraints, sc.getAuthenticatedAccount(), servletRequestContext.getCurrentServlet().getManagedServlet().getServletInfo(), servletRequestContext.getOriginalRequest(), servletRequestContext.getDeployment())) {

            HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
            response.sendError(StatusCodes.FORBIDDEN);
            return;
        }
    }
    next.handleRequest(exchange);
}
 
Example 4
Source File: DefaultServlet.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private boolean isAllowed(String path, DispatcherType dispatcherType) {
    if (!path.isEmpty()) {
        if (dispatcherType == DispatcherType.REQUEST) {
            //WFLY-3543 allow the dispatcher to access stuff in web-inf and meta inf
            if (path.startsWith("/META-INF") ||
                    path.startsWith("META-INF") ||
                    path.startsWith("/WEB-INF") ||
                    path.startsWith("WEB-INF")) {
                return false;
            }
        }
    }
    if (defaultAllowed && disallowed.isEmpty()) {
        return true;
    }
    int pos = path.lastIndexOf('/');
    final String lastSegment;
    if (pos == -1) {
        lastSegment = path;
    } else {
        lastSegment = path.substring(pos + 1);
    }
    if (lastSegment.isEmpty()) {
        return true;
    }
    int ext = lastSegment.lastIndexOf('.');
    if (ext == -1) {
        //no extension
        return true;
    }
    final String extension = lastSegment.substring(ext + 1, lastSegment.length());
    if (defaultAllowed) {
        return !disallowed.contains(extension);
    } else {
        return allowed.contains(extension);
    }
}
 
Example 5
Source File: ServletInitialHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private void dispatchRequest(final HttpServerExchange exchange, final ServletRequestContext servletRequestContext, final ServletChain servletChain, final DispatcherType dispatcherType) throws Exception {
    servletRequestContext.setDispatcherType(dispatcherType);
    servletRequestContext.setCurrentServlet(servletChain);
    if (dispatcherType == DispatcherType.REQUEST || dispatcherType == DispatcherType.ASYNC) {
        firstRequestHandler.call(exchange, servletRequestContext);
    } else {
        next.handleRequest(exchange);
    }
}
 
Example 6
Source File: Request.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    if (internalDispatcherType == null) {
        return DispatcherType.REQUEST;
    }

    return this.internalDispatcherType;
}
 
Example 7
Source File: Request.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    if (internalDispatcherType == null) {
        return DispatcherType.REQUEST;
    }

    return this.internalDispatcherType;
}
 
Example 8
Source File: ServletInitialHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void dispatchRequest(final HttpServerExchange exchange, final ServletRequestContext servletRequestContext, final ServletChain servletChain, final DispatcherType dispatcherType) throws Exception {
    servletRequestContext.setDispatcherType(dispatcherType);
    servletRequestContext.setCurrentServlet(servletChain);
    if (dispatcherType == DispatcherType.REQUEST || dispatcherType == DispatcherType.ASYNC) {
        firstRequestHandler.call(exchange, servletRequestContext);
    } else {
        next.handleRequest(exchange);
    }
}
 
Example 9
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isAllowed(String path, DispatcherType dispatcherType) {
    if (!path.isEmpty()) {
        if(dispatcherType == DispatcherType.REQUEST) {
            //WFLY-3543 allow the dispatcher to access stuff in web-inf and meta inf
            if (path.startsWith("/META-INF") ||
                    path.startsWith("META-INF") ||
                    path.startsWith("/WEB-INF") ||
                    path.startsWith("WEB-INF")) {
                return false;
            }
        }
    }
    if(defaultAllowed && disallowed.isEmpty()) {
        return true;
    }
    int pos = path.lastIndexOf('/');
    final String lastSegment;
    if (pos == -1) {
        lastSegment = path;
    } else {
        lastSegment = path.substring(pos + 1);
    }
    if (lastSegment.isEmpty()) {
        return true;
    }
    int ext = lastSegment.lastIndexOf('.');
    if (ext == -1) {
        //no extension
        return true;
    }
    final String extension = lastSegment.substring(ext + 1, lastSegment.length());
    if (defaultAllowed) {
        return !disallowed.contains(extension);
    } else {
        return allowed.contains(extension);
    }
}
 
Example 10
Source File: TlsClientAuthenticationEnforcer.java    From vespa with Apache License 2.0 4 votes vote down vote up
private boolean isHttpsRequest(Request request) {
    return request.getDispatcherType() == DispatcherType.REQUEST && request.getScheme().equalsIgnoreCase("https");
}
 
Example 11
Source File: HttpRequestImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    return DispatcherType.REQUEST;
}
 
Example 12
Source File: InternalHttpRequest.java    From Web-API with MIT License 4 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    return DispatcherType.REQUEST;
}
 
Example 13
Source File: Request.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public Object get(Request request, String name) {
    return (request.internalDispatcherType == null) ? DispatcherType.REQUEST
            : request.internalDispatcherType;
}
 
Example 14
Source File: LocalHttpServletRequest.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    return DispatcherType.REQUEST;
}
 
Example 15
Source File: Request.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public Object get(Request request, String name) {
    return (request.internalDispatcherType == null) ? DispatcherType.REQUEST
            : request.internalDispatcherType;
}
 
Example 16
Source File: NettyHttpServletRequest.java    From Jinx with Apache License 2.0 4 votes vote down vote up
@Override
public DispatcherType getDispatcherType() {
    return attributes.containsKey(DISPATCHER_TYPE) ? (DispatcherType) attributes.get(DISPATCHER_TYPE) : DispatcherType.REQUEST;
}
 
Example 17
Source File: ServletApiAdvice.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterServletService(@Advice.Argument(0) ServletRequest servletRequest,
                                         @Advice.Local("transaction") Transaction transaction,
                                         @Advice.Local("scope") Scope scope) {
    if (tracer == null) {
        return;
    }
    // re-activate transactions for async requests
    final Transaction transactionAttr = (Transaction) servletRequest.getAttribute(TRANSACTION_ATTRIBUTE);
    if (tracer.currentTransaction() == null && transactionAttr != null) {
        scope = transactionAttr.activateInScope();
    }
    if (tracer.isRunning() &&
        servletTransactionHelper != null &&
        servletRequest instanceof HttpServletRequest &&
        servletRequest.getDispatcherType() == DispatcherType.REQUEST &&
        !Boolean.TRUE.equals(excluded.get())) {

        ServletContext servletContext = servletRequest.getServletContext();
        if (servletContext != null) {
            // this makes sure service name discovery also works when attaching at runtime
            determineServiceName(servletContext.getServletContextName(), servletContext.getClassLoader(), servletContext.getContextPath());
        }

        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (ServletInstrumentation.servletTransactionCreationHelperManager != null) {
            ServletInstrumentation.ServletTransactionCreationHelper<HttpServletRequest> helper =
                ServletInstrumentation.servletTransactionCreationHelperManager.getForClassLoaderOfClass(HttpServletRequest.class);
            if (helper != null) {
                transaction = helper.createAndActivateTransaction(request);
            }
        }

        if (transaction == null) {
            // if the request is excluded, avoid matching all exclude patterns again on each filter invocation
            excluded.set(Boolean.TRUE);
            return;
        }
        final Request req = transaction.getContext().getRequest();
        if (transaction.isSampled() && tracer.getConfig(CoreConfiguration.class).isCaptureHeaders()) {
            if (request.getCookies() != null) {
                for (Cookie cookie : request.getCookies()) {
                    req.addCookie(cookie.getName(), cookie.getValue());
                }
            }
            final Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while (headerNames.hasMoreElements()) {
                    final String headerName = headerNames.nextElement();
                    req.addHeader(headerName, request.getHeaders(headerName));
                }
            }
        }
        transaction.setFrameworkName(FRAMEWORK_NAME);

        servletTransactionHelper.fillRequestContext(transaction, request.getProtocol(), request.getMethod(), request.isSecure(),
            request.getScheme(), request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString(),
            request.getRemoteAddr(), request.getHeader("Content-Type"));
    }
}
 
Example 18
Source File: TestHttpServletRequest.java    From caja with Apache License 2.0 4 votes vote down vote up
public DispatcherType getDispatcherType() {
  return DispatcherType.REQUEST;
}
 
Example 19
Source File: TestApplicationContextGetRequestDispatcherB.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    Assert.assertEquals(expectedRequestURI, req.getRequestURI());
    Assert.assertEquals(expectedContextPath, req.getContextPath());
    Assert.assertEquals(expectedServletPath, req.getServletPath());
    Assert.assertEquals(expectedPathInfo, req.getPathInfo());
    Assert.assertEquals(expectedQueryString, req.getQueryString());
    HttpServletMapping mapping =
            ((org.apache.catalina.servlet4preview.http.HttpServletRequest) req).getHttpServletMapping();
    Assert.assertEquals(expectedMappingMatch, mapping.getMappingMatch());
    Assert.assertEquals(expectedMappingPattern, mapping.getPattern());
    Assert.assertEquals(expectedMappingMatchValue, mapping.getMatchValue());
    Assert.assertEquals(expectedMappingServletName, mapping.getServletName());

    for (DispatcherType type : DispatcherType.values()) {
        if (type == dispatcherType) {
            String name = dispatcherType.name().toLowerCase(Locale.ENGLISH);
            Assert.assertEquals(expectedDispatcherRequestURI,
                    req.getAttribute("javax.servlet." + name + ".request_uri"));
            Assert.assertEquals(expectedDispatcherContextPath,
                    req.getAttribute("javax.servlet." + name + ".context_path"));
            Assert.assertEquals(expectedDispatcherServletPath,
                    req.getAttribute("javax.servlet." + name + ".servlet_path"));
            Assert.assertEquals(expectedDispatcherPathInfo,
                    req.getAttribute("javax.servlet." + name + ".path_info"));
            Assert.assertEquals(expectedDispatcherQueryString,
                    req.getAttribute("javax.servlet." + name + ".query_string"));
            HttpServletMapping dispatcherMapping =
                    (HttpServletMapping) ((org.apache.catalina.servlet4preview.http.HttpServletRequest) req).getAttribute(
                            "javax.servlet." + name + ".mapping");
            Assert.assertNotNull(dispatcherMapping);
            Assert.assertEquals(expectedDispatcherMappingMatch,
                    dispatcherMapping.getMappingMatch());
            Assert.assertEquals(expectedDispatcherMappingPattern,
                    dispatcherMapping.getPattern());
            Assert.assertEquals(expectedDispatcherMappingMatchValue,
                    dispatcherMapping.getMatchValue());
            Assert.assertEquals(expectedDispatcherMappingServletName,
                    dispatcherMapping.getServletName());
        } else if (type == DispatcherType.ERROR || type == DispatcherType.REQUEST) {
            // Skip - not tested
        } else {
            assertAllNull(req, type.name().toLowerCase(Locale.ENGLISH));
        }
    }

    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter pw = resp.getWriter();
    pw.print("OK");
}
 
Example 20
Source File: Request.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Object get(Request request, String name) {
    return (request.internalDispatcherType == null) ? DispatcherType.REQUEST
            : request.internalDispatcherType;
}