org.apache.http.client.ResponseHandler Java Examples

The following examples show how to use org.apache.http.client.ResponseHandler. 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: UnixHttpClientTestCase.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * UnixHttpClient can execute the HttpRequest with the given host,
 * response handler and context.
 * @throws IOException If something goes wrong.
 */
@Test
public void executesRequestWithHostHandlerAndContext() throws IOException {
    final HttpHost host = new HttpHost("127.0.0.1");
    final HttpRequest req = Mockito.mock(HttpRequest.class);
    final ResponseHandler<String> handler = Mockito.mock(
        ResponseHandler.class
    );
    final HttpContext context = Mockito.mock(HttpContext.class);
    final HttpClient decorated = Mockito.mock(HttpClient.class);
    Mockito.when(
        decorated.execute(host, req, handler, context)
    ).thenReturn("executed");

    final HttpClient unix = new UnixHttpClient(() -> decorated);
    MatcherAssert.assertThat(
        unix.execute(host, req, handler, context),
        Matchers.equalTo("executed")
    );
    Mockito.verify(
        decorated, Mockito.times(1)
    ).execute(host, req, handler, context);
}
 
Example #3
Source File: ApacheHttpRestClient.java    From sparkler with Apache License 2.0 6 votes vote down vote up
public ApacheHttpRestClient() {

        this.httpClient = HttpClients.createDefault();

        //TODO lambda
        this.responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(
                    final HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                }
                else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

       };
    }
 
Example #4
Source File: Item.java    From customer-review-crawler with The Unlicense 6 votes vote down vote up
/**
 * @return the RAW XML document of ItemLookup (Large Response) from Amazon
 *         product advertisement API
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws ClientProtocolException
 * @throws IOException
 */
public String getXMLLargeResponse() throws InvalidKeyException,
		NoSuchAlgorithmException, ClientProtocolException, IOException {
	String responseBody = "";
	String signedurl = signInput();
	try {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(signedurl);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		responseBody = httpclient.execute(httpget, responseHandler);
		// responseBody now contains the contents of the page
		// System.out.println(responseBody);
		httpclient.getConnectionManager().shutdown();
	} catch (Exception e) {
		System.out.println("Exception" + " " + itemID + " " + e.getClass());
	}
	return responseBody;
}
 
Example #5
Source File: InventoryIndexManagerImpl.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void sendBulk(final String requestBody, final String inventoryName) {
    try {
        HttpPost post = new HttpPost(bulkUri);
        StringEntity body = new StringEntity(requestBody);
        logger.trace(String.format("%s:\n%s", inventoryName, requestBody));
        body.setChunked(false);
        post.setEntity(body);
        ResponseHandler<Void> rspHandler = new ResponseHandler<Void>() {
            @Override
            public Void handleResponse(HttpResponse rsp) throws ClientProtocolException, IOException {
                if (rsp.getStatusLine().getStatusCode() != HttpStatus.SC_OK && rsp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
                    logger.warn(String.format("Failed to do bulk operation on[%s] , because: \nstatus line: %s\nresponse body: %s\nrequest body: %s",
                            inventoryName, rsp.getStatusLine(), EntityUtils.toString(rsp.getEntity()), requestBody));
                } else {
                    logger.trace(String.format("Successfully did bulk operation on[%s], %s", inventoryName, EntityUtils.toString(rsp.getEntity())));
                }
                return null;
            }
        };
        httpClient.execute(post, rspHandler);
    } catch (Exception e) {
        logger.warn(String.format("Failed to do bulk operation on inventory[%s]", inventoryName), e);
    }
}
 
Example #6
Source File: MDSInterface2.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public synchronized static final <T> Response<T> syncUpdate(Context ctx, Uri uri,
                                                            String username, String password,
                                                            Bundle form,
                                                            Bundle files,
                                                            ResponseHandler<Response<T>> handler) {
    Log.i(TAG, "syncUpdate()");
    Map<String, String> data = new HashMap<String, String>();
    Cursor c = null;
    Response<T> response = Response.empty();
    // Should have at least one field that need to be updated
    if (form != null) {
        Iterator<String> keys = form.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            data.put(key, form.getString(key));
            Log.d(TAG, "key: " + key + " --> value: " + form.getString(key));
        }
    }
    try {
        URI target = iriToURI(ctx, uri);
        response = apiPost(target, username, password, data, handler);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}
 
