Skip to content

Commit

Permalink
Add ResponseTimeAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
emag committed Dec 9, 2013
1 parent 285c358 commit 6401d61
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
@@ -0,0 +1,43 @@
package io.undertow.attribute;

import io.undertow.server.HttpServerExchange;

/**
* The response time
*/
public class ResponseTimeAttribute implements ExchangeAttribute {

public static final String RESPONSE_TIME_SHORT = "%D";
public static final String RESPONSE_TIME = "%{RESPONSE_TIME}";

public static final ExchangeAttribute INSTANCE = new ResponseTimeAttribute();

private ResponseTimeAttribute() {}

@Override
public String readAttribute(HttpServerExchange exchange) {
return String.valueOf(System.currentTimeMillis() - exchange.getStartTime());
}

@Override
public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException {
throw new ReadOnlyAttributeException("Response Time", newValue);
}

public static final class Builder implements ExchangeAttributeBuilder {

@Override
public String name() {
return "Response Time";
}

@Override
public ExchangeAttribute build(String token) {
if (token.equals(RESPONSE_TIME) || token.equals(RESPONSE_TIME_SHORT)) {
return ResponseTimeAttribute.INSTANCE;
}
return null;
}
}

}
7 changes: 7 additions & 0 deletions core/src/main/java/io/undertow/server/HttpServerExchange.java
Expand Up @@ -125,6 +125,7 @@ public final class HttpServerExchange extends AbstractAttachable {
private int state = 200;
private HttpString requestMethod;
private String requestScheme;
private long startTime;

/**
* The original request URI. This will include the host name if it was specified by the client.
Expand Down Expand Up @@ -264,11 +265,13 @@ public final class HttpServerExchange extends AbstractAttachable {
public HttpServerExchange(final ServerConnection connection, long maxEntitySize) {
this.connection = connection;
this.maxEntitySize = maxEntitySize;
this.startTime = System.currentTimeMillis();
}

public HttpServerExchange(final ServerConnection connection) {
this.connection = connection;
this.maxEntitySize = 0;
this.startTime = System.currentTimeMillis();
}

/**
Expand Down Expand Up @@ -390,6 +393,10 @@ public void setRequestURI(final String requestURI, boolean containsHost) {
}
}

public long getStartTime() {
return this.startTime;
}

/**
* If a request was submitted to the server with a full URI instead of just a path this
* will return true. For example:
Expand Down
Expand Up @@ -22,3 +22,4 @@ io.undertow.attribute.QueryParameterAttribute$Builder
io.undertow.attribute.SslClientCertAttribute$Builder
io.undertow.attribute.SslCipherAttribute$Builder
io.undertow.attribute.SslSessionIdAttribute$Builder
io.undertow.attribute.ResponseTimeAttribute$Builder

0 comments on commit 6401d61

Please sign in to comment.