Java Code Examples for org.apache.http.client.ResponseHandler#handleResponse()

The following examples show how to use org.apache.http.client.ResponseHandler#handleResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectedRESTQA.java    From java-client-api with Apache License 2.0 7 votes vote down vote up
public static String[] getHosts() {
	try {
		DefaultHttpClient client = new DefaultHttpClient();
		client.getCredentialsProvider().setCredentials(new AuthScope(host_name, getAdminPort()),
				new UsernamePasswordCredentials("admin", "admin"));
		HttpGet get = new HttpGet("http://" + host_name + ":" + admin_port + "/manage/v2/hosts?format=json");

		HttpResponse response = client.execute(get);
		ResponseHandler<String> handler = new BasicResponseHandler();
		String body = handler.handleResponse(response);
		JsonNode actualObj = new ObjectMapper().readTree(body);
		JsonNode nameNode = actualObj.path("host-default-list").path("list-items");
		List<String> hosts = nameNode.findValuesAsText("nameref");
		String[] s = new String[hosts.size()];
		hosts.toArray(s);
		return s;

	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 2
Source File: WBFailover.java    From java-client-api with Apache License 2.0 7 votes vote down vote up
private boolean isRunning(String host) {
	try {

		DefaultHttpClient client = new DefaultHttpClient();
		client.getCredentialsProvider().setCredentials(new AuthScope(host, 7997),
				new UsernamePasswordCredentials("admin", "admin"));

		HttpGet get = new HttpGet("http://" + host + ":7997?format=json");
		HttpResponse response = client.execute(get);
		ResponseHandler<String> handler = new BasicResponseHandler();
		String body = handler.handleResponse(response);
		if (body.contains("Healthy")) {
			return true;
		}

	} catch (Exception e) {
		return false;
	}
	return false;
}
 
Example 3
Source File: SyndesisHttpClient.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public InputStream executeGET(String url, Header... headers) throws IOException{
    HttpGet request = new HttpGet(url);
    if (headers != null) {
        for (Header header : headers) {
            request.addHeader(header);
        }
    }
    addDefaultHeaders(request);

    HttpResponse response = this.client.execute(request);
    ResponseHandler<InputStream> handler = new AbstractResponseHandler<InputStream>(){
        @Override
        public InputStream handleEntity(final HttpEntity entity) throws IOException {
            return entity.getContent();
        }
    };
    return handler.handleResponse(response);
}
 
Example 4
Source File: QBFailover.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
private boolean isRunning(String host) {
	try {

		DefaultHttpClient client = new DefaultHttpClient();
		client.getCredentialsProvider().setCredentials(new AuthScope(host, 7997),
				new UsernamePasswordCredentials("admin", "admin"));

		HttpGet get = new HttpGet("http://" + host + ":7997?format=json");
		HttpResponse response = client.execute(get);
		ResponseHandler<String> handler = new BasicResponseHandler();
		String body = handler.handleResponse(response);
		if (body.toLowerCase().contains("healthy")) {
			return true;
		} else {
			return false;
		}

	} catch (Exception e) {
		return false;
	}
}
 
Example 5
Source File: AssertRequest.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <T> T execute(final HttpUriRequest request,
    final ResponseHandler<? extends T> responseHandler)
    throws IOException, ClientProtocolException {
    this.check(request);
    return responseHandler.handleResponse(this.response);
}
 
Example 6
Source File: SyndesisHttpClient.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public InputStream executePOST(String url, String payload) throws IOException{
    HttpPost request = new HttpPost(url);
    addDefaultHeaders(request);
    request.setEntity(new StringEntity(payload));
    HttpResponse response = this.client.execute(request);
    ResponseHandler<InputStream> handler = new AbstractResponseHandler<InputStream>(){
        @Override
        public InputStream handleEntity(final HttpEntity entity) throws IOException {
            return entity.getContent();
        }
    };
    return handler.handleResponse(response);
}
 
Example 7
Source File: SyndesisHttpClient.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public InputStream executeDELETE(String url) throws IOException {
    HttpDelete request = new HttpDelete(url);
    addDefaultHeaders(request);
    HttpResponse response = this.client.execute(request);
    ResponseHandler<InputStream> handler = new AbstractResponseHandler<InputStream>(){
        @Override
        public InputStream handleEntity(final HttpEntity entity) throws IOException {
            return entity.getContent();
        }
    };
    return handler.handleResponse(response);
}
 
Example 8
Source File: TrackerClient.java    From joal with Apache License 2.0 5 votes vote down vote up
private <T> T handleResponse(final HttpResponse response, final ResponseHandler<T> handler) throws ClientProtocolException, IOException {
    try {
        return handler.handleResponse(response);
    } finally {
        try {
            final HttpEntity entity = response.getEntity();
            final InputStream content = entity.getContent();
            if (content != null) {
                content.close();
            }
        } catch (final Exception ignore) {
        }
    }
}
 
Example 9
Source File: ConnectedRESTQA.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
public static JsonNode getState(Map<String, String> properties, String endpoint) {
	try {

		DefaultHttpClient client = new DefaultHttpClient();
		client.getCredentialsProvider().setCredentials(new AuthScope(host_name, getAdminPort()),
				new UsernamePasswordCredentials("admin", "admin"));

		StringBuilder xmlBuff = new StringBuilder();
		Iterator it = properties.entrySet().iterator();
		int size = properties.size();
		int j = 0;
		while (it.hasNext()) {
			Map.Entry pair = (Map.Entry) it.next();
			xmlBuff.append(pair.getKey());
			if (j == (size - 1)) {
				xmlBuff.append('=').append(pair.getValue());
			} else {
				xmlBuff.append('=').append(pair.getValue()).append('&');
			}

			j++;
		}
		HttpGet get = new HttpGet(
				"http://" + host_name + ":" + admin_port + endpoint + "?format=json&" + xmlBuff.toString());
		HttpResponse response = client.execute(get);
		ResponseHandler<String> handler = new BasicResponseHandler();
		String body = handler.handleResponse(response);
		JsonNode actualObj = new ObjectMapper().readTree(body);
		return actualObj;

	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 10
Source File: Tools.java    From torrenttunes-client with GNU General Public License v3.0 3 votes vote down vote up
public static String uploadTorrentInfoToTracker(String jsonInfo) {


		String postURL = DataSources.TORRENT_INFO_UPLOAD_URL;

		String message = "";
		try {

			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).
					build();
			CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();			


			HttpPost httpPost = new HttpPost(postURL);
			httpPost.setEntity(new StringEntity(jsonInfo, "UTF-8"));



			//			httpPost.setEntity(new StringEntity("L"));

			ResponseHandler<String> handler = new BasicResponseHandler();


			CloseableHttpResponse response = httpClient.execute(httpPost);



			message = handler.handleResponse(response);

			httpClient.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new NoSuchElementException("Couldn't save the torrent info");
		}

		message = "upload status : " + message;
		log.debug(message);
		return message;
	}
 
Example 11
Source File: CachingHttpClient.java    From apigee-android-sdk with Apache License 2.0 3 votes vote down vote up
/**
 * Execute an {@link HttpRequest} @ a given {@link HttpHost} with a
 * specified {@link ResponseHandler} that will deal with the result of the
 * call using a specific {@link HttpContext}
 * 
 * @param target
 *            the target host for the request. Implementations may accept
 *            <code>null</code> if they can still determine a route, for
 *            example to a default target or by inspecting the request.
 * @param request
 *            the request to execute
 * @param responseHandler
 *            the response handler
 * @param context
 *            the context to use for the execution, or <code>null</code> to
 *            use the default context
 * @param <T>
 *            The Return Type Identified by the generic type of the
 *            {@link ResponseHandler}
 * @return T The response type as handled by ResponseHandler
 * @throws IOException
 */
public <T> T execute(HttpHost target, HttpRequest request,
		ResponseHandler<? extends T> responseHandler, HttpContext context)
		throws IOException {
	HttpResponse resp = execute(target, request, context);
	return responseHandler.handleResponse(resp);
}
 
Example 12
Source File: CachingHttpClient.java    From apigee-android-sdk with Apache License 2.0 3 votes vote down vote up
/**
 * @param request
 *            the request to execute
 * @param responseHandler
 *            the response handler
 * @param context
 *            the http context
 * @param <T>
 *            The Return Type Identified by the generic type of the
 *            {@link ResponseHandler}
 * @return T The response type as handled by ResponseHandler
 * @throws IOException
 */
public <T> T execute(HttpUriRequest request,
		ResponseHandler<? extends T> responseHandler, HttpContext context)
		throws IOException {
	HttpResponse resp = execute(request, context);
	return responseHandler.handleResponse(resp);
}
 
Example 13
Source File: HttpDispatcher.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 *
 * @param request
 * @param handler
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public <T> T execute(HttpUriRequest request, ResponseHandler<T> handler) throws ClientProtocolException, IOException {
    return handler.handleResponse(client.execute(request));
}