Java Code Examples for io.undertow.server.HttpServerExchange#setResponseHeader()
The following examples show how to use
io.undertow.server.HttpServerExchange#setResponseHeader() .
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: RedirectDirHandler.java From quarkus-http with Apache License 2.0 | 6 votes |
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { final String path = exchange.getRelativePath(); final ServletPathMatch info = paths.getServletHandlerByPath(path); // https://issues.jboss.org/browse/WFLY-3439 // if the request is an upgrade request then we don't want to redirect // as there is a good chance the web socket client won't understand the redirect // we make an exception for HTTP2 upgrade requests, as this would have already be handled at // the connector level if it was going to be handled. String upgradeString = exchange.getRequestHeader(HttpHeaderNames.UPGRADE); boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX); if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { // UNDERTOW-89 // we redirect on GET requests to the root context to add an / to the end if (exchange.getRequestMethod().equals(HttpMethodNames.GET) || exchange.getRequestMethod().equals(HttpMethodNames.GET)) { exchange.setStatusCode(StatusCodes.FOUND); } else { exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT); } exchange.setResponseHeader(HttpHeaderNames.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true)); return; } next.handleRequest(exchange); }
Example 2
Source File: AbstractConfidentialityHandler.java From quarkus-http with Apache License 2.0 | 6 votes |
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { if (isConfidential(exchange) || !confidentialityRequired(exchange)) { next.handleRequest(exchange); } else { try { URI redirectUri = getRedirectURI(exchange); UndertowLogger.SECURITY_LOGGER.debugf("Redirecting request %s to %s to meet confidentiality requirements", exchange, redirectUri); exchange.setStatusCode(StatusCodes.FOUND); exchange.setResponseHeader(HttpHeaderNames.LOCATION, redirectUri.toString()); } catch (Exception e) { UndertowLogger.REQUEST_LOGGER.exceptionProcessingRequest(e); exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); } exchange.endExchange(); } }
Example 3
Source File: DirectoryUtils.java From quarkus-http with Apache License 2.0 | 6 votes |
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 4
Source File: ResponseHeaderAttribute.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException { if(newValue == null) { exchange.removeResponseHeader(responseHeader); } else { exchange.setResponseHeader(responseHeader, newValue); } }
Example 5
Source File: DirectoryUtils.java From quarkus-http with Apache License 2.0 | 5 votes |
/** * 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 6
Source File: SimpleErrorPageHandler.java From quarkus-http with Apache License 2.0 | 5 votes |
@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 7
Source File: DateHandler.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { // better method is used in DateUtils#addDateHeaderIfRequired long time = System.nanoTime(); if(time < nextUpdateTime) { exchange.setResponseHeader(HttpHeaderNames.DATE, cachedDateString); } else { long realTime = System.currentTimeMillis(); String dateString = DateUtils.toDateString(new Date(realTime)); cachedDateString = dateString; nextUpdateTime = time + 1000000000; exchange.setResponseHeader(HttpHeaderNames.DATE, dateString); } next.handleRequest(exchange); }
Example 8
Source File: ByteRangeHandler.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public void beforeCommit(HttpServerExchange exchange) { if (!exchange.containsResponseHeader(HttpHeaderNames.ACCEPT_RANGES)) { if (exchange.containsResponseHeader(HttpHeaderNames.CONTENT_LENGTH)) { exchange.setResponseHeader(HttpHeaderNames.ACCEPT_RANGES, "bytes"); } else { exchange.setResponseHeader(HttpHeaderNames.ACCEPT_RANGES, "none"); } } }
Example 9
Source File: HttpTraceHandler.java From quarkus-http with Apache License 2.0 | 5 votes |
@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 10
Source File: PathTestCase.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { exchange.addResponseHeader(MATCHED, matched); exchange.addResponseHeader(PATH, exchange.getRelativePath()); for(Map.Entry<String, Deque<String>> param : exchange.getQueryParameters().entrySet()) { exchange.setResponseHeader(param.getKey(), param.getValue().getFirst()); } exchange.endExchange(); }
Example 11
Source File: FormAuthenticationMechanism.java From quarkus-http with Apache License 2.0 | 4 votes |
static void sendRedirect(final HttpServerExchange exchange, final String location) { // TODO - String concatenation to construct URLS is extremely error prone - switch to a URI which will better handle this. String loc = exchange.getRequestScheme() + "://" + exchange.getHostAndPort() + location; exchange.setResponseHeader(HttpHeaderNames.LOCATION, loc); }
Example 12
Source File: SetHeaderHandler.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { exchange.setResponseHeader(header, value.readAttribute(exchange)); next.handleRequest(exchange); }
Example 13
Source File: RedirectHandler.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { exchange.setStatusCode(StatusCodes.FOUND); exchange.setResponseHeader(HttpHeaderNames.LOCATION, attribute.readAttribute(exchange)); exchange.endExchange(); }
Example 14
Source File: DateUtils.java From quarkus-http with Apache License 2.0 | 4 votes |
public static void addDateHeaderIfRequired(HttpServerExchange exchange) { if (exchange.getUndertowOptions().get(UndertowOptions.ALWAYS_SET_DATE, true) && !exchange.containsResponseHeader(HttpHeaderNames.DATE)) { String dateString = getCurrentDateTime(exchange); exchange.setResponseHeader(HttpHeaderNames.DATE, dateString); } }
Example 15
Source File: ExtendedAccessLogFileTestCase.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { exchange.setResponseHeader("aa", "bb"); exchange.writeAsync("Hello"); }
Example 16
Source File: AccessLogTestCase.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, "text/plain"); exchange.writeAsync("HelloResponse"); }