io.undertow.server.handlers.ResponseCodeHandler Java Examples

The following examples show how to use io.undertow.server.handlers.ResponseCodeHandler. 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: SimpleUndertowLoadBalancer.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private HttpHandler createHandler() throws Exception {

        // TODO: configurable options if needed
        String[] sessionIds = {AUTH_SESSION_ID, AUTH_SESSION_ID + LEGACY_COOKIE};
        int connectionsPerThread = 20;
        int problemServerRetry = 5; // In case of unavailable node, we will try to ping him every 5 seconds to check if it's back
        int maxTime = 3600000; // 1 hour for proxy request timeout, so we can debug the backend keycloak servers
        int requestQueueSize = 10;
        int cachedConnectionsPerThread = 10;
        int connectionIdleTimeout = 60;
        int maxRetryAttempts = backendNodes.size() - 1;

        lb = new CustomLoadBalancingClient(exchange -> exchange.getRequestHeaders().contains(Headers.UPGRADE), maxRetryAttempts)
                .setConnectionsPerThread(connectionsPerThread)
                .setMaxQueueSize(requestQueueSize)
                .setSoftMaxConnectionsPerThread(cachedConnectionsPerThread)
                .setTtl(connectionIdleTimeout)
                .setProblemServerRetry(problemServerRetry);
        for (String id : sessionIds) {
            lb.addSessionCookieName(id);
        }

        return new ProxyHandler(lb, maxTime, ResponseCodeHandler.HANDLE_404);
    }
 
Example #2
Source File: ManagementHttpServer.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    if (Methods.POST.equals(exchange.getRequestMethod()))  {
        ResponseCodeHandler.HANDLE_405.handleRequest(exchange);
        return;
    }
    String origReqPath = exchange.getRelativePath();
    String remapped = remapper.remapPath(origReqPath);
    if (remapped == null) {
        ResponseCodeHandler.HANDLE_404.handleRequest(exchange);
        return;
    }
    exchange.setRelativePath(remapped);

    // Note: we only change the relative path, not other exchange data that
    // incorporates it (like getRequestPath(), getRequestURL()) and not the
    // resolved path. If this request gets to DomainApiHandler, it should
    // work off the relative path. Other handlers in between may need the
    // original data.

    next.handleRequest(exchange);
}
 
Example #3
Source File: AbstractAdapterClusteredTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareReverseProxy() throws Exception {
    loadBalancerToNodes = new LoadBalancingProxyClient().addHost(NODE_1_URI, NODE_1_NAME).setConnectionsPerThread(10);
    int maxTime = 3600000; // 1 hour for proxy request timeout, so we can debug the backend keycloak servers
    reverseProxyToNodes = Undertow.builder()
      .addHttpListener(HTTP_PORT_NODE_REVPROXY, "localhost")
      .setIoThreads(2)
      .setHandler(new ProxyHandler(loadBalancerToNodes, maxTime, ResponseCodeHandler.HANDLE_404)).build();
    reverseProxyToNodes.start();
}
 
Example #4
Source File: ResponseCodeHandlerBuilder.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public HandlerWrapper build(final Map<String, Object> config) {
    final Integer value = (Integer) config.get("value");
    return new HandlerWrapper() {
        @Override
        public HttpHandler wrap(HttpHandler handler) {
            return new ResponseCodeHandler(value);
        }
    };
}
 
Example #5
Source File: PredicatedHandlersParserTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsedHandler1() {
    String value = "dump-request";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpHandler handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
}
 
Example #6
Source File: PredicatedHandlersParserTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsedHandler2() {
    String value = "header(header=a, value='a%%lb')";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    SetHeaderHandler handler = (SetHeaderHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals("a", handler.getHeader().toString());
    Assert.assertEquals("a%lb", handler.getValue().readAttribute(null));
}
 
Example #7
Source File: ResponseCodeHandlerBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public HandlerWrapper build(final Map<String, Object> config) {
    final Integer value = (Integer) config.get("value");
    return new HandlerWrapper() {
        @Override
        public HttpHandler wrap(HttpHandler handler) {
            return new ResponseCodeHandler(value);
        }
    };
}
 
Example #8
Source File: PredicatedHandlersParserTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsedPredicatedHandler1() {
    String value = "contains(value='a', search=b) -> dump-request";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpHandler handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);

    ContainsPredicate predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[]{"b"}, predicate.getValues());

    value = "contains(value='a', search={b}) -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);

    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[]{"b"}, predicate.getValues());

    value = "contains[value='a', search={b, c}] -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);

    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[]{"b", "c"}, predicate.getValues());
}
 
Example #9
Source File: PredicatedHandlersParserTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearHeader() throws Exception {
    String value = "set(attribute=%{i,User-Agent}, value=%{NULL})";

    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpServerExchange exchange = new HttpServerExchange(new MockHttpExchange(), -1);
    exchange.setRequestHeader(HttpHeaderNames.USER_AGENT, "firefox");
    ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200).handleRequest(exchange);
    Assert.assertNull(exchange.getRequestHeader(HttpHeaderNames.USER_AGENT));
}
 
Example #10
Source File: ResourceHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ResourceHandler(ResourceManager resourceSupplier) {
    this(resourceSupplier, ResponseCodeHandler.HANDLE_404);
}
 
