/** * @param headerName the header name * @return a list of headers in the response */ public List<String> getHeader(String headerName) { return httpResponse.getHeaders().getHeaderStringValues(headerName); }
/** @return the original request URL */ public GenericUrl getRequestUrl() { return httpResponse.getRequest().getUrl(); } }
public static boolean isRetryable(Throwable t) { if (t instanceof HttpResponseException) { final HttpResponseException e = (HttpResponseException) t; return e.getStatusCode() == 429 || (e.getStatusCode() / 500 == 1); } return t instanceof IOException; } }
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 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); } }
/** * 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(); }
private static void validateResponse( HttpRequest request, HttpResponse response, int expectedCode) throws IOException { if (response.getStatusCode() != expectedCode) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage() + "\nfrom:\n" + request.getUrl() + "\nHeaders:\n" + response.getHeaders()); } }
/** @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(); }
@Override public void initialize(HttpRequest httpRequest) throws IOException { delegate.initialize(httpRequest); httpRequest.getHeaders().setUserAgent(applicationName); } };
/** @return the HTTP status code of the response */ public int getStatusCode() { return httpResponse.getStatusCode(); }
public SolidUtilities(String authCookie) { this.authCookie = authCookie; this.factory = TRANSPORT.createRequestFactory(); }
/** * @return the HTTP response body as a {@link Blob}. * @throws IOException if getting the HTTP response content fails. */ public Blob getBody() throws IOException { return Blobs.from(httpResponse.getContent()); }
/** * @return the first {@code Content-Length} header, or {@code -1} if not found * @throws NumberFormatException if parsing the content length header fails */ public long getContentLength() throws NumberFormatException { String contentLengthHeader = httpResponse.getHeaders().getFirstHeaderStringValue(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader == null) { return -1; } try { return Long.parseLong(contentLengthHeader); } catch (NumberFormatException ex) { return -1; } }
@Override public void put(HttpHeaders carrier, String key, String value) { carrier.set(key, value); } }
@Override public void close() throws IOException { if (httpResponse == null) { return; } httpResponse.disconnect(); }
/** * Sets the {@code User-Agent} header. * * @param userAgent the user agent * @return this */ public Builder setUserAgent(String userAgent) { headers.setUserAgent(userAgent); return this; }
/** * Sets the {@code Accept} header. * * @param mimeTypes the items to pass into the accept header * @return this */ public Builder setAccept(List<String> mimeTypes) { headers.setAccept(String.join(",", mimeTypes)); return this; }
//Send Http PUT request to: "http://some.url" with request header: String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send String url="http://some.url";//URL address where we need to send it HttpRequest req=new HttpRequest(url);//HttpRequest to url: "http://some.url" req.withHeaders("Content-Type: application/json");//add request header: "Content-Type: application/json" req.prepare(HttpRequest.Method.PUT);//Set HttpRequest method as PUT req.withData(json);//Add json data to request body JSONObject res=req.sendAndReadJSON();//Accept response as JSONObject
public void close() throws IOException { httpTransport.shutdown(); }
//Equivalent to previous example, but in a shorter way (using methods chaining): String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send String url="http://some.url";//URL address where we need to send it //Shortcut for example 5 complex request sending & reading response in one (chained) line JSONObject res=new HttpRequest(url).withHeaders("Content-Type: application/json").prepare(HttpRequest.Method.PUT).withData(json).sendAndReadJSON();