Java 类org.apache.http.client.entity.EntityBuilder 实例源码

项目:HttpToolbox    文件:HttpClientExample.java   
/**
 * XXX 通过RequestBuilder.post构造的post请求的formurlencoded编码有问题(使用了默认的ISO-8859-1, 应该采用UTF-8),
 * 这会造成无法对中文参数进行正确的编码, 请使用setEntity来避免这个问题, 稍微丧失了RequestBuilder一点点易用性
 * 
 * @return
 */
private String requestBuilderPost() {
    HttpEntity entity = EntityBuilder.create()
                                     .setContentType(ContentType.APPLICATION_FORM_URLENCODED.withCharset(Consts.UTF_8))
                                     .setParameters(
                                          new BasicNameValuePair("a", "1"),
                                          new BasicNameValuePair("b", "中文test123")
                                      ).build();

    HttpUriRequest request = RequestBuilder.post()
                                           .setUri("http://www.baidu.com")
                                           .addHeader("User-Agent", "A")
                                           .addHeader("X-A", "AAA")
                                           // .addParameter("a", "1")
                                           // .addParameter("b", "中文test123")
                                           .setEntity(entity) // 使用setEntity来避免默认的URL编码问题
                                           .build();

    return executeRequest(request, true);
}
项目:bubble2    文件:HttpUtils.java   
private HttpUtils(HttpRequestBase request) {
    this.request = request;

    this.clientBuilder = HttpClientBuilder.create();
    this.isHttps = request.getURI().getScheme().equalsIgnoreCase("https");
    this.config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
    this.cookieStore = new BasicCookieStore();

    if (request instanceof HttpPost) {
        this.type = 1;
        this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>());

    } else if (request instanceof HttpGet) {
        this.type = 2;
        this.uriBuilder = new URIBuilder();

    } else if (request instanceof HttpPut) {
        this.type = 3;
        this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>());

    } else if (request instanceof HttpDelete) {
        this.type = 4;
        this.uriBuilder = new URIBuilder();
    }
}
项目:integration-test-helper    文件:AomHttpClient.java   
/**
 * Posts static data, either as image or as file
 *
 * @param content the content
 * @param isImage indicates whether this is an image or a file
 * @return request object to check status codes and return values
 */
public Response postStaticData( final byte[ ] content, final boolean isImage )
{
    final HttpPost request =
        new HttpPost( this.yambasBase + "apps/" + this.appName + "/data/" + ( isImage ? "images/" : "files/" ) );
    request.setEntity( EntityBuilder.create( ).setBinary( content ).build( ) );
    request.addHeader( "Content-Type", "application/octet-stream" );
    request.addHeader( "x-apiomat-apikey", this.apiKey );
    request.addHeader( "x-apiomat-system", this.system.toString( ) );
    try
    {
        final HttpResponse response = this.client.execute( request );
        return new Response( response );
    }
    catch ( final IOException e )
    {
        e.printStackTrace( );
    }
    return null;
}
项目:integration-test-helper    文件:AomHttpClient.java   
/**
 * Sends a request to yambas base URL + path. yambas base URL is: yambasHost + "/yambas/rest/"
 *
 * @param path
 *        the path
 * @param payLoad
 *        the payload as input stream
 * @return request object to check status codes and return values
 */
public Response postRequestRestEndpoint( String path, InputStream payLoad )
{
    final HttpPost request = new HttpPost( this.yambasBase + path );

    setAuthorizationHeader( request );
    request.addHeader( "ContentType", "application/json" );
    request.addHeader( "x-apiomat-apikey", getApiKey( ) );
    request.addHeader( "x-apiomat-system", getSystem( ).toString( ) );

    try
    {
        request.setEntity( EntityBuilder.create( ).setStream( payLoad ).build( ) );
        final HttpResponse response = this.client.execute( request );
        return new Response( response );
    }
    catch ( final IOException e )
    {
        e.printStackTrace( );
    }
    return null;
}
项目:integration-test-helper    文件:AomHttpClient.java   
/**
 * Imports a CSV dump into an existing app
 *
 * @param appName
 *        the Appname
 * @param data
 *        payload of CSV-zip
 *
 * @return request object to check status codes and return values
 */