Example #11
Source File: ResourceHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * You should use {@link ResourceHandler(ResourceManager)} instead.
 */
@Deprecated
public ResourceHandler() {
    this.next = ResponseCodeHandler.HANDLE_404;
}
 
Example #12
Source File: ResourceHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ResourceHandler(ResourceSupplier resourceSupplier) {
    this(resourceSupplier, ResponseCodeHandler.HANDLE_404);
}
 
Example #13
Source File: ResourceHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public ResourceHandler(ResourceManager resourceSupplier) {
    this(resourceSupplier, ResponseCodeHandler.HANDLE_404);
}
 
Example #14
Source File: PathGlobHandlerTest.java    From galeb with Apache License 2.0 4 votes vote down vote up
@Test
public void addRemoveTest() {
    PathGlobHandler pathGlobHandler = new PathGlobHandler();

    pathGlobHandler.addPath("x", 0, ResponseCodeHandler.HANDLE_200);
    pathGlobHandler.addPath("x", 0, ResponseCodeHandler.HANDLE_200);
    pathGlobHandler.addPath("y", 0, ResponseCodeHandler.HANDLE_200);
    pathGlobHandler.addPath("z", 1, ResponseCodeHandler.HANDLE_200);
    pathGlobHandler.addPath("w", 1, ResponseCodeHandler.HANDLE_200);

    try {
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(4));

        assertThat(pathGlobHandler.contains("x"), is(true));
        pathGlobHandler.removePath("x");
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(3));
        assertThat(pathGlobHandler.getPaths().keySet(), Matchers.contains(
                new PathGlobHandler.PathOrdered("y", 0),
                new PathGlobHandler.PathOrdered("w", 1),
                new PathGlobHandler.PathOrdered("z", 1))
        );

        assertThat(pathGlobHandler.contains("z"), is(true));
        pathGlobHandler.removePath("z");
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(2));
        assertThat(pathGlobHandler.getPaths().keySet(), Matchers.contains(
                new PathGlobHandler.PathOrdered("y", 0),
                new PathGlobHandler.PathOrdered("w", 1)
                )
        );

        assertThat(pathGlobHandler.contains("y"), is(true));
        pathGlobHandler.removePath("y");
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(1));
        assertThat(pathGlobHandler.getPaths().keySet(), Matchers.contains(
                new PathGlobHandler.PathOrdered("w", 1))
        );

        pathGlobHandler.removePath("k");
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(1));
        assertThat(pathGlobHandler.getPaths().keySet(), Matchers.contains(
                new PathGlobHandler.PathOrdered("w", 1))
        );

        assertThat(pathGlobHandler.contains("w"), is(true));
        pathGlobHandler.removePath("w");
        assertThat(pathGlobHandler.getPaths().entrySet(), Matchers.hasSize(0));

    } catch (AssertionError e) {
        logger.error("size wrong. pathGlobHandler registered paths are:");
        pathGlobHandler.getPaths().forEach((k, v) -> logger.error(k.getPath()));
        throw e;
    }
}
 
Example #15
Source File: PredicatedHandlersParserTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsedHandler3() {
    String value = "allowed-methods(GET)";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    AllowedMethodsHandler handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET")), handler.getAllowedMethods());

    value = "allowed-methods(methods=GET)";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET")), handler.getAllowedMethods());

    value = "allowed-methods(methods={GET})";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET")), handler.getAllowedMethods());

    value = "allowed-methods({GET})";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET")), handler.getAllowedMethods());


    value = "allowed-methods({GET, POST})";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET", "POST")), handler.getAllowedMethods());

    value = "allowed-methods(methods={GET, POST})";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET", "POST")), handler.getAllowedMethods());

    value = "allowed-methods(GET, POST)";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = (AllowedMethodsHandler) ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertEquals(new HashSet<>(Arrays.asList("GET", "POST")), handler.getAllowedMethods());
}
 
Example #16
Source File: InvalidHtpRequestTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() {
    DefaultServer.setRootHandler(ResponseCodeHandler.HANDLE_200);
}
 
Example #17
Source File: ResourceHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
/**
 * You should use {@link ResourceHandler(ResourceManager)} instead.
 */
@Deprecated
public ResourceHandler() {
    this.next = ResponseCodeHandler.HANDLE_404;
}
 
Example #18
Source File: ResourceHandler.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public ResourceHandler(ResourceSupplier resourceSupplier) {
    this(resourceSupplier, ResponseCodeHandler.HANDLE_404);
}
 
Example #19
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param callback The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                 established
 */
public WebSocketProtocolHandshakeHandler(final WebSocketConnectionCallback callback) {
    this(callback, ResponseCodeHandler.HANDLE_404);
}
 
Example #20
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final WebSocketConnectionCallback callback) {
    this(handshakes, callback, ResponseCodeHandler.HANDLE_404);
}
 
Example #21
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param callback The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                 established
 */
public WebSocketProtocolHandshakeHandler(final HttpUpgradeListener callback) {
    this(callback, ResponseCodeHandler.HANDLE_404);
}
 
Example #22
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final HttpUpgradeListener callback) {
    this(handshakes, callback, ResponseCodeHandler.HANDLE_404);
}