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; }
@Test public void testGetBookSubresourceWebClientProxyBean() throws Exception { WebClient client = WebClient.create("http://localhost:" + PORT + "/test/services/rest"); client.type(MediaType.TEXT_PLAIN_TYPE) .accept(MediaType.APPLICATION_XML_TYPE, MediaType.TEXT_XML_TYPE); BookStoreJaxrsJaxws proxy = JAXRSClientFactory.fromClient(client, BookStoreJaxrsJaxws.class, true); doTestSubresource(proxy); BookStoreJaxrsJaxws proxy2 = JAXRSClientFactory.fromClient( WebClient.client(proxy), BookStoreJaxrsJaxws.class); doTestSubresource(proxy2); }
/** * Creates a proxy using user resource model * @param baseAddress baseAddress * @param cls proxy class, if not interface then a CGLIB proxy will be created * @param modelBeans model beans * @param configLocation classpath location of the configuration resource * @return typed proxy */ public static <T> T createFromModel(String baseAddress, Class<T> cls, List<UserResource> modelBeans, String configLocation) { return createFromModel(baseAddress, cls, modelBeans, Collections.emptyList(), configLocation); }
@Test public void testServerWebApplicationExceptionWithProxy2() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); try { store.throwException(); fail("Exception expected"); } catch (WebApplicationException ex) { assertEquals(500, ex.getResponse().getStatus()); assertEquals("This is a WebApplicationException", ex.getResponse().readEntity(String.class)); } }
@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()); }
.createFromModel("http://localhost:" + port + "/services/jaxrs", CustomerService.class, "classpath:/data/model/CustomerService-jaxrs.xml", providers, null); throw new RuntimeException("Barry should not be found"); System.out.println("Status : " + WebClient.client(customerService).getResponse().getStatus());
@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 testAddBookProxyResponse() { Book b = new Book("CXF rocks", 123L); System.out.println(Arrays.deepToString(Arrays.asList(b, b).toArray())); BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); Response r = store.addBook(b); assertNotNull(r); InputStream is = (InputStream)r.getEntity(); assertNotNull(is); XMLSource source = new XMLSource(is); source.setBuffering(); assertEquals(124L, Long.parseLong(source.getValue("Book/id"))); assertEquals("CXF rocks", source.getValue("Book/name")); }
public void sayHelloRest() throws Exception { final String address = "http://localhost:" + port + "/services/hello-rest"; System.out.println("Using CXF JAX-RS proxy to invoke on HelloWorld service"); // Admin HelloWorld service = JAXRSClientFactory.create(address, HelloWorld.class, "admin", "admin", null); WebClient.getConfig(service).getHttpConduit().getClient().setReceiveTimeout(100000000); useHelloServiceRest(service, "Barry", true); // User service = JAXRSClientFactory.create(address, HelloWorld.class, "user", "user", null); useHelloServiceRest(service, "Barry", false); }
@Test public void testGetBook123WebClientToProxy() throws Exception { WebClient wc = WebClient.create("https://localhost:" + PORT, CLIENT_CONFIG_FILE1); wc.path("/bookstore/securebooks/123").accept(MediaType.APPLICATION_XML_TYPE); Book b = wc.get(Book.class); assertEquals(123, b.getId()); wc.back(true); BookStore bs = JAXRSClientFactory.fromClient(wc, BookStore.class); Book b2 = bs.getSecureBook("123"); assertEquals(b2.getId(), 123); }
@Test public void testGetBook123ProxyToWebClient() throws Exception { BookStore bs = JAXRSClientFactory.create("https://localhost:" + PORT, BookStore.class, CLIENT_CONFIG_FILE1); Book b = bs.getSecureBook("123"); assertEquals(b.getId(), 123); WebClient wc = WebClient.fromClient(WebClient.client(bs)); wc.path("/bookstore/securebooks/123").accept(MediaType.APPLICATION_XML_TYPE); Book b2 = wc.get(Book.class); assertEquals(123, b2.getId()); }
@Test public void testGetBookJsonProxy() throws Exception { String address = "http://localhost:" + PORT; MultipartStore client = JAXRSClientFactory.create(address, MultipartStore.class); Map<String, Book> map = client.getBookJson(); List<Book> result = new ArrayList<>(map.values()); assertEquals(1, result.size()); Book json = result.get(0); assertEquals("json", json.getName()); assertEquals(1L, json.getId()); String contentType = WebClient.client(client).getResponse().getMetadata().getFirst("Content-Type").toString(); MediaType mt = MediaType.valueOf(contentType); assertEquals("multipart", mt.getType()); assertEquals("mixed", mt.getSubtype()); }
@Test public void testGetBookSubresourceWebClientProxy2() throws Exception { WebClient client = WebClient.create("http://localhost:" + PORT + "/test/services/rest/bookstore") .path("/books/378"); client.type(MediaType.TEXT_PLAIN_TYPE).accept(MediaType.APPLICATION_XML_TYPE); BookSubresource proxy = JAXRSClientFactory.fromClient(client, BookSubresource.class); Book b = proxy.getTheBook2("CXF ", "in ", "Acti", "on ", "- 3", "7", "8"); assertEquals(378, b.getId()); assertEquals("CXF in Action - 378", b.getName()); }
@Test public void testProxyWithQuery() throws Exception { BookStore localProxy = JAXRSClientFactory.create("local://books", BookStore.class); WebClient.getConfig(localProxy).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE); Book book = localProxy.getBookByURLQuery(new String[] {"1", "2", "3"}); assertEquals(123L, book.getId()); }
/** * Creates a JAXRS web client for the given JAXRS client */ @Override protected <T> T createWebClient(Class<T> clientType) { List<Object> providers = WebClients.createProviders(); providers.add(new Authenticator()); WebClient webClient = WebClient.create(address, providers); disableSslChecks(webClient); //configureUserAndPassword(webClient, username, password); return JAXRSClientFactory.fromClient(webClient, clientType); }
@Test public void testBookWithExceptionsNoMapper() throws Exception { BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class); try { store.getBookWithExceptions(true); fail(); } catch (WebApplicationException ex) { assertEquals("notReturned", ex.getResponse().getHeaderString("Status")); } }
@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()); }
@Test public void testAddOrderFormBean() throws Exception { String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress, BookStoreJaxrsJaxws.class); WebClient.getConfig(proxy).getOutInterceptors().add(new LoggingOutInterceptor()); WebClient.getConfig(proxy).getInInterceptors().add(new LoggingInInterceptor()); BookSubresource bs = proxy.getBookSubresource("139"); OrderBean order = new OrderBean(); order.setId(123L); order.setWeight(100); order.setCustomerTitle(OrderBean.Title.MS); OrderBean order2 = bs.addOrder(order); assertEquals(Long.valueOf(123L), Long.valueOf(order2.getId())); assertEquals(OrderBean.Title.MS, order2.getCustomerTitle()); }