@Override protected void cacheResult(Method method, Response response) { CacheKey cacheKey; if (response.getStatusCode() == Response.SC_CREATED && response.getHeader("Location") != null) { String uri; if (response.getHeader("Location").startsWith("http")) { uri = response.getHeader("Location"); } else { // TODO very fragile way of getting the URL uri = method.builder.getUrl().replaceFirst("/[^/]*$", "") + response.getHeader("Location"); } cacheKey = new UrlCacheKey(uri); } else { cacheKey = cacheKey(method.builder); } if (RequestBuilder.DELETE.toString().equalsIgnoreCase(method.builder.getHTTPMethod()) || // in case of a conflict the next GET request needs to // go remote !! response.getStatusCode() == Response.SC_CONFLICT) { cache.remove(cacheKey); } else if (method.builder.getUrl().matches(".*/[0-9]+$")) { // if url has an ID at the end then treat it as single entity // otherwise assume a collection which are not cached. cache.putResult(cacheKey, response); } } }
@Override protected void cacheResult(Method method, Response response) { CacheKey cacheKey; if (response.getStatusCode() == Response.SC_CREATED && response.getHeader("Location") != null) { String uri; if (response.getHeader("Location").startsWith("http")) { uri = response.getHeader("Location"); } else { // TODO very fragile way of getting the URL uri = method.builder.getUrl().replaceFirst("/[^/]*$", "") + response.getHeader("Location"); } cacheKey = new UrlCacheKey(uri); } else { cacheKey = cacheKey(method.builder); } if (RequestBuilder.DELETE.toString().equalsIgnoreCase(method.builder.getHTTPMethod()) || // in case of a conflict the next GET request needs to // go remote !! response.getStatusCode() == Response.SC_CONFLICT) { cache.remove(cacheKey); } else if (method.builder.getUrl().matches(".*/[0-9]+$")) { // if url has an ID at the end then treat it as single entity // otherwise assume a collection which are not cached. cache.putResult(cacheKey, response); } } }
public Promise<Response> send(Method method, String url, String data) { final Deferred<Response> deferred = q.defer(); try { //TODO support cancellation of requests //TODO https://github.com/kyoken74/gwt-angular/issues/69 RequestBuilder builder = new RequestBuilder(method, url); RequestCallback callback = new DeferredRequestCallback(deferred); Request request = builder.sendRequest(data, callback); deferred.progress(new HttpClientProgress(method.toString() + " " + url, request)); } catch (RequestException e) { deferred.reject(e); } catch (RuntimeException e) { deferred.reject(e); } return deferred.promise(); } }
/** * Creates a builder using the parameters for configuration. * * @param httpMethod HTTP method to use for the request * @param url URL that has already has already been encoded. Please see * {@link com.google.gwt.http.client.URL#encode(String)}, * {@link com.google.gwt.http.client.URL#encodePathSegment(String)} and * {@link com.google.gwt.http.client.URL#encodeQueryString(String)} for * how to do this. * @throws IllegalArgumentException if the httpMethod or URL are empty * @throws NullPointerException if the httpMethod or the URL are null */ public RequestBuilder(Method httpMethod, String url) { this((httpMethod == null) ? null : httpMethod.toString(), url); }
@Test public void testMakeRequest() { final String url = "http://localhost:8080/kie-server/services/rest/server"; final String none = "none"; final RequestCallback callback = mock(RequestCallback.class); final String expectedHTTPMethod = RequestBuilder.GET.toString(); doReturn(callback).when(presenter).makeCallback(onSuccess, onError); final RequestBuilder builder = presenter.makeRequest(url, onSuccess, onError); assertEquals(expectedHTTPMethod, builder.getHTTPMethod()); assertEquals(url, builder.getUrl()); assertEquals(none, builder.getUser()); assertEquals(none, builder.getPassword()); assertEquals(callback, builder.getCallback()); assertEquals("*/*", builder.getHeader("Accept")); }
@Override public void setFallbackMethod(Method method) { setFallbackMethodImpl(method.toString()); }
@Override public void setMethod(Method method) { setMethodImpl(method.toString()); }
/** * Creates a builder using the parameters for configuration. * * @param httpMethod HTTP method to use for the request * @param url URL that has already has already been encoded. Please see * {@link com.google.gwt.http.client.URL#encode(String)}, * {@link com.google.gwt.http.client.URL#encodePathSegment(String)} and * {@link com.google.gwt.http.client.URL#encodeQueryString(String)} for * how to do this. * @throws IllegalArgumentException if the httpMethod or URL are empty * @throws NullPointerException if the httpMethod or the URL are null */ public RequestBuilder(Method httpMethod, String url) { this((httpMethod == null) ? null : httpMethod.toString(), url); }
@Override public void setFallbackMethod(Method method) { setFallbackMethodImpl(method.toString()); }
@Override public void setMethod(Method method) { setMethodImpl(method.toString()); }
protected CacheKey cacheKey(RequestBuilder builder) { if (RequestBuilder.GET.toString().equalsIgnoreCase(builder.getHTTPMethod())) { return new ComplexCacheKey(builder); } return null; }
@Override protected CacheKey cacheKey(RequestBuilder builder) { if (RequestBuilder.GET.toString().equalsIgnoreCase(builder.getHTTPMethod())) { return new UrlCacheKey(builder); } return null; } }
protected CacheKey cacheKey(RequestBuilder builder) { if (RequestBuilder.GET.toString().equalsIgnoreCase(builder.getHTTPMethod())) { return new ComplexCacheKey(builder); } return null; }
@Override protected CacheKey cacheKey(RequestBuilder builder) { if (RequestBuilder.GET.toString().equalsIgnoreCase(builder.getHTTPMethod())) { return new UrlCacheKey(builder); } return null; } }
/** * Creates a builder using the parameters for configuration. * * @param httpMethod HTTP method to use for the request * @param url URL that has already has already been encoded. Please see * {@link com.google.gwt.http.client.URL#encode(String)}, * {@link com.google.gwt.http.client.URL#encodePathSegment(String)} and * {@link com.google.gwt.http.client.URL#encodeQueryString(String)} for * how to do this. * @throws IllegalArgumentException if the httpMethod or URL are empty * @throws NullPointerException if the httpMethod or the URL are null */ public RequestBuilder(Method httpMethod, String url) { this((httpMethod == null) ? null : httpMethod.toString(), url); }