Java Code Examples for com.google.appengine.api.urlfetch.HTTPResponse#getContent()

The following examples show how to use com.google.appengine.api.urlfetch.HTTPResponse#getContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GceInstanceCreator.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param instanceName the name of the instance to create.
 * @param bootDiskName the name of the disk to create the instance with.
 * @param projectApiKey the project API key.
 * @param accessToken the access token.
 * @param configProperties the configuration properties.
 * @throws MalformedURLException
 * @throws IOException
 * @throws OrchestratorException if the REST API call failed to create instance.
 */
private void createInstance(String instanceName, String bootDiskName, String projectApiKey,
    String accessToken, Map<String, String> configProperties)
    throws MalformedURLException, IOException, OrchestratorException {
  String url = GceApiUtils.composeInstanceApiUrl(
      ConfigProperties.urlPrefixWithProjectAndZone, projectApiKey);
  String payload = createPayload_instance(instanceName, bootDiskName, configProperties);
  logger.info(
      "Calling " + url + " to create instance " + instanceName + "with payload " + payload);
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, payload, HTTPMethod.POST);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Failed to create GCE instance. " + instanceName
        + ". Response code " + responseCode + " Reason: "
        + new String(httpResponse.getContent()));
  }
}
 
Example 2
Source File: GceInstanceCreator.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the disk or instance is available.
 *
 * @param accessToken the access token.
 * @param url the URL to check whether the disk/instance has been created.
 * @return true if the disk/instance is available, false otherwise.
 * @throws MalformedURLException
 * @throws IOException
 */
private boolean checkDiskOrInstance(String accessToken, String url)
    throws MalformedURLException, IOException {
  HTTPResponse httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.GET);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    logger.fine("Disk/instance not ready. Response code " + responseCode + " Reason: "
        + new String(httpResponse.getContent()));
    return false;
  }
  // Check if the disk/instance is in status "READY".
  String contentStr = new String(httpResponse.getContent());
  JsonParser parser = new JsonParser();
  JsonObject o = (JsonObject) parser.parse(contentStr);
  String status = o.get("status").getAsString();
  if (!status.equals("READY") && !status.equals("RUNNING")) {
    return false;
  }
  return true;
}
 
Example 3
Source File: GceInstanceDestroyer.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes an instance.
 *
 * @param name the name of the instance to delete.
 * @throws OrchestratorException if delete failed.
 */
private void deleteInstance(String name, String accessToken, String url)
    throws OrchestratorException {
  logger.info("Shutting down instance: " + name);
  HTTPResponse httpResponse;
  try {
    httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.DELETE);
    int responseCode = httpResponse.getResponseCode();
    if (!(responseCode == 200 || responseCode == 204)) {
      throw new OrchestratorException("Delete Instance failed. Response code " + responseCode
          + " Reason: " + new String(httpResponse.getContent()));
    }
  } catch (IOException e) {
    throw new OrchestratorException(e);
  }
}
 
Example 4
Source File: NordnUploadAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Upload LORDN file to MarksDB.
 *
 * <p>Idempotency: If the exact same LORDN report is uploaded twice, the MarksDB server will
 * return the same confirmation number.
 *
 * @see <a href="http://tools.ietf.org/html/draft-lozano-tmch-func-spec-08#section-6.3">
 *     TMCH functional specifications - LORDN File</a>
 */
private void uploadCsvToLordn(String urlPath, String csvData) throws IOException {
  String url = tmchMarksdbUrl + urlPath;
  logger.atInfo().log(
      "LORDN upload task %s: Sending to URL: %s ; data: %s", actionLogId, url, csvData);
  HTTPRequest req = new HTTPRequest(new URL(url), POST, validateCertificate().setDeadline(60d));
  lordnRequestInitializer.initialize(req, tld);
  setPayloadMultipart(req, "file", "claims.csv", CSV_UTF_8, csvData, random);
  HTTPResponse rsp;
  try {
    rsp = fetchService.fetch(req);
  } catch (IOException e) {
    throw new IOException(
        String.format("Error connecting to MarksDB at URL %s", url), e);
  }
  if (logger.atInfo().isEnabled()) {
    String response =
        (rsp.getContent() == null) ? "(null)" : new String(rsp.getContent(), US_ASCII);
    logger.atInfo().log(
        "LORDN upload task %s response: HTTP response code %d, response data: %s",
        actionLogId, rsp.getResponseCode(), response);
  }
  if (rsp.getResponseCode() != SC_ACCEPTED) {
    throw new UrlFetchException(
        String.format(
            "LORDN upload task %s error: Failed to upload LORDN claims to MarksDB", actionLogId),
        req,
        rsp);
  }
  Optional<String> location = getHeaderFirst(rsp, LOCATION);
  if (!location.isPresent()) {
    throw new UrlFetchException(
        String.format(
            "LORDN upload task %s error: MarksDB failed to provide a Location header",
            actionLogId),
        req,
        rsp);
  }
  getQueue(NordnVerifyAction.QUEUE).add(makeVerifyTask(new URL(location.get())));
}
 
