protected WebClient createWebClient(final String url, final String media) { final List< ? > providers = Arrays.asList(new JacksonJsonProvider()); final WebClient wc = WebClient .create("http://localhost:" + getPort() + url, providers) .accept(media); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(8000L); return wc; }
public static void main(String[] args) throws IOException { WebClient client = WebClient.create("http://localhost:8080/services/kp/upload"); ClientConfiguration config = WebClient.getConfig(client); config.getInInterceptors().add(new LoggingInInterceptor()); config.getOutInterceptors().add(new LoggingOutInterceptor()); client.type("multipart/form-data"); InputStream is = FileUtils.openInputStream(new File("vCenter_del.jpg")); List<Attachment> attachments = new ArrayList<>(); ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg"); Attachment att = new Attachment("File", is, cd); Attachment pageNumber = new Attachment("DATA1", MediaType.TEXT_PLAIN, "1"); Attachment OutputType = new Attachment("DATA2", MediaType.TEXT_PLAIN, "2"); attachments.add(att); attachments.add(pageNumber); attachments.add(OutputType); MultipartBody body = new MultipartBody(attachments); Response res = client.post(body); String data = res.readEntity(String.class); System.out.println(data); }
public long getSynchronousTimeout() { Conduit conduit = getConduit(); if (conduit instanceof HTTPConduit) { return ((HTTPConduit)conduit).getClient().getReceiveTimeout(); } return synchronousTimeout; }
public HelloWorld createServiceProxy(String username, String password) { HelloWorld service = JAXRSClientFactory.create(address, HelloWorld.class, username, password, null); WebClient.getConfig(service).getHttpConduit().getClient().setReceiveTimeout(100000000); WebClient.getConfig(service).getOutInterceptors().add(new LoggingOutInterceptor()); return service; }
AbstractWordpressServiceAdapter(final String wordpressUrl, final String apiVersion) { checkNotNull(emptyToNull(apiVersion)); this.apiVersion = apiVersion; // @formatter:off this.spi = JAXRSClientFactory.create(wordpressUrl, this.getSpiType(), Collections.singletonList(new JacksonJsonProvider())); // @formatter:on WebClient.client(spi).type(MediaType.APPLICATION_JSON_TYPE); WebClient.client(spi).accept(MediaType.APPLICATION_JSON_TYPE); // TODO: leave this kind of configuration to API clients WebClient.getConfig(spi).getHttpConduit().getClient().setAutoRedirect(true); /* * TODO: aggregate a configuration object to customize the JAXRS behavior, eg.: adding handlers or interceptors */ WebClient.getConfig(spi).getInInterceptors().add(new LoggingInInterceptor()); WebClient.getConfig(spi).getOutInterceptors().add(new LoggingOutInterceptor()); LOGGER.info("******* {} API initialized *********", spi.getClass().getSimpleName()); }
@org.junit.Test public void testBadEncryptingKey() throws Exception { URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); List<Object> providers = new ArrayList<>(); providers.add(new JacksonJsonProvider()); providers.add(new JweWriterInterceptor()); String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; WebClient client = WebClient.create(address, providers, busFile.toString()); client.type("application/json").accept("application/json"); Map<String, Object> properties = new HashMap<>(); properties.put("rs.security.keystore.type", "jwk"); properties.put("rs.security.keystore.alias", "AliceCert"); properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); properties.put("rs.security.encryption.content.algorithm", "A128GCM"); properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); WebClient.getConfig(client).getRequestContext().putAll(properties); Response response = client.post(new Book("book", 123L)); assertNotEquals(response.getStatus(), 200); }
@org.junit.Test public void testAuthorizationCodeBadScope() throws Exception { URL busFile = AuthorizationGrantTest.class.getResource("client.xml"); String address = "https://localhost:" + port + "/services/"; WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString()); // Save the Cookie for the second request... WebClient.getConfig(client).getRequestContext().put( org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE); // Get Authorization Code // Make initial authorization request client.type("application/json").accept("application/json"); client.query("client_id", "consumer-id"); client.query("response_type", "code"); client.query("redirect_uri", "http://www.blah.bad.apache.org"); client.query("scope", "unknown-scope"); client.path("authorize/"); // No redirect URI Response response = client.get(); assertEquals(400, response.getStatus()); }
@Test public void testPostAnd401WithText() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/post401"; WebClient wc = WebClient.create(endpointAddress); WebClient.getConfig(wc).getHttpConduit().getClient().setAllowChunking(false); Response r = wc.post(null); assertEquals(401, r.getStatus()); assertEquals("This is 401", getStringFromInputStream((InputStream)r.getEntity())); }
@Test public void testBookDepthExceededXMLSource() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/the/thebooks9/depth-source"; WebClient wc = WebClient.create(endpointAddress); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L); Response r = wc.type("application/xml").post(new Book("CXF", 123L)); assertEquals(413, r.getStatus()); }
@Test public void testGetBookFromWebClient() throws Exception { // setup the the client String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?replyToName=dynamicQueues/test.jmstransport.response" + "&jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&jndiURL=tcp://localhost:" + JMS_PORT; WebClient client = WebClient.create(endpointAddressUrlEncoded); WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor()); WebClient.getConfig(client).getRequestContext() .put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books/123"); Book book = client.get(Book.class); assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus()); assertEquals("Get a wrong book id.", 123, book.getId()); }
private void doTestGetBookHTML(String endpointAddress) throws Exception { WebClient client = WebClient.create(endpointAddress); client.accept("text/html"); WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(100000000); XMLSource source = client.accept("text/html").get(XMLSource.class); Map<String, String> namespaces = new HashMap<>(); namespaces.put("xhtml", "http://www.w3.org/1999/xhtml"); namespaces.put("books", "http://www.w3.org/books"); String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces); assertEquals("CXF Rocks", value); String ct = client.getResponse().getMetadata().getFirst("Content-Type").toString(); assertEquals("text/html", ct); }
@Test public void testProxyDirectDispatchPostWithGzip() throws Exception { BookStore localProxy = JAXRSClientFactory.create("local://books", BookStore.class); WebClient.getConfig(localProxy).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE); Response response = localProxy.addBook(new Book("New", 124L)); assertEquals(200, response.getStatus()); assertTrue(response.getMetadata().getFirst("Location") instanceof URI); }
@Test public void testGetBookRelativeUriAutoRedirect() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/redirect/relative?loop=false"; WebClient wc = WebClient.create(address); assertEquals(address, wc.getCurrentURI().toString()); WebClient.getConfig(wc).getRequestContext().put("http.redirect.relative.uri", "true"); WebClient.getConfig(wc).getHttpConduit().getClient().setAutoRedirect(true); Response r = wc.get(); Book book = r.readEntity(Book.class); assertEquals(124L, book.getId()); String newAddress = "http://localhost:" + PORT + "/bookstore/redirect/relative?redirect=true"; assertEquals(newAddress, wc.getCurrentURI().toString()); }
@Test public void testProxyServerInFaultDirectDispatch() throws Exception { BookStore localProxy = JAXRSClientFactory.create("local://books", BookStore.class); WebClient.getConfig(localProxy).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, "true"); WebClient.getConfig(localProxy).getInFaultInterceptors().add(new TestFaultInInterceptor()); Response r = localProxy.infault2(); assertEquals(500, r.getStatus()); }
@Test public void testProxyEmptyResponseDirectDispatch() throws Exception { BookStore localProxy = JAXRSClientFactory.create("local://books", BookStore.class); WebClient.getConfig(localProxy).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, "true"); assertNull(localProxy.getEmptyBook()); assertEquals(204, WebClient.client(localProxy).getResponse().getStatus()); }
@Test public void testProxyServerOutFaultDirectDispacth() throws Exception { BookStore localProxy = JAXRSClientFactory.create("local://books", BookStore.class); WebClient.getConfig(localProxy).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, "true"); Response r = localProxy.outfault(); assertEquals(403, r.getStatus()); }
private void doTestEchoBookForm(String address) throws Exception { WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L); Book b = wc.form(new Form().param("name", "CXFForm").param("id", "125")) .readEntity(Book.class); assertEquals("CXFForm", b.getName()); assertEquals(125L, b.getId()); } @Test
@Test public void testGetBookDescriptionHttpResponse() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/httpresponse"; WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getInInterceptors().add(new LoggingInInterceptor()); Response r = wc.get(); assertEquals("text/plain", r.getMediaType().toString()); assertEquals("Good Book", r.readEntity(String.class)); }
@Test public void testPostEmptyFormAsInStream() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/emptyform"; WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getRequestContext().put("org.apache.cxf.empty.request", true); wc.type(MediaType.APPLICATION_FORM_URLENCODED); Response r = wc.post(new ByteArrayInputStream("".getBytes())); assertEquals("empty form", r.readEntity(String.class)); }
@Test @Ignore("Spring Security 3 does not preserve POSTed form parameters as HTTPServletRequest parameters") public void testBookFromHttpRequestParameters() throws Exception { WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstorestorage/bookforms2", "foo", "bar", null); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("application/xml"); Response r = wc.form(new Form().param("name", "CXF Rocks").param("id", "123")); Book b = readBook((InputStream)r.getEntity()); assertEquals("CXF Rocks", b.getName()); assertEquals(123L, b.getId()); }