/** * Return the request method. * @return The request method. * @see java.net.HttpURLConnection#getRequestMethod() * @see org.apache.commons.httpclient.HttpMethod#getName() */ public String getRequestMethod() { LOG.trace("enter HttpURLConnection.getRequestMethod()"); return this.method.getName(); }
public String getMethod() { return httpMethod.getName(); }
/** * @param httpClient * @param httpMethod * @return * Returns the HTTP Status Code or -1 if an exception occurred. */ public static int executeMethod(HttpClient httpClient, HttpMethod httpMethod) { // Execute GetMethod try { return httpClient.executeMethod(httpMethod); } catch (IOException e) { LOGGER.warn("Exception while executing HttpMethod " + httpMethod.getName() + " on URL " + httpMethod.getPath()); return -1; } }
public InputStream getMessagePayload() throws IOException { InputStream body = null; if(httpMethod instanceof PostMethod){ PostMethod postMethod = (PostMethod) httpMethod; ByteArrayOutputStream out = new ByteArrayOutputStream(); postMethod.getRequestEntity().writeRequest(out); body = new ByteArrayInputStream(out.toByteArray()); }else{ throw new IllegalStateException("HTTP method " + httpMethod.getName() + " not supported"); } return body; }
/** * Return the request method */ public String getMethod() { return method.getName(); }
/** * Return the request method. * @return The request method. * @see java.net.HttpURLConnection#getRequestMethod() * @see org.apache.commons.httpclient.HttpMethod#getName() */ public String getRequestMethod() { LOG.trace("enter HttpURLConnection.getRequestMethod()"); return this.method.getName(); }
/** * Return the request method. * @return The request method. * @see java.net.HttpURLConnection#getRequestMethod() * @see org.apache.commons.httpclient.HttpMethod#getName() */ public String getRequestMethod() { LOG.trace("enter HttpURLConnection.getRequestMethod()"); return this.method.getName(); }
/** * Return the request method. * @return The request method. * @see java.net.HttpURLConnection#getRequestMethod() * @see org.apache.commons.httpclient.HttpMethod#getName() */ public String getRequestMethod() { LOG.trace("enter HttpURLConnection.getRequestMethod()"); return this.method.getName(); }
/** * Return the request method. * @return The request method. * @see java.net.HttpURLConnection#getRequestMethod() * @see org.apache.commons.httpclient.HttpMethod#getName() */ public String getRequestMethod() { LOG.trace("enter HttpURLConnection.getRequestMethod()"); return this.method.getName(); }
/** * @param httpClient * @param httpMethod * @return Returns the HTTP Status Code or -1 if an exception occurred. */ public static int executeMethod(final HttpClient httpClient, final HttpMethod httpMethod) { // Execute GetMethod try { return httpClient.executeMethod(httpMethod); } catch (final IOException e) { LOGGER.warn("Exception while executing HttpMethod " + httpMethod.getName() + " on URL " + httpMethod.getPath()); return -1; } }
@Override public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { // Retry the method if either: // 1) By default it would be retried (e.g. if server dropped our connection): if (super.retryMethod(method, exception, executionCount)) { return true; } // 2) This is an idempotent method that has only been tried once, // and failed with a SocketException (in particular this catches "Connection reset"): // - for now only consider GET and HEAD idempotent; // - PUT and DELETE should be idempotent according to the HTTP philosophy, // but might not always be for AG. if ((executionCount == 1) && (exception instanceof java.net.SocketException)) { switch (method.getName()) { case "GET": case "HEAD": return true; default: break; } } return false; } }
protected int executeMethod( ProxyRepository repository, ResourceStoreRequest request, HttpMethod method, URL remoteUrl ) throws RemoteStorageException { final Stopwatch stopwatch = timingLog.isDebugEnabled() ? new Stopwatch().start() : null; try { return doExecuteMethod( repository, request, method, remoteUrl ); } finally { if ( stopwatch != null ) { stopwatch.stop(); timingLog.debug( "[{}] {} {} took {}", new Object[] { repository.getId(), method.getName(), remoteUrl, stopwatch } ); } } }
/** * Execute the request with the request and response logged at debug level * @param method method to execute * @param client client to use * @param <M> method type * @return the status code * @throws IOException any failure reported by the HTTP client. */ private <M extends HttpMethod> int execWithDebugOutput(M method, HttpClient client) throws IOException { if (LOG.isDebugEnabled()) { StringBuilder builder = new StringBuilder( method.getName() + " " + method.getURI() + "\n"); for (Header header : method.getRequestHeaders()) { builder.append(header.toString()); } LOG.debug(builder); } int statusCode = client.executeMethod(method); if (LOG.isDebugEnabled()) { LOG.debug("Status code = " + statusCode); } return statusCode; }
/** * Execute a transaction method given a complete URI. * @param method the transaction method * @param headers HTTP header values to send * @param uri a properly urlencoded URI * @return the HTTP response code * @throws IOException */ public int executeURI(HttpMethod method, Header[] headers, String uri) throws IOException { method.setURI(new URI(uri, true)); for (Map.Entry<String, String> e: extraHeaders.entrySet()) { method.addRequestHeader(e.getKey(), e.getValue()); } if (headers != null) { for (Header header: headers) { method.addRequestHeader(header); } } long startTime = System.currentTimeMillis(); int code = httpClient.executeMethod(method); long endTime = System.currentTimeMillis(); if (LOG.isDebugEnabled()) { LOG.debug(method.getName() + " " + uri + " " + code + " " + method.getStatusText() + " in " + (endTime - startTime) + " ms"); } return code; }
@SneakyThrows public static void prepareRPC(HttpMethod httpMethod) { val context = threadLocal.get(); if (context == null) return; val method = httpMethod.getName(); trace("RPC", method + " " + httpMethod.getURI()); httpMethod.setRequestHeader(BLACKCAT_TRACEID, context.getTraceId()); val linkId = context.getParentLinkId() + String.format(".%06d", context.getSubLinkId()); httpMethod.setRequestHeader(BLACKCAT_LINKID, linkId); }
/** * Treats the response as JSON string and performs a databinding. */ public <T> T bind(Class<T> type, BeesClient root) throws IOException { if (getCode() >= 300) throw new IOException("Failed to " + method.getName() + " : " + method.getURI() + " : code=" + getCode() + " response=" + getContent()); if (type != null && getContent() != null) { T ret = BeesClient.MAPPER.readValue(getContent(), type); if (ret instanceof CBObject) {// TODO: nested objects? ((CBObject) ret).root = root; } return ret; } return null; }
/** * Executes an HTTP method. */ private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException { BeesClientConfiguration conf = getBeesClientConfiguration(); HttpClient httpClient = HttpClientHelper.createClient(conf); if (encodedAccountAuthorization != null) httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization); if (headers != null) { for (Map.Entry<String, String> entry: headers.entrySet()) { httpMethod.setRequestHeader(entry.getKey(), entry.getValue()); } } int status = 500; String rsp = "Error"; try { status = httpClient.executeMethod(httpMethod); rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream()); } catch (IOException e) { throw (IOException)new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI() + " : code=" + status + " response=" + e.getMessage()).initCause(e); } finally { httpMethod.releaseConnection(); } trace(status + ": " + rsp); return new HttpReply(httpMethod, status, rsp); }
/** * Executes an HTTP method. */ private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException { BeesClientConfiguration conf = getBeesClientConfiguration(); HttpClient httpClient = HttpClientHelper.createClient(conf); if (encodedAccountAuthorization != null) httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization); if (headers != null) { for (Map.Entry<String, String> entry: headers.entrySet()) { httpMethod.setRequestHeader(entry.getKey(), entry.getValue()); } } int status = 500; String rsp = "Error"; try { status = httpClient.executeMethod(httpMethod); rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream()); } catch (IOException e) { throw (IOException)new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI() + " : code=" + status + " response=" + e.getMessage()).initCause(e); } finally { httpMethod.releaseConnection(); } trace(status + ": " + rsp); return new HttpReply(httpMethod, status, rsp); }
/** * Treats the response as JSON string and performs a databinding. */ public <T> T bind(Class<T> type, BeesClient root) throws IOException { if (getCode() >= 300) throw new IOException("Failed to " + method.getName() + " : " + method.getURI() + " : code=" + getCode() + " response=" + getContent()); if (type != null && getContent() != null) { T ret = BeesClient.MAPPER.readValue(getContent(), type); if (ret instanceof CBObject) {// TODO: nested objects? ((CBObject) ret).root = root; } return ret; } return null; }