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; }
private Book doRetrieveBook(boolean useReflection) { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); wc.type("application/xml").accept("application/xml"); if (!useReflection) { WebClient.getConfig(wc).getRequestContext().put("use.httpurlconnection.method.reflection", false); } // CXF RS Client code will set this property to true if the http verb is unknown // and this property is not already set. The async conduit is loaded in the tests module // but we do want to test HTTPUrlConnection reflection hence we set this property to false WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", false); WebClient.getConfig(wc).getRequestContext().put("response.stream.auto.close", true); return wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class); }
private void doTestGetBook(String address, boolean useAsync) { WebClient wc = createWebClient(address); if (useAsync) { WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); } Book book = wc.get(Book.class); assertEquals(124L, book.getId()); validateResponse(wc); }
@Test public void testBookWithSpaceProxyWithBufferedStream() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); WebClient.getConfig(store).getResponseContext().put("buffer.proxy.response", "true"); Book book = store.getBookWithSpace("123"); assertEquals(123L, book.getId()); assertTrue(WebClient.client(store).getResponse().readEntity(String.class).contains("<Book")); }
@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 testJweRsaJwsRsaCertInHeaders() throws Exception { String address = "https://localhost:" + PORT + "/jwejwsrsaCertInHeaders"; BookStore bs = createJweJwsBookStore(address, null, null); WebClient.getConfig(bs).getRequestContext().put("rs.security.signature.include.cert", "true"); WebClient.getConfig(bs).getRequestContext().put("rs.security.encryption.include.cert", "true"); String text = bs.echoText("book"); assertEquals("book", text); } @Test
@Test public void testGetBookSubresourceParamExtensions2() throws Exception { String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress, BookStoreJaxrsJaxws.class); WebClient.getConfig(proxy).getOutInterceptors().add(new LoggingOutInterceptor()); BookSubresource bs = proxy.getBookSubresource("139"); BookBean bean = new BookBean("CXF Rocks", 139L); bean.getComments().put(1L, "Good"); bean.getComments().put(2L, "Good"); BookBean b = bs.getTheBookQueryBean(bean); assertEquals(139, b.getId()); assertEquals("CXF Rocks", b.getName()); }
private void doTestGetBookISOXML(String charset, String pathSegment) throws Exception { String address = "http://localhost:" + PORT + "/the/bookstore/ISO-8859-1/" + pathSegment; WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L); wc.accept("application/xml" + (charset == null ? "" : ";charset=ISO-8859-1")); byte[] iso88591bytes = wc.get(byte[].class); String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1"); String name = helloStringISO88591.substring( helloStringISO88591.indexOf("<name>") + "<name>".length(), helloStringISO88591.indexOf("</name>")); compareNames(name); }
@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 public void testAddBookWebClient() { InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/add_book.txt"); String address = "http://localhost:" + PORT + "/bookstore/books/jaxb"; WebClient client = WebClient.create(address); WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true"); client.type("multipart/related;type=text/xml").accept("text/xml"); Book book = client.post(is1, Book.class); assertEquals("CXF in Action - 2", book.getName()); }
private void doTestGetBookWebEx(String address) throws Exception { WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L); try { wc.accept("text/plain", "application/json").get(Book.class); fail("InternalServerErrorException is expected"); } catch (InternalServerErrorException ex) { String errorMessage = ex.getResponse().readEntity(String.class); assertEquals("Book web exception", errorMessage); } }
@Test public void testAddGetImageWebClient() throws Exception { InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"); String address = "http://localhost:" + PORT + "/bookstore/books/image"; WebClient client = WebClient.create(address); client.type("multipart/mixed").accept("multipart/mixed"); WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true"); InputStream is2 = client.post(is1, InputStream.class); byte[] image1 = IOUtils.readBytesFromStream( getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); byte[] image2 = IOUtils.readBytesFromStream(is2); assertTrue(Arrays.equals(image1, image2)); }
private Response doTestPerRequest(String address) throws Exception { WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("application/xml"); Response r = wc.get(); Book book = r.readEntity(Book.class); assertEquals("CXF in Action", book.getName()); assertEquals(123L, book.getId()); return r; }
@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()); }
protected WebClient createWebClient(final String url) { final List< ? > providers = Arrays.asList(new JacksonJsonProvider()); final WebClient wc = WebClient .create("http://localhost:" + getPort(), providers) .path(getContextPath()) .path(url) .accept(MediaType.APPLICATION_JSON); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L); return wc; }
@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()); }
@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 testWrongKeyEncryptionAlgorithm() 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", "2011-04-29"); 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", "RSA1_5"); WebClient.getConfig(client).getRequestContext().putAll(properties); Response response = client.post(new Book("book", 123L)); assertNotEquals(response.getStatus(), 200); }
@org.junit.Test public void testSmallEncryptionKeySize() 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 + "/jwesmallkey/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", "jks"); properties.put("rs.security.keystore.alias", "smallkey"); properties.put("rs.security.keystore.password", "security"); properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/smallkeysize.jks"); 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); }