Java Code Examples for io.undertow.util.PathTemplateMatcher#PathMatchResult

The following examples show how to use io.undertow.util.PathTemplateMatcher#PathMatchResult . 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: PathTemplateHandler.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    PathTemplateMatcher.PathMatchResult<HttpHandler> match = pathTemplateMatcher.match(exchange.getRelativePath());
    if (match == null) {
        next.handleRequest(exchange);
        return;
    }
    exchange.putAttachment(PATH_TEMPLATE_MATCH, new PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    exchange.putAttachment(io.undertow.util.PathTemplateMatch.ATTACHMENT_KEY, new io.undertow.util.PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    match.getValue().handleRequest(exchange);
}
 
Example 2
Source File: PathTemplateHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    PathTemplateMatcher.PathMatchResult<HttpHandler> match = pathTemplateMatcher.match(exchange.getRelativePath());
    if (match == null) {
        next.handleRequest(exchange);
        return;
    }
    exchange.putAttachment(PATH_TEMPLATE_MATCH, new PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    exchange.putAttachment(io.undertow.util.PathTemplateMatch.ATTACHMENT_KEY, new io.undertow.util.PathTemplateMatch(match.getMatchedTemplate(), match.getParameters()));
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    match.getValue().handleRequest(exchange);
}
 
Example 3
Source File: ParameterHandler.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
	PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher.match(exchange.getRequestPath());
	
	if (result != null) {
		exchange.putAttachment(ATTACHMENT_KEY,
				new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
		for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
			exchange.addQueryParam(entry.getKey(), entry.getValue());
			
			exchange.addPathParam(entry.getKey(), entry.getValue());
		}
	}
	
	Handler.next(exchange, next);
}
 
Example 4
Source File: ParameterHandler.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
	PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher.match(exchange.getRequestPath());
	
	if (result != null) {
		exchange.putAttachment(ATTACHMENT_KEY,
				new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
		for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
			exchange.addQueryParam(entry.getKey(), entry.getValue());
			
			exchange.addPathParam(entry.getKey(), entry.getValue());
		}
	}
	
	Handler.next(exchange, next);
}
 
Example 5
Source File: HandlerTest.java    From light-4j with Apache License 2.0 6 votes vote down vote up
@Test
public void mixedPathsAndSource() {
    Handler.config.setPaths(Arrays.asList(
        mkPathChain(null, "/my-api/first", "post", "third"),
        mkPathChain(MockEndpointSource.class.getName(), null, null, "secondBeforeFirst", "third"),
        mkPathChain(null, "/my-api/second", "put", "third")
    ));
    Handler.init();

    Map<HttpString, PathTemplateMatcher<String>> methodToMatcher = Handler.methodToMatcherMap;

    PathTemplateMatcher<String> getMatcher = methodToMatcher.get(Methods.GET);
    PathTemplateMatcher.PathMatchResult<String> getFirst = getMatcher.match("/my-api/first");
    Assert.assertNotNull(getFirst);
    PathTemplateMatcher.PathMatchResult<String> getSecond = getMatcher.match("/my-api/second");
    Assert.assertNotNull(getSecond);
    PathTemplateMatcher.PathMatchResult<String> getThird = getMatcher.match("/my-api/third");
    Assert.assertNull(getThird);
}
 
Example 6
Source File: RoutingHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {

    PathTemplateMatcher<RoutingMatch> matcher = matches.get(exchange.getRequestMethod());
    if (matcher == null) {
        handleNoMatch(exchange);
        return;
    }
    PathTemplateMatcher.PathMatchResult<RoutingMatch> match = matcher.match(exchange.getRelativePath());
    if (match == null) {
        handleNoMatch(exchange);
        return;
    }
    exchange.putAttachment(PathTemplateMatch.ATTACHMENT_KEY, match);
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    for (HandlerHolder handler : match.getValue().predicatedHandlers) {
        if (handler.predicate.resolve(exchange)) {
            handler.handler.handleRequest(exchange);
            return;
        }
    }
    if (match.getValue().defaultHandler != null) {
        match.getValue().defaultHandler.handleRequest(exchange);
    } else {
        fallbackHandler.handleRequest(exchange);
    }
}
 
