/** * Make sure to wrap with a try-with-resource to ensure that the connection is closed after usage. * * @param url the url to send the request to */ @VisibleForTesting Connection(URL url, HttpTransport transport) { this.url = new GenericUrl(url); requestFactory = transport.createRequestFactory(); }
/** @return {@code location} with query parameter 'digest' set to the BLOB's digest */ @Override public URL getApiRoute(String apiRouteBase) { return new GenericUrl(location).set("digest", blobDigest).toURL(); }
static <T> T makePostRequest(HttpTransport httpTransport, String url, HttpContent httpContent, Class<T> clazz) throws IOException { HttpRequestFactory factory = httpTransport.createRequestFactory(); HttpRequest postRequest = factory.buildPostRequest(new GenericUrl(url), httpContent); HttpResponse response = postRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return new ObjectMapper().readValue(result, clazz); } }
@Nullable private T fallBackToHttp(URL url) throws IOException, RegistryException { GenericUrl httpUrl = new GenericUrl(url); httpUrl.setScheme("http"); eventDispatcher.dispatch( LogEvent.info( "Failed to connect to " + url + " over HTTPS. Attempting again with HTTP: " + httpUrl)); return call(httpUrl.toURL(), connectionFactory); }
private <T> T makeGetRequest(String url, Optional<Map<String, String>> parameters, Class<T> clazz) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest getRequest = requestFactory .buildGetRequest(new GenericUrl(url + "?" + generateParamsString(parameters))); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return objectMapper.readValue(result, clazz); }
private String getToken(String frob) throws IOException { URL signedUrl = signatureGenerator.getSignature( GET_TOKEN_URL, ImmutableMap.of("frob", frob, "method", GET_TOKEN_METHOD)); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedUrl)); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } AuthElement authElement = xmlMapper.readValue(response.getContent(), AuthElement.class); Preconditions.checkState(authElement.stat.equals("ok"), "state must be ok: %s", authElement); Preconditions.checkState( !Strings.isNullOrEmpty(authElement.auth.token), "token must not be empty", authElement); return authElement.auth.token; } }
private <T> T makeRequest(String url, Class<T> clazz, TokensAndUrlAuthData authData) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest( new GenericUrl(url + "?access_token=" + authData.getAccessToken())); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return objectMapper.readValue(result, clazz); } }
<T> T makePostRequest(String url, Optional<Map<String, String>> parameters, HttpContent httpContent, Class<T> clazz) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest postRequest = requestFactory .buildPostRequest(new GenericUrl(url + "?" + generateParamsString(parameters)), httpContent); HttpResponse response = postRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); if (clazz.isAssignableFrom(String.class)) { return (T) result; } else { return objectMapper.readValue(result, clazz); } }
private GenericUrl buildTargetUrl(String path) { GenericUrl genericUrl = new GenericUrl(translate.getBaseUrl() + "v2/" + path); if (options.getApiKey() != null) { genericUrl.put("key", options.getApiKey()); } return genericUrl; }
private void delete(String url) { HttpHeaders headers = new HttpHeaders(); headers.setAccept("text/turtle"); headers.setCookie(authCookie); try { HttpRequest deleteRequest = factory.buildDeleteRequest(new GenericUrl(url)) .setThrowExceptionOnExecuteError(false); deleteRequest.setHeaders(headers); validateResponse(deleteRequest.execute(), 200); logger.debug("Deleted: {}", url); } catch (IOException e) { throw new IllegalStateException("Couldn't delete: " + url, e); } }
private String requestRaw(String path) throws IOException { HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest( new GenericUrl(baseUrl + path)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); getRequest.setHeaders(headers); HttpResponse response = getRequest.execute(); validateResponse(getRequest, response, 200); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(response.getContent(), byteArrayOutputStream, true); return byteArrayOutputStream.toString(); }
private <T extends RememberTheMilkResponse> T makeRequest( Map<String, String> parameters, Class<T> dataClass) throws IOException { URL signedUrl = signatureGenerator.getSignature(BASE_URL, parameters); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedUrl)); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } T parsedResponse = xmlMapper.readValue(response.getContent(), dataClass); if (parsedResponse.error != null) { throw new IOException( "Error making call to " + signedUrl + " error: " + parsedResponse.error); } return parsedResponse; }
private String makeCall(HttpTransport transport) throws IOException { HttpRequest get = transport.createRequestFactory() .buildPostRequest(new GenericUrl(INRPUT_LOGIN_SERVER), null) .setFollowRedirects(false) .setThrowExceptionOnExecuteError(false); HttpResponse response = get.execute(); if (response.getStatusCode() != 302) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage()); } String cookieValue = response.getHeaders().getFirstHeaderStringValue("set-cookie"); if (Strings.isNullOrEmpty(cookieValue)) { throw new IOException("Couldn't extract cookie value from headers: " + response.getHeaders()); } return cookieValue; } }
/** * Parses the contents of a URL to produce an RDF model. */ public Model getModel(String url) throws IOException { HttpRequestFactory factory = TRANSPORT.createRequestFactory(); HttpRequest rootGetRequest = factory.buildGetRequest( new GenericUrl(url)); HttpHeaders headers = new HttpHeaders(); headers.setCookie(authCookie); headers.setAccept("text/turtle"); rootGetRequest.setHeaders(headers); HttpResponse response = rootGetRequest.execute(); if (response.getStatusCode() != 200) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage()); } StringWriter writer = new StringWriter(); IOUtils.copy(response.getContent(), writer, "UTF-8"); String fixedString = fixProblematicPeriods(writer.toString()); Model defaultModel = ModelFactory.createDefaultModel(); return defaultModel.read( new StringReader(fixedString), url, "TURTLE"); }
/** Posts an RDF model to a Solid server. **/ public String postContent( String url, String slug, String type, Model model) throws IOException { StringWriter stringWriter = new StringWriter(); model.write(stringWriter, "TURTLE"); HttpContent content = new ByteArrayContent("text/turtle", stringWriter.toString().getBytes()); HttpRequest postRequest = factory.buildPostRequest( new GenericUrl(url), content); HttpHeaders headers = new HttpHeaders(); headers.setCookie(authCookie); headers.set("Link", "<" + type + ">; rel=\"type\""); headers.set("Slug", slug); postRequest.setHeaders(headers); HttpResponse response = postRequest.execute(); validateResponse(response, 201); return response.getHeaders().getLocation(); }
/** Posts a new status for the user, initially marked as private.**/ public void postStatus(String content, String idempotencyKey) throws IOException { ImmutableMap<String, String> formParams = ImmutableMap.of( "status", content, // Default everything to private to avoid a privacy incident "visibility", "private" ); UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formParams); HttpRequest postRequest = TRANSPORT.createRequestFactory() .buildPostRequest( new GenericUrl(baseUrl + POST_URL), urlEncodedContent) .setThrowExceptionOnExecuteError(false); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); if (!Strings.isNullOrEmpty(idempotencyKey)) { // This prevents the same post from being posted twice in the case of network errors headers.set("Idempotency-Key", idempotencyKey); } postRequest.setHeaders(headers); HttpResponse response = postRequest.execute(); validateResponse(postRequest, response, 200); }
@Override public void submit() { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_BATCH_SUBMIT); Scope scope = tracer.withSpan(span); try { span.putAttribute("batch size", AttributeValue.longAttributeValue(batches.size())); for (BatchRequest batch : batches) { // TODO(hailongwen@): instrument 'google-api-java-client' to further break down the span. // Here we only add a annotation to at least know how much time each batch takes. span.addAnnotation("Execute batch request"); batch.setBatchUrl( new GenericUrl(String.format("%s/batch/storage/v1", options.getHost()))); batch.execute(); } } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } } }
@Before public void setUp() throws IOException { httpRequest = new NetHttpTransport() .createRequestFactory() .buildRequest("GET", new GenericUrl("https://www.google.com"), null); }
private static String getAppEngineProjectIdFromMetadataServer() throws IOException { String metadata = "http://metadata.google.internal"; String projectIdURL = "/computeMetadata/v1/project/project-id"; GenericUrl url = new GenericUrl(metadata + projectIdURL); HttpTransport netHttpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory(); HttpRequest request = requestFactory .buildGetRequest(url) .setConnectTimeout(500) .setReadTimeout(500) .setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google")); HttpResponse response = request.execute(); return headerContainsMetadataFlavor(response) ? response.parseAsString() : null; }
@Override public String open(Job loadJob) { try { String builder = BASE_RESUMABLE_URI + options.getProjectId() + "/jobs"; GenericUrl url = new GenericUrl(builder); url.set("uploadType", "resumable"); JsonFactory jsonFactory = bigquery.getJsonFactory(); HttpRequestFactory requestFactory = bigquery.getRequestFactory(); HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob)); httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream"); HttpResponse response = httpRequest.execute(); return response.getHeaders().getLocation(); } catch (IOException ex) { throw translate(ex); } }