Java Code Examples for javax.servlet.http.HttpServletRequest#getDispatcherType()

The following examples show how to use javax.servlet.http.HttpServletRequest#getDispatcherType() . 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: ServletInvocationServiceInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }

    if (!argumentValidator.validate(args)) {
        return;
    }

    try {
        final HttpServletRequest request = (HttpServletRequest) args[0];
        if (request.getDispatcherType() == DispatcherType.ASYNC) {
            if (isDebug) {
                logger.debug("Skip async servlet Request. isAsyncStarted={}, dispatcherType={}", request.isAsyncStarted(), request.getDispatcherType());
            }
            return;
        }

        this.servletRequestListenerInterceptorHelper.initialized(request, ResinConstants.RESIN_METHOD, this.methodDescriptor);
    } catch (Throwable t) {
        if (isInfo) {
            logger.info("Failed to servlet request event handle.", t);
        }
    }
}
 
Example 2
Source File: DefaultServlet.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (allowPost) {
        doGet(req, resp);
    } else {
        /*
         * Where a servlet has received a POST request we still require the capability to include static content.
         */
        switch (req.getDispatcherType()) {
            case INCLUDE:
            case FORWARD:
            case ERROR:
                doGet(req, resp);
                break;
            default:
                super.doPost(req, resp);
        }
    }
}
 
Example 3
Source File: AbstractServerHandleInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }

    try {
        final HttpServletRequest request = toHttpServletRequest(args);
        if (request.getDispatcherType() == DispatcherType.ASYNC || request.getDispatcherType() == DispatcherType.ERROR) {
            if (isDebug) {
                logger.debug("Skip async servlet request event. isAsyncStarted={}, dispatcherType={}", request.isAsyncStarted(), request.getDispatcherType());
            }
            return;
        }
        this.servletRequestListenerInterceptorHelper.initialized(request, JettyConstants.JETTY_METHOD, this.methodDescriptor);
    } catch (Throwable t) {
        logger.info("Failed to servlet request event handle.", t);
    }
}
 
Example 4
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private String getPath(final HttpServletRequest request) {
    String servletPath;
    String pathInfo;

    if (request.getDispatcherType() == DispatcherType.INCLUDE && request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null) {
        pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
        servletPath = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
    } else {
        pathInfo = request.getPathInfo();
        servletPath = request.getServletPath();
    }
    String result = pathInfo;
    if (result == null) {
        result = CanonicalPathUtils.canonicalize(servletPath);
    } else if(resolveAgainstContextRoot) {
        result = servletPath + CanonicalPathUtils.canonicalize(pathInfo);
    } else {
        result = CanonicalPathUtils.canonicalize(result);
    }
    if ((result == null) || (result.isEmpty())) {
        result = "/";
    }
    return result;

}
 
Example 5
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if(allowPost) {
        doGet(req, resp);
    } else {
        /*
         * Where a servlet has received a POST request we still require the capability to include static content.
         */
        switch (req.getDispatcherType()) {
            case INCLUDE:
            case FORWARD:
            case ERROR:
                doGet(req, resp);
                break;
            default:
                super.doPost(req, resp);
        }
    }
}
 
Example 6
Source File: DefaultServlet.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
private String getPath(final HttpServletRequest request) {
    String servletPath;
    String pathInfo;

    if (request.getDispatcherType() == DispatcherType.INCLUDE && request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null) {
        pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
        servletPath = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
    } else {
        pathInfo = request.getPathInfo();
        servletPath = request.getServletPath();
    }
    String result = pathInfo;
    if (result == null) {
        result = CanonicalPathUtils.canonicalize(servletPath);
    } else if (resolveAgainstContextRoot) {
        result = servletPath + CanonicalPathUtils.canonicalize(pathInfo);
    } else {
        result = CanonicalPathUtils.canonicalize(result);
    }
    if ((result == null) || (result.isEmpty())) {
        result = "/";
    }
    return result;

}
 
Example 7
Source File: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if (req.getDispatcherType() == DispatcherType.ERROR) {
        doGet(req, resp);
    } else {
        super.service(req, resp);
    }
}
 
Example 8
Source File: ServletInvocationServiceInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }

    if (!argumentValidator.validate(args)) {
        return;
    }

    try {
        final HttpServletRequest request = (HttpServletRequest) args[0];
        final HttpServletResponse response = (HttpServletResponse) args[1];
        if (request.getDispatcherType() == DispatcherType.ASYNC) {
            if (isDebug) {
                logger.debug("Skip async servlet Request. isAsyncStarted={}, dispatcherType={}", request.isAsyncStarted(), request.getDispatcherType());
            }
            return;
        }

        int statusCode = getStatusCode(response);
        this.servletRequestListenerInterceptorHelper.destroyed(request, throwable, statusCode);
    } catch (Throwable t) {
        if (isInfo) {
            logger.info("Failed to servlet request event handle.", t);
        }
    }
}
 
