Search in sources :

Example 1 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImpl method fetch.

@Override
public HTTPResponse fetch(HTTPRequest request) throws IOException {
    byte[] responseBytes;
    try {
        responseBytes = ApiProxy.makeSyncCall(PACKAGE, "Fetch", convertToPb(request).toByteArray(), createApiConfig(request.getFetchOptions()));
    } catch (ApiProxy.RequestTooLargeException ex) {
        throw new IOException("The request exceeded the maximum permissible size");
    } catch (ApiProxy.ApplicationException ex) {
        Throwable cause = convertApplicationException(request.getURL(), ex);
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw new RuntimeException(cause);
        }
    } catch (ApiProxy.ApiDeadlineExceededException ex) {
        throw new SocketTimeoutException("Timeout while fetching URL: " + request.getURL());
    } catch (ApiProxy.ApiProxyException ex) {
        throw new IOException(ex);
    }
    URLFetchResponse responseProto = URLFetchResponse.parseFrom(responseBytes, ExtensionRegistry.getEmptyRegistry());
    if (!request.getFetchOptions().getAllowTruncate() && responseProto.getContentWasTruncated()) {
        throw new ResponseTooLargeException(request.getURL().toString());
    }
    return convertFromPb(responseProto);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) SocketTimeoutException(java.net.SocketTimeoutException) ApiProxy(com.google.apphosting.api.ApiProxy) IOException(java.io.IOException)

Example 2 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImpl method fetchAsync.

@Override
public Future<HTTPResponse> fetchAsync(HTTPRequest request) {
    final FetchOptions fetchOptions = request.getFetchOptions();
    final URL url = request.getURL();
    Future<byte[]> response = ApiProxy.makeAsyncCall(PACKAGE, "Fetch", convertToPb(request).toByteArray(), createApiConfig(fetchOptions));
    // Request is not held onto after return to avoid holding more memory than needed.
    return new FutureWrapper<byte[], HTTPResponse>(response) {

        @Override
        protected HTTPResponse wrap(byte @Nullable [] responseBytes) throws IOException {
            URLFetchResponse responseProto = URLFetchResponse.newBuilder().mergeFrom(responseBytes).build();
            if (!fetchOptions.getAllowTruncate() && responseProto.getContentWasTruncated()) {
                throw new ResponseTooLargeException(url.toString());
            }
            return convertFromPb(responseProto);
        }

        @Override
        protected Throwable convertException(Throwable cause) {
            if (cause instanceof ApiProxy.ApplicationException) {
                return convertApplicationException(url, (ApiProxy.ApplicationException) cause);
            } else if (cause instanceof ApiProxy.ApiDeadlineExceededException) {
                return new SocketTimeoutException("Timeout while fetching URL: " + url);
            }
            return cause;
        }
    };
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) SocketTimeoutException(java.net.SocketTimeoutException) ApiProxy(com.google.apphosting.api.ApiProxy) FutureWrapper(com.google.appengine.api.utils.FutureWrapper) URL(java.net.URL)

Example 3 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method deadlineMatches.

private void deadlineMatches(Double expectedDeadline, FetchOptions options) throws Exception {
    URL url = new URL("http://www.google.com");
    HTTPRequest request;
    if (options == null) {
        request = new HTTPRequest(url, HTTPMethod.GET);
    } else {
        request = new HTTPRequest(url, HTTPMethod.GET, options);
    }
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    String responseContent = "<p>This is the desired response.</p>";
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContent(ByteString.copyFromUtf8(responseContent)).build();
    when(delegate.makeSyncCall(hasCorrectDeadline(ApiProxy.getCurrentEnvironment(), expectedDeadline), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    URLFetchServiceImpl.DeadlineParser.INSTANCE.refresh();
    HTTPResponse response = new URLFetchServiceImpl().fetch(request);
    assertThat(new String(response.getContent(), UTF_8)).isEqualTo(responseContent);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) ByteString(com.google.protobuf.ByteString) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL)

Example 4 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method testSync_TruncateResponse_DisallowTruncate.

@Test
public void testSync_TruncateResponse_DisallowTruncate() throws Exception {
    URL url = new URL("http://non-existent-domain.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, disallowTruncate());
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContentWasTruncated(true).build();
    when(delegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    ResponseTooLargeException ex = assertThrows(ResponseTooLargeException.class, () -> new URLFetchServiceImpl().fetch(request));
    assertThat(ex).hasMessageThat().contains(url.toString());
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL) Test(org.junit.Test)

Example 5 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method checkTlsValidation.

private void checkTlsValidation(FetchOptions fetchOptions, Consumer<URLFetchRequest> check) throws Exception {
    URL url = new URL("https://validate.me/");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
    URLFetchResponse urlFetchResponse = URLFetchResponse.newBuilder().setStatusCode(200).build();
    when(delegate.makeSyncCall(any(), any(), any(), any())).thenReturn(urlFetchResponse.toByteArray());
    new URLFetchServiceImpl().fetch(request);
    ArgumentCaptor<byte[]> requestProtoCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(delegate).makeSyncCall(any(), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), requestProtoCaptor.capture());
    URLFetchRequest urlFetchRequest = URLFetchRequest.parseFrom(requestProtoCaptor.getValue(), ExtensionRegistry.getGeneratedRegistry());
    check.accept(urlFetchRequest);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL)

Aggregations

URLFetchResponse (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse)21 URLFetchRequest (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest)19 URL (java.net.URL)18 Test (org.junit.Test)16 ByteString (com.google.protobuf.ByteString)11 SocketTimeoutException (java.net.SocketTimeoutException)3 ApiProxy (com.google.apphosting.api.ApiProxy)2 IOException (java.io.IOException)2 FutureWrapper (com.google.appengine.api.utils.FutureWrapper)1 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)1 LocalRpcService (com.google.appengine.tools.development.LocalRpcService)1 ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)1 SSLException (javax.net.ssl.SSLException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 HttpParams (org.apache.http.params.HttpParams)1