SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).setHeader("Custom", "custom").setListener(listener).build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10);
@Override protected void initHttpHeaders( XmlRpcRequest xmlRpcRequest ) throws XmlRpcClientException { if ( !XmlRpcClientConfigImpl.class.isAssignableFrom( xmlRpcRequest.getConfig().getClass() ) ) { throw new XmlRpcClientException( "Invalid XmlRpcRequest", null ); } XmlRpcClientConfigImpl config = XmlRpcClientConfigImpl.class.cast( xmlRpcRequest.getConfig() ); super.initHttpHeaders( xmlRpcRequest ); configureProxy( config ); configureTimeout( config ); if ( config.getSslContext() != null ) { builder.setSSLContext( config.getSslContext() ); } if (config.isGzipRequesting()) { builder.setRequestCompressionLevel(6); } builder.setUrl( config.getServerURL().toString() ).setFollowRedirects( config.isFollowRedirect() ); }
@Test(groups = { "online", "default_provider" }) public void testSimpleAHCConfigProxy() throws IOException, InterruptedException, ExecutionException, TimeoutException { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder()// .setProxyProtocol(ProxyServer.Protocol.HTTPS)// .setProxyHost("127.0.0.1")// .setProxyPort(port1)// .setFollowRedirects(true)// .setUrl(getTargetUrl2())// .setAcceptAnyCertificate(true)// .setHeader("Content-Type", "text/html").build(); try { Response r = client.get().get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getHeader("X-Connection"), "keep-alive"); } finally { client.close(); } }
@Override protected InputStream getInputStream() throws XmlRpcException { try { client = builder.setHeaders( headers ).build(); // Check the status of the return Response response = client.post( new ByteArrayBodyGenerator( byteOs.toByteArray() ) ).get(); return response.getResponseBodyAsStream(); } catch ( InterruptedException e ) { throw new XmlRpcClientException( "Interruption during communication: " + e.getMessage(), e ); } catch ( ExecutionException e ) { throw new XmlRpcClientException( "Execution error during communication: " + e.getMessage(), e ); } catch ( IOException e ) { throw new XmlRpcClientException( "I/O error in server communication: " + e.getMessage(), e ); } finally { byteOs.reset(); } }
@Test(groups = { "standalone", "default_provider" }) public void testMultiPartPost() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build(); try { Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get(); String body = response.getResponseBody(); String contentType = response.getHeader("X-Content-Type"); assertTrue(contentType.contains("multipart/form-data")); String boundary = contentType.substring(contentType.lastIndexOf("=") + 1); assertTrue(body.startsWith("--" + boundary)); assertTrue(body.trim().endsWith("--" + boundary + "--")); assertTrue(body.contains("Content-Disposition:")); assertTrue(body.contains("Content-Type: application/test")); assertTrue(body.contains("name=\"baPart")); assertTrue(body.contains("filename=\"fileName")); } finally { client.close(); } } }
private void configureProxy( XmlRpcClientConfigImpl config ) { if ( config.getProxyHost() != null ) { builder.setProxyHost( config.getProxyHost() ); } if ( config.getProxyPort() != 0 ) { builder.setProxyPort( config.getProxyPort() ); } if ( config.getProxyPrincipal() != null ) { builder.setProxyPrincipal( config.getProxyPrincipal() ); } if ( config.getProxyPassword() != null ) { builder.setProxyPassword( config.getProxyPassword() ); } }
@Test(groups = { "standalone", "default_provider" }) public void testDeriveOverrideURL() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl("http://invalid.url").build(); SimpleAsyncHttpClient derived = client.derive().setUrl(getTargetUrl()).build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); InputStreamBodyGenerator generator = new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())); OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o); Future<Response> future = derived.post(generator, consumer); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); derived.close(); } }
/** * Returns a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests. * <p/> * <p/> * <p/> * The original SimpleAsyncHttpClient is responsible for managing the * underlying AsyncHttpClient. For the derived instance, {@link #close()} is * a NOOP. If the original SimpleAsyncHttpClient is closed, all derived * instances become invalid. * * @return a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests, never * {@code null}. */ public DerivedBuilder derive() { return new Builder(this); }
@Test(groups = { "standalone", "default_provider" }) public void testMultiPartPut() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build(); try { Response response = client.put(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get(); String body = response.getResponseBody(); String contentType = response.getHeader("X-Content-Type"); assertTrue(contentType.contains("multipart/form-data")); String boundary = contentType.substring(contentType.lastIndexOf("=") + 1); assertTrue(body.startsWith("--" + boundary)); assertTrue(body.trim().endsWith("--" + boundary + "--")); assertTrue(body.contains("Content-Disposition:")); assertTrue(body.contains("Content-Type: application/test")); assertTrue(body.contains("name=\"baPart")); assertTrue(body.contains("filename=\"fileName")); } finally { client.close(); } }
/** * See https://issues.sonatype.org/browse/AHC-5 */ @Test(groups = { "standalone", "default_provider" }, enabled = true) public void testPutZeroBytesFileTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setPooledConnectionIdleTimeout(100).setMaximumConnectionsTotal(50).setRequestTimeout(5 * 1000).setUrl(getTargetUrl() + "/testPutZeroBytesFileTest.txt").setHeader("Content-Type", "text/plain") .build(); try { File tmpfile = File.createTempFile("testPutZeroBytesFile", ".tmp"); tmpfile.deleteOnExit(); Future<Response> future = client.put(new FileBodyGenerator(tmpfile)); Response response = future.get(); tmpfile.delete(); assertEquals(response.getStatusCode(), 200); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void testCloseMasterInvalidDerived() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).build(); SimpleAsyncHttpClient derived = client.derive().build(); client.close(); try { derived.get().get(); fail("Expected closed AHC"); // expected -- Seems to me that this behavior conflicts with the requirements of Future.get() } catch (ExecutionException e) { assertTrue(e.getCause() instanceof IOException); } finally { client.close(); derived.close(); } }
/** * Returns a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests. * <p/> * <p/> * <p/> * The original SimpleAsyncHttpClient is responsible for managing the * underlying AsyncHttpClient. For the derived instance, {@link #close()} is * a NOOP. If the original SimpleAsyncHttpClient is closed, all derived * instances become invalid. * * @return a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests, never * {@code null}. */ public DerivedBuilder derive() { return new Builder(this); }
/** * Returns a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests. * <p/> * <p/> * <p/> * The original SimpleAsyncHttpClient is responsible for managing the * underlying AsyncHttpClient. For the derived instance, {@link #close()} is * a NOOP. If the original SimpleAsyncHttpClient is closed, all derived * instances become invalid. * * @return a Builder for a derived SimpleAsyncHttpClient that uses the same * instance of {@link AsyncHttpClient} to execute requests, never * {@code null}. */ public DerivedBuilder derive() { return new Builder(this); }
@Test(groups = { "standalone", "default_provider" }) public void requestByteArrayOutputStreamBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); } }
public Builder setRealmName(String realmName) { realm().setRealmName(realmName); return this; }
public Builder setRealmNtlmDomain(String ntlmDomain) { realm().setNtlmDomain(ntlmDomain); return this; }
@Test(groups = { "standalone", "default_provider" }) public void byteArrayOutputStreamBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setPooledConnectionIdleTimeout(100).setMaximumConnectionsTotal(50).setRequestTimeout(5 * 60 * 1000).setUrl(getTargetUrl()).setHeader("Content-Type", "text/html").build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); } }
public Builder setRealmPassword(String password) { realm().setPassword(password); return this; }
public Builder setRealmUsePreemptiveAuth(boolean usePreemptiveAuth) { realm().setUsePreemptiveAuth(usePreemptiveAuth); return this; }
public Builder setRealmPassword(String password) { realm().setPassword(password); return this; }