Example 7
Source File: RoutingHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {

    PathTemplateMatcher<RoutingMatch> matcher = matches.get(exchange.getRequestMethod());
    if (matcher == null) {
        handleNoMatch(exchange);
        return;
    }
    PathTemplateMatcher.PathMatchResult<RoutingMatch> match = matcher.match(exchange.getRelativePath());
    if (match == null) {
        handleNoMatch(exchange);
        return;
    }
    exchange.putAttachment(PathTemplateMatch.ATTACHMENT_KEY, match);
    if (rewriteQueryParameters) {
        for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
            exchange.addQueryParam(entry.getKey(), entry.getValue());
        }
    }
    for (HandlerHolder handler : match.getValue().predicatedHandlers) {
        if (handler.predicate.resolve(exchange)) {
            handler.handler.handleRequest(exchange);
            return;
        }
    }
    if (match.getValue().defaultHandler != null) {
        match.getValue().defaultHandler.handleRequest(exchange);
    } else {
        fallbackHandler.handleRequest(exchange);
    }
}
 
Example 8
Source File: Handler.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * On the first step of the request, match the request against the configured
 * paths. If the match is successful, store the chain id within the exchange.
 * Otherwise return false.
 *
 * @param httpServerExchange
 *            The current requests server exchange.
 * @return true if a handler has been defined for the given path.
 */
public static boolean start(HttpServerExchange httpServerExchange) {
	// Get the matcher corresponding to the current request type.
	PathTemplateMatcher<String> pathTemplateMatcher = methodToMatcherMap.get(httpServerExchange.getRequestMethod());
	if (pathTemplateMatcher != null) {
		// Match the current request path to the configured paths.
		PathTemplateMatcher.PathMatchResult<String> result = pathTemplateMatcher
				.match(httpServerExchange.getRequestPath());
		if (result != null) {
			// Found a match, configure and return true;
			// Add path variables to query params.
			httpServerExchange.putAttachment(ATTACHMENT_KEY,
					new io.undertow.util.PathTemplateMatch(result.getMatchedTemplate(), result.getParameters()));
			for (Map.Entry<String, String> entry : result.getParameters().entrySet()) {
				// the values shouldn't be added to query param. but this is left as it was to keep backward compatability
				httpServerExchange.addQueryParam(entry.getKey(), entry.getValue());
				
				// put values in path param map
				httpServerExchange.addPathParam(entry.getKey(), entry.getValue());
			}
			String id = result.getValue();
			httpServerExchange.putAttachment(CHAIN_ID, id);
			httpServerExchange.putAttachment(CHAIN_SEQ, 0);
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: JsrWebSocketFilter.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    if (req.getHeader(HttpHeaderNames.UPGRADE) != null) {
        final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp);

        String path;
        if (req.getPathInfo() == null) {
            path = req.getServletPath();
        } else {
            path = req.getServletPath() + req.getPathInfo();
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        PathTemplateMatcher.PathMatchResult<WebSocketHandshakeHolder> matchResult = pathTemplateMatcher.match(path);
        if (matchResult != null) {
            Handshake handshaker = null;
            for (Handshake method : matchResult.getValue().handshakes) {
                if (method.matches(facade)) {
                    handshaker = method;
                    break;
                }
            }

            if (handshaker != null) {
                if (container.isClosed()) {
                    resp.sendError(StatusCodes.SERVICE_UNAVAILABLE);
                    return;
                }
                facade.putAttachment(HandshakeUtil.PATH_PARAMS, matchResult.getParameters());
                facade.putAttachment(HandshakeUtil.PRINCIPAL, req.getUserPrincipal());
                final Handshake selected = handshaker;
                ServletRequestContext src = ServletRequestContext.requireCurrent();
                final HttpSessionImpl session = src.getCurrentServletContext().getSession(src.getExchange(), false);
                handshaker.handshake(facade, new Consumer<ChannelHandlerContext>() {
                    @Override
                    public void accept(ChannelHandlerContext context) {
                        UndertowSession channel = callback.connected(context, selected.getConfig(), facade, src.getOriginalResponse().getHeader(io.netty.handler.codec.http.HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL.toString()));
                        if (session != null && channel != null) {
                            final Session underlying;
                            if (System.getSecurityManager() == null) {
                                underlying = session.getSession();
                            } else {
                                underlying = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
                            }
                            List<UndertowSession> connections;
                            synchronized (underlying) {
                                connections = (List<UndertowSession>) underlying.getAttribute(SESSION_ATTRIBUTE);
                                if (connections == null) {
                                    underlying.setAttribute(SESSION_ATTRIBUTE, connections = new ArrayList<>());
                                }
                                connections.add(channel);
                            }
                            final List<UndertowSession> finalConnections = connections;
                            context.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {
                                @Override
                                public void operationComplete(Future<? super Void> future) throws Exception {
                                    synchronized (underlying) {
                                        finalConnections.remove(channel);
                                    }
                                }
                            });
                        }
                    }
                });
                return;
            }
        }
    }
    chain.doFilter(request, response);
}