use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class StoredResponseHandler method handleRequest.
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {
@Override
public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
return new StoredResponseStreamSinkConduit(factory.create(), exchange);
}
});
next.handleRequest(exchange);
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class ExtendedAccessLogParser method getXParameterElement.
protected ExchangeAttribute getXParameterElement(PatternTokenizer tokenizer) throws IOException {
if (!tokenizer.hasSubToken()) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
return null;
}
final String token = tokenizer.getToken();
if (!tokenizer.hasParameter()) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
return null;
}
String parameter = tokenizer.getParameter();
if (parameter == null) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing();
return null;
}
if ("A".equals(token)) {
return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{sc," + parameter + "}"), "-");
} else if ("C".equals(token)) {
return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(new CookieAttribute(parameter), "-");
} else if ("R".equals(token)) {
return parser.parse("%{r," + parameter + "}");
} else if ("S".equals(token)) {
return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{s," + parameter + "}"), "-");
} else if ("H".equals(token)) {
return getServletRequestElement(parameter);
} else if ("P".equals(token)) {
return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{rp," + parameter + "}"), "-");
} else if ("O".equals(token)) {
return new QuotingExchangeAttribute(new ExchangeAttribute() {
@Override
public String readAttribute(HttpServerExchange exchange) {
HeaderValues values = exchange.getResponseHeaders().get(parameter);
if (values != null && values.size() > 0) {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < values.size(); i++) {
String string = values.get(i);
buffer.append(string);
if (i + 1 < values.size())
buffer.append(",");
}
return buffer.toString();
}
return null;
}
@Override
public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException {
throw new ReadOnlyAttributeException();
}
});
}
UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecodeXParamValue(token);
return null;
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class MetricsHandler method handleRequest.
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (!exchange.isComplete()) {
final long start = System.currentTimeMillis();
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
long time = System.currentTimeMillis() - start;
totalResult.update((int) time, exchange.getStatusCode());
nextListener.proceed();
}
});
}
next.handleRequest(exchange);
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class RequestDumpingHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final StringBuilder sb = new StringBuilder();
// Log pre-service information
final SecurityContext sc = exchange.getSecurityContext();
sb.append("\n----------------------------REQUEST---------------------------\n");
sb.append(" URI=" + exchange.getRequestURI() + "\n");
sb.append(" characterEncoding=" + exchange.getRequestHeaders().get(Headers.CONTENT_ENCODING) + "\n");
sb.append(" contentLength=" + exchange.getRequestContentLength() + "\n");
sb.append(" contentType=" + exchange.getRequestHeaders().get(Headers.CONTENT_TYPE) + "\n");
if (sc != null) {
if (sc.isAuthenticated()) {
sb.append(" authType=" + sc.getMechanismName() + "\n");
sb.append(" principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
} else {
sb.append(" authType=none" + "\n");
}
}
for (Cookie cookie : exchange.requestCookies()) {
sb.append(" cookie=" + cookie.getName() + "=" + cookie.getValue() + "\n");
}
for (HeaderValues header : exchange.getRequestHeaders()) {
for (String value : header) {
sb.append(" header=" + header.getHeaderName() + "=" + value + "\n");
}
}
sb.append(" locale=" + LocaleUtils.getLocalesFromHeader(exchange.getRequestHeaders().get(Headers.ACCEPT_LANGUAGE)) + "\n");
sb.append(" method=" + exchange.getRequestMethod() + "\n");
Map<String, Deque<String>> pnames = exchange.getQueryParameters();
for (Map.Entry<String, Deque<String>> entry : pnames.entrySet()) {
String pname = entry.getKey();
Iterator<String> pvalues = entry.getValue().iterator();
sb.append(" parameter=");
sb.append(pname);
sb.append('=');
while (pvalues.hasNext()) {
sb.append(pvalues.next());
if (pvalues.hasNext()) {
sb.append(", ");
}
}
sb.append("\n");
}
sb.append(" protocol=" + exchange.getProtocol() + "\n");
sb.append(" queryString=" + exchange.getQueryString() + "\n");
sb.append(" remoteAddr=" + exchange.getSourceAddress() + "\n");
sb.append(" remoteHost=" + exchange.getSourceAddress().getHostName() + "\n");
sb.append(" scheme=" + exchange.getRequestScheme() + "\n");
sb.append(" host=" + exchange.getRequestHeaders().getFirst(Headers.HOST) + "\n");
sb.append(" serverPort=" + exchange.getDestinationAddress().getPort() + "\n");
sb.append(" isSecure=" + exchange.isSecure() + "\n");
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) {
dumpRequestBody(exchange, sb);
// Log post-service information
sb.append("--------------------------RESPONSE--------------------------\n");
if (sc != null) {
if (sc.isAuthenticated()) {
sb.append(" authType=" + sc.getMechanismName() + "\n");
sb.append(" principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
} else {
sb.append(" authType=none" + "\n");
}
}
sb.append(" contentLength=" + exchange.getResponseContentLength() + "\n");
sb.append(" contentType=" + exchange.getResponseHeaders().getFirst(Headers.CONTENT_TYPE) + "\n");
for (Cookie cookie : exchange.responseCookies()) {
sb.append(" cookie=" + cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain() + "; path=" + cookie.getPath() + "\n");
}
for (HeaderValues header : exchange.getResponseHeaders()) {
for (String value : header) {
sb.append(" header=" + header.getHeaderName() + "=" + value + "\n");
}
}
sb.append(" status=" + exchange.getStatusCode() + "\n");
String storedResponse = StoredResponse.INSTANCE.readAttribute(exchange);
if (storedResponse != null) {
sb.append("body=\n");
sb.append(storedResponse);
}
sb.append("\n==============================================================");
nextListener.proceed();
UndertowLogger.REQUEST_DUMPER_LOGGER.info(sb.toString());
}
});
// Perform the exchange
next.handleRequest(exchange);
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class ChannelUpgradeHandler method addProtocol.
private synchronized void addProtocol(String productString, HttpUpgradeListener openListener, final ChannelListener<? super StreamConnection> channelListener, final HttpUpgradeHandshake handshake) {
if (productString == null) {
throw new IllegalArgumentException("productString is null");
}
if (openListener == null && channelListener == null) {
throw new IllegalArgumentException("openListener is null");
}
if (openListener == null) {
openListener = new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
ChannelListeners.invokeChannelListener(streamConnection, channelListener);
}
};
}
List<Holder> list = handlers.get(productString);
if (list == null) {
handlers.put(productString, list = new CopyOnWriteArrayList<>());
}
list.add(new Holder(openListener, handshake, channelListener));
}
Aggregations