private static String extractResponseString(URLConnection connection) throws IOException { /* Handle input. */ HttpURLConnection http = (HttpURLConnection) connection; int status = http.getResponseCode(); String charset = getCharset(connection.getHeaderField("Content-Type")); if (status == 200) { return readResponseBody(http, charset); } else { return readErrorResponseBody(http, status, charset); } }
/** * Pings the Consul Agent. */ public void pingAgent() { HTTP.Response response = HTTP.getResponse(createURI("/self").toString()); if (response.status() != 200) { die("Error pinging Consul", response.payloadAsString()); } }
/** * Register a service with Consul. * * @param registration The registration payload. */ public void register(final Registration registration) { final URI uri = createURI("/service/register"); HTTP.Response response = HTTP.jsonRestCallViaPUT(uri.toString(), toJson(registration)); if (response.status() != 200) { die("Error registering service with Consul", uri, registration, response.payloadAsString()); } }
private static byte[] extractResponseBytes(URLConnection connection) throws IOException { /* Handle input. */ HttpURLConnection http = (HttpURLConnection) connection; int status = http.getResponseCode(); //System.out.println("CONTENT-TYPE" + connection.getHeaderField("Content-TypeT")); if (status == 200) { return readResponseBodyAsBytes(http); } else { String charset = getCharset(connection.getHeaderField("Content-Type")); readErrorResponseBody(http, status, charset); return null; } }
private static URLConnection doGet(String url, Map<String, ?> headers, String contentType, String charset, boolean binary) throws IOException { URLConnection connection;/* Handle output. */ connection = new URL(url).openConnection(); connection.setConnectTimeout(DEFAULT_TIMEOUT_SECONDS * 1000); manageContentTypeHeaders(contentType, charset, connection, binary); manageHeaders(headers, connection); return connection; }
private static Response extractResponseObject(URLConnection connection) throws IOException { /* Handle input. */ HttpURLConnection http = (HttpURLConnection) connection; int status = http.getResponseCode(); String charset = getCharset(connection.getHeaderField("Content-Type")); String body; if (status == 200) { body = readResponseBody(http, charset); } else { body = readErrorResponseBodyDoNotDie(http, status, charset); } return Response.response(status, http.getHeaderFields(), http.getResponseMessage(), body); }
public static String postText( final String url, final String body) { return postBodyTextWithContentType(url, "text/plain", body); }
public static Response jsonRestCallViaPOST( final String url, final String jsonString) { return postBodyTextWithContentTypeReturnResponse(url, APPLICATION_JSON, jsonString); }
public static String putJSON( final String url, final String jsonString) { return putBodyTextWithContentType(url, APPLICATION_JSON, jsonString); }
public static Response jsonRestCallViaPUT( final String url, final String jsonString) { return putBodyTextWithContentTypeReturnResponse(url, APPLICATION_JSON, jsonString); }
private static URLConnection doGet(String url, Map<String, ?> headers, String contentType, String charset) throws IOException { URLConnection connection;/* Handle output. */ connection = new URL(url).openConnection(); connection.setConnectTimeout(DEFAULT_TIMEOUT_SECONDS * 1000); manageContentTypeHeaders(contentType, charset, connection); manageHeaders(headers, connection); return connection; }
private static Response extractResponseObject( URLConnection connection ) throws IOException { /* Handle input. */ HttpURLConnection http = ( HttpURLConnection ) connection; int status = http.getResponseCode(); String charset = getCharset( connection.getHeaderField( "Content-Type" ) ); String body; if ( status == 200 ) { body = readResponseBody( http, charset ); } else { body = readErrorResponseBodyDoNotDie( http, status, charset ); } return Response.response(status, http.getHeaderFields(), http.getResponseMessage(), body); }
private static byte[] extractResponseBytes(URLConnection connection) throws IOException { /* Handle input. */ HttpURLConnection http = (HttpURLConnection) connection; int status = http.getResponseCode(); //System.out.println("CONTENT-TYPE" + connection.getHeaderField("Content-TypeT")); if (status == 200) { return readResponseBodyAsBytes(http); } else { String charset = getCharset(connection.getHeaderField("Content-Type")); readErrorResponseBody(http, status, charset); return null; } }
public static String postXML( final String url, final String jsonString) { return postBodyTextWithContentType(url, "text/xml", jsonString); }
public static Response jsonRestCallViaPOST( final String url, final String jsonString ) { return postBodyTextWithContentTypeReturnResponse(url, APPLICATION_JSON, jsonString); }
public static String putJSON( final String url, final String jsonString) { return putBodyTextWithContentType(url, APPLICATION_JSON, jsonString); }
public static Response jsonRestCallViaPUT( final String url, final String jsonString) { return putBodyTextWithContentTypeReturnResponse(url, APPLICATION_JSON, jsonString); }
private static URLConnection doDelete(String url, Map<String, ?> headers, String contentType, String charset) throws IOException { HttpURLConnection connection;/* Handle output. */ connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(DEFAULT_TIMEOUT_SECONDS * 1000); connection.setRequestMethod("DELETE"); manageContentTypeHeaders(contentType, charset, connection); manageHeaders(headers, connection); return connection; }
/** * Checks in with Consul. * * @param checkId The Check ID to check in. * @param status The current state of the Check. * @param note Any note to associate with the Check. */ public void check(String checkId, Status status, String note) { final URI uri = createURI("/check/" + status.getUri() + "/" + checkId); final HTTP.Response httpResponse = Str.isEmpty(note) ? HTTP.getResponse(uri.toString()) : HTTP.getResponse(uri.toString() + "?note=" + note); if (httpResponse.code() != 200) { notRegistered("Unable to perform check", uri, httpResponse.code(), httpResponse.statusMessageAsString(), httpResponse.body()); } }
private static String extractResponseString(URLConnection connection) throws IOException { /* Handle input. */ HttpURLConnection http = (HttpURLConnection) connection; int status = http.getResponseCode(); String charset = getCharset(connection.getHeaderField("Content-Type")); if (status == 200) { return readResponseBody(http, charset); } else { return readErrorResponseBody(http, status, charset); } }