Example #7
Source File: UnixHttpClientTestCase.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * UnixHttpClient can execute the HttpUriRequest with the given
 * response handler and context.
 * @throws IOException If something goes wrong.
 */
@Test
public void executesUriRequestWithHandlerAndContext() throws IOException {
    final HttpUriRequest req = Mockito.mock(HttpUriRequest.class);
    final ResponseHandler<String> handler = Mockito.mock(
        ResponseHandler.class
    );
    final HttpContext context = Mockito.mock(HttpContext.class);
    final HttpClient decorated = Mockito.mock(HttpClient.class);
    Mockito.when(
        decorated.execute(req, handler, context)
    ).thenReturn("executed");

    final HttpClient unix = new UnixHttpClient(() -> decorated);
    MatcherAssert.assertThat(
        unix.execute(req, handler, context), Matchers.equalTo("executed")
    );
    Mockito.verify(
        decorated, Mockito.times(1)
    ).execute(req, handler, context);
}
 
Example #8
Source File: LocalHttpClient.java    From springboot-security-wechat with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param mch_id mch_id
 * @param request request
 * @param clazz clazz
 * @param sign_type 数据返回验证签名类型
 * @param key 数据返回验证签名key
 * @since 2.8.5
 * @return result
 */
public static <T> T keyStoreExecuteXmlResult(String mch_id,HttpUriRequest request,Class<T> clazz,String sign_type,String key){
	String uriId = loggerRequest(request);
	ResponseHandler<T> responseHandler = XmlResponseHandler.createResponseHandler(clazz,sign_type,key);
	if(responseHandler instanceof LocalResponseHandler){
		LocalResponseHandler lrh = (LocalResponseHandler) responseHandler;
		lrh.setUriId(uriId);
	}
	try {
		T t = httpClient_mchKeyStore.get(mch_id).execute(request,responseHandler,HttpClientContext.create());
		if(resultErrorHandler != null){
			resultErrorHandler.doHandle(uriId, request, t);
		}
		return t;
	} catch (Exception e) {
		e.printStackTrace();
		logger.error(e.getMessage());
	}
	return null;
}
 
Example #9
Source File: HttpClientIT.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost(webServer.getCallHttpUrl());
        post.addHeader("Content-Type", "application/json;charset=UTF-8");

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        httpClient.execute(post, responseHandler);
    } catch (Exception ignored) {
    } finally {
        if (null != httpClient && null != httpClient.getConnectionManager()) {
            httpClient.getConnectionManager().shutdown();
        }
    }

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Class<?> connectorClass;
    
    try {
        connectorClass = Class.forName("org.apache.http.impl.conn.ManagedClientConnectionImpl");
    } catch (ClassNotFoundException e) {
        connectorClass = Class.forName("org.apache.http.impl.conn.AbstractPooledConnAdapter");
    }
    
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", AbstractHttpClient.class.getMethod("execute", HttpUriRequest.class, ResponseHandler.class)));
    final String hostname = webServer.getHostAndPort();
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", connectorClass.getMethod("open", HttpRoute.class, HttpContext.class, HttpParams.class), annotation("http.internal.display", hostname)));
    verifier.verifyTrace(event("HTTP_CLIENT_4", HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class, HttpContext.class), null, null, hostname, annotation("http.url", "/"), annotation("http.status.code", 200), annotation("http.io", anyAnnotationValue())));
    verifier.verifyTraceCount(0);
}
 
Example #10
Source File: LyricsWSController.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
private String executeGetRequest(String url) throws IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(15000)
            .setSocketTimeout(15000)
            .build();
    HttpGet method = new HttpGet(url);
    method.setConfig(requestConfig);
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return client.execute(method, responseHandler);
    }
}
 
Example #11
Source File: AsyncEventHandlerTest.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that {@link IOException}s are caught, rather than being propagated (which would cause a worker
 * thread to die).
 */
@SuppressWarnings("unchecked")
@Test
public void testIOExceptionsCaughtInDispatch() throws Exception {
    AsyncEventHandler eventHandler = new AsyncEventHandler(mockHttpClient, MoreExecutors.newDirectExecutorService());

    // have the http client throw an IOException on execute
    when(mockHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class))).thenThrow(IOException.class);
    eventHandler.dispatchEvent(createLogEvent());
    verify(mockHttpClient).execute(any(HttpGet.class), any(ResponseHandler.class));
}
 
Example #12
Source File: SonosServiceRegistration.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
private String executeRequest(HttpUriRequest request) throws IOException {

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            return client.execute(request, responseHandler);

        }
    }
 