public Response importCSVToApp( String appName, InputStream data )
{
    final HttpPost request = new HttpPost( this.yambasBase + "modules/csv/spec/" + appName );
    setAuthorizationHeader( request );
    request.addHeader( "x-apiomat-system", this.system.toString( ) );
    request.addHeader( "Content-Type", "application/octet-stream" );
    request.addHeader( "X-apiomat-apikey", this.apiKey );

    HttpEntity entity = EntityBuilder.create( ).setStream( data ).build( );
    request.setEntity( entity );
    try
    {
        final HttpResponse response = this.client.execute( request );
        return new Response( response );
    }
    catch ( final IOException e )
    {
        e.printStackTrace( );
    }
    return null;
}
项目:hbase    文件:TestHttpProxyExample.java   
@Test
public void test() throws Exception {
  try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
    HttpPut put = new HttpPut(
        String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER));
    put.setEntity(EntityBuilder.create().setText(VALUE)
        .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build());
    try (CloseableHttpResponse resp = client.execute(put)) {
      assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
    }
    HttpGet get = new HttpGet(
        String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER));
    try (CloseableHttpResponse resp = client.execute(get)) {
      assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
      assertEquals("value",
        Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent())));
    }
    get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever",
      FAMILY, QUALIFIER));
    try (CloseableHttpResponse resp = client.execute(get)) {
      assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode());
    }
  }
}
项目:khronus-java-client    文件:Sender.java   
public Sender(KhronusConfig config) {
    this.hosts = config.getHosts();
    this.endpoint = config.getEndpoint();

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(config.getMaxConnections());

    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager);

    this.httpClient = builder.build();
    this.responseHandler = new BasicResponseHandler();
    this.requestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .build();
    this.entityBuilder = EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).gzipCompress();
    LOG.debug("Sender to send metrics created [Hosts: {}; MaxConnections: {}; socketTimeout: {}; connectionRequestTimeout: {}; connectTimeout: {}]",
            config.getHosts(), config.getMaxConnections(), socketTimeout, connectionRequestTimeout, connectTimeout);
}
项目:ymate-module-wechat    文件:HttpClientHelper.java   
public String doPost(String url, String content) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {
        _LOG.debug("Request URL [" + url + "] PostBody [" + content + "]");
        String _result = _httpClient.execute(RequestBuilder.post()
                .setUri(url)
                .setEntity(EntityBuilder.create()
                        .setContentEncoding(DEFAULT_CHARSET)
                        .setContentType(ContentType.create("application/x-www-form-urlencoded", DEFAULT_CHARSET))
                        .setText(content).build()).build(), new ResponseHandler<String>() {

            public String handleResponse(HttpResponse response) throws IOException {
                return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
            }

        });
        _LOG.debug("Request URL [" + url + "] Response [" + _result + "]");
        return _result;
    } finally {
        _httpClient.close();
    }
}
项目:ymate-module-wechat    文件:HttpClientHelper.java   
public String doPost(String url, byte[] content) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {
        _LOG.debug("Request URL [" + url + "] PostBody [" + content + "]");
        String _result = _httpClient.execute(RequestBuilder.post()
                .setUri(url)
                .setEntity(EntityBuilder.create()
                        .setContentEncoding(DEFAULT_CHARSET)
                        .setContentType(ContentType.create("application/x-www-form-urlencoded", DEFAULT_CHARSET))
                        .setBinary(content).build())
                .build(), new ResponseHandler<String>() {

            public String handleResponse(HttpResponse response) throws IOException {
                return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
            }

        });
        _LOG.debug("Request URL [" + url + "] Response [" + _result + "]");
        return _result;
    } finally {
        _httpClient.close();
    }
}
项目:ymate-module-wechat    文件:HttpClientHelper.java   
public String doPost(String url, Map<String, String> params) throws Exception {
    CloseableHttpClient _httpClient = __doBuildHttpClient();
    try {
        _LOG.debug("Request URL [" + url + "] PostBody [" + params + "]");
        String _result = _httpClient.execute(RequestBuilder.post()
                .setUri(url)
                .setEntity(EntityBuilder.create()
                        .setContentEncoding(DEFAULT_CHARSET)
                        .setParameters(__doBuildNameValuePairs(params)).build()).build(), new ResponseHandler<String>() {

            public String handleResponse(HttpResponse response) throws IOException {
                return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
            }

        });
        _LOG.debug("Request URL [" + url + "] Response [" + _result + "]");
        return _result;
    } finally {
        _httpClient.close();
    }
}
项目:BenchmarkRunner    文件:GeoserverCommunicator.java   
public boolean createShapefileBackedDataStore(String workspaceName,
        String dataStoreName, String filePath) throws Exception {
    String geoserverHostAndPort = this.getGeoserverHostAndPort();

    logger.debug("About to load data from [" + filePath
            + "] into data store [" + dataStoreName + "] in workspace ["
            + workspaceName + "]");
    HttpPut request = new HttpPut("http://" + geoserverHostAndPort
            + "/geoserver/rest/workspaces/" + workspaceName
            + "/datastores/" + dataStoreName
            + "/file.shp?configure=all&target=shp");
    request.addHeader("Content-type", "application/zip");
    request.setEntity(EntityBuilder.create().setFile(new File(filePath))
            .build());

    return (Boolean) process(request, 201);
}
项目:Works-on-My-Box    文件:SuspectTest.java   
public void testCreateSuspect() throws IOException {
       CloseableHttpClient httpclient = HttpClients.createDefault();
       StringBuilder url = new StringBuilder(BASE_URL);
    url.append("suspect/");
    HttpPost httpPost = new HttpPost(url.toString());

       EntityBuilder entityBuilder = EntityBuilder.create();
       entityBuilder.setContentType(ContentType.APPLICATION_JSON);

    Suspect aaron = createMockSuspect("982c4a67-bb39-41f9-9872-88356822b7ad", "Aaron");
    JsonHelper jsonHelper = new JsonHelper();
    String jsonRequest = jsonHelper.convertToJson(aaron);

       entityBuilder.setText(jsonRequest);
       httpPost.setEntity(entityBuilder.build());
       CloseableHttpResponse response = httpclient.execute(httpPost);
    Assert.assertTrue(true);
}
项目:datasync    文件:HttpUtilityTest.java   
@Test
public void testHttpPost() throws Exception {
    URI postUri = new URIBuilder()
            .setScheme("https")
            .setHost(DOMAIN.split("//")[1])
            .setPath("/datasync/id/" + UNITTEST_DATASET_ID)
            .build();
    InputStream is = new FileInputStream(new File("src/test/resources/example_patch.sdiff"));
    byte[] data = IOUtils.toByteArray(is);
    HttpEntity entity = EntityBuilder.create().setBinary(data).build();

    try(CloseableHttpResponse response = http.post(postUri, entity);
        InputStream body = response.getEntity().getContent()) {
        // uncomment the test below, when di2 is running in prod
        /*
        TestCase.assertEquals(201, response.getStatusLine().getStatusCode());
        String blobId = mapper.readValue(response.getEntity().getContent(), BlobId.class).blobId;
        TestCase.assertNotNull(blobId);
        */
    }
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test
public void testExecRequestRetryOnAuthChallenge() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpClientContext context = new HttpClientContext();
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
    final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Huh?");
    final InputStream instream1 = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    response1.setEntity(EntityBuilder.create()
            .setStream(instream1)
            .build());
    final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
    final InputStream instream2 = Mockito.spy(new ByteArrayInputStream(new byte[] {2, 3, 4}));
    response2.setEntity(EntityBuilder.create()
            .setStream(instream2)
            .build());

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(managedConn.isStale()).thenReturn(Boolean.FALSE);
    Mockito.when(requestExecutor.execute(
            Mockito.same(request),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenReturn(response1, response2);
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.<HttpResponse>any(),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.when(targetAuthStrategy.isAuthenticationRequested(
            Mockito.eq(target),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);

    final CloseableHttpResponse finalResponse = mainClientExec.execute(
            route, request, context, execAware);
    Mockito.verify(requestExecutor, Mockito.times(2)).execute(request, managedConn, context);
    Mockito.verify(instream1).close();
    Mockito.verify(instream2, Mockito.never()).close();

    Assert.assertNotNull(finalResponse);
    Assert.assertEquals(200, finalResponse.getStatusLine().getStatusCode());
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test
public void testEstablishRouteViaProxyTunnelRetryOnAuthChallengePersistentConnection() throws Exception {
    final AuthState authState = new AuthState();
    final HttpRoute route = new HttpRoute(target, null, proxy, true);
    final HttpClientContext context = new HttpClientContext();
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
    final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Huh?");
    final InputStream instream1 = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    response1.setEntity(EntityBuilder.create()
            .setStream(instream1)
            .build());
    final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(proxyAuthStrategy.isAuthenticationRequested(
            Mockito.eq(proxy),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.<HttpResponse>any(),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.when(requestExecutor.execute(
            Mockito.<HttpRequest>any(),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenReturn(response1, response2);

    mainClientExec.establishRoute(authState, managedConn, route, request, context);

    Mockito.verify(connManager).connect(managedConn, route, 0, context);
    Mockito.verify(connManager).routeComplete(managedConn, route, context);
    Mockito.verify(instream1).close();
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test
public void testEstablishRouteViaProxyTunnelRetryOnAuthChallengeNonPersistentConnection() throws Exception {
    final AuthState authState = new AuthState();
    final HttpRoute route = new HttpRoute(target, null, proxy, true);
    final HttpClientContext context = new HttpClientContext();
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
    final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Huh?");
    final InputStream instream1 = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    response1.setEntity(EntityBuilder.create()
            .setStream(instream1)
            .build());
    final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(proxyAuthStrategy.isAuthenticationRequested(
            Mockito.eq(proxy),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.<HttpResponse>any(),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.FALSE);
    Mockito.when(requestExecutor.execute(
            Mockito.<HttpRequest>any(),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenReturn(response1, response2);

    mainClientExec.establishRoute(authState, managedConn, route, request, context);

    Mockito.verify(connManager).connect(managedConn, route, 0, context);
    Mockito.verify(connManager).routeComplete(managedConn, route, context);
    Mockito.verify(instream1, Mockito.never()).close();
    Mockito.verify(managedConn).close();
}
项目:purecloud-iot    文件:TestRedirectExec.java   
@Test(expected = ProtocolException.class)
public void testRedirectProtocolException() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpGet get = new HttpGet("/test");
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(get);
    final HttpClientContext context = HttpClientContext.create();

    final CloseableHttpResponse response1 = Mockito.mock(CloseableHttpResponse.class);
    final InputStream instream1 = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    final HttpEntity entity1 = EntityBuilder.create()
            .setStream(instream1)
            .build();
    Mockito.when(response1.getEntity()).thenReturn(entity1);
    Mockito.when(requestExecutor.execute(
            Mockito.eq(route),
            Mockito.same(request),
            Mockito.<HttpClientContext>any(),
            Mockito.<HttpExecutionAware>any())).thenReturn(response1);
    Mockito.when(redirectStrategy.isRedirected(
            Mockito.same(get),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.doThrow(new ProtocolException("Oppsie")).when(redirectStrategy).getRedirect(
            Mockito.same(get),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any());

    try {
        redirectExec.execute(route, request, context, execAware);
    } catch (final Exception ex) {
        Mockito.verify(instream1).close();
        Mockito.verify(response1).close();
        throw ex;
    }
}
项目:callfire-api-client-java    文件:AbstractApiTest.java   
protected ArgumentCaptor<HttpUriRequest> mockHttpResponse(String responseJson, Integer responseCode)
    throws Exception {
    when(mockHttpResponse.getStatusLine()).thenReturn(getStatusForCode(responseCode));
    if (responseJson != null) {
        when(mockHttpResponse.getEntity()).thenReturn(EntityBuilder.create().setText(responseJson).build());
    }

    ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    doReturn(mockHttpResponse).when(mockHttpClient).execute(captor.capture());

    return captor;
}
项目:Plugins    文件:HttpRequestExecutor.java   
/**
     * Executes FILE (binary) HTTP POST request based on configuration
     * 
     * @param config
     *            DPU configuration
     * @param client
     *            HTTP client used to execute request
     * @return HTTP response
     * @throws Exception
     *             if request execution fails
     */
    public CloseableHttpResponse sendFilePostRequest(HttpRequestConfig_V1 config, File file, CloseableHttpClient client) throws Exception {
        CloseableHttpResponse response = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
            uriBuilder.setPath(uriBuilder.getPath());

            HttpPost request = new HttpPost(uriBuilder.build().normalize());
//            if (rdfConfig.isUseAuthentication()) {
//                addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
//            }

            EntityBuilder builder = EntityBuilder.create();
            builder.setContentEncoding(config.getCharset());

            //ContentType contentType = ContentType.DEFAULT_BINARY;
            ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
            builder.setFile(file);
            builder.setContentType(contentType);

            HttpEntity entity = builder.build();
            request.setEntity(entity);
            request.addHeader("Content-Type", contentType.toString());

            LOG.info("Request: {}", request.toString());

            response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
            //response = client.execute(request);
            checkHttpResponseStatus(response);

        } catch (URISyntaxException | IllegalStateException | IOException ex) {
            String errorMsg = String.format("Failed to execute HTTP file POST request to URL %s", config.getRequestURL());
            LOG.error(errorMsg, ex);
            throw new Exception(errorMsg, ex);
        }
        return response;

    }
项目:Plugins    文件:HttpRequestExecutor.java   
/**
     * Executes FILE (binary) HTTP PUT request based on configuration
     *
     * @param config
     *            DPU configuration
     * @param client
     *            HTTP client used to execute request
     * @return HTTP response
     * @throws Exception
     *             if request execution fails
     */
    public CloseableHttpResponse sendFilePutRequest(HttpRequestConfig_V1 config, File file, CloseableHttpClient client) throws Exception {
        CloseableHttpResponse response = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
            uriBuilder.setPath(uriBuilder.getPath());

            HttpPut request = new HttpPut(uriBuilder.build().normalize());
//            if (rdfConfig.isUseAuthentication()) {
//                addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
//            }

            EntityBuilder builder = EntityBuilder.create();
            builder.setContentEncoding(config.getCharset());

            //ContentType contentType = ContentType.DEFAULT_BINARY;
            ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
            builder.setFile(file);
            builder.setContentType(contentType);

            HttpEntity entity = builder.build();
            request.setEntity(entity);
            request.addHeader("Content-Type", contentType.toString());

            LOG.info("Request: {}", request.toString());

            response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
            //response = client.execute(request);
            checkHttpResponseStatus(response);

        } catch (URISyntaxException | IllegalStateException | IOException ex) {
            String errorMsg = String.format("Failed to execute HTTP file PUT request to URL %s", config.getRequestURL());
            LOG.error(errorMsg, ex);
            throw new Exception(errorMsg, ex);
        }
        return response;

    }
项目:Plugins    文件:HttpRequestExecutor.java   
/**
     * Executes RAW data (text data) HTTP POST request based on configuration
     * 
     * @param config
     *            DPU configuration
     * @param client
     *            HTTP client used to execute request
     * @return HTTP response
     * @throws Exception
     *             if request execution fails
     */
    public CloseableHttpResponse sendRawDataPostRequest(HttpRequestConfig_V1 config, CloseableHttpClient client) throws Exception {
        CloseableHttpResponse response = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
            uriBuilder.setPath(uriBuilder.getPath());

            HttpPost request = new HttpPost(uriBuilder.build().normalize());
//            if (rdfConfig.isUseAuthentication()) {
//                addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
//            }

            EntityBuilder builder = EntityBuilder.create();
            builder.setContentEncoding(config.getCharset());

            ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
            builder.setText(config.getRawRequestBody());
            builder.setContentType(contentType);

            HttpEntity entity = builder.build();
            request.setEntity(entity);
            request.addHeader("Content-Type", contentType.toString());

            response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
//            response = client.execute(request);
            checkHttpResponseStatus(response);

        } catch (URISyntaxException | IllegalStateException | IOException ex) {
            String errorMsg = String.format("Failed to execute HTTP raw POST request to URL %s", config.getRequestURL());
            LOG.error(errorMsg);
            throw new Exception(errorMsg, ex);
        }
        return response;
    }
项目:Plugins    文件:HttpRequestExecutor.java   
/**
     * Executes RAW data (text data) HTTP PUT request based on configuration
     *
     * @param config
     *            DPU configuration
     * @param client
     *            HTTP client used to execute request
     * @return HTTP response
     * @throws Exception
     *             if request execution fails
     */
    public CloseableHttpResponse sendRawDataPutRequest(HttpRequestConfig_V1 config, CloseableHttpClient client) throws Exception {
        CloseableHttpResponse response = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(config.getRequestURL());
            uriBuilder.setPath(uriBuilder.getPath());

            HttpPut request = new HttpPut(uriBuilder.build().normalize());
//            if (rdfConfig.isUseAuthentication()) {
//                addBasiAuthenticationForHttpRequest(request, rdfConfig.getUserName(), rdfConfig.getPassword());
//            }

            EntityBuilder builder = EntityBuilder.create();
            builder.setContentEncoding(config.getCharset());

            ContentType contentType = ContentType.create(config.getContentType().getDescription()).withCharset(config.getCharset());
            builder.setText(config.getRawRequestBody());
            builder.setContentType(contentType);

            HttpEntity entity = builder.build();
            request.setEntity(entity);
            request.addHeader("Content-Type", contentType.toString());

            response = this.httpWrapper.getClient().execute(this.httpWrapper.getHost(), request, this.httpWrapper.getContext());
//            response = client.execute(request);
            checkHttpResponseStatus(response);

        } catch (URISyntaxException | IllegalStateException | IOException ex) {
            String errorMsg = String.format("Failed to execute HTTP raw PUT request to URL %s", config.getRequestURL());
            LOG.error(errorMsg);
            throw new Exception(errorMsg, ex);
        }
        return response;
    }
项目:BenchmarkRunner    文件:GeoserverCommunicator.java   
public boolean createWorkspace(String workspaceName) throws Exception {
    String geoserverHostAndPort = this.getGeoserverHostAndPort();

    logger.debug("About to create workspace: " + workspaceName);
    HttpPost request = new HttpPost("http://" + geoserverHostAndPort
            + "/geoserver/rest/workspaces");
    request.addHeader("Accept", ContentType.APPLICATION_JSON.toString());
    request.setEntity(EntityBuilder.create()
            .setText("{'workspace': {'name': '" + workspaceName + "'}}")
            .setContentType(ContentType.APPLICATION_JSON).build());

    return (Boolean) process(request, 201);
}
项目:datasync    文件:DeltaImporter2Publisher.java   
/**
 * Chunks up the signature patch file into ~4MB chunks and posts these to delta-importer-2
 * @param patchStream an inputStream to the patch
 * @param datasetId the 4x4 of the dataset being patched
 * @return the list of blobIds corresponding to each successful post
 */
private List<String> postPatchBlobs(InputStream patchStream, String datasetId, int chunkSize) throws
        IOException, URISyntaxException, HttpException {
    System.out.println("Chunking and posting the diff");

    URI postingPath = baseUri.setPath(datasyncPath + "/" + datasetId).build();
    List<String> blobIds = new LinkedList<>();
    byte[] bytes = new byte[chunkSize];
    StatusLine statusLine;
    int status;
    int bytesRead;

    while ((bytesRead = Utils.readChunk(patchStream, bytes, 0, bytes.length)) != -1) {
        System.out.println("\tUploading " + bytesRead + " bytes of the diff");
        byte[] chunk = bytesRead == bytes.length ? bytes : Arrays.copyOf(bytes, bytesRead);
        HttpEntity entity = EntityBuilder.create().setBinary(chunk).build();
        int retries = 0;
        do {
            try(CloseableHttpResponse response = http.post(postingPath, entity)) {
                statusLine = response.getStatusLine();
                status = statusLine.getStatusCode();
                if (status != HttpStatus.SC_CREATED) {
                    retries += 1;
                } else {
                    String blobId = mapper.readValue(response.getEntity().getContent(), BlobId.class).blobId;
                    blobIds.add(blobId);
                }
            }
        } while (status != HttpStatus.SC_CREATED && retries < httpRetries);
        //We hit the max number of retries without success and should throw an exception accordingly.
        if (retries >= httpRetries) throw new HttpException(statusLine.toString());
        System.out.println("\tUploaded " + bytesRead + " bytes");
    }
    return blobIds;
}
项目:matrix-java-sdk    文件:AMatrixHttpClient.java   
protected HttpEntity getJsonEntity(Object o) {
    return EntityBuilder.create().setText(gson.toJson(o)).setContentType(ContentType.APPLICATION_JSON).build();
}
项目:mxhsd    文件:HttpFederationClient.java   
protected HttpEntity getJsonEntity(Object o) {
    return EntityBuilder.create()
            .setText(GsonUtil.get().toJson(o))
            .setContentType(ContentType.APPLICATION_JSON)
            .build();
}
项目:Telejam    文件:SendSticker.java   
@Override
public HttpEntity getHttpEntity() {
  if (newSticker != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (chatId != null)
      builder.addTextBody(CHAT_ID_FIELD, chatId);

    if (disableNotification != null)
      builder.addTextBody(DISABLE_NOTIFICATION_FIELD, disableNotification.toString());

    if (replyToMessageId != null)
      builder.addTextBody(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString());

    if (replyMarkup != null)
      builder.addTextBody(REPLY_MARKUP_FIELD, replyMarkup.toString());

    builder.addBinaryBody(STICKER_FIELD, newSticker, ContentType.APPLICATION_OCTET_STREAM, fileName);

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (chatId != null)
      params.add(
          new BasicNameValuePair(CHAT_ID_FIELD, chatId)
      );

    if (disableNotification != null)
      params.add(
          new BasicNameValuePair(DISABLE_NOTIFICATION_FIELD, disableNotification.toString())
      );

    if (replyToMessageId != null)
      params.add(
          new BasicNameValuePair(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString())
      );

    if (replyMarkup != null)
      params.add(
          new BasicNameValuePair(REPLY_MARKUP_FIELD, replyMarkup.toString())
      );

    if (sticker != null)
      params.add(
          new BasicNameValuePair(STICKER_FIELD, sticker)
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:SendVideoNote.java   
@Override
public HttpEntity getHttpEntity() {
  if (newVideo != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (chatId != null)
      builder.addTextBody(CHAT_ID_FIELD, chatId);

    if (disableNotification != null)
      builder.addTextBody(DISABLE_NOTIFICATION_FIELD, disableNotification.toString());

    if (replyToMessageId != null)
      builder.addTextBody(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString());

    if (replyMarkup != null)
      builder.addTextBody(REPLY_MARKUP_FIELD, replyMarkup.toString());

    builder.addBinaryBody(VIDEO_FIELD, newVideo, ContentType.APPLICATION_OCTET_STREAM, fileName);

    if (size != null)
      builder.addTextBody(SIZE_FIELD, size.toString());

    if (duration != null)
      builder.addTextBody(DURATION_FIELD, duration.toString());

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (chatId != null)
      params.add(
          new BasicNameValuePair(CHAT_ID_FIELD, chatId)
      );

    if (disableNotification != null)
      params.add(
          new BasicNameValuePair(DISABLE_NOTIFICATION_FIELD, disableNotification.toString())
      );

    if (replyToMessageId != null)
      params.add(
          new BasicNameValuePair(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString())
      );

    if (replyMarkup != null)
      params.add(
          new BasicNameValuePair(REPLY_MARKUP_FIELD, replyMarkup.toString())
      );

    if (video != null)
      params.add(
          new BasicNameValuePair(VIDEO_FIELD, video)
      );

    if (size != null)
      params.add(
          new BasicNameValuePair(SIZE_FIELD, size.toString())
      );

    if (duration != null)
      params.add(
          new BasicNameValuePair(DURATION_FIELD, duration.toString())
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:SendDocument.java   
@Override
public HttpEntity getHttpEntity() {
  if (newFile != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (chatId != null)
      builder.addTextBody(CHAT_ID_FIELD, chatId);

    if (disableNotification != null)
      builder.addTextBody(DISABLE_NOTIFICATION_FIELD, disableNotification.toString());

    if (replyToMessageId != null)
      builder.addTextBody(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString());

    if (replyMarkup != null)
      builder.addTextBody(REPLY_MARKUP_FIELD, replyMarkup.toString());

    builder.addBinaryBody(FILE_FIELD, newFile, ContentType.APPLICATION_OCTET_STREAM, fileName);

    if (caption != null)
      builder.addTextBody(CAPTION_FIELD, caption);

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (chatId != null)
      params.add(
          new BasicNameValuePair(CHAT_ID_FIELD, chatId)
      );

    if(disableNotification != null)
      params.add(
          new BasicNameValuePair(DISABLE_NOTIFICATION_FIELD, disableNotification.toString())
      );

    if(replyToMessageId != null)
      params.add(
          new BasicNameValuePair(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString())
      );

    if(replyMarkup != null)
      params.add(
          new BasicNameValuePair(REPLY_MARKUP_FIELD, replyMarkup.toString())
      );

    if (file != null)
      params.add(
          new BasicNameValuePair(FILE_FIELD, file)
      );

    if (caption != null)
      params.add(
          new BasicNameValuePair(CAPTION_FIELD, caption)
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:CreateNewStickerSet.java   
@Override
public HttpEntity getHttpEntity() {
  if (newSticker != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (userId != null)
      builder.addTextBody(USER_ID_FIELD, userId.toString());

    if (name != null)
      builder.addTextBody(NAME, name);

    if (title != null)
      builder.addTextBody(TITLE_FIELD, title);

    builder.addBinaryBody(STICKER_FIELD, newSticker);

    if (emojis != null)
      builder.addTextBody(EMOJIS_FIELD, emojis);

    if (containsMasks != null)
      builder.addTextBody(CONTAINS_MASKS_FIELD, containsMasks.toString());

    if (maskPosition != null)
      builder.addTextBody(MASK_POSITION_FIELD, maskPosition.toJson());

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (userId != null)
      params.add(
          new BasicNameValuePair(USER_ID_FIELD, userId.toString())
      );

    if (name != null)
      params.add(
          new BasicNameValuePair(NAME_FIELD, name)
      );

    if (title != null)
      params.add(
          new BasicNameValuePair(TITLE_FIELD, title)
      );

    if (sticker != null)
      params.add(
          new BasicNameValuePair(STICKER_FIELD, sticker)
      );

    if (emojis != null)
      params.add(
          new BasicNameValuePair(EMOJIS_FIELD, emojis)
      );

    if (containsMasks != null)
      params.add(
          new BasicNameValuePair(CONTAINS_MASKS_FIELD, containsMasks.toString())
      );

    if (maskPosition != null)
      params.add(
          new BasicNameValuePair(MASK_POSITION_FIELD, maskPosition.toJson())
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:SendVoice.java   
@Override
public HttpEntity getHttpEntity() {
  if (newVoice != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (chatId != null)
      builder.addTextBody(CHAT_ID_FIELD, chatId);

    if (disableNotification != null)
      builder.addTextBody(DISABLE_NOTIFICATION_FIELD, disableNotification.toString());

    if (replyToMessageId != null)
      builder.addTextBody(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString());

    if (replyMarkup != null)
      builder.addTextBody(REPLY_MARKUP_FIELD, replyMarkup.toString());

    builder.addBinaryBody(VOICE_FIELD, newVoice, ContentType.APPLICATION_OCTET_STREAM, fileName);

    if (caption != null)
      builder.addTextBody(CAPTION_FIELD, caption);

    if (duration != null)
      builder.addTextBody(DURATION_FIELD, duration.toString());

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (chatId != null)
      params.add(
          new BasicNameValuePair(CHAT_ID_FIELD, chatId)
      );

    if(disableNotification != null)
      params.add(
          new BasicNameValuePair(DISABLE_NOTIFICATION_FIELD, disableNotification.toString())
      );

    if(replyToMessageId != null)
      params.add(
          new BasicNameValuePair(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString())
      );

    if(replyMarkup != null)
      params.add(
          new BasicNameValuePair(REPLY_MARKUP_FIELD, replyMarkup.toString())
      );

    if (voice != null)
      params.add(
          new BasicNameValuePair(VOICE_FIELD, voice)
      );

    if (caption != null)
      params.add(
          new BasicNameValuePair(CAPTION_FIELD, caption)
      );

    if (duration != null)
      params.add(
          new BasicNameValuePair(DURATION_FIELD, duration.toString())
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:AddStickerToSet.java   
@Override
public HttpEntity getHttpEntity() {
  if (newSticker) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (userId != null)
      builder.addTextBody(USER_ID_FIELD, userId.toString());

    if (name != null)
      builder.addTextBody(NAME, name);

    if (sticker != null)
      builder.addBinaryBody(STICKER_FIELD, new File(sticker));

    if (emojis != null)
      builder.addTextBody(EMOJIS_FIELD, emojis);

    if (maskPosition != null)
      builder.addTextBody(MASK_POSITION_FIELD, maskPosition.toJson());

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (userId != null)
      params.add(
          new BasicNameValuePair(USER_ID_FIELD, userId.toString())
      );

    if(name != null)
      params.add(
          new BasicNameValuePair(NAME_FIELD, name)
      );

    if(sticker != null)
      params.add(
          new BasicNameValuePair(STICKER_FIELD, sticker)
      );

    if (emojis != null)
      params.add(
          new BasicNameValuePair(EMOJIS_FIELD, emojis)
      );

    if (maskPosition != null)
      params.add(
          new BasicNameValuePair(MASK_POSITION_FIELD, maskPosition.toJson())
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:Telejam    文件:SendPhoto.java   
@Override
public HttpEntity getHttpEntity() {
  if (newPhoto != null) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    if (chatId != null)
      builder.addTextBody(CHAT_ID_FIELD, chatId);

    if (disableNotification != null)
      builder.addTextBody(DISABLE_NOTIFICATION_FIELD, disableNotification.toString());

    if (replyToMessageId != null)
      builder.addTextBody(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString());

    if (replyMarkup != null)
      builder.addTextBody(REPLY_MARKUP_FIELD, replyMarkup.toString());

    builder.addBinaryBody(PHOTO_FIELD, newPhoto, ContentType.APPLICATION_OCTET_STREAM, fileName);

    if (caption != null)
      builder.addTextBody(CAPTION_FIELD, caption);

    return builder.build();
  } else {
    List<NameValuePair> params = new ArrayList<>();

    if (chatId != null)
      params.add(
          new BasicNameValuePair(CHAT_ID_FIELD, chatId)
      );

    if(disableNotification != null)
      params.add(
          new BasicNameValuePair(DISABLE_NOTIFICATION_FIELD, disableNotification.toString())
      );

    if(replyToMessageId != null)
      params.add(
          new BasicNameValuePair(REPLY_TO_MESSAGE_ID_FIELD, replyToMessageId.toString())
      );

    if(replyMarkup != null)
      params.add(
          new BasicNameValuePair(REPLY_MARKUP_FIELD, replyMarkup.toString())
      );

    if (photo != null)
      params.add(
          new BasicNameValuePair(PHOTO_FIELD, photo)
      );

    if (caption != null)
      params.add(
          new BasicNameValuePair(CAPTION_FIELD, caption)
      );

    return EntityBuilder.create()
        .setParameters(params)
        .build();
  }
}
项目:illuminati    文件:ESclientImpl.java   
private HttpEntity getHttpEntity(final IlluminatiEsModel entity) {
    return EntityBuilder.create().setText(entity.getJsonString()).setContentType(this.contentType).build();
}
项目:instagram4j    文件:InstagramUploadVideoJobRequest.java   
@Override
public StatusResult execute() throws ClientProtocolException, IOException {

    String url = getUrl();
    log.info("URL Upload: " + url);

    HttpPost post = new HttpPost(url);
    post.addHeader("X-IG-Capabilities", "3Q4=");
    post.addHeader("X-IG-Connection-Type", "WIFI");
    post.addHeader("Cookie2", "$Version=1");
    post.addHeader("Accept-Language", "en-US");
    post.addHeader("Accept-Encoding", "gzip, deflate");
    post.addHeader("Content-Type", "application/octet-stream");
    post.addHeader("Session-ID", uploadId);
    post.addHeader("Connection", "keep-alive");
    post.addHeader("Content-Disposition", "attachment; filename=\"video.mp4\"");
    post.addHeader("job", uploadJob);
    post.addHeader("Host", "upload.instagram.com");
    post.addHeader("User-Agent", InstagramConstants.USER_AGENT);

    log.info("User-Agent: " + InstagramConstants.USER_AGENT);

    try (FileInputStream is = new FileInputStream(videoFile)) {
        byte[] videoData = MyStreamUtils.readContentBytes(is);

        //TODO: long ranges? need to handle?
        int requestSize = (int) Math.floor(videoData.length / 4.0);
        int lastRequestExtra = (int) (videoData.length - (requestSize * 3));


        for (int i = 0; i < 4; i++) {

            int start = i * requestSize;
            int end;
            if (i == 3) {
                end = i * requestSize + lastRequestExtra;
            } else {
                end = (i + 1) * requestSize;
            }

            int actualLength = (i == 3 ? lastRequestExtra : requestSize);

            String contentRange = String.format("bytes %s-%s/%s", start, end - 1, videoData.length);
            //post.setHeader("Content-Length", String.valueOf(end - start));
            post.setHeader("Content-Range", contentRange);

            byte[] range = Arrays.copyOfRange(videoData, start, start + actualLength);
            log.info("Total is " + videoData.length + ", sending " + actualLength + " (starting from " + start + ") -- " + range.length + " bytes.");

            post.setEntity(EntityBuilder.create().setBinary(range).build());

            try (CloseableHttpResponse response = api.getClient().execute(post)) {
                int resultCode = response.getStatusLine().getStatusCode();
                String content = EntityUtils.toString(response.getEntity());
                log.info("Result of part " + i + ": " + content);

                post.releaseConnection();
                response.close();

                if (resultCode != 200 && resultCode != 201) {
                    throw new IllegalStateException("Failed uploading video (" + resultCode + "): " + content);
                }

            }

        }

        return new StatusResult("ok");
    }
}
项目:SignPicture    文件:GooglShortener.java   
@Override
public void communicate() {
    final String url = "https://www.googleapis.com/urlshortener/v1/url?key=%s";

    InputStream resstream = null;
    JsonReader jsonReader1 = null;
    try {
        setCurrent();
        // create the get request.
        final HttpPost httppost = new HttpPost(String.format(url, this.key));
        final EntityBuilder builder = EntityBuilder.create();

        final String reqjson = Client.gson.toJson(new GooglRequest(this.shortreq.getLongURL()));
        builder.setContentType(ContentType.APPLICATION_JSON);
        builder.setText(reqjson);
        httppost.setEntity(builder.build());

        // execute request
        final HttpResponse response = Downloader.downloader.client.execute(httppost);

        if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
            final HttpEntity resEntity = response.getEntity();
            if (resEntity!=null) {
                resstream = resEntity.getContent();
                this.result = Client.gson.<GooglResult> fromJson(jsonReader1 = new JsonReader(new InputStreamReader(resstream, Charsets.UTF_8)), GooglResult.class);
                onDone(new CommunicateResponse(true, null));
                return;
            }
        } else {
            onDone(new CommunicateResponse(false, new IOException("Bad Response")));
            return;
        }
    } catch (final Exception e) {
        onDone(new CommunicateResponse(false, e));
        return;
    } finally {
        unsetCurrent();
        IOUtils.closeQuietly(resstream);
        IOUtils.closeQuietly(jsonReader1);
    }
    onDone(new CommunicateResponse(false, null));
    return;
}
项目:CurseLib    文件:ApiRequester.java   
public ApiResponse doPost(String url, NameValuePair... params) throws ClientProtocolException, IOException, URISyntaxException {
    mPostRequest.setEntity(EntityBuilder.create().setParameters(params).build());
    return doRequest(url, mPostRequest);
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test
public void testExecRequestConnectionRelease() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpClientContext context = new HttpClientContext();
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
    // The entity is streaming
    response.setEntity(EntityBuilder.create()
            .setStream(new ByteArrayInputStream(new byte[]{}))
            .build());

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(managedConn.isStale()).thenReturn(Boolean.FALSE);
    Mockito.when(requestExecutor.execute(
            Mockito.same(request),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenReturn(response);
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.same(response),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.FALSE);

    final CloseableHttpResponse finalResponse = mainClientExec.execute(
            route, request, context, execAware);
    Mockito.verify(connManager).requestConnection(route, null);
    Mockito.verify(connRequest).get(0, TimeUnit.MILLISECONDS);
    Mockito.verify(requestExecutor, Mockito.times(1)).execute(request, managedConn, context);
    Mockito.verify(connManager, Mockito.never()).releaseConnection(
            Mockito.same(managedConn),
            Mockito.any(),
            Mockito.anyInt(),
            Mockito.<TimeUnit>any());
    Mockito.verify(managedConn, Mockito.never()).close();

    Assert.assertNotNull(finalResponse);
    Assert.assertTrue(finalResponse instanceof HttpResponseProxy);
    finalResponse.close();

    Mockito.verify(connManager, Mockito.times(1)).releaseConnection(
            managedConn, null, 0, TimeUnit.MILLISECONDS);
    Mockito.verify(managedConn, Mockito.times(1)).close();
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test
public void testExecEntityEnclosingRequestRetryOnAuthChallenge() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
    final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Huh?");
    final InputStream instream1 = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    response1.setEntity(EntityBuilder.create()
            .setStream(instream1)
            .build());
    final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
    final InputStream instream2 = Mockito.spy(new ByteArrayInputStream(new byte[] {2, 3, 4}));
    response2.setEntity(EntityBuilder.create()
            .setStream(instream2)
            .build());

    final AuthState proxyAuthState = new AuthState();
    proxyAuthState.setState(AuthProtocolState.SUCCESS);
    proxyAuthState.update(new NTLMScheme(), new NTCredentials("user:pass"));

    final HttpClientContext context = new HttpClientContext();
    context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, proxyAuthState);

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(managedConn.isStale()).thenReturn(Boolean.FALSE);
    Mockito.when(requestExecutor.execute(
            Mockito.same(request),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenReturn(response1, response2);
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.<HttpResponse>any(),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.FALSE);
    Mockito.when(targetAuthStrategy.isAuthenticationRequested(
            Mockito.eq(target),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);

    final CloseableHttpResponse finalResponse = mainClientExec.execute(
            route, request, context, execAware);
    Mockito.verify(requestExecutor, Mockito.times(2)).execute(request, managedConn, context);
    Mockito.verify(managedConn).close();
    Mockito.verify(instream2, Mockito.never()).close();

    Assert.assertNotNull(finalResponse);
    Assert.assertEquals(200, finalResponse.getStatusLine().getStatusCode());
    Assert.assertNull(proxyAuthState.getAuthScheme());
    Assert.assertNull(proxyAuthState.getCredentials());
}
项目:purecloud-iot    文件:TestMainClientExec.java   
@Test(expected = NonRepeatableRequestException.class)
public void testExecEntityEnclosingRequest() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpClientContext context = new HttpClientContext();
    final HttpPost post = new HttpPost("http://bar/test");
    final InputStream instream0 = new ByteArrayInputStream(new byte[] {1, 2, 3});
    post.setEntity(EntityBuilder.create()
            .setStream(instream0)
            .build());
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(post);

    final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Huh?");
    final InputStream instream1 = new ByteArrayInputStream(new byte[] {1, 2, 3});
    response1.setEntity(EntityBuilder.create()
            .setStream(instream1)
            .build());

    Mockito.when(managedConn.isOpen()).thenReturn(Boolean.TRUE);
    Mockito.when(managedConn.isStale()).thenReturn(Boolean.FALSE);
    Mockito.when(requestExecutor.execute(
            Mockito.same(request),
            Mockito.<HttpClientConnection>any(),
            Mockito.<HttpClientContext>any())).thenAnswer(new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(final InvocationOnMock invocationOnMock) throws Throwable {
            final Object[] args = invocationOnMock.getArguments();
            final HttpEntityEnclosingRequest requestEE = (HttpEntityEnclosingRequest) args[0];
            requestEE.getEntity().writeTo(new ByteArrayOutputStream());
            return response1;
        }

    });
    Mockito.when(reuseStrategy.keepAlive(
            Mockito.<HttpResponse>any(),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
    Mockito.when(targetAuthStrategy.isAuthenticationRequested(
            Mockito.eq(target),
            Mockito.same(response1),
            Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);

    mainClientExec.execute(route, request, context, execAware);
}