public boolean isForbidden() throws IOException { return getStatusCode() == SC_FORBIDDEN; }
HttpRequest(HttpRequestBase method, Credentials credentials) { this(method); if (credentials != null) setAuthentication(credentials); }
public InputStream executeAsStream() throws IOException { this.response = execute(); // no response body then HttpEntity entity = response.getEntity(); InputStream body = entity != null ? entity.getContent() : null; if (!isSuccessful() && !isNotModified()) log.warning(format("Cannot read response body for %s", method.getURI())); return body; }
public String executeAsString() throws IOException { try { this.response = execute(); HttpEntity entity = response.getEntity(); // HEAD requests don't have a body String body = entity != null ? new String(readBytes(entity.getContent()), StandardCharsets.UTF_8) : null; if (!isSuccessful() && body != null) log.warning(format("Body of %s not null: %s", response, body)); return body; } finally { release(); } }
protected HttpResponse execute() throws IOException { Proxy proxy = findProxy(method.getURI()); if(proxy != NO_PROXY && !proxy.type().equals(DIRECT)) { SocketAddress address = proxy.address(); if(address instanceof InetSocketAddress) { InetSocketAddress inetSocketAddress = (InetSocketAddress) address; requestConfigBuilder.setProxy(new HttpHost(inetSocketAddress.getHostName(), inetSocketAddress.getPort())); log.info(format("Using proxy %s for %s", proxy.toString(), method.getURI())); } } RequestConfig requestConfig = requestConfigBuilder.build(); clientBuilder.setDefaultRequestConfig(requestConfig); try { return clientBuilder.build().execute(method, context); } catch (SocketException e) { if (throwsSocketExceptionIfUnAuthorized()) return new BasicHttpResponse(HTTP_1_1, SC_UNAUTHORIZED, "socket exception since unauthorized"); else throw e; } }
protected String getHeader(String name) throws IOException { assertExecuted(); Header header = response.getFirstHeader(name); return header != null ? header.getValue() : null; }
protected HttpResponse execute() throws IOException { if (builder != null) { HttpEntity entity = builder.build(); getHttpEntityEnclosingRequestBase().setEntity(entity); } return super.execute(); }
protected void setHeader(String name, String value) { getMethod().setHeader(name, value); }
HttpRequest(HttpRequestBase method) { this.log = Logger.getLogger(getClass().getName()); requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(15 * 1000); requestConfigBuilder.setSocketTimeout(90 * 1000); clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); try { HttpClientConnectionManager connectionManager = new SSLConnectionManagerFactory().createConnectionManager(); clientBuilder.setConnectionManager(connectionManager); } catch (Exception e) { log.severe("Cannot create SSL connection manager that supports letsencrypt root certificate: " + getLocalizedMessage(e)); } setUserAgent("RouteConverter REST Client/" + System.getProperty("rest", "1.8")); this.method = method; }
public/*for tests*/ List<Header> getHeaders() throws IOException { assertExecuted(); return asList(response.getAllHeaders()); }
public boolean isPreconditionFailed() throws IOException { return getStatusCode() == SC_PRECONDITION_FAILED; } }
public int getStatusCode() throws IOException { assertExecuted(); return response.getStatusLine().getStatusCode(); }
private void setAuthentication(Credentials credentials) { URI uri = method.getURI(); setAuthentication(credentials.getUserName(), credentials.getPassword(), uri); }
public boolean isSuccessful() throws IOException { return getStatusCode() >= SC_OK && getStatusCode() < SC_MULTIPLE_CHOICES; }
public boolean isBadRequest() throws IOException { return getStatusCode() == SC_BAD_REQUEST; }
public boolean isNotModified() throws IOException { return getStatusCode() == SC_NOT_MODIFIED; }
public boolean isPartialContent() throws IOException { return getStatusCode() == SC_PARTIAL_CONTENT; }
public boolean isOk() throws IOException { return getStatusCode() == SC_OK; }
public boolean isUnAuthorized() throws IOException { return getStatusCode() == SC_UNAUTHORIZED; }
public boolean isNotFound() throws IOException { return getStatusCode() == SC_NOT_FOUND; }