Introduced Request.header(HttpHeader, String) to simplify code.

This commit is contained in:
Simone Bordet 2013-03-04 17:05:18 +01:00
parent 868458f980
commit 7037bca94d
11 changed files with 51 additions and 32 deletions

View File

@ -205,7 +205,7 @@ public class HttpConnection extends AbstractConnection implements Connection
}
case POST:
{
request.header(HttpHeader.CONTENT_TYPE.asString(), MimeTypes.Type.FORM_ENCODED.asString());
request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString());
request.content(new StringContentProvider(params.toString()));
break;
}

View File

@ -485,7 +485,7 @@ public class HttpDestination implements Destination, AutoCloseable, Dumpable
.scheme(HttpScheme.HTTP.asString())
.method(HttpMethod.CONNECT)
.path(target)
.header(HttpHeader.HOST.asString(), target)
.header(HttpHeader.HOST, target)
.timeout(client.getConnectTimeout(), TimeUnit.MILLISECONDS);
connection.send(connect, new Response.CompleteListener()
{

View File

@ -223,6 +223,16 @@ public class HttpRequest implements Request
return this;
}
@Override
public Request header(HttpHeader header, String value)
{
if (value == null)
headers.remove(header);
else
headers.add(header, value);
return this;
}
@Override
public Request attribute(String name, Object value)
{
@ -371,7 +381,7 @@ public class HttpRequest implements Request
public Request content(ContentProvider content, String contentType)
{
if (contentType != null)
header(HttpHeader.CONTENT_TYPE.asString(), contentType);
header(HttpHeader.CONTENT_TYPE, contentType);
this.content = content;
return this;
}
@ -386,7 +396,7 @@ public class HttpRequest implements Request
public Request file(Path file, String contentType) throws IOException
{
if (contentType != null)
header(HttpHeader.CONTENT_TYPE.asString(), contentType);
header(HttpHeader.CONTENT_TYPE, contentType);
return content(new PathContentProvider(file));
}

View File

@ -32,6 +32,7 @@ import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.util.Fields;
@ -135,6 +136,13 @@ public interface Request
*/
Request header(String name, String value);
/**
* @param header the header name
* @param value the value of the header
* @return this request object
*/
Request header(HttpHeader header, String value);
/**
* @param name the name of the attribute
* @param value the value of the attribute

View File

@ -99,7 +99,7 @@ public class BasicAuthentication implements Authentication
public void apply(Request request)
{
if (request.getURI().toString().startsWith(uri.toString()))
request.header(HttpHeader.AUTHORIZATION.asString(), value);
request.header(HttpHeader.AUTHORIZATION, value);
}
@Override

View File

@ -262,7 +262,7 @@ public class DigestAuthentication implements Authentication
}
value.append(", response=\"").append(hashA3).append("\"");
request.header(HttpHeader.AUTHORIZATION.asString(), value.toString());
request.header(HttpHeader.AUTHORIZATION, value.toString());
}
private String nextNonceCount()

View File

@ -84,7 +84,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(contents))
.timeout(5, TimeUnit.SECONDS)
.send();
@ -124,7 +124,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
byte[] content2 = new byte[16384];
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content1, content2)
{
@Override
@ -176,7 +176,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content1, content2))
.send(new BufferingResponseListener()
{
@ -227,7 +227,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
.scheme(scheme)
.method(HttpMethod.POST)
.path("/continue")
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content))
.send(new BufferingResponseListener()
{
@ -276,7 +276,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
.scheme(scheme)
.method(HttpMethod.POST)
.path("/redirect")
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content))
.send(new BufferingResponseListener()
{
@ -322,7 +322,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content))
.send(new BufferingResponseListener()
{
@ -369,7 +369,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content))
.send(new BufferingResponseListener()
{
@ -433,7 +433,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(content))
.send(new BufferingResponseListener()
{
@ -480,7 +480,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
DeferredContentProvider content = new DeferredContentProvider();
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(content)
.send(new BufferingResponseListener()
{
@ -529,7 +529,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1));
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(content)
.send(new BufferingResponseListener()
{
@ -602,7 +602,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.onRequestHeaders(new org.eclipse.jetty.client.api.Request.HeadersListener()
{
@Override
@ -676,7 +676,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(content)
.send(new BufferingResponseListener()
{

View File

@ -38,6 +38,7 @@ import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.server.handler.AbstractHandler;
@ -132,7 +133,7 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest
// Choose randomly whether to close the connection on the client or on the server
if (!ssl && random.nextBoolean())
request.header("Connection", "close");
request.header(HttpHeader.CONNECTION, "close");
else if (!ssl && random.nextBoolean())
request.header("X-Close", "true");

View File

@ -24,7 +24,6 @@ import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -185,7 +184,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest
public void onBegin(Request request)
{
// Remove the host header, this will make the request invalid
request.header(HttpHeader.HOST.asString(), null);
request.header(HttpHeader.HOST, null);
}
@Override
@ -245,7 +244,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest
public void onBegin(Request request)
{
// Remove the host header, this will make the request invalid
request.header(HttpHeader.HOST.asString(), null);
request.header(HttpHeader.HOST, null);
}
@Override

View File

@ -44,6 +44,7 @@ import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.InputStreamContentProvider;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.handler.ContextHandler;
@ -397,14 +398,14 @@ public class ProxyServlet extends HttpServlet
// Force the Host header if configured
if (_hostHeader != null)
proxyRequest.header("Host", _hostHeader);
proxyRequest.header(HttpHeader.HOST, _hostHeader);
// Add proxy headers
proxyRequest.header("Via", "http/1.1 " + _viaHost);
proxyRequest.header("X-Forwarded-For", request.getRemoteAddr());
proxyRequest.header("X-Forwarded-Proto", request.getScheme());
proxyRequest.header("X-Forwarded-Host", request.getHeader("Host"));
proxyRequest.header("X-Forwarded-Server", request.getLocalName());
proxyRequest.header(HttpHeader.VIA, "http/1.1 " + _viaHost);
proxyRequest.header(HttpHeader.X_FORWARDED_FOR, request.getRemoteAddr());
proxyRequest.header(HttpHeader.X_FORWARDED_PROTO, request.getScheme());
proxyRequest.header(HttpHeader.X_FORWARDED_HOST, request.getHeader(HttpHeader.HOST.asString()));
proxyRequest.header(HttpHeader.X_FORWARDED_SERVER, request.getLocalName());
proxyRequest.content(new InputStreamContentProvider(request.getInputStream())
{

View File

@ -29,7 +29,6 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -38,6 +37,7 @@ import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
@ -153,7 +153,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest
++result;
httpClient.newRequest("localhost", connector.getLocalPort())
.path(path)
.header("Referer", referrer)
.header(HttpHeader.REFERER, referrer)
.send(new TestListener());
}
for (int i = 0; i < jsResources.length; ++i)
@ -162,7 +162,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest
++result;
httpClient.newRequest("localhost", connector.getLocalPort())
.path(path)
.header("Referer", referrer)
.header(HttpHeader.REFERER, referrer)
.send(new TestListener());
}
for (int i = 0; i < pngResources.length; ++i)
@ -171,7 +171,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest
++result;
httpClient.newRequest("localhost", connector.getLocalPort())
.path(path)
.header("Referer", referrer)
.header(HttpHeader.REFERER, referrer)
.send(new TestListener());
}