Search in sources :

Example 1 with ServletPathMatch

use of io.undertow.servlet.handlers.ServletPathMatch 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 2 with ServletPathMatch

use of io.undertow.servlet.handlers.ServletPathMatch in project undertow by undertow-io.

the class RequestDispatcherImpl method forwardImpl.

private void forwardImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext) throws ServletException, IOException {
    final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    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);
            }
        }
    }
    response.resetBuffer();
    final ServletRequest oldRequest = servletRequestContext.getServletRequest();
    final ServletResponse oldResponse = servletRequestContext.getServletResponse();
    Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
    request.removeAttribute(INCLUDE_REQUEST_URI);
    request.removeAttribute(INCLUDE_CONTEXT_PATH);
    request.removeAttribute(INCLUDE_SERVLET_PATH);
    request.removeAttribute(INCLUDE_PATH_INFO);
    request.removeAttribute(INCLUDE_QUERY_STRING);
    final String oldURI = requestImpl.getExchange().getRequestURI();
    final String oldRequestPath = requestImpl.getExchange().getRequestPath();
    final String oldPath = requestImpl.getExchange().getRelativePath();
    final ServletPathMatch oldServletPathMatch = requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletPathMatch();
    if (!named) {
        // only update if this is the first forward
        if (request.getAttribute(FORWARD_REQUEST_URI) == null) {
            requestImpl.setAttribute(FORWARD_REQUEST_URI, requestImpl.getRequestURI());
            requestImpl.setAttribute(FORWARD_CONTEXT_PATH, requestImpl.getContextPath());
            requestImpl.setAttribute(FORWARD_SERVLET_PATH, requestImpl.getServletPath());
            requestImpl.setAttribute(FORWARD_PATH_INFO, requestImpl.getPathInfo());
            requestImpl.setAttribute(FORWARD_QUERY_STRING, requestImpl.getQueryString());
        }
        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.getExchange().setQueryString(newQueryString);
            requestImpl.setQueryParameters(newQueryParameters);
        }
        String newRequestUri = servletContext.getContextPath() + newServletPath;
        requestImpl.getExchange().setRelativePath(newServletPath);
        requestImpl.getExchange().setRequestPath(newRequestUri);
        requestImpl.getExchange().setRequestURI(newRequestUri);
        requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(pathMatch);
        requestImpl.setServletContext(servletContext);
        responseImpl.setServletContext(servletContext);
    }
    try {
        try {
            servletRequestContext.setServletRequest(request);
            servletRequestContext.setServletResponse(response);
            if (named) {
                servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.FORWARD);
            } else {
                servletContext.getDeployment().getServletDispatcher().dispatchToPath(requestImpl.getExchange(), pathMatch, DispatcherType.FORWARD);
            }
            // if we are not in an async or error dispatch then we close the response
            if (!request.isAsyncStarted()) {
                if (response instanceof HttpServletResponseImpl) {
                    responseImpl.closeStreamAndWriter();
                } else {
                    try {
                        final PrintWriter writer = response.getWriter();
                        writer.flush();
                        writer.close();
                    } catch (IllegalStateException e) {
                        final ServletOutputStream outputStream = response.getOutputStream();
                        outputStream.flush();
                        outputStream.close();
                    }
                }
            }
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        servletRequestContext.setServletRequest(oldRequest);
        servletRequestContext.setServletResponse(oldResponse);
        final boolean preservePath = servletRequestContext.getDeployment().getDeploymentInfo().isPreservePathOnForward();
        if (preservePath) {
            requestImpl.getExchange().setRelativePath(oldPath);
            requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(oldServletPathMatch);
            requestImpl.getExchange().setRequestPath(oldRequestPath);
            requestImpl.getExchange().setRequestURI(oldURI);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletOutputStream(javax.servlet.ServletOutputStream) 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) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) PrintWriter(java.io.PrintWriter)

Example 3 with ServletPathMatch