Example #13
Source File: FileGroupsClient.java    From LiquidDonkey with MIT License 5 votes vote down vote up
FileGroupsClient(
        ResponseHandler<ChunkServer.FileGroups> filesGroupsHandler,
        ResponseHandler<List<ICloud.MBSFileAuthToken>> mbsFileAuthTokenListHandler,
        Headers headers) {

    this.filesGroupsHandler = Objects.requireNonNull(filesGroupsHandler);
    this.mbsFileAuthTokenListHandler = Objects.requireNonNull(mbsFileAuthTokenListHandler);
    this.headers = Objects.requireNonNull(headers);
}
 
Example #14
Source File: DockerAccessWithHcClientTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void givenThatGetWillSucceedWithOk() throws IOException {
    new Expectations() {{
        mockDelegate.get(anyString, (ResponseHandler<Integer>) any, HTTP_OK, HTTP_NOT_FOUND);
        result = HTTP_OK;
    }};
}
 
Example #15
Source File: ApacheHttpClientDelegate.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public <T> T post(String url, Object body, Map<String, String> headers,
                  ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
    HttpUriRequest request = newPost(url, body);
    for (Entry<String, String> entry : headers.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());
    }

    return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
 
Example #16
Source File: TracedHttpClient.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(final HttpUriRequest request,
        final ResponseHandler<? extends T> responseHandler) throws IOException,
        ClientProtocolException {
    Subsegment subsegment = recorder.beginSubsegment(determineTarget(request).getHostName());
    return wrapHttpSupplier(subsegment, () -> {
        if (null != subsegment) {
            TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request));
        }
        TracedResponseHandler<? extends T> wrappedHandler = new TracedResponseHandler<>(responseHandler);
        T response = wrappedClient.execute(request, wrappedHandler);
        return response;
    });
}
 
Example #17
Source File: ApacheHttpClientDelegate.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public <T> T post(String url, Object body, Map<String, String> headers,
                  ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
    HttpUriRequest request = newPost(url, body);
    for (Entry<String, String> entry : headers.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());
    }

    return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
 
Example #18
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request,target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example #19
Source File: HttpClientAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Object[] enter(final Object arg0, final Object arg1, final Object arg2) {
  final HttpRequest request = arg0 instanceof HttpRequest ? (HttpRequest)arg0 : arg1 instanceof HttpRequest ? (HttpRequest)arg1 : null;
  if (request == null)
    return null;

  if (request.getHeaders("amz-sdk-invocation-id").length > 0) {
    // skip embedded Apache HttpClient in AWS SDK Client, because it breaks
    // request signature and AWS SDK gets traced by the aws-sdk rule
    return null;
  }

  final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME);
  if (context != null) {
    context.increment();
    return null;
  }

  final Tracer tracer = GlobalTracer.get();
  final Span span = tracer
    .buildSpan(request.getRequestLine().getMethod())
    .withTag(Tags.COMPONENT, COMPONENT_NAME)
    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
    .withTag(Tags.HTTP_METHOD, request.getRequestLine().getMethod())
    .withTag(Tags.HTTP_URL, request.getRequestLine().getUri()).start();

  for (final ApacheClientSpanDecorator decorator : Configuration.spanDecorators)
    decorator.onRequest(request, arg0 instanceof HttpHost ? (HttpHost)arg0 : null, span);

  LocalSpanContext.set(COMPONENT_NAME, span, null);

  tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request));
  if (arg1 instanceof ResponseHandler)
    return new Object[] {WrapperProxy.wrap(arg1, new TracingResponseHandler<>((ResponseHandler<?>)arg1, span))};

  if (arg2 instanceof ResponseHandler)
    return new Object[] {null, WrapperProxy.wrap(arg2, new TracingResponseHandler<>((ResponseHandler<?>)arg2, span))};

  return null;
}
 
Example #20
Source File: Renren.java    From java-Crawler with MIT License 5 votes vote down vote up
private String getText(String redirectLocation) {
    HttpGet httpget = new HttpGet(redirectLocation);
    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = "";
    try {
        responseBody = httpclient.execute(httpget, responseHandler);
    } catch (Exception e) {
        e.printStackTrace();
        responseBody = null;
    } finally {
        httpget.abort();
        httpclient.getConnectionManager().shutdown();
    }
    return responseBody;
}
 