Example 9
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    switch (req.getDispatcherType()) {
        case INCLUDE:
        case FORWARD:
        case ERROR:
            doGet(req, resp);
            break;
        default:
            super.doTrace(req, resp);
    }
}
 
Example 10
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    switch (req.getDispatcherType()) {
        case INCLUDE:
        case FORWARD:
        case ERROR:
            doGet(req, resp);
            break;
        default:
            super.doDelete(req, resp);
    }
}
 
Example 11
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    switch (req.getDispatcherType()) {
        case INCLUDE:
        case FORWARD:
        case ERROR:
            doGet(req, resp);
            break;
        default:
            super.doPut(req, resp);
    }
}
 
Example 12
Source File: TestAsyncContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if ("y".equals(req.getParameter(DISPATCH_CHECK))) {
        if (req.getDispatcherType() != DispatcherType.ASYNC) {
            track("WrongDispatcherType-");
        }
    }
    track("DispatchingServletGet-");
    final int iter = Integer.parseInt(req.getParameter(ITER_PARAM)) - 1;
    final AsyncContext ctxt = req.startAsync();
    if (addTrackingListener) {
        TrackingListener listener =
            new TrackingListener(completeOnError, true, null);
        ctxt.addListener(listener);
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (iter > 0) {
                ctxt.dispatch("/stage1?" + ITER_PARAM + "=" + iter +
                        "&" + DISPATCH_CHECK + "=y");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 13
Source File: TestAsyncContextImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if ("y".equals(req.getParameter(DISPATCH_CHECK))) {
        if (req.getDispatcherType() != DispatcherType.ASYNC) {
            track("WrongDispatcherType-");
        }
    }
    track("DispatchingServletGet-");
    final int iter = Integer.parseInt(req.getParameter(ITER_PARAM)) - 1;
    final AsyncContext ctxt = req.startAsync();
    if (addTrackingListener) {
        TrackingListener listener =
            new TrackingListener(completeOnError, true, null);
        ctxt.addListener(listener);
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (iter > 0) {
                ctxt.dispatch("/stage1?" + ITER_PARAM + "=" + iter +
                        "&" + DISPATCH_CHECK + "=y");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 14
Source File: DefaultServlet.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    switch (req.getDispatcherType()) {
        case INCLUDE:
        case FORWARD:
        case ERROR:
            doGet(req, resp);
            break;
        default:
            super.doOptions(req, resp);
    }
}
 
Example 15
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (DispatcherType.ASYNC == req.getDispatcherType()) {
        if (ENCODED_URI.equals(req.getRequestURI())) {
            resp.getWriter().print("OK");
        } else {
            resp.getWriter().print("FAIL");
        }
    } else {
        AsyncContext ac = req.startAsync();
        ac.dispatch();
    }
}
 
Example 16
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if ("y".equals(req.getParameter(DISPATCH_CHECK))) {
        if (req.getDispatcherType() != DispatcherType.ASYNC) {
            track("WrongDispatcherType-");
        }
    }
    track("DispatchingServletGet-");
    final int iter = Integer.parseInt(req.getParameter(ITER_PARAM)) - 1;
    final AsyncContext ctxt = req.startAsync();
    if (trackingListener != null) {
        ctxt.addListener(trackingListener);
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (iter > 0) {
                ctxt.dispatch("/stage1?" + ITER_PARAM + "=" + iter +
                        "&" + DISPATCH_CHECK + "=y");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 17
Source File: IdempotentInterceptor.java    From springcloud-idempotent-starter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
	String requestId = request.getHeader(Constants.REQ_IDEM_ID);
	DispatcherType dispatcherType = request.getDispatcherType();
	String method = request.getMethod().toUpperCase();
	logger.info("[preHandle] url:{}, requestId:{}, method:{}, dispatcherType:{}", request.getRequestURI(),
			requestId, method, dispatcherType);

	if (requestId == null || ("GET".equals(method))) {
		return true;
	}

	IdempotentVo idempotentVo = null;
	// 如果redis出现连接异常。所有的幂等操作全部取消
	try {
		idempotentVo = idempotentCacheInterface.get(requestId);
	} catch (Exception e) {
		return true;
	}

	if (idempotentVo.getIdempotentKey() == null) {
		idempotentVo = new IdempotentVo();
		idempotentVo.setIdempotentKey(requestId);
		idempotentVo.setIdempotentStatus(IdempotentVo.IDEMPOMENT_STATUS_START);

		boolean isCreatedLock = idempotentCacheInterface.lock(requestId, idempotentVo);
		
		if (!isCreatedLock) {
			inProcessResp(response);
			return false;
		}
		
		IdempotentHolder.setIdempotentVo(idempotentVo);
		return true;
	}
	if (DispatcherType.REQUEST.equals(dispatcherType)) {
		// String key = IdempotentHolder.getIdempotentKey();
		// logger.info("[preHandle] IdempotentHolder.getIdempotentKey():
		// {}", key);
		// logger.info("[preHandle] is a Idempotent Call: {}");
		if (IdempotentVo.IDEMPOMENT_STATUS_FINISIED.equals(idempotentVo.getIdempotentStatus())) {
			response.setStatus(idempotentVo.getStatusCode());
			if (null != idempotentVo.getResult()) {
				response.getOutputStream().write(idempotentVo.getResult().getBytes());
			}
			if (null != idempotentVo.getHeaders()) {
				Map<String, String> headers = idempotentVo.getHeaders();
				for (String name : headers.keySet()) {
					response.setHeader(name, headers.get(name));
				}
			}

			response.getOutputStream().flush();
			return false;
		} else if (IdempotentVo.IDEMPOMENT_STATUS_REDIRECT.equals(idempotentVo.getIdempotentStatus())) {
			// 除get请求外。其它的请求默认不支持redirect。暂时保留这块。预防以后需要支持get的操作
			logger.debug("[preHandle] reffer to a redirect request");
			IdempotentHolder.setIdempotentVo(idempotentVo);
			return true;
		} else {// 如果不为FINISHED状态。说明正在进展中
			inProcessResp(response);
			return false;
		}

	} else if (DispatcherType.ERROR.equals(dispatcherType)) {
		IdempotentHolder.setIdempotentVo(idempotentVo);
		return true;
	} else if (DispatcherType.FORWARD.equals(dispatcherType)) {
		IdempotentHolder.setIdempotentVo(idempotentVo);
		return true;
	}
	return true;
}
 
Example 18
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    String path = getPath(req);
    if (!isAllowed(path, req.getDispatcherType())) {
        resp.sendError(StatusCodes.NOT_FOUND);
        return;
    }
    if(File.separatorChar != '/') {
        //if the separator char is not / we want to replace it with a / and canonicalise
        path = CanonicalPathUtils.canonicalize(path.replace(File.separatorChar, '/'));
    }

    HttpServerExchange exchange = SecurityActions.requireCurrentServletRequestContext().getOriginalRequest().getExchange();
    final Resource resource;
    //we want to disallow windows characters in the path
    if(File.separatorChar == '/' || !path.contains(File.separator)) {
        resource = resourceSupplier.getResource(exchange, path);
    } else {
        resource = null;
    }

    if (resource == null) {
        if (req.getDispatcherType() == DispatcherType.INCLUDE) {
            //servlet 9.3
            throw new FileNotFoundException(path);
        } else {
            resp.sendError(StatusCodes.NOT_FOUND);
        }
        return;
    } else if (resource.isDirectory()) {
        if ("css".equals(req.getQueryString())) {
            resp.setContentType("text/css");
            resp.getWriter().write(DirectoryUtils.Blobs.FILE_CSS);
            return;
        } else if ("js".equals(req.getQueryString())) {
            resp.setContentType("application/javascript");
            resp.getWriter().write(DirectoryUtils.Blobs.FILE_JS);
            return;
        }
        if (directoryListingEnabled) {
            StringBuilder output = DirectoryUtils.renderDirectoryListing(req.getRequestURI(), resource);
            resp.getWriter().write(output.toString());
        } else {
            resp.sendError(StatusCodes.FORBIDDEN);
        }
    } else {
        if(path.endsWith("/")) {
            //UNDERTOW-432
            resp.sendError(StatusCodes.NOT_FOUND);
            return;
        }
        serveFileBlocking(req, resp, resource, exchange);
    }
}
 
Example 19
Source File: Servlet3ApiHelper.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAsyncDispatcherAfter(HttpServletRequest request) {
    return request.getDispatcherType() == DispatcherType.ASYNC;
}
 
Example 20
Source File: Servlet3ApiHelper.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAsyncDispatcherBefore(HttpServletRequest request) {
    return request.isAsyncStarted() || request.getDispatcherType() == DispatcherType.ASYNC;
}