use of io.undertow.servlet.handlers.ServletPathMatch in project undertow by undertow-io.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch(final ServletContext context, final String path) {
    if (dispatched) {
        throw UndertowServletMessages.MESSAGES.asyncRequestAlreadyDispatched();
    }
    HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    final HttpServerExchange exchange = requestImpl.getExchange();
    exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).setDispatcherType(DispatcherType.ASYNC);
    requestImpl.setAttribute(ASYNC_REQUEST_URI, requestImpl.getOriginalRequestURI());
    requestImpl.setAttribute(ASYNC_CONTEXT_PATH, requestImpl.getOriginalContextPath());
    requestImpl.setAttribute(ASYNC_SERVLET_PATH, requestImpl.getOriginalServletPath());
    requestImpl.setAttribute(ASYNC_QUERY_STRING, requestImpl.getOriginalQueryString());
    String newQueryString = "";
    int qsPos = path.indexOf("?");
    String newServletPath = path;
    if (qsPos != -1) {
        newQueryString = newServletPath.substring(qsPos + 1);
        newServletPath = newServletPath.substring(0, qsPos);
    }
    String newRequestUri = context.getContextPath() + newServletPath;
    // todo: a more efficient impl
    Map<String, Deque<String>> newQueryParameters = new HashMap<>();
    for (String part : newQueryString.split("&")) {
        String name = part;
        String value = "";
        int equals = part.indexOf('=');
        if (equals != -1) {
            name = part.substring(0, equals);
            value = part.substring(equals + 1);
        }
        Deque<String> queue = newQueryParameters.get(name);
        if (queue == null) {
            newQueryParameters.put(name, queue = new ArrayDeque<>(1));
        }
        queue.add(value);
    }
    requestImpl.setQueryParameters(newQueryParameters);
    requestImpl.getExchange().setRelativePath(newServletPath);
    requestImpl.getExchange().setQueryString(newQueryString);
    requestImpl.getExchange().setRequestPath(newRequestUri);
    requestImpl.getExchange().setRequestURI(newRequestUri);
    requestImpl.setServletContext((ServletContextImpl) context);
    responseImpl.setServletContext((ServletContextImpl) context);
    Deployment deployment = requestImpl.getServletContext().getDeployment();
    ServletPathMatch info = deployment.getServletPaths().getServletHandlerByPath(newServletPath);
    requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(info);
    dispatchAsyncRequest(deployment.getServletDispatcher(), info, exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HashMap(java.util.HashMap) Deployment(io.undertow.servlet.api.Deployment) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) ArrayDeque(java.util.ArrayDeque)

Example 4 with ServletPathMatch

use of io.undertow.servlet.handlers.ServletPathMatch in project undertow by undertow-io.

the class HttpServletRequestImpl method getMapping.

@Override
public Mapping getMapping() {
    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletPathMatch match = src.getOriginalServletPathMatch();
    String matchValue;
    switch(match.getMappingMatch()) {
        case EXACT:
            matchValue = getServletPath();
            break;
        case DEFAULT:
            matchValue = "/";
            break;
        case CONTEXT_ROOT:
            matchValue = "";
            break;
        case PATH:
            matchValue = match.getRemaining();
            break;
        case EXTENSION:
            matchValue = match.getMatched().substring(0, match.getMatched().length() - match.getMatchString().length() + 1);
            break;
        default:
            matchValue = match.getRemaining();
    }
    return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch());
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) HttpString(io.undertow.util.HttpString)

Aggregations

ServletPathMatch (io.undertow.servlet.handlers.ServletPathMatch)4 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)2 HttpString (io.undertow.util.HttpString)2 Deque (java.util.Deque)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Deployment (io.undertow.servlet.api.Deployment)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 DispatcherType (javax.servlet.DispatcherType)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ServletRequest (javax.servlet.ServletRequest)1 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)1 ServletResponse (javax.servlet.ServletResponse)1 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1