Example #21
Source File: AbstractHttpCheck.java    From appstatus with Apache License 2.0 5 votes vote down vote up
protected String doHttpGet(String url) throws ClientProtocolException,
		IOException {

	HttpClient httpclient = new DefaultHttpClient();
	HttpGet httpget = new HttpGet(url);
	ResponseHandler<String> responseHandler = new BasicResponseHandler();

	try {
		String responseBody = httpclient.execute(httpget, responseHandler);
		return responseBody;
	} finally {
		httpclient.getConnectionManager().shutdown();
	}

}
 
Example #22
Source File: StreamClientImpl.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
protected ResponseHandler<StreamResponseMessage> createResponseHandler() {
    return new ResponseHandler<StreamResponseMessage>() {
        public StreamResponseMessage handleResponse(final HttpResponse httpResponse) throws IOException {

            StatusLine statusLine = httpResponse.getStatusLine();
            if (log.isLoggable(Level.FINE))
                log.fine("Received HTTP response: " + statusLine);

            // Status
            UpnpResponse responseOperation =
                new UpnpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase());

            // Message
            StreamResponseMessage responseMessage = new StreamResponseMessage(responseOperation);

            // Headers
            responseMessage.setHeaders(new UpnpHeaders(HeaderUtil.get(httpResponse)));

            // Body
            HttpEntity entity = httpResponse.getEntity();
            if (entity == null || entity.getContentLength() == 0) return responseMessage;

            if (responseMessage.isContentTypeMissingOrText()) {
                if (log.isLoggable(Level.FINE))
                    log.fine("HTTP response message contains text entity");
                responseMessage.setBody(UpnpMessage.BodyType.STRING, EntityUtils.toString(entity));
            } else {
                if (log.isLoggable(Level.FINE))
                    log.fine("HTTP response message contains binary entity");
                responseMessage.setBody(UpnpMessage.BodyType.BYTES, EntityUtils.toByteArray(entity));
            }

            return responseMessage;
        }
    };
}
 
Example #23
Source File: DockerAccessWithHcClientTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void givenThePushWillFailAndEventuallySucceed(final int retries) throws IOException {
    new Expectations() {{
        int fail = retries;
        mockDelegate.post(anyString, null, (Map<String, String>) any, (ResponseHandler) any,  200);
        minTimes = fail; maxTimes = fail;
        result = new HttpResponseException(HTTP_INTERNAL_ERROR, "error");
        mockDelegate.post(anyString, null, (Map<String, String>) any, (ResponseHandler) any, 200);
        minTimes = 1; maxTimes = 1;
    }};
}
 
