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(); }
/** * 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); }
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; }
new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(404, exception.getCode()); assertFalse(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(503, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(502, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(500, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(429, null, new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(429, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(408, null, new HttpHeaders()).build(); exception = new StorageException(httpResponseException); assertEquals(408, exception.getCode());
new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build(); exception = new BigQueryException(httpResponseException); assertEquals(404, exception.getCode()); assertFalse(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(504, null, new HttpHeaders()).build(); exception = new BigQueryException(httpResponseException); assertEquals(504, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build(); exception = new BigQueryException(httpResponseException); assertEquals(503, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build(); exception = new BigQueryException(httpResponseException); assertEquals(502, exception.getCode()); assertTrue(exception.isRetryable()); httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build(); exception = new BigQueryException(httpResponseException); assertEquals(500, exception.getCode());
final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("x-goog-encryption-algorithm", "AES256"); httpHeaders.set("x-goog-encryption-key", base64CseKey);
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH")); HttpResponse response = request.execute();
final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("x-goog-encryption-algorithm", "AES256"); httpHeaders.set("x-goog-encryption-key", base64CseKey);
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH")); HttpResponse response = request.execute();
final HttpHeaders httpHeaders = new HttpHeaders();
@Override protected HttpHeaders getHttpHeaders() { return new HttpHeaders().setAccept(ACCEPT_HEADER); } }
@Override protected HttpHeaders getHttpHeaders() { return new HttpHeaders().setAccept(ACCEPT_HEADER).setBasicAuthentication(apiKey, ""); } }
private HttpHeaders createHttpHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType("application/xml"); headers.setUserAgent(session.getUserAgent()); return headers; }
private HttpHeaders createHttpHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType("application/xml"); headers.setUserAgent(session.getUserAgent()); return headers; }
void throwNotFound(String format, Object... args) throws IOException { throw new IOException( String.format(format, args), new GoogleJsonResponseException.Builder(404, String.format(format, args), new HttpHeaders()) .build()); } }
@Override public void initialize(HttpRequest httpRequest) throws IOException { HttpHeaders headers = new HttpHeaders(); httpRequest.setConnectTimeout(timeout); httpRequest.setReadTimeout(timeout); httpRequest.setHeaders(headers); credential.initialize(httpRequest); } };
@Override public void initialize(HttpRequest hr) throws IOException { hr.setParser(new XmlObjectParser(DefaultConfigs.DOUBAN_XML_NAMESPACE)); HttpHeaders header = new HttpHeaders(); header.setUserAgent("Dongxuexidu - Douban Java SDK"); hr.setHeaders(header); hr.setNumberOfRetries(3); } });