392959 - Review HttpClient.getConversation(long).

Modified to HttpClient.getConversation(long, boolean) in order
to specify whether the conversation must be created or not.
This commit is contained in:
Simone Bordet 2012-10-30 12:12:54 +01:00
parent 1173916da1
commit ebb76ecfb9
8 changed files with 16 additions and 15 deletions

View File

@ -79,7 +79,7 @@ public class AuthenticationProtocolHandler implements ProtocolHandler
public void onComplete(Result result) public void onComplete(Result result)
{ {
Request request = result.getRequest(); Request request = result.getRequest();
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), false);
Response.Listener listener = conversation.getExchanges().peekFirst().getResponseListener(); Response.Listener listener = conversation.getExchanges().peekFirst().getResponseListener();
ContentResponse response = new HttpContentResponse(result.getResponse(), getContent(), getEncoding()); ContentResponse response = new HttpContentResponse(result.getResponse(), getContent(), getEncoding());
if (result.isFailed()) if (result.isFailed())

View File

@ -41,7 +41,8 @@ public class ContinueProtocolHandler implements ProtocolHandler
public boolean accept(Request request, Response response) public boolean accept(Request request, Response response)
{ {
boolean expect100 = request.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()); boolean expect100 = request.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString());
boolean handled100 = client.getConversation(request.getConversationID()).getAttribute(ATTRIBUTE) != null; HttpConversation conversation = client.getConversation(request.getConversationID(), false);
boolean handled100 = conversation != null && conversation.getAttribute(ATTRIBUTE) != null;
return expect100 && !handled100; return expect100 && !handled100;
} }
@ -60,7 +61,7 @@ public class ContinueProtocolHandler implements ProtocolHandler
// Handling of success must be done here and not from onComplete(), // Handling of success must be done here and not from onComplete(),
// since the onComplete() is not invoked because the request is not completed yet. // since the onComplete() is not invoked because the request is not completed yet.
HttpConversation conversation = client.getConversation(response.getConversationID()); HttpConversation conversation = client.getConversation(response.getConversationID(), false);
// Mark the 100 Continue response as handled // Mark the 100 Continue response as handled
conversation.setAttribute(ATTRIBUTE, Boolean.TRUE); conversation.setAttribute(ATTRIBUTE, Boolean.TRUE);
@ -92,7 +93,7 @@ public class ContinueProtocolHandler implements ProtocolHandler
@Override @Override
public void onFailure(Response response, Throwable failure) public void onFailure(Response response, Throwable failure)
{ {
HttpConversation conversation = client.getConversation(response.getConversationID()); HttpConversation conversation = client.getConversation(response.getConversationID(), false);
// Mark the 100 Continue response as handled // Mark the 100 Continue response as handled
conversation.setAttribute(ATTRIBUTE, Boolean.TRUE); conversation.setAttribute(ATTRIBUTE, Boolean.TRUE);

View File

@ -365,10 +365,10 @@ public class HttpClient extends ContainerLifeCycle
} }
} }
protected HttpConversation getConversation(long id) protected HttpConversation getConversation(long id, boolean create)
{ {
HttpConversation conversation = conversations.get(id); HttpConversation conversation = conversations.get(id);
if (conversation == null) if (conversation == null && create)
{ {
conversation = new HttpConversation(this, id); conversation = new HttpConversation(this, id);
HttpConversation existing = conversations.putIfAbsent(id, conversation); HttpConversation existing = conversations.putIfAbsent(id, conversation);

View File

@ -111,7 +111,7 @@ public class HttpConnection extends AbstractConnection implements Connection
idleTimeout = endPoint.getIdleTimeout(); idleTimeout = endPoint.getIdleTimeout();
endPoint.setIdleTimeout(request.getIdleTimeout()); endPoint.setIdleTimeout(request.getIdleTimeout());
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), true);
HttpExchange exchange = new HttpExchange(conversation, this, request, listener); HttpExchange exchange = new HttpExchange(conversation, this, request, listener);
setExchange(exchange); setExchange(exchange);
conversation.getExchanges().offer(exchange); conversation.getExchanges().offer(exchange);

View File

@ -331,8 +331,8 @@ public class HttpRequest implements Request
aborted = true; aborted = true;
if (client.provideDestination(getScheme(), getHost(), getPort()).abort(this, reason)) if (client.provideDestination(getScheme(), getHost(), getPort()).abort(this, reason))
return true; return true;
HttpConversation conversation = client.getConversation(getConversationID()); HttpConversation conversation = client.getConversation(getConversationID(), false);
return conversation.abort(reason); return conversation != null && conversation.abort(reason);
} }
@Override @Override

View File

@ -123,7 +123,7 @@ public class HttpSender
return; return;
final Request request = exchange.getRequest(); final Request request = exchange.getRequest();
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = exchange.getConversation();
HttpGenerator.RequestInfo requestInfo = null; HttpGenerator.RequestInfo requestInfo = null;
boolean expect100 = request.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()); boolean expect100 = request.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString());

View File

@ -105,7 +105,7 @@ public class RedirectProtocolHandler extends Response.Listener.Empty implements
private void redirect(Result result, HttpMethod method, String location) private void redirect(Result result, HttpMethod method, String location)
{ {
final Request request = result.getRequest(); final Request request = result.getRequest();
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), false);
Integer redirects = (Integer)conversation.getAttribute(ATTRIBUTE); Integer redirects = (Integer)conversation.getAttribute(ATTRIBUTE);
if (redirects == null) if (redirects == null)
redirects = 0; redirects = 0;
@ -151,7 +151,7 @@ public class RedirectProtocolHandler extends Response.Listener.Empty implements
{ {
Request request = result.getRequest(); Request request = result.getRequest();
Response response = result.getResponse(); Response response = result.getResponse();
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), false);
Response.Listener listener = conversation.getExchanges().peekFirst().getResponseListener(); Response.Listener listener = conversation.getExchanges().peekFirst().getResponseListener();
// TODO: should we replay all events, or just the failure ? // TODO: should we replay all events, or just the failure ?
notifier.notifyFailure(listener, response, failure); notifier.notifyFailure(listener, response, failure);

View File

@ -126,7 +126,7 @@ public class ResponseNotifier
public void forwardSuccessComplete(Response.Listener listener, Request request, Response response) public void forwardSuccessComplete(Response.Listener listener, Request request, Response response)
{ {
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), false);
forwardSuccess(listener, response); forwardSuccess(listener, response);
conversation.complete(); conversation.complete();
notifyComplete(listener, new Result(request, response)); notifyComplete(listener, new Result(request, response));
@ -143,7 +143,7 @@ public class ResponseNotifier
public void forwardFailureComplete(Response.Listener listener, Request request, Throwable requestFailure, Response response, Throwable responseFailure) public void forwardFailureComplete(Response.Listener listener, Request request, Throwable requestFailure, Response response, Throwable responseFailure)
{ {
HttpConversation conversation = client.getConversation(request.getConversationID()); HttpConversation conversation = client.getConversation(request.getConversationID(), false);
forwardFailure(listener, response, responseFailure); forwardFailure(listener, response, responseFailure);
conversation.complete(); conversation.complete();
notifyComplete(listener, new Result(request, requestFailure, response, responseFailure)); notifyComplete(listener, new Result(request, requestFailure, response, responseFailure));