Example 5
Source File: Marksdb.java    From nomulus with Apache License 2.0 5 votes vote down vote up
byte[] fetch(URL url, Optional<String> loginAndPassword) throws IOException {
  HTTPRequest req = new HTTPRequest(url, GET, validateCertificate().setDeadline(60d));
  setAuthorizationHeader(req, loginAndPassword);
  HTTPResponse rsp;
  try {
    rsp = fetchService.fetch(req);
  } catch (IOException e) {
    throw new IOException(
        String.format("Error connecting to MarksDB at URL %s", url), e);
  }
  if (rsp.getResponseCode() != SC_OK) {
    throw new UrlFetchException("Failed to fetch from MarksDB", req, rsp);
  }
  return rsp.getContent();
}
 
Example 6
Source File: RdeReporter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Unmarshals IIRDEA XML result object from {@link HTTPResponse} payload.
 *
 * @see <a href="http://tools.ietf.org/html/draft-lozano-icann-registry-interfaces-05#section-4.1">
 *     ICANN Registry Interfaces - IIRDEA Result Object</a>
 */
private XjcIirdeaResult parseResult(HTTPResponse rsp) throws XmlException {
  byte[] responseBytes = rsp.getContent();
  logger.atInfo().log("Received response:\n%s", new String(responseBytes, UTF_8));
  XjcIirdeaResponseElement response = XjcXmlTransformer.unmarshal(
      XjcIirdeaResponseElement.class, new ByteArrayInputStream(responseBytes));
  return response.getResult();
}
 
Example 7
Source File: AppEngineHttpFetcher.java    From openid4java with Apache License 2.0 5 votes vote down vote up
private static int getContentLength(HTTPResponse httpResponse) {
  byte[] content = httpResponse.getContent();
  if (content == null) {
    return 0;
  } else {
    return content.length;
  }
}
 
Example 8
Source File: URLFetchUtils.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
static void appendResponse(HTTPResponse resp, StringBuilder b) {
  byte[] content = resp.getContent();
  b.append(resp.getResponseCode()).append(" with ").append(content == null ? 0 : content.length);
  b.append(" bytes of content");
  for (HTTPHeader h : resp.getHeadersUncombined()) {
    b.append('\n').append(h.getName()).append(": ").append(h.getValue());
  }
  b.append('\n').append(content == null ? "" : new String(content, UTF_8)).append('\n');
}
 
Example 9
Source File: URLFetchTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPayload() throws Exception {
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    URL url = getFetchUrl();

    HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
    req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
    req.setPayload("Tralala".getBytes(UTF_8));

    HTTPResponse response = service.fetch(req);
    String content = new String(response.getContent());
    Assert.assertEquals("Hopsasa", content);
}
 
Example 10
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
private static Response toRequestorResponse(HTTPResponse response) {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    for (HTTPHeader header : response.getHeadersUncombined()) {
        List<String> existing = headers.get(header.getName());
        if (existing == null) {
            existing = new ArrayList<String>();
            headers.put(header.getName(), existing);
        }
        existing.add(header.getValue());
    }

    return new Response(response.getResponseCode(),
                        new ByteArrayInputStream(response.getContent()),
                        headers);
}
 
Example 11
Source File: HttpClientGAE.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
public static String processResponse(HTTPResponse response) {
	String content = new String(response.getContent(), UTF8);
	if (response.getResponseCode() >= 400) {
		throw new RuntimeException("HttpError:" + response.getResponseCode() + "\n" + content);
	}
	return content;
}
 
