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

The following examples show how to use com.google.appengine.api.urlfetch.HTTPResponse#getResponseCode() . 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: CurlServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException {
  String url = req.getParameter("url");
  String deadlineSecs = req.getParameter("deadline");
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  HTTPRequest fetchReq = new HTTPRequest(new URL(url));
  if (deadlineSecs != null) {
    fetchReq.getFetchOptions().setDeadline(Double.valueOf(deadlineSecs));
  }
  HTTPResponse fetchRes = service.fetch(fetchReq);
  for (HTTPHeader header : fetchRes.getHeaders()) {
    res.addHeader(header.getName(), header.getValue());
  }
  if (fetchRes.getResponseCode() == 200) {
    res.getOutputStream().write(fetchRes.getContent());
  } else {
    res.sendError(fetchRes.getResponseCode(), "Error while fetching");
  }
}
 
Example 2
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public RawGcsCreationToken beginObjectCreation(
    GcsFilename filename, GcsFileOptions options, long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(filename, null, POST, timeoutMillis);
  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);
    Preconditions.checkState(params.containsKey(UPLOAD_ID),
        LOCATION + " header," + location + ", has a query string without " + UPLOAD_ID);
    return new GcsRestCreationToken(filename, params.get(UPLOAD_ID), 0);
  } else {
    throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
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: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
/**
 * Given a HTTPResponce, process it, throwing an error if needed and return a Token for the next
 * request.
 */
GcsRestCreationToken handlePutResponse(final GcsRestCreationToken token,
    final boolean isFinalChunk,
    final int length,
    final HTTPRequestInfo reqInfo,
    HTTPResponse resp) throws Error, IOException {
  switch (resp.getResponseCode()) {
    case 200:
      if (!isFinalChunk) {
        throw new RuntimeException("Unexpected response code 200 on non-final chunk. Request: \n"
            + URLFetchUtils.describeRequestAndResponse(reqInfo, resp));
      } else {
        return null;
      }
    case 308:
      if (isFinalChunk) {
        throw new RuntimeException("Unexpected response code 308 on final chunk: "
            + URLFetchUtils.describeRequestAndResponse(reqInfo, resp));
      } else {
        return new GcsRestCreationToken(token.filename, token.uploadId, token.offset + length);
      }
    default:
      throw HttpErrorHandler.error(resp.getResponseCode(),
          URLFetchUtils.describeRequestAndResponse(reqInfo, resp));
  }
}
 
Example 5
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
/** True if deleted, false if not found. */
@Override
public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(filename, null, DELETE, timeoutMillis);
  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:
      throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
Example 6
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public GcsFileMetadata getObjectMetadata(GcsFilename filename, long timeoutMillis)
    throws IOException {
  HTTPRequest req = makeRequest(filename, null, HEAD, timeoutMillis);
  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);
  }
  return getMetadataFromResponse(
      filename, resp, getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH));
}
 
Example 7
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 8
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public void copyObject(GcsFilename source, GcsFilename dest, GcsFileOptions fileOptions,
    long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(dest, null, PUT, timeoutMillis);
  req.setHeader(new HTTPHeader(X_GOOG_COPY_SOURCE, makePath(source)));
  if (fileOptions != null) {
    req.setHeader(REPLACE_METADATA_HEADER);
    addOptionsHeaders(req, fileOptions);
  }
  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);
  }
}
 
Example 9
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 10
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 11
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 12
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 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: 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 15
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 16
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 17
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 18
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 19
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);
    }
  };
}