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);
}
}
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);
}
}
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);
}
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));
}
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);
}
Aggregations