Created
February 27, 2010 02:17
-
-
Save mraible/316414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.appfuse.gwt.service.client.rest.RestConstants.*; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.http.client.Header; | |
import com.google.gwt.http.client.Request; | |
import com.google.gwt.http.client.RequestBuilder; | |
import com.google.gwt.http.client.RequestCallback; | |
import com.google.gwt.http.client.RequestException; | |
import com.google.gwt.http.client.Response; | |
/** | |
* This class wraps GWT's RequestBuilder to use a Command Pattern | |
* around RequestBuilder and allow chained callbacks (both success and error). | |
*/ | |
public class RestRequest { | |
public enum Method { | |
GET, | |
POST, | |
PUT, | |
DELETE | |
} | |
private RequestBuilder rb; | |
public static RestRequest get(String url) { | |
return new RestRequest(Method.GET, url); | |
} | |
public static RestRequest post(String url) { | |
return new RestRequest(Method.POST, url); | |
} | |
protected RestRequest(Method method, String url) { | |
this.rb = (method == Method.GET) | |
? new RequestBuilder(RequestBuilder.GET, url) | |
: new RequestBuilder(RequestBuilder.POST, url); | |
setHeader(AcceptHeader.JSON); | |
} | |
public RestRequest setTimeoutMillis(int timeout) { | |
this.rb.setTimeoutMillis(timeout); | |
return this; | |
} | |
public RestRequest setHeader(Header header) { | |
rb.setHeader(header.getName(), header.getValue()); | |
return this; | |
} | |
public RestRequest setHeader(String header, String value) { | |
rb.setHeader(header, value); | |
return this; | |
} | |
public RestRequest setContentType(String value) { | |
rb.setHeader(CONTENT_TYPE_HEADER, value); | |
return this; | |
} | |
public RestRequest setRequestData(String requestData) { | |
setContentType(RestConstants.HEADER_CONTENT_TYPE_URLENCODED); | |
rb.setRequestData(requestData); | |
return this; | |
} | |
public RestRequest setRequestData(String contentType, String requestData) { | |
setContentType(contentType); | |
rb.setRequestData(requestData); | |
return this; | |
} | |
public RestRequest setRequestData(Representation representation) { | |
setContentType(representation.getMimeType()); | |
rb.setRequestData(representation.getData()); | |
return this; | |
} | |
public void execute() { | |
build().run(); | |
} | |
public Deferred<Representation> build() { | |
if (rb.getTimeoutMillis() == 0) { | |
rb.setTimeoutMillis(RestConstants.DEFAULT_TIMEOUT); | |
} | |
return new AsyncDeferred<Representation>() { | |
final AsyncDeferred<Representation> deferred = this; | |
@Override | |
protected void asyncExecute() { | |
rb.setCallback(new RequestCallback() { | |
public void onResponseReceived(Request request, Response response) { | |
if (response.getStatusCode() >= Response.SC_INTERNAL_SERVER_ERROR) { | |
GWT.log("Received status code " + response.getStatusCode(), null); | |
onError(request, new ErrorResponseException(response)); | |
} else { | |
Representation representation = getRepresentation(response); | |
deferred.onResult(representation); | |
} | |
} | |
public void onError(Request request, Throwable exception) { | |
// TODO: Change to use common error-reporting mechanism | |
// https://track.chordiant.net/jira/browse/CXDES-16 | |
deferred.onError(exception); | |
} | |
}); | |
try { | |
rb.send(); | |
} catch (RequestException e) { | |
onError(e); | |
} | |
} | |
}; | |
} | |
private Representation getRepresentation(Response response) { | |
return new Representation(response.getHeader(CONTENT_TYPE_HEADER), | |
response.getText(), response.getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment