Skip to content

Commit

Permalink
restclient: enhancements to debug output. print basic response info.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilmer committed Oct 24, 2011
1 parent ac8e2c9 commit fe17429
Showing 1 changed file with 79 additions and 31 deletions.
110 changes: 79 additions & 31 deletions org.touge.restclient/src/org/touge/restclient/ReSTClient.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -206,6 +209,10 @@ public void handleError(int code) throws IOException {
} }


}; };
/**
* Time format for debug messages.
*/
private static final String DEBUG_TIME_FORMAT = "H:mm:ss:SSS";


/** /**
* The response from the server for a given request. * The response from the server for a given request.
Expand Down Expand Up @@ -357,8 +364,9 @@ public String getName() {
private List<ConnectionInitializer> connectionInitializers; private List<ConnectionInitializer> connectionInitializers;


private ErrorHandler errorHandler; private ErrorHandler errorHandler;
private OutputStream debugStream; private PrintWriter debugStream;
private StringBuilder debugBuffer; //private StringBuilder debugBuffer;
private SimpleDateFormat debugTimeFormat;


/** /**
* Default constructor. * Default constructor.
Expand Down Expand Up @@ -434,7 +442,7 @@ public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer i
* @param debugStream OutputStream to pass debug messages to. If null, no debug output. * @param debugStream OutputStream to pass debug messages to. If null, no debug output.
*/ */
public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer, public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer,
ResponseDeserializer<?> deserializer, ErrorHandler errorHandler, OutputStream debugStream) { ResponseDeserializer<?> deserializer, ErrorHandler errorHandler, PrintWriter debugStream) {
this.connectionProvider = connectionProvider; this.connectionProvider = connectionProvider;
this.connectionInitializers = new ArrayList<ConnectionInitializer>(); this.connectionInitializers = new ArrayList<ConnectionInitializer>();
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
Expand Down Expand Up @@ -471,8 +479,12 @@ public void setErrorHandler(ErrorHandler handler) {
* *
* @param debugStream OutputStream * @param debugStream OutputStream
*/ */
public void setDebugStream(OutputStream debugStream) { public void setDebugWriter(PrintWriter writer) {
this.debugStream = debugStream; this.debugStream = writer;

if (writer != null && debugTimeFormat == null) {
debugTimeFormat = new SimpleDateFormat(DEBUG_TIME_FORMAT);
}
} }


/** /**
Expand Down Expand Up @@ -532,15 +544,13 @@ public <T> Response<T> call(final HttpMethod method, final String url, final Res
if (!url.toLowerCase().startsWith("http://")) if (!url.toLowerCase().startsWith("http://"))
httpUrl = "http://" + url; httpUrl = "http://" + url;


StringBuilder debugBuffer = null;
if (debugStream != null) if (debugStream != null)
debugStart(httpUrl); debugBuffer = debugStart(httpUrl, method.toString());


final HttpURLConnection connection = connectionProvider.getConnection(httpUrl); final HttpURLConnection connection = connectionProvider.getConnection(httpUrl);
connection.setRequestMethod(method.toString()); connection.setRequestMethod(method.toString());


if (debugStream != null)
debugMid(method.toString());

for (ConnectionInitializer initializer : connectionInitializers) for (ConnectionInitializer initializer : connectionInitializers)
initializer.initialize(connection); initializer.initialize(connection);


Expand All @@ -562,7 +572,7 @@ public <T> Response<T> call(final HttpMethod method, final String url, final Res
baos.close(); baos.close();


if (debugStream != null) if (debugStream != null)
debugMid(new String(baos.toByteArray())); debugMid(debugBuffer, new String(baos.toByteArray()));
break; break;
case PUT: case PUT:
connection.setDoOutput(true); connection.setDoOutput(true);
Expand All @@ -572,7 +582,7 @@ public <T> Response<T> call(final HttpMethod method, final String url, final Res
baos.close(); baos.close();


if (debugStream != null) if (debugStream != null)
debugMid(new String(baos.toByteArray())); debugMid(debugBuffer, new String(baos.toByteArray()));
break; break;
case DELETE: case DELETE:
connection.setDoInput(true); connection.setDoInput(true);
Expand All @@ -585,17 +595,24 @@ public <T> Response<T> call(final HttpMethod method, final String url, final Res
throw new RuntimeException("Unhandled HTTP method."); throw new RuntimeException("Unhandled HTTP method.");
} }


if (debugStream != null) if (debugStream != null)
debugEnd(); debugEnd(debugBuffer);


return new Response<T>() { return new Response<T>() {


private boolean done; private boolean done;
private boolean cancelled; private boolean cancelled;
private StringBuilder responseBuffer;


@Override @Override
public int getCode() throws IOException { public int getCode() throws IOException {
return connection.getResponseCode(); int code = connection.getResponseCode();

if (debugStream != null) {
responseBuffer = debugStart(code, connection.getResponseMessage());
}

return code;
} }


@Override @Override
Expand All @@ -615,19 +632,24 @@ public HttpURLConnection getConnection() {


@Override @Override
public boolean isError() { public boolean isError() {
int code;
try { try {
code = getCode(); int code = getCode();
return code >= HttpURLConnection.HTTP_BAD_REQUEST && code < HttpURLConnection.HTTP_VERSION; return code >= HttpURLConnection.HTTP_BAD_REQUEST && code < HttpURLConnection.HTTP_VERSION;
} catch (IOException e) { } catch (IOException e) {
return true; return true;
} }
} }


@Override @Override
public boolean cancel(boolean flag) { public boolean cancel(boolean flag) {
connection.disconnect(); connection.disconnect();
cancelled = true; cancelled = true;

if (responseBuffer != null) {
debugMid(responseBuffer, "[CANCELLED]");
debugEnd(responseBuffer);
}

return cancelled; return cancelled;
} }


Expand All @@ -644,6 +666,10 @@ public boolean isDone() {
@Override @Override
public T getContent() throws IOException { public T getContent() throws IOException {
if (isError()) { if (isError()) {
if (responseBuffer != null) {
debugEnd(responseBuffer);
}

if (errorHandler != null) if (errorHandler != null)
errorHandler.handleError(getCode()); errorHandler.handleError(getCode());


Expand All @@ -658,41 +684,63 @@ public T getContent() throws IOException {
// If no deserializer is specified, use String. // If no deserializer is specified, use String.
T response = (T) ReSTClient.STRING_DESERIALIZER.deserialize(connection.getInputStream(), 0, null); T response = (T) ReSTClient.STRING_DESERIALIZER.deserialize(connection.getInputStream(), 0, null);
done = true; done = true;

if (responseBuffer != null) {
debugMid(responseBuffer, ((T)response).toString());
debugEnd(responseBuffer);
}

return (T) response; return (T) response;
} }


T response = (T) deserializer.deserialize(connection.getInputStream(), T response = (T) deserializer.deserialize(connection.getInputStream(),
connection.getResponseCode(), connection.getHeaderFields()); connection.getResponseCode(), connection.getHeaderFields());


done = true; done = true;

if (responseBuffer != null) {
debugEnd(responseBuffer);
}

return response; return response;
} }


}; };
} }


private void debugStart(String httpUrl) { private StringBuilder debugStart(String httpUrl, String httpMethod) {
debugBuffer = new StringBuilder(); StringBuilder debugBuffer = new StringBuilder();
debugBuffer.append(this.getClass().getSimpleName()); debugBuffer.append(debugTimeFormat.format(new Date(System.currentTimeMillis())));
debugBuffer.append(' '); debugBuffer.append(' ');
debugBuffer.append(System.currentTimeMillis()); debugBuffer.append(httpMethod.subSequence(0, 3));
debugBuffer.append(' '); debugBuffer.append(' ');
debugBuffer.append(httpUrl); debugBuffer.append(httpUrl);
debugBuffer.append(' '); debugBuffer.append(' ');

return debugBuffer;
} }


private void debugMid(String element) { private StringBuilder debugStart(int responseCode, String responseMessage) {
StringBuilder debugBuffer = new StringBuilder();
debugBuffer.append(debugTimeFormat.format(new Date(System.currentTimeMillis())));
debugBuffer.append(' ');
debugBuffer.append("HTTP Response ");
debugBuffer.append(responseCode);
debugBuffer.append(": ");
debugBuffer.append(responseMessage);
debugBuffer.append(' ');

return debugBuffer;
}

private void debugMid(StringBuilder debugBuffer, String element) {
debugBuffer.append(element); debugBuffer.append(element);
debugBuffer.append(' '); debugBuffer.append(' ');
} }


private void debugEnd() { private void debugEnd(StringBuilder debugBuffer) {
debugBuffer.append('\n'); debugStream.println(debugBuffer.toString());
try { debugStream.flush();
debugStream.write(debugBuffer.toString().getBytes());
debugStream.flush();
} catch (IOException e) {
}
} }


/** /**
Expand Down

0 comments on commit fe17429

Please sign in to comment.