Package com.google.appengine.api.urlfetch

Examples of com.google.appengine.api.urlfetch.HTTPResponse


  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);
View Full Code Here


    assertEquals(expected, result);
  }

  @Test
  public void testGetSingleHeader() {
    HTTPResponse response = mock(HTTPResponse.class);
    try {
      URLFetchUtils.getSingleHeader(response, "k1");
      fail("NoSuchElementException expected");
    } catch (NoSuchElementException expected) {
    }

    List<HTTPHeader> headers =
        ImmutableList.of(new HTTPHeader("k3", "v3"), new HTTPHeader("k1", "v1"));
    when(response.getHeadersUncombined()).thenReturn(headers);
    assertEquals("v1", URLFetchUtils.getSingleHeader(response, "k1"));

    headers = ImmutableList.of(new HTTPHeader("k3", "v3"), new HTTPHeader("k1", "v1"),
        new HTTPHeader("k1", "v2"));
    when(response.getHeadersUncombined()).thenReturn(headers);
    try {
      URLFetchUtils.getSingleHeader(response, "k1");
      fail("NoSuchElementException expected");
    } catch (IllegalArgumentException  expected) {
    }
View Full Code Here

  public RawGcsCreationToken beginObjectCreation(
      GcsFilename filename, GcsFileOptions options, long timeoutMillis) throws IOException {
    HTTPRequest req = makeRequest(filename, null, POST, timeoutMillis, headers);
    req.setHeader(RESUMABLE_HEADER);
    addOptionsHeaders(req, options);
    HTTPResponse resp;
    try {
      resp = urlfetch.fetch(req);
    } catch (IOException e) {
      throw createIOException(new HTTPRequestInfo(req), e);
    }
    if (resp.getResponseCode() == 201) {
      String location = URLFetchUtils.getSingleHeader(resp, LOCATION);
      String queryString = new URL(location).getQuery();
      Preconditions.checkState(
          queryString != null, LOCATION + " header," + location + ", witout a query string");
      Map<String, String> params = Splitter.on('&').withKeyValueSeparator('=').split(queryString);
View Full Code Here

      long timeoutMillis) throws IOException {
    HTTPRequest req = makeRequest(filename, null, PUT, timeoutMillis, headers);
    req.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(content.remaining())));
    req.setPayload(peekBytes(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);
    }
  }
View Full Code Here

  private RawGcsCreationToken put(final GcsRestCreationToken token, ByteBuffer chunk,
      final boolean isFinalChunk, long timeoutMillis) throws IOException {
    final int length = chunk.remaining();
    HTTPRequest req = createPutRequest(token, chunk, isFinalChunk, timeoutMillis, length);
    HTTPRequestInfo info = new HTTPRequestInfo(req);
    HTTPResponse response;
    try {
      response = urlfetch.fetch(req);
    } catch (IOException e) {
      throw createIOException(info, e);
    }
View Full Code Here

  /** True if deleted, false if not found. */
  @Override
  public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException {
    HTTPRequest req = makeRequest(filename, null, DELETE, timeoutMillis, headers);
    HTTPResponse resp;
    try {
      resp = urlfetch.fetch(req);
    } catch (IOException e) {
      throw createIOException(new HTTPRequestInfo(req), e);
    }
    switch (resp.getResponseCode()) {
      case 204:
        return true;
      case 404:
        return false;
      default:
View Full Code Here

  @Override
  public GcsFileMetadata getObjectMetadata(GcsFilename filename, long timeoutMillis)
      throws IOException {
    HTTPRequest req = makeRequest(filename, null, HEAD, timeoutMillis, headers);
    HTTPResponse resp;
    try {
      resp = urlfetch.fetch(req);
    } catch (IOException e) {
      throw createIOException(new HTTPRequestInfo(req), e);
    }
    int responseCode = resp.getResponseCode();
    if (responseCode == 404) {
      return null;
    }
    if (responseCode != 200) {
      throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
View Full Code Here

    }
    xmlContent.append("</ComposeRequest>");
    byte[] payload = xmlContent.toString().getBytes(UTF_8);
    req.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length)));
    req.setPayload(payload);
    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);
    }
  }
View Full Code Here

  public void copyObject(GcsFilename source, GcsFilename dest, long timeoutMillis)
      throws IOException {
    HTTPRequest req = makeRequest(dest, null, PUT, timeoutMillis, headers);
    req.setHeader(new HTTPHeader(X_GOOG_COPY_SOURCE, makePath(source)));
    req.setHeader(ZERO_CONTENT_LENGTH);
    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);
    }
  }
View Full Code Here

    if (maxResults >= 0) {
      queryStrings.put(MAX_KEYS, String.valueOf(maxResults));
    }
    HTTPRequest req = makeRequest(filename, queryStrings, GET, timeoutMillis, headers);
    req.setHeader(ZERO_CONTENT_LENGTH);
    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":
View Full Code Here

TOP

Related Classes of com.google.appengine.api.urlfetch.HTTPResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.