Example 12
Source File: GceInstanceCreator.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new disk.
 *
 * @param diskName the name of the disk to create.
 * @param accessToken the access token.
 * @param configProperties the configuration properties.
 * @throws MalformedURLException
 * @throws IOException
 * @throws OrchestratorException if the REST API call failed to create disk.
 */
private void createDisk(String diskName, String accessToken, String projectApiKey,
    Map<String, String> configProperties)
    throws MalformedURLException, IOException, OrchestratorException {
  String url =
      GceApiUtils.composeDiskApiUrl(ConfigProperties.urlPrefixWithProjectAndZone, projectApiKey);
  String payload = createPayload_disk(diskName, configProperties);
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, payload, HTTPMethod.POST);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Failed to create Disk " + diskName + ". Response code "
        + responseCode + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 13
Source File: GceApiUtils.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a disk.
 *
 * @param diskName the name of the disk to delete.
 * @throws IOException thrown by makeHttpRequest if the Compute Engine API could not be contacted.
 * @throws OrchestratorException if the REST API call failed to delete the disk.
 */
public static void deleteDisk(String diskName, String accessToken, String url)
    throws IOException, OrchestratorException {
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.DELETE);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Delete Disk failed. Response code " + responseCode
        + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 14
Source File: GceInstanceDestroyer.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the instance is deleted.
 *
 * @param accessToken the access token.
 * @param url the URL to check whether the instance is still up.
 * @return true if the instance is not found, false otherwise.
 * @throws IOException thrown by makeHttpRequest if the Compute Engine API could not be contacted.
 * @throws OrchestratorException
 */
private boolean isInstanceDeleted(String accessToken, String url)
    throws IOException, OrchestratorException {
  HTTPResponse httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.GET);
  int responseCode = httpResponse.getResponseCode();
  if (responseCode == 404) { // Not Found.
    return true;
  } else if (responseCode == 200 || responseCode == 204) {
    return false;
  } else {
    throw new OrchestratorException("Failed to check if instance is deleted. Response code "
        + responseCode + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 15
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 4 votes vote down vote up
/**
 * Might not fill all of dst.
 */
@Override
public Future<GcsFileMetadata> readObjectAsync(final ByteBuffer dst, final GcsFilename filename,
    long startOffsetBytes, long timeoutMillis) {
  Preconditions.checkArgument(startOffsetBytes >= 0, "%s: offset must be non-negative: %s", this,
      startOffsetBytes);
  final int n = dst.remaining();
  Preconditions.checkArgument(n > 0, "%s: dst full: %s", this, dst);
  final int want = Math.min(READ_LIMIT_BYTES, n);

  final HTTPRequest req = makeRequest(filename, null, GET, timeoutMillis);
  req.setHeader(
      new HTTPHeader(RANGE, "bytes=" + startOffsetBytes + "-" + (startOffsetBytes + want - 1)));
  final HTTPRequestInfo info = new HTTPRequestInfo(req);
  return new FutureWrapper<HTTPResponse, GcsFileMetadata>(urlfetch.fetchAsync(req)) {
    @Override
    protected GcsFileMetadata wrap(HTTPResponse resp) throws IOException {
      long totalLength;
      switch (resp.getResponseCode()) {
        case 200:
          totalLength = getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH);
          break;
        case 206:
          totalLength = getLengthFromContentRange(resp);
          break;
        case 404:
          throw new FileNotFoundException("Could not find: " + filename);
        case 416:
          throw new BadRangeException("Requested Range not satisfiable; perhaps read past EOF? "
              + URLFetchUtils.describeRequestAndResponse(info, resp));
        default:
          throw HttpErrorHandler.error(info, resp);
      }
      byte[] content = resp.getContent();
      Preconditions.checkState(content.length <= want, "%s: got %s > wanted %s", this,
          content.length, want);
      dst.put(content);
      return getMetadataFromResponse(filename, resp, totalLength);
    }

    @Override
    protected Throwable convertException(Throwable e) {
      return OauthRawGcsService.convertException(info, e);
    }
  };
}
 
Example 16
Source File: URLFetchServiceTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected String fetchUrl(String url, int expectedResponse) throws IOException {
    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse httpResponse = urlFetchService.fetch(new URL(url));
    assertEquals(url, expectedResponse, httpResponse.getResponseCode());
    return new String(httpResponse.getContent());
}