Example #24
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 #25
Source File: RestSearchExecutorUtil.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static  <T> T _executeRequest(String httpPool,Map<String, String> headers,String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception {
	T response = null;
	if (entity == null){
		if(action == null)
			response = HttpRequestUtil.httpPostforString(httpPool,url, null, headers,  responseHandler);
		else if(action == ClientUtil.HTTP_POST )
			response = HttpRequestUtil.httpPostforString(httpPool,url, null, headers,  responseHandler);
		else if( action == ClientUtil.HTTP_PUT)
			response = HttpRequestUtil.httpPutforString(httpPool,url, null, headers,  responseHandler);
		else if(action == ClientUtil.HTTP_GET)
			response = HttpRequestUtil.httpGetforString(httpPool,url, headers,  responseHandler);
		else if(action == ClientUtil.HTTP_DELETE)
			response = HttpRequestUtil.httpDelete(httpPool,url, null, headers,  responseHandler);
		else if(action == ClientUtil.HTTP_HEAD)
			response = HttpRequestUtil.httpHead(httpPool,url, null, headers,  responseHandler);
		else
			throw new IllegalArgumentException("not support http action:"+action+",url:"+url);
	}
	else
	{
		if(action == ClientUtil.HTTP_POST )
			response = HttpRequestUtil.sendJsonBody(httpPool,entity, url, headers,  responseHandler);
		else if( action == ClientUtil.HTTP_PUT)
		{
			response = HttpRequestUtil.putJson(httpPool,entity, url, headers,  responseHandler);
		}
		else if(action == ClientUtil.HTTP_DELETE)
			response = HttpRequestUtil.httpDelete(httpPool,url, entity,null, headers,  responseHandler);
		else
			throw new IllegalArgumentException("not support http action:"+action+",url:"+url);

	}
	return response;
}
 
Example #26
Source File: HttpClientWrapper.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(final HttpUriRequest request,
		final ResponseHandler<? extends T> responseHandler) throws IOException,
		ClientProtocolException {
	final HttpContext context = new BasicHttpContext();
	return execute(request, responseHandler, context);
}
 
Example #27
Source File: RestSearchExecutor.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
/**

	 * @param entity
	 * @param action get,post,put,delete
	 * @return
	 * @throws ElasticSearchException
	 */
	public <T> T executeHttp(String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception {
//		return _executeHttp(  url,   entity,  action, responseHandler);
		Integer slowDslThreshold = elasticSearchClient.slowDslThreshold();
		if(slowDslThreshold == null) {
			return RestSearchExecutorUtil.__executeHttp(    httpPool,  (Map<String, String>)null,  url,   entity,  action,  responseHandler);
		}

		else{
			long start = System.currentTimeMillis();
			try {
				return RestSearchExecutorUtil.__executeHttp(    httpPool,  (Map<String, String>)null,  url,   entity,  action,  responseHandler);
			}
			finally {
				long end = System.currentTimeMillis();
				long time = end - start;
				if (time > slowDslThreshold.intValue()) {
					if (elasticSearchClient.getSlowDslCallback() == null) {
						if(logger.isWarnEnabled()) {
							logger.warn("Slow request[{}] action[{}] took time:{} ms > slowDslThreshold[{} ms], use DSL[{}]", url,action, time, slowDslThreshold.intValue(),  RestSearchExecutorUtil.chunkEntity(entity));

						}
					}else {
						SlowDsl slowDsl = new SlowDsl();
						slowDsl.setUrl(url);
						slowDsl.setAction(action);
						slowDsl.setTime(time);
						slowDsl.setSlowDslThreshold(slowDslThreshold);
						slowDsl.setEntity(entity);
						slowDsl.setStartTime(new Date(start));
						slowDsl.setEndTime(new Date(end));
						elasticSearchClient.getSlowDslCallback().slowDslHandle( slowDsl);
					}

				}
			}
		}
	}
 
Example #28
Source File: RestSearchExecutorUtil.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static <T> T __executeHttp(String httpPool,Map<String, String> headers,String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception{
	T response = null;
	if (entity == null) {
		if (action == null)
			response = HttpRequestUtil.httpPostforString(httpPool, url, null, headers, responseHandler);
		else if (action == ClientUtil.HTTP_POST)
			response = HttpRequestUtil.httpPostforString(httpPool, url, null, headers, responseHandler);
		else if (action == ClientUtil.HTTP_PUT)
			response = HttpRequestUtil.httpPutforString(httpPool, url, null, headers, responseHandler);
		else if (action == ClientUtil.HTTP_GET)
			response = HttpRequestUtil.httpGetforString(httpPool, url, headers, responseHandler);
		else if (action == ClientUtil.HTTP_DELETE)
			response = HttpRequestUtil.httpDelete(httpPool, url, null, headers, responseHandler);
		else if (action == ClientUtil.HTTP_HEAD)
			response = HttpRequestUtil.httpHead(httpPool, url, null, headers, responseHandler);
		else
			throw new IllegalArgumentException("not support http action:" + action+",url:"+url);
	} else {
		if (action == ClientUtil.HTTP_POST)
			response = HttpRequestUtil.sendJsonBody(httpPool, entity, url, headers, responseHandler);
		else if (action == ClientUtil.HTTP_PUT) {
			response = HttpRequestUtil.putJson(httpPool, entity, url, headers, responseHandler);
		} else if (action == ClientUtil.HTTP_DELETE)
			response = HttpRequestUtil.httpDelete(httpPool, url, entity, null, headers, responseHandler);
		else
			throw new IllegalArgumentException("not support http action:" + action+",url:"+url);
	}
	return response;
}
 
Example #29
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example #30
Source File: GetASINbyNode.java    From customer-review-crawler with The Unlicense 5 votes vote down vote up
/**
 * Returns a webpage's html code
 * @param weburl The URL to read webpage from
 * @return return a string that contains the HTML code of the webpage
 * @throws IOException
 */
public String readWebPage(String weburl) throws IOException{
	HttpClient httpclient = new DefaultHttpClient();
	//HttpProtocolParams.setUserAgent(httpclient.getParams(), "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
	HttpGet httpget = new HttpGet(weburl); 
	ResponseHandler<String> responseHandler = new BasicResponseHandler();    
	String responseBody = httpclient.execute(httpget, responseHandler);
	// responseBody now contains the contents of the page
	// System.out.println(responseBody);
	httpclient.getConnectionManager().shutdown();
	return responseBody;
}