use of org.apache.http.HttpResponse in project hbase by apache.
the class Client method get.
/**
* Send a GET request
* @param c the cluster definition
* @param path the path or URI
* @param headers the HTTP headers to include in the request
* @return a Response object with response detail
* @throws IOException
*/
public Response get(Cluster c, String path, Header[] headers) throws IOException {
if (httpGet != null) {
httpGet.releaseConnection();
}
httpGet = new HttpGet(path);
HttpResponse resp = execute(c, httpGet, headers, path);
return new Response(resp.getStatusLine().getStatusCode(), resp.getAllHeaders(), resp, resp.getEntity() == null ? null : resp.getEntity().getContent());
}
use of org.apache.http.HttpResponse in project hadoop by apache.
the class WebAppProxyServlet method proxyLink.
/**
* Download link and have it be the response.
* @param req the http request
* @param resp the http response
* @param link the link to download
* @param c the cookie to set if any
* @param proxyHost the proxy host
* @param method the http method
* @throws IOException on any error.
*/
private static void proxyLink(final HttpServletRequest req, final HttpServletResponse resp, final URI link, final Cookie c, final String proxyHost, final HTTP method) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY).setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
// Make sure we send the request from the proxy address in the config
// since that is what the AM filter checks against. IP aliasing or
// similar could cause issues otherwise.
InetAddress localAddress = InetAddress.getByName(proxyHost);
if (LOG.isDebugEnabled()) {
LOG.debug("local InetAddress for proxy host: {}", localAddress);
}
client.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
HttpRequestBase base = null;
if (method.equals(HTTP.GET)) {
base = new HttpGet(link);
} else if (method.equals(HTTP.PUT)) {
base = new HttpPut(link);
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
((HttpPut) base).setEntity(new StringEntity(sb.toString()));
} else {
resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
@SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
if (PASS_THROUGH_HEADERS.contains(name)) {
String value = req.getHeader(name);
if (LOG.isDebugEnabled()) {
LOG.debug("REQ HEADER: {} : {}", name, value);
}
base.setHeader(name, value);
}
}
String user = req.getRemoteUser();
if (user != null && !user.isEmpty()) {
base.setHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
}
OutputStream out = resp.getOutputStream();
try {
HttpResponse httpResp = client.execute(base);
resp.setStatus(httpResp.getStatusLine().getStatusCode());
for (Header header : httpResp.getAllHeaders()) {
resp.setHeader(header.getName(), header.getValue());
}
if (c != null) {
resp.addCookie(c);
}
InputStream in = httpResp.getEntity().getContent();
if (in != null) {
IOUtils.copyBytes(in, out, 4096, true);
}
} finally {
base.releaseConnection();
}
}
use of org.apache.http.HttpResponse in project hive by apache.
the class JIRAService method publishComments.
void publishComments(String comments) {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
URL apiURL = new URL(mUrl);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(mUser, mPassword));
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute("preemptive-auth", new BasicScheme());
httpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
HttpPost request = new HttpPost(url);
ObjectMapper mapper = new ObjectMapper();
StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments)));
request.addHeader("Content-Type", "application/json");
request.setEntity(params);
HttpResponse httpResponse = httpClient.execute(request, localcontext);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() != 201) {
throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
mLogger.info("JIRA Response Metadata: " + httpResponse);
} catch (Exception e) {
mLogger.error("Encountered error attempting to post comment to " + mName, e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
use of org.apache.http.HttpResponse in project hive by apache.
the class PTestClient method downloadTestResults.
private void downloadTestResults(String testHandle, String testOutputDir) throws Exception {
HttpGet request = new HttpGet(mLogsEndpoint + testHandle + "/test-results.tar.gz");
FileOutputStream output = null;
try {
HttpResponse httpResponse = mHttpClient.execute(request);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() != 200) {
throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
output = new FileOutputStream(new File(testOutputDir, "test-results.tar.gz"));
IOUtils.copyLarge(httpResponse.getEntity().getContent(), output);
output.flush();
} finally {
request.abort();
if (output != null) {
output.close();
}
}
}
use of org.apache.http.HttpResponse in project hive by apache.
the class PTestClient method post.
private <S extends GenericResponse> S post(Object payload, boolean agressiveRetry) throws Exception {
EndPointResponsePair endPointResponse = Preconditions.checkNotNull(REQUEST_TO_ENDPOINT.get(payload.getClass()), payload.getClass().getName());
HttpPost request = new HttpPost(mApiEndPoint + endPointResponse.getEndpoint());
try {
String payloadString = mMapper.writeValueAsString(payload);
StringEntity params = new StringEntity(payloadString);
request.addHeader("content-type", "application/json");
request.setEntity(params);
if (agressiveRetry) {
mHttpClient.setHttpRequestRetryHandler(new PTestHttpRequestRetryHandler());
}
HttpResponse httpResponse = mHttpClient.execute(request);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() != 200) {
throw new IllegalStateException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
@SuppressWarnings("unchecked") S result = (S) endPointResponse.getResponseClass().cast(mMapper.readValue(response, endPointResponse.getResponseClass()));
Status.assertOK(result.getStatus());
if (System.getProperty("DEBUG_PTEST_CLIENT") != null) {
System.err.println("payload " + payloadString);
if (result instanceof TestLogResponse) {
System.err.println("response " + ((TestLogResponse) result).getOffset() + " " + ((TestLogResponse) result).getStatus());
} else {
System.err.println("response " + response);
}
}
Thread.sleep(1000);
return result;
} finally {
request.abort();
}
}
Aggregations