Search in sources :

Example 6 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method composeObject.

@Override
public void composeObject(Iterable<String> source, GcsFilename dest, long timeoutMillis) throws IOException {
    StringBuilder xmlContent = new StringBuilder(Iterables.size(source) * 50);
    Escaper escaper = XmlEscapers.xmlContentEscaper();
    xmlContent.append("<ComposeRequest>");
    for (String srcFileName : source) {
        xmlContent.append("<Component><Name>").append(escaper.escape(srcFileName)).append("</Name></Component>");
    }
    xmlContent.append("</ComposeRequest>");
    HTTPRequest req = makeRequest(dest, COMPOSE_QUERY_STRINGS, PUT, timeoutMillis, xmlContent.toString().getBytes(UTF_8));
    HTTPResponse resp;
    try {
        resp = urlfetch.fetch(req);
    } catch (IOException e) {
        throw createIOException(new HTTPRequestInfo(req), e);
    }
    if (resp.getResponseCode() != 200) {
        throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
    }
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) IOException(java.io.IOException) Escaper(com.google.common.escape.Escaper)

Example 7 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method putObject.

@Override
public void putObject(GcsFilename filename, GcsFileOptions options, ByteBuffer content, long timeoutMillis) throws IOException {
    HTTPRequest req = makeRequest(filename, null, PUT, timeoutMillis, content);
    addOptionsHeaders(req, options);
    HTTPResponse resp;
    try {
        resp = urlfetch.fetch(req);
    } catch (IOException e) {
        throw createIOException(new HTTPRequestInfo(req), e);
    }
    if (resp.getResponseCode() != 200) {
        throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
    }
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) IOException(java.io.IOException)

Example 8 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class URLFetchUtilsTest method testDescribeRequestAndResponseF.

@Test
public void testDescribeRequestAndResponseF() throws Exception {
    HTTPRequest request = new HTTPRequest(new URL("http://ping/pong"));
    request.setPayload("hello".getBytes());
    request.addHeader(new HTTPHeader("k1", "v1"));
    request.addHeader(new HTTPHeader("k2", "v2"));
    HTTPResponse response = mock(HTTPResponse.class);
    when(response.getHeadersUncombined()).thenReturn(ImmutableList.of(new HTTPHeader("k3", "v3")));
    when(response.getResponseCode()).thenReturn(500);
    when(response.getContent()).thenReturn("bla".getBytes());
    String expected = "Request: GET http://ping/pong\nk1: v1\nk2: v2\n\n" + "5 bytes of content\n\nResponse: 500 with 3 bytes of content\nk3: v3\nbla\n";
    String result = URLFetchUtils.describeRequestAndResponse(new HTTPRequestInfo(request), response);
    assertEquals(expected, result);
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) URL(java.net.URL) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader) Test(org.junit.Test)

Example 9 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class URLFetchUtilsTest method testDescribeRequestAndResponseForApiClient.

@Test
public void testDescribeRequestAndResponseForApiClient() throws Exception {
    HttpRequestInitializer initializer = mock(HttpRequestInitializer.class);
    NetHttpTransport transport = new NetHttpTransport();
    Storage storage = new Storage.Builder(transport, new JacksonFactory(), initializer).setApplicationName("bla").build();
    HttpRequest request = storage.objects().delete("bucket", "object").buildHttpRequest();
    request.getHeaders().clear();
    request.getHeaders().put("k1", "v1");
    request.getHeaders().put("k2", "v2");
    HttpResponseException exception = null;
    try {
        request.execute();
    } catch (HttpResponseException ex) {
        exception = ex;
    }
    String expected = "Request: DELETE " + Storage.DEFAULT_BASE_URL + "b/bucket/o/object\n" + "k1: v1\nk2: v2\n\nno content\n\nResponse: 40";
    String result = URLFetchUtils.describeRequestAndResponse(new HTTPRequestInfo(request), exception);
    assertTrue(expected + "\nis not a prefix of:\n" + result, result.startsWith(expected));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Storage(com.google.api.services.storage.Storage) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponseException(com.google.api.client.http.HttpResponseException) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Test(org.junit.Test)

Example 10 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method list.

@Override
public ListItemBatch list(String bucket, String prefix, String delimiter, String marker, int maxResults, long timeoutMillis) throws IOException {
    GcsFilename filename = new GcsFilename(bucket, "");
    Map<String, String> queryStrings = new LinkedHashMap<>();
    if (!Strings.isNullOrEmpty(prefix)) {
        queryStrings.put(PREFIX, prefix);
    }
    if (!Strings.isNullOrEmpty(delimiter)) {
        queryStrings.put(DELIMITER, delimiter);
    }
    if (!Strings.isNullOrEmpty(marker)) {
        queryStrings.put(MARKER, marker);
    }
    if (maxResults >= 0) {
        queryStrings.put(MAX_KEYS, String.valueOf(maxResults));
    }
    HTTPRequest req = makeRequest(filename, queryStrings, GET, timeoutMillis);
    HTTPResponse resp;
    try {
        resp = urlfetch.fetch(req);
    } catch (IOException e) {
        throw createIOException(new HTTPRequestInfo(req), e);
    }
    if (resp.getResponseCode() != 200) {
        throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
    }
    String nextMarker = null;
    List<ListItem> items = new ArrayList<>();
    try {
        XmlHandler xmlHandler = new XmlHandler(resp.getContent(), PATHS);
        while (xmlHandler.hasNext()) {
            XmlHandler.XmlEvent event = xmlHandler.next();
            if (event.getEventType() == EventType.CLOSE_ELEMENT) {
                switch(event.getName()) {
                    case "NextMarker":
                        nextMarker = event.getValue();
                        break;
                    case "Prefix":
                        String name = event.getValue();
                        items.add(new ListItem.Builder().setName(name).setDirectory(true).build());
                        break;
                    default:
                        break;
                }
            } else if (event.getName().equals("Contents")) {
                items.add(parseContents(xmlHandler));
            }
        }
    } catch (XMLStreamException e) {
        throw HttpErrorHandler.createException("Failed to parse response", e.getMessage());
    }
    return new ListItemBatch(items, nextMarker);
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) XMLStreamException(javax.xml.stream.XMLStreamException) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) ListItem(com.google.appengine.tools.cloudstorage.ListItem) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Aggregations

HTTPRequestInfo (com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo)12 HTTPRequest (com.google.appengine.api.urlfetch.HTTPRequest)11 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)11 IOException (java.io.IOException)8 HTTPHeader (com.google.appengine.api.urlfetch.HTTPHeader)3 FutureWrapper (com.google.appengine.api.utils.FutureWrapper)2 URL (java.net.URL)2 Test (org.junit.Test)2 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Storage (com.google.api.services.storage.Storage)1 BadRangeException (com.google.appengine.tools.cloudstorage.BadRangeException)1 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 ListItem (com.google.appengine.tools.cloudstorage.ListItem)1 Escaper (com.google.common.escape.Escaper)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1