Java Code Examples for com.google.appengine.api.urlfetch.HTTPResponse

The following examples show how to use com.google.appengine.api.urlfetch.HTTPResponse. These examples are extracted from open source projects. 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 Project: google-http-java-client   Author: googleapis   File: UrlFetchRequest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public LowLevelHttpResponse execute() throws IOException {
  // write content
  if (getStreamingContent() != null) {
    String contentType = getContentType();
    if (contentType != null) {
      addHeader("Content-Type", contentType);
    }
    String contentEncoding = getContentEncoding();
    if (contentEncoding != null) {
      addHeader("Content-Encoding", contentEncoding);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    getStreamingContent().writeTo(out);
    byte[] payload = out.toByteArray();
    if (payload.length != 0) {
      request.setPayload(payload);
    }
  }
  // connect
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  HTTPResponse response = service.fetch(request);
  return new UrlFetchResponse(response);
}
 
Example #2
Source Project: google-http-java-client   Author: googleapis   File: UrlFetchResponse.java    License: Apache License 2.0 6 votes vote down vote up
UrlFetchResponse(HTTPResponse fetchResponse) {
  this.fetchResponse = fetchResponse;
  for (HTTPHeader header : fetchResponse.getHeadersUncombined()) {
    String name = header.getName();
    String value = header.getValue();
    if (name != null && value != null) {
      headerNames.add(name);
      headerValues.add(value);
      if ("content-type".equalsIgnoreCase(name)) {
        contentType = value;
      } else if ("content-encoding".equalsIgnoreCase(name)) {
        contentEncoding = value;
      } else if ("content-length".equalsIgnoreCase(name)) {
        try {
          contentLength = Long.parseLong(value);
        } catch (NumberFormatException e) {
          // ignore
        }
      }
    }
  }
}
 
Example #3
Source Project: appengine-java-vm-runtime   Author: GoogleCloudPlatform   File: CurlServlet.java    License: 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 #4
Source Project: google-maps-services-java   Author: googlemaps   File: GaePendingResult.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public T await() throws ApiException, IOException, InterruptedException {
  try {
    HTTPResponse result = call.get();
    metrics.endNetwork();
    return parseResponse(this, result);
  } catch (ExecutionException e) {
    if (e.getCause() instanceof IOException) {
      throw (IOException) e.getCause();
    } else {
      // According to
      // https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/urlfetch/URLFetchService
      // all exceptions should be subclass of IOException so this should not happen.
      throw new UnknownErrorException("Unexpected exception from " + e.getMessage());
    }
  }
}
 
Example #5
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #6
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #7
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Same as {@link #put} but is runs asynchronously and returns a future. In the event of an error
 * the exception out of the future will be an ExecutionException with the cause set to the same
 * exception that would have been thrown by put.
 */
private Future<RawGcsCreationToken> putAsync(final GcsRestCreationToken token,
    ByteBuffer chunk, final boolean isFinalChunk, long timeoutMillis) {
  final int length = chunk.remaining();
  HTTPRequest request = createPutRequest(token, chunk, isFinalChunk, timeoutMillis, length);
  final HTTPRequestInfo info = new HTTPRequestInfo(request);
  return new FutureWrapper<HTTPResponse, RawGcsCreationToken>(urlfetch.fetchAsync(request)) {
    @Override
    protected Throwable convertException(Throwable e) {
      return OauthRawGcsService.convertException(info, e);
    }

    @Override
    protected GcsRestCreationToken wrap(HTTPResponse resp) throws Exception {
      return handlePutResponse(token, isFinalChunk, length, info, resp);
    }
  };
}
 
Example #8
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #9
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #10
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #11
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: OauthRawGcsService.java    License: 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 #12
Source Project: appengine-gcs-client   Author: GoogleCloudPlatform   File: URLFetchUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #13
Source Project: appengine-tck   Author: GoogleCloudPlatform   File: URLFetchTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncOps() throws Exception {
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    URL adminConsole = findAvailableUrl(URLS);
    Future<HTTPResponse> response = service.fetchAsync(adminConsole);
    printResponse(response.get(5, TimeUnit.SECONDS));

    response = service.fetchAsync(new HTTPRequest(adminConsole));
    printResponse(response.get(5, TimeUnit.SECONDS));

    URL jbossOrg = new URL("http://www.jboss.org");
    if (available(jbossOrg)) {
        response = service.fetchAsync(jbossOrg);
        printResponse(response.get(30, TimeUnit.SECONDS));
    }

    sync(5000L); // wait a bit for async to finish
}
 
Example #14
Source Project: flickr-uploader   Author: rafali   File: HttpClientGAE.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static String getResponseDELETE(String url, Map<String, String> params, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			logger.debug("DELETE : " + urlStr);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.DELETE, FetchOptions.Builder.withDeadline(deadline));
			HTTPResponse response = fetcher.fetch(httpRequest);
			return processResponse(response);
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
Example #15
Source Project: flickr-uploader   Author: rafali   File: HttpClientGAE.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static String getResponseProxyPOST(URL url) throws MalformedURLException, UnsupportedEncodingException, IOException {
	URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
	String base64payload = "base64url=" + Base64UrlSafe.encodeServer(url.toString());
	String urlStr = url.toString();
	if (urlStr.contains("?")) {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr.substring(0, urlStr.indexOf("?")));
		base64payload += "&base64content=" + Base64UrlSafe.encodeServer(urlStr.substring(urlStr.indexOf("?") + 1));
	} else {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr);
	}
	HTTPRequest httpRequest = new HTTPRequest(new URL(HttpClientGAE.POSTPROXY_PHP), HTTPMethod.POST, FetchOptions.Builder.withDeadline(30d).doNotValidateCertificate());
	httpRequest.setPayload(base64payload.getBytes(UTF8));
	HTTPResponse response = fetcher.fetch(httpRequest);
	String processResponse = HttpClientGAE.processResponse(response);
	logger.info("proxying " + url + "\nprocessResponse:" + processResponse);
	return processResponse;
}
 
