List of usage examples for java.net HttpURLConnection getResponseMessage
public String getResponseMessage() throws IOException
From source file:org.apache.tez.runtime.library.common.shuffle.impl.Fetcher.java
/** * The crux of the matter.../* ww w. j a va2 s . c o m*/ * * @param host {@link MapHost} from which we need to * shuffle available map-outputs. */ @VisibleForTesting protected void copyFromHost(MapHost host) throws IOException { // Get completed maps on 'host' List<InputAttemptIdentifier> srcAttempts = scheduler.getMapsForHost(host); // Sanity check to catch hosts with only 'OBSOLETE' maps, // especially at the tail of large jobs if (srcAttempts.size() == 0) { return; } if (LOG.isDebugEnabled()) { LOG.debug("Fetcher " + id + " going to fetch from " + host + " for: " + srcAttempts); } // List of maps to be fetched yet remaining = new LinkedHashSet<InputAttemptIdentifier>(srcAttempts); // Construct the url and connect DataInputStream input; boolean connectSucceeded = false; try { URL url = getMapOutputURL(host, srcAttempts); HttpURLConnection connection = openConnection(url); // generate hash of the url String msgToEncode = SecureShuffleUtils.buildMsgFrom(url); String encHash = SecureShuffleUtils.hashFromString(msgToEncode, jobTokenSecret); // put url hash into http header connection.addRequestProperty(SecureShuffleUtils.HTTP_HEADER_URL_HASH, encHash); // set the read timeout connection.setReadTimeout(readTimeout); // put shuffle version into http header connection.addRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); connection.addRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); connect(connection, connectionTimeout); connectSucceeded = true; input = new DataInputStream(connection.getInputStream()); // Validate response code int rc = connection.getResponseCode(); if (rc != HttpURLConnection.HTTP_OK) { throw new IOException("Got invalid response code " + rc + " from " + url + ": " + connection.getResponseMessage()); } // get the shuffle version if (!ShuffleHeader.DEFAULT_HTTP_HEADER_NAME .equals(connection.getHeaderField(ShuffleHeader.HTTP_HEADER_NAME)) || !ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION .equals(connection.getHeaderField(ShuffleHeader.HTTP_HEADER_VERSION))) { throw new IOException("Incompatible shuffle response version"); } // get the replyHash which is HMac of the encHash we sent to the server String replyHash = connection.getHeaderField(SecureShuffleUtils.HTTP_HEADER_REPLY_URL_HASH); if (replyHash == null) { throw new IOException("security validation of TT Map output failed"); } LOG.debug("url=" + msgToEncode + ";encHash=" + encHash + ";replyHash=" + replyHash); // verify that replyHash is HMac of encHash SecureShuffleUtils.verifyReply(replyHash, encHash, jobTokenSecret); LOG.info("for url=" + msgToEncode + " sent hash and receievd reply"); } catch (IOException ie) { ioErrs.increment(1); LOG.warn("Failed to connect to " + host + " with " + remaining.size() + " map outputs", ie); // If connect did not succeed, just mark all the maps as failed, // indirectly penalizing the host if (!connectSucceeded) { for (InputAttemptIdentifier left : remaining) { scheduler.copyFailed(left, host, connectSucceeded); } } else { // If we got a read error at this stage, it implies there was a problem // with the first map, typically lost map. So, penalize only that map // and add the rest InputAttemptIdentifier firstMap = srcAttempts.get(0); scheduler.copyFailed(firstMap, host, connectSucceeded); } // Add back all the remaining maps, WITHOUT marking them as failed for (InputAttemptIdentifier left : remaining) { // TODO Should the first one be skipped ? scheduler.putBackKnownMapOutput(host, left); } return; } try { // Loop through available map-outputs and fetch them // On any error, faildTasks is not null and we exit // after putting back the remaining maps to the // yet_to_be_fetched list and marking the failed tasks. InputAttemptIdentifier[] failedTasks = null; while (!remaining.isEmpty() && failedTasks == null) { failedTasks = copyMapOutput(host, input); } if (failedTasks != null && failedTasks.length > 0) { LOG.warn("copyMapOutput failed for tasks " + Arrays.toString(failedTasks)); for (InputAttemptIdentifier left : failedTasks) { scheduler.copyFailed(left, host, true); } } IOUtils.cleanup(LOG, input); // Sanity check if (failedTasks == null && !remaining.isEmpty()) { throw new IOException( "server didn't return all expected map outputs: " + remaining.size() + " left."); } } finally { for (InputAttemptIdentifier left : remaining) { scheduler.putBackKnownMapOutput(host, left); } } }
From source file:it.greenvulcano.gvesb.adapter.http.mapping.ForwardHttpServletMapping.java
@SuppressWarnings("deprecation") @Override//from ww w . jav a 2 s .c o m public void handleRequest(String methodName, HttpServletRequest req, HttpServletResponse resp) throws InboundHttpResponseException { logger.debug("BEGIN forward: " + req.getRequestURI()); final HttpURLConnection httpConnection = (HttpURLConnection) connection; ; try { httpConnection.setRequestMethod(methodName); httpConnection.setReadTimeout(soTimeout); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); if (dump) { StringBuffer sb = new StringBuffer(); DumpUtils.dump(req, sb); logger.info(sb.toString()); } String mapping = req.getPathInfo(); if (mapping == null) { mapping = "/"; } String destPath = contextPath + mapping; String queryString = req.getQueryString(); if (queryString != null) { destPath += "?" + queryString; } if (!destPath.startsWith("/")) { destPath = "/" + destPath; } logger.info("Resulting QueryString: " + destPath); Collections.list(req.getHeaderNames()).forEach(headerName -> { httpConnection.setRequestProperty(headerName, Collections.list(req.getHeaders(headerName)).stream().collect(Collectors.joining(";"))); }); if (methodName.equalsIgnoreCase("POST") || methodName.equalsIgnoreCase("PUT")) { httpConnection.setDoOutput(true); IOUtils.copy(req.getInputStream(), httpConnection.getOutputStream()); } httpConnection.connect(); int status = httpConnection.getResponseCode(); resp.setStatus(status, httpConnection.getResponseMessage()); httpConnection.getHeaderFields().entrySet().stream().forEach(header -> { resp.addHeader(header.getKey(), header.getValue().stream().collect(Collectors.joining(";"))); }); ByteArrayOutputStream bodyOut = new ByteArrayOutputStream(); IOUtils.copy(httpConnection.getInputStream(), bodyOut); OutputStream out = resp.getOutputStream(); out.write(bodyOut.toByteArray()); //IOUtils.copy(method.getResponseBodyAsStream(), out); out.flush(); out.close(); if (dump) { StringBuffer sb = new StringBuffer(); DumpUtils.dump(resp, bodyOut, sb); logger.info(sb.toString()); } } catch (Exception exc) { logger.error("ERROR on forwarding: " + req.getRequestURI(), exc); throw new InboundHttpResponseException("GV_CALL_SERVICE_ERROR", new String[][] { { "message", exc.getMessage() } }, exc); } finally { try { if (httpConnection != null) { httpConnection.disconnect(); } } catch (Exception exc) { logger.warn("Error while releasing connection", exc); } logger.debug("END forward: " + req.getRequestURI()); } }
From source file:com.oneops.metrics.es.ElasticsearchReporter.java
/** * This index template is automatically applied to all indices which start with the index name * The index template simply configures the name not to be analyzed */// w w w. ja v a 2 s. c o m private void checkForIndexTemplate() { try { HttpURLConnection connection = openConnection("/_template/metrics_template", "HEAD"); if (connection == null) { LOGGER.error("Could not connect to any configured elasticsearch instances: {}", Arrays.asList(hosts)); return; } connection.disconnect(); boolean isTemplateMissing = connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND; // nothing there, lets create it if (isTemplateMissing) { LOGGER.debug("No metrics template found in elasticsearch. Adding..."); HttpURLConnection putTemplateConnection = openConnection("/_template/metrics_template", "PUT"); JsonGenerator json = new JsonFactory().createGenerator(putTemplateConnection.getOutputStream()); json.writeStartObject(); json.writeStringField("template", index + "*"); json.writeObjectFieldStart("mappings"); json.writeObjectFieldStart("_default_"); json.writeObjectFieldStart("_all"); json.writeBooleanField("enabled", false); json.writeEndObject(); json.writeObjectFieldStart("properties"); json.writeObjectFieldStart("name"); json.writeObjectField("type", "string"); json.writeObjectField("index", "not_analyzed"); json.writeEndObject(); json.writeEndObject(); json.writeEndObject(); json.writeEndObject(); json.writeEndObject(); json.flush(); putTemplateConnection.disconnect(); if (putTemplateConnection.getResponseCode() != 200) { LOGGER.error( "Error adding metrics template to elasticsearch: {}/{}" + putTemplateConnection.getResponseCode(), putTemplateConnection.getResponseMessage()); } } checkedForIndexTemplate = true; } catch (IOException e) { LOGGER.error("Error when checking/adding metrics template to elasticsearch", e); } }
From source file:net.myrrix.client.ClientRecommender.java
private boolean isPartitionReady(int partition) throws TasteException { String urlPath = "/ready"; TasteException savedException = null; for (HostAndPort replica : partitions.get(partition)) { HttpURLConnection connection = null; try {//from w w w .j a v a2s .c om connection = buildConnectionToReplica(replica, urlPath, "HEAD"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: return true; case HttpURLConnection.HTTP_UNAVAILABLE: return false; default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }
From source file:com.yohanliyanage.jenkins.plugins.sparkdeploy.deployer.DeploymentManager.java
@SuppressWarnings("unchecked") private Map<String, Object> invokeUrl(String method, URL url, String payload) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setRequestProperty("Accept", "application/json"); if (payload != null) { if (verbose) { logger.println("[Spark-Deployer] VERBOSE : Invoking URL: " + method + " " + url.toString() + " with payload : \n" + payload); }/*from www . ja v a 2s.c o m*/ connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); OutputStream os = connection.getOutputStream(); os.write(payload.getBytes()); os.flush(); } else { if (verbose) { logger.println("[Spark-Deployer] VERBOSE : Invoking URL " + method + " " + url.toString()); } } InputStream responseStream; if (isSuccessResponseCode(connection.getResponseCode())) { // 2xx - Success responseStream = connection.getInputStream(); } else { responseStream = connection.getErrorStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(responseStream)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line).append(System.lineSeparator()); } if (verbose) { logger.println("[Spark-Deployer] VERBOSE : Response from Spark : \n" + response); } if (!isSuccessResponseCode(connection.getResponseCode())) { throw new RuntimeException("Operation Failed. Response is " + connection.getResponseCode() + " : " + connection.getResponseMessage()); } return (Map) mapper.readValue(response.toString(), HashMap.class); }
From source file:sit.web.client.HttpHelper.java
public HTTPResponse doHttpUrlConnectionRequest(URL url, String method, String contentType, byte[] payload, String unamePword64) throws MalformedURLException, ProtocolException, IOException, URISyntaxException { if (payload == null) { //make sure payload is initialized payload = new byte[0]; }//from ww w.jav a 2s . c o m boolean isHTTPS = url.getProtocol().equalsIgnoreCase("https"); HttpURLConnection connection; if (isHTTPS) { connection = (HttpsURLConnection) url.openConnection(); } else { connection = (HttpURLConnection) url.openConnection(); } connection.setRequestMethod(method); connection.setRequestProperty("Host", url.getHost()); connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Content-Length", String.valueOf(payload.length)); if (isHTTPS) { connection.setRequestProperty("Authorization", "Basic " + unamePword64); } Logger.getLogger(HttpHelper.class.getName()).log(Level.FINER, "trying to connect:\n" + method + " " + url + "\nhttps:" + isHTTPS + "\nContentType:" + contentType + "\nContent-Length:" + String.valueOf(payload.length)); connection.setDoInput(true); if (payload.length > 0) { // open up the output stream of the connection connection.setDoOutput(true); FilterOutputStream output = new FilterOutputStream(connection.getOutputStream()); // write out the data output.write(payload); output.close(); } HTTPResponse response = new HTTPResponse(method + " " + url.toString(), payload, Charset.defaultCharset()); //TODO forward charset ot this method response.code = connection.getResponseCode(); response.message = connection.getResponseMessage(); Logger.getLogger(HttpHelper.class.getName()).log(Level.FINE, "received response: " + response.message + " with code: " + response.code); if (response.code != 500) { byte[] buffer = new byte[20480]; ByteBuilder bytes = new ByteBuilder(); // get ready to read the response from the cgi script DataInputStream input = new DataInputStream(connection.getInputStream()); boolean done = false; while (!done) { int readBytes = input.read(buffer); done = (readBytes == -1); if (!done) { bytes.append(buffer, readBytes); } } input.close(); response.reply = bytes.toString(Charset.defaultCharset()); } return response; }
From source file:com.googlecode.jmxtrans.model.output.StackdriverWriter.java
/** * Post the formatted results to the gateway URL over HTTP * /*w w w . j ava 2s .c o m*/ * @param gatewayMessage String in the Stackdriver custom metrics JSON format containing the data points */ private void doSend(final String gatewayMessage) { HttpURLConnection urlConnection = null; try { if (proxy == null) { urlConnection = (HttpURLConnection) gatewayUrl.openConnection(); } else { urlConnection = (HttpURLConnection) gatewayUrl.openConnection(proxy); } urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setReadTimeout(timeoutInMillis); urlConnection.setRequestProperty("content-type", "application/json; charset=utf-8"); urlConnection.setRequestProperty("x-stackdriver-apikey", apiKey); // Stackdriver's own implementation does not specify char encoding // to use. Let's take the simplest approach and at lest ensure that // if we have problems they can be reproduced in consistant ways. // See https://github.com/Stackdriver/stackdriver-custommetrics-java/blob/master/src/main/java/com/stackdriver/api/custommetrics/CustomMetricsPoster.java#L262 // for details. urlConnection.getOutputStream().write(gatewayMessage.getBytes(ISO_8859_1)); int responseCode = urlConnection.getResponseCode(); if (responseCode != 200 && responseCode != 201) { logger.warn("Failed to send results to Stackdriver server: responseCode=" + responseCode + " message=" + urlConnection.getResponseMessage()); } } catch (Exception e) { logger.warn("Failure to send result to Stackdriver server", e); } finally { if (urlConnection != null) { try { InputStream in = urlConnection.getInputStream(); in.close(); InputStream err = urlConnection.getErrorStream(); if (err != null) { err.close(); } urlConnection.disconnect(); } catch (IOException e) { logger.warn("Error flushing http connection for one result, continuing"); logger.debug("Stack trace for the http connection, usually a network timeout", e); } } } }
From source file:net.myrrix.client.ClientRecommender.java
@Override public List<RecommendedItem> mostPopularItems(int howMany) throws TasteException { StringBuilder urlPath = new StringBuilder(32); urlPath.append("/mostPopularItems"); appendCommonQueryParams(howMany, false, null, urlPath); // Always send to partition 0 for consistency TasteException savedException = null; for (HostAndPort replica : choosePartitionAndReplicas(0L)) { HttpURLConnection connection = null; try {/* ww w . j a v a 2s. c om*/ connection = buildConnectionToReplica(replica, urlPath.toString(), "GET"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: return consumeItems(connection); case HttpURLConnection.HTTP_UNAVAILABLE: throw new NotReadyException(); default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }
From source file:io.fabric8.devops.connector.DevOpsConnector.java
protected JsonNode parseLastBuildJson(String urlText) { HttpURLConnection connection = null; String message = null;//from www . j a v a 2s . c o m try { URL url = new URL(urlText); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); int status = connection.getResponseCode(); message = connection.getResponseMessage(); getLog().info("Got response code from URL: " + url + " " + status + " message: " + message); if (status != 200 || Strings.isNullOrBlank(message)) { getLog().debug("Failed to load URL " + url + ". Status: " + status + " message: " + message); } else { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.reader().readTree(message); } } catch (Exception e) { getLog().debug("Failed to load URL " + urlText + ". " + e, e); } finally { if (connection != null) { connection.disconnect(); } } return null; }
From source file:net.myrrix.client.ClientRecommender.java
private void getAllIDsFromPartition(int partition, boolean user, FastIDSet result) throws TasteException { String urlPath = '/' + (user ? "user" : "item") + "/allIDs"; TasteException savedException = null; for (HostAndPort replica : partitions.get(partition)) { HttpURLConnection connection = null; try {/* w ww.j a v a 2s .c o m*/ connection = buildConnectionToReplica(replica, urlPath, "GET"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: consumeIDs(connection, result); return; case HttpURLConnection.HTTP_UNAVAILABLE: throw new NotReadyException(); default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }