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

The following examples show how to use io.undertow.server.HttpServerExchange#writeAsync() . 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: DirectoryUtils.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
public static void renderDirectoryListing(HttpServerExchange exchange, Resource resource) {
    String requestPath = exchange.getRequestPath();
    if (!requestPath.endsWith("/")) {
        exchange.setStatusCode(StatusCodes.FOUND);
        exchange.setResponseHeader(HttpHeaderNames.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true));
        exchange.endExchange();
        return;
    }

    StringBuilder builder = renderDirectoryListing(requestPath, resource);

    exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
    exchange.setResponseHeader(HttpHeaderNames.LAST_MODIFIED, DateUtils.toDateString(new Date()));
    exchange.setResponseHeader(HttpHeaderNames.CACHE_CONTROL, "must-revalidate");
    exchange.writeAsync(builder.toString());

    exchange.endExchange();
}
 
Example 2
Source File: DirectoryUtils.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
/**
 * Serve static resource for the directory listing
 *
 * @param exchange The exchange
 * @return true if resources were served
 */
public static boolean sendRequestedBlobs(HttpServerExchange exchange) {
    ByteBuf buffer = null;
    String type = null;
    String etag = null;
    String quotedEtag = null;
    if ("css".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_CSS_BUFFER.duplicate();
        type = "text/css";
        etag = Blobs.FILE_CSS_ETAG;
        quotedEtag = Blobs.FILE_CSS_ETAG_QUOTED;
    } else if ("js".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_JS_BUFFER.duplicate();
        type = "application/javascript";
        etag = Blobs.FILE_JS_ETAG;
        quotedEtag = Blobs.FILE_JS_ETAG_QUOTED;
    }

    if (buffer != null) {

        if (!ETagUtils.handleIfNoneMatch(exchange, new ETag(false, etag), false)) {
            exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
            return true;
        }

        exchange.setResponseHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()));
        exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, type);
        exchange.setResponseHeader(HttpHeaderNames.ETAG, quotedEtag);
        if (HttpMethodNames.HEAD.equals(exchange.getRequestMethod())) {
            exchange.endExchange();
            return true;
        }
        exchange.writeAsync(buffer.duplicate(), true, IoCallback.END_EXCHANGE, null);

        return true;
    }

    return false;
}
 
Example 3
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 4
Source File: HttpTraceHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if(exchange.getRequestMethod().equals(HttpMethodNames.TRACE)) {
        exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, "message/http");
        StringBuilder body = new StringBuilder("TRACE ");
        body.append(exchange.getRequestURI());
        if(!exchange.getQueryString().isEmpty()) {
            body.append('?');
            body.append(exchange.getQueryString());
        }
        body.append(' ');
        body.append(exchange.getProtocol());
        body.append("\r\n");
        for(String header : exchange.getRequestHeaderNames()) {
            for(String value : exchange.getRequestHeaders(header)) {
                body.append(header);
                body.append(": ");
                body.append(value);
                body.append("\r\n");
            }
        }
        body.append("\r\n");
        exchange.writeAsync(body.toString());
    } else {
        handler.handleRequest(exchange);
    }
}
 
Example 5
Source File: FormAuthTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
protected void setRootHandler(HttpHandler current) {
    final PredicateHandler handler = new PredicateHandler(Predicates.path("/login"), new HttpHandler() {
        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.writeAsync("Login Page");
        }
    }, current);
    super.setRootHandler(new SessionAttachmentHandler(handler, new InMemorySessionManager("test"), new SessionCookieConfig()));
}
 
Example 6
Source File: SetAttributeTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final StringBuilder sb = new StringBuilder("URI: " + exchange.getRequestURI()
            + " relative: " + exchange.getRelativePath()
            + " QS:" + exchange.getQueryString());
    for (Map.Entry<String, Deque<String>> param : exchange.getQueryParameters().entrySet()) {
        sb.append(" " + param.getKey() + ": " + param.getValue().getFirst());
    }
    exchange.writeAsync(sb.toString());
}
 
Example 7
Source File: ExtendedAccessLogFileTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.setResponseHeader("aa", "bb");
    exchange.writeAsync("Hello");
}
 
Example 8
Source File: AccessLogTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, "text/plain");
    exchange.writeAsync("HelloResponse");
}
 
Example 9
Source File: AccessLogFileTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.writeAsync("Hello");
}
 
Example 10
Source File: JDBCLogDatabaseTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.writeAsync("Hello");
}