Example #16
Source Project: solutions-google-compute-engine-orchestrator   Author: GoogleCloudPlatform   File: GceInstanceCreator.java    License: 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 #17
Source Project: solutions-google-compute-engine-orchestrator   Author: GoogleCloudPlatform   File: GceInstanceCreator.java    License: 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 #18
Source Project: solutions-google-compute-engine-orchestrator   Author: GoogleCloudPlatform   File: GceApiUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates an HTTPRequest with the information passed in.
 *
 * @param accessToken the access token necessary to authorize the request.
 * @param url the url to query.
 * @param payload the payload for the request.
 * @return the created HTTP request.
 * @throws IOException
 */
public static HTTPResponse makeHttpRequest(
    String accessToken, final String url, String payload, HTTPMethod method) throws IOException {

  // Create HTTPRequest and set headers
  HTTPRequest httpRequest = new HTTPRequest(new URL(url.toString()), method);
  httpRequest.addHeader(new HTTPHeader("Authorization", "OAuth " + accessToken));
  httpRequest.addHeader(new HTTPHeader("Host", "www.googleapis.com"));
  httpRequest.addHeader(new HTTPHeader("Content-Length", Integer.toString(payload.length())));
  httpRequest.addHeader(new HTTPHeader("Content-Type", "application/json"));
  httpRequest.addHeader(new HTTPHeader("User-Agent", "google-api-java-client/1.0"));
  httpRequest.setPayload(payload.getBytes());

  URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  HTTPResponse httpResponse = fetcher.fetch(httpRequest);
  return httpResponse;
}
 
Example #19
Source Project: solutions-google-compute-engine-orchestrator   Author: GoogleCloudPlatform   File: GceInstanceDestroyer.java    License: 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 #20
Source Project: nomulus   Author: google   File: NordnUploadAction.java    License: 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 #21
Source Project: nomulus   Author: google   File: Marksdb.java    License: 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 #22
Source Project: nomulus   Author: google   File: RdeReporter.java    License: Apache License 2.0 5 votes vote down vote up
/** Uploads {@code reportBytes} to ICANN. */
public void send(byte[] reportBytes) throws XmlException {
  XjcRdeReportReport report = XjcXmlTransformer.unmarshal(
      XjcRdeReportReport.class, new ByteArrayInputStream(reportBytes));
  XjcRdeHeader header = report.getHeader().getValue();

  // Send a PUT request to ICANN's HTTPS server.
  URL url = makeReportUrl(header.getTld(), report.getId());
  String username = header.getTld() + "_ry";
  String token = base64().encode(String.format("%s:%s", username, password).getBytes(UTF_8));
  final HTTPRequest req = new HTTPRequest(url, PUT, validateCertificate().setDeadline(60d));
  req.addHeader(new HTTPHeader(CONTENT_TYPE, REPORT_MIME));
  req.addHeader(new HTTPHeader(AUTHORIZATION, "Basic " + token));
  req.setPayload(reportBytes);
  logger.atInfo().log("Sending report:\n%s", new String(reportBytes, UTF_8));
  HTTPResponse rsp =
      retrier.callWithRetry(
          () -> {
            HTTPResponse rsp1 = urlFetchService.fetch(req);
            switch (rsp1.getResponseCode()) {
              case SC_OK:
              case SC_BAD_REQUEST:
                break;
              default:
                throw new UrlFetchException("PUT failed", req, rsp1);
            }
            return rsp1;
          },
          SocketTimeoutException.class);

  // Ensure the XML response is valid.
  XjcIirdeaResult result = parseResult(rsp);
  if (result.getCode().getValue() != 1000) {
    logger.atWarning().log(
        "PUT rejected: %d %s\n%s",
        result.getCode().getValue(), result.getMsg(), result.getDescription());
    throw new InternalServerErrorException(result.getMsg());
  }
}
 
Example #23
Source Project: nomulus   Author: google   File: RdeReporter.java    License: 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 #24
Source Project: nomulus   Author: google   File: FakeURLFetchService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HTTPResponse fetch(HTTPRequest request) {
  URL requestURL = request.getURL();
  if (backingMap.containsKey(requestURL)) {
    return backingMap.get(requestURL);
  } else {
    return new HTTPResponse(HttpURLConnection.HTTP_NOT_FOUND, null, null, ImmutableList.of());
  }
}
 
Example #25
Source Project: nomulus   Author: google   File: ForwardingURLFetchService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Future<HTTPResponse> fetchAsync(HTTPRequest request) {
  try {
    return Futures.immediateFuture(fetch(request));
  } catch (Exception e) {
    return Futures.immediateFailedFuture(e);
  }
}
 
Example #26
Source Project: openid4java   Author: jbufu   File: AppEngineHttpFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private HttpResponse fetch(String url, HttpRequestOptions requestOptions,
    HTTPMethod method, String content) throws IOException {

  final FetchOptions options = getFetchOptions(requestOptions);

  String currentUrl = url;

  for (int i = 0; i <= requestOptions.getMaxRedirects(); i++) {

    HTTPRequest httpRequest = new HTTPRequest(new URL(currentUrl),
        method, options);

    addHeaders(httpRequest, requestOptions);

    if (method == HTTPMethod.POST && content != null) {
      httpRequest.setPayload(content.getBytes());
    }

    HTTPResponse httpResponse;
    try {
      httpResponse = fetchService.fetch(httpRequest);
    } catch (ResponseTooLargeException e) {
      return new TooLargeResponse(currentUrl);
    }

    if (!isRedirect(httpResponse.getResponseCode())) {
      boolean isResponseTooLarge =
        (getContentLength(httpResponse) > requestOptions.getMaxBodySize());
      return new AppEngineFetchResponse(httpResponse,
          isResponseTooLarge, currentUrl);
    } else {
      currentUrl = getResponseHeader(httpResponse, "Location").getValue();
    }
  }
  throw new IOException("exceeded maximum number of redirects");
}
 
Example #27
Source Project: openid4java   Author: jbufu   File: AppEngineHttpFetcher.java    License: 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 #28
Source Project: openid4java   Author: jbufu   File: AppEngineHttpFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private static Header getResponseHeader(HTTPResponse httpResponse, String headerName) {
  Header[] allHeaders = getResponseHeaders(httpResponse, headerName);
  if (allHeaders.length == 0) {
    return null;
  } else {
    return allHeaders[0];
  }
}
 
Example #29
Source Project: openid4java   Author: jbufu   File: AppEngineHttpFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private static Header[] getResponseHeaders(HTTPResponse httpResponse, String headerName) {
  List<HTTPHeader> allHeaders = httpResponse.getHeaders();
  List<Header> matchingHeaders = new ArrayList<Header>();
  for (HTTPHeader header : allHeaders) {
    if (header.getName().equalsIgnoreCase(headerName)) {
      matchingHeaders.add(new BasicHeader(header.getName(), header.getValue()));
    }
  }
  return matchingHeaders.toArray(new Header[matchingHeaders.size()]);
}
 
Example #30
Source Project: openid4java   Author: jbufu   File: AppEngineHttpFetcher.java    License: Apache License 2.0 5 votes vote down vote up
public AppEngineFetchResponse(
    com.google.appengine.api.urlfetch.HTTPResponse httpResponse,
    boolean bodySizeExceeded,
    String finalUri) {
  this.httpResponse = httpResponse;
  this.bodySizeExceeded = bodySizeExceeded;
  this.finalUri = finalUri;
}