Java Code Examples for io.undertow.server.HttpServerExchange#isResponseStarted()

The following examples show how to use io.undertow.server.HttpServerExchange#isResponseStarted() . 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: ProxyHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
void cancel(final HttpServerExchange exchange) {
    final ProxyConnection connectionAttachment = exchange.getAttachment(CONNECTION);
    if (connectionAttachment != null) {
        ClientConnection clientConnection = connectionAttachment.getConnection();
        UndertowLogger.PROXY_REQUEST_LOGGER.timingOutRequest(clientConnection.getPeerAddress() + "" + exchange.getRequestURI());
        IoUtils.safeClose(clientConnection);
    } else {
        UndertowLogger.PROXY_REQUEST_LOGGER.timingOutRequest(exchange.getRequestURI());
    }
    if (exchange.isResponseStarted()) {
        IoUtils.safeClose(exchange.getConnection());
    } else {
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        exchange.endExchange();
    }
}
 
Example 2
Source File: SimpleErrorPageHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public boolean handleDefaultResponse(final HttpServerExchange exchange) {
    if (exchange.isResponseStarted()) {
        return false;
    }
    Set<Integer> codes = responseCodes;
    if (codes == null ? exchange.getStatusCode() >= StatusCodes.BAD_REQUEST : codes.contains(Integer.valueOf(exchange.getStatusCode()))) {
        final String errorPage = "<html><head><title>Error</title></head><body>" + exchange.getStatusCode() + " - " + StatusCodes.getReason(exchange.getStatusCode()) + "</body></html>";
        exchange.setResponseHeader(HttpHeaderNames.CONTENT_LENGTH, "" + errorPage.length());
        exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, "text/html");
        exchange.writeAsync(errorPage);
        return true;
    }
    return false;
}
 
Example 3
Source File: HttpContinue.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if this exchange requires the server to send a 100 (Continue) response.
 *
 * @param exchange The exchange
 * @return <code>true</code> if the server needs to send a continue response
 */
public static boolean requiresContinueResponse(final HttpServerExchange exchange) {
    if (!COMPATIBLE_PROTOCOLS.contains(exchange.getProtocol()) || exchange.isResponseStarted() || !exchange.getConnection().isContinueResponseSupported() || exchange.getAttachment(ALREADY_SENT) != null) {
        return false;
    }

    HeaderMap requestHeaders = exchange.getRequestHeaders();
    return requiresContinueResponse(requestHeaders);
}
 
Example 4
Source File: HttpContinueReadHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public StreamSourceConduit wrap(final ConduitFactory<StreamSourceConduit> factory, final HttpServerExchange exchange) {
    if(exchange.isRequestChannelAvailable() && !exchange.isResponseStarted()) {
        return new ContinueConduit(factory.create(), exchange);
    }
    return factory.create();
}
 
Example 5
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void couldNotResolveBackend(HttpServerExchange exchange) {
    if (exchange.isResponseStarted()) {
        IoUtils.safeClose(exchange.getConnection());
    } else {
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        exchange.endExchange();
    }
}
 
Example 6
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
static void handleFailure(HttpServerExchange exchange, ProxyClientHandler proxyClientHandler, Predicate idempotentRequestPredicate, IOException e) {
    UndertowLogger.PROXY_REQUEST_LOGGER.proxyRequestFailed(exchange.getRequestURI(), e);
    if(exchange.isResponseStarted()) {
        IoUtils.safeClose(exchange.getConnection());
    } else if(idempotentRequestPredicate.resolve(exchange) && proxyClientHandler != null) {
        proxyClientHandler.failed(exchange); //this will attempt a retry if configured to do so
    } else {
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        exchange.endExchange();
    }
}
 
Example 7
Source File: BlockingReceiverImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void error(HttpServerExchange exchange, IOException e) {
    if(!exchange.isResponseStarted()) {
        exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
    }
    exchange.setPersistent(false);
    UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
    exchange.endExchange();
}
 
Example 8
Source File: HttpContexts.java    From thorntail with Apache License 2.0 4 votes vote down vote up
private void proxyRequests(HttpServerExchange exchange) {

        if (monitor.getHealthURIs().isEmpty()) {
            noHealthEndpoints(exchange);
        } else {

            try {
                final List<InVMResponse> responses = new CopyOnWriteArrayList<>();
                CountDownLatch latch = new CountDownLatch(monitor.getHealthURIs().size());
                dispatched.set(latch);

                for (HealthMetaData healthCheck : monitor.getHealthURIs()) {
                    invokeHealthInVM(exchange, healthCheck, responses, latch);
                }

                latch.await(10, TimeUnit.SECONDS);

                if (latch.getCount() > 0) {
                    throw new Exception("Probe timed out");
                }


                boolean failed = false;
                if (!responses.isEmpty()) {

                    if (responses.size() != monitor.getHealthURIs().size()) {
                        throw new RuntimeException("The number of responses does not match!");
                    }

                    StringBuffer sb = new StringBuffer("{");
                    sb.append("\"checks\": [\n");

                    int i = 0;
                    for (InVMResponse resp : responses) {

                        sb.append(resp.getPayload());

                        if (!failed) {
                            failed = resp.getStatus() != 200;
                        }

                        if (i < responses.size() - 1) {
                            sb.append(",\n");
                        }
                        i++;
                    }
                    sb.append("],\n");

                    String outcome = failed ? "DOWN" : "UP"; // we don't have policies yet, so keep it simple
                    sb.append("\"outcome\": \"" + outcome + "\"\n");
                    sb.append("}\n");

                    // send a response
                    if (failed) {
                        exchange.setStatusCode(503);
                    }

                    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
                    exchange.getResponseSender().send(sb.toString());

                } else {
                    new RuntimeException("Responses should not be empty").printStackTrace();
                    exchange.setStatusCode(500);
                }

                exchange.endExchange();


            } catch (Throwable t) {
                LOG.error("Health check failed", t);

                if (!exchange.isResponseStarted()) {
                    exchange.setStatusCode(500);
                }
                exchange.endExchange();
            }

        }

    }