@Override public List<Interceptor<? extends Message>> getOutInterceptors() { return config.getOutInterceptors(); }
@Override public List<Interceptor<? extends Message>> getOutInterceptors() { return config.getOutInterceptors(); }
@Override public List<Interceptor<? extends Message>> getOutInterceptors() { return config.getOutInterceptors(); }
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); }
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()); }
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; }
private String getSubjectName(String apiEndpoint, String accessToken, TrustedIdp trustedIdp) { WebClient client = WebClient.create(apiEndpoint, Collections.singletonList(new JsonMapObjectProvider()), "cxf-tls.xml"); client.path("/me"); ClientConfiguration config = WebClient.getConfig(client); if (LOG.isDebugEnabled()) { config.getOutInterceptors().add(new LoggingOutInterceptor()); config.getInInterceptors().add(new LoggingInInterceptor()); } client.accept("application/json"); client.query("access_token", accessToken); String subjectName = getProperty(trustedIdp, SUBJECT_CLAIM); if (subjectName == null || subjectName.isEmpty()) { subjectName = "email"; } client.query("fields", subjectName); JsonMapObject mapObject = client.get(JsonMapObject.class); String parsedSubjectName = (String)mapObject.getProperty(subjectName); if (subjectName.contains("email")) { parsedSubjectName = parsedSubjectName.replace("\\u0040", "@"); } return parsedSubjectName; }
private void checkFiInterceptors(ClientConfiguration cfg) { int count = 0; for (Interceptor<?> in : cfg.getInInterceptors()) { if (in instanceof FIStaxInInterceptor) { count++; break; } } for (Interceptor<?> in : cfg.getOutInterceptors()) { if (in instanceof FIStaxOutInterceptor) { count++; break; } } assertEquals("In and Out FastInfoset interceptors are expected", 2, count); }
private ClientAccessToken getAccessTokenUsingCode(String tokenEndpoint, String code, String clientId, String clientSecret, String redirectURI) { // Here we need to get the AccessToken using the authorization code List<Object> providers = new ArrayList<>(); providers.add(new OAuthJSONProvider()); WebClient client = WebClient.create(tokenEndpoint, providers, "cxf-tls.xml"); ClientConfiguration config = WebClient.getConfig(client); if (LOG.isDebugEnabled()) { config.getOutInterceptors().add(new LoggingOutInterceptor()); config.getInInterceptors().add(new LoggingInInterceptor()); } client.type("application/x-www-form-urlencoded"); client.accept("application/json"); Form form = new Form(); form.param("grant_type", "authorization_code"); form.param("code", code); form.param("client_id", clientId); form.param("redirect_uri", redirectURI); form.param("client_secret", clientSecret); Response response = client.post(form); return response.readEntity(ClientAccessToken.class); }
/** * New REST client uses old REST service */ public void useOldRESTServiceWithNewClient() throws Exception { List<Object> providers = createJAXRSProviders(); org.customer.service.CustomerService customerService = JAXRSClientFactory .createFromModel("http://localhost:" + port + "/examples/direct/rest", org.customer.service.CustomerService.class, "classpath:/model/CustomerService-jaxrs.xml", providers, null); // The outgoing new Customer data needs to be transformed for // the old service to understand it and the response from the old service // needs to be transformed for this new client to understand it. ClientConfiguration config = WebClient.getConfig(customerService); addTransformInterceptors(config.getInInterceptors(), config.getOutInterceptors(), true); System.out.println("Using old RESTful CustomerService with new client"); customer.v2.Customer customer = createNewCustomer("Smith New to Old REST"); customerService.updateCustomer(customer); customer = customerService.getCustomerByName("Smith New to Old REST"); printNewCustomerDetails(customer); }
protected static PhaseInterceptorChain setupOutInterceptorChain(ClientConfiguration cfg) { PhaseManager pm = cfg.getBus().getExtension(PhaseManager.class); List<Interceptor<? extends Message>> i1 = cfg.getBus().getOutInterceptors(); List<Interceptor<? extends Message>> i2 = cfg.getOutInterceptors(); List<Interceptor<? extends Message>> i3 = cfg.getConduitSelector().getEndpoint().getOutInterceptors(); PhaseInterceptorChain chain = new PhaseChainCache().get(pm.getOutPhases(), i1, i2, i3); chain.add(new ClientRequestFilterInterceptor()); return chain; }
protected static PhaseInterceptorChain setupOutInterceptorChain(ClientConfiguration cfg) { PhaseManager pm = cfg.getBus().getExtension(PhaseManager.class); List<Interceptor<? extends Message>> i1 = cfg.getBus().getOutInterceptors(); List<Interceptor<? extends Message>> i2 = cfg.getOutInterceptors(); List<Interceptor<? extends Message>> i3 = cfg.getConduitSelector().getEndpoint().getOutInterceptors(); PhaseInterceptorChain chain = new PhaseChainCache().get(pm.getOutPhases(), i1, i2, i3); chain.add(new ClientRequestFilterInterceptor()); return chain; }
protected static PhaseInterceptorChain setupOutInterceptorChain(ClientConfiguration cfg) { PhaseManager pm = cfg.getBus().getExtension(PhaseManager.class); List<Interceptor<? extends Message>> i1 = cfg.getBus().getOutInterceptors(); List<Interceptor<? extends Message>> i2 = cfg.getOutInterceptors(); List<Interceptor<? extends Message>> i3 = cfg.getConduitSelector().getEndpoint().getOutInterceptors(); PhaseInterceptorChain chain = new PhaseChainCache().get(pm.getOutPhases(), i1, i2, i3); chain.add(new ClientRequestFilterInterceptor()); return chain; }
@Test public void testPostBookTransform() throws Exception { String address = "http://localhost:" + PORT + "/test/v1/rest-transform/bookstore/books"; TransformOutInterceptor out = new TransformOutInterceptor(); out.setOutTransformElements( Collections.singletonMap("{http://www.example.org/books}*", "{http://www.example.org/super-books}*")); TransformInInterceptor in = new TransformInInterceptor(); Map<String, String> map = new HashMap<>(); // If Book2 didn't have {http://www.example.org/books}Book // then we'd just do '"*" : "{http://www.example.org/books}*' // but given that we have TheBook being returned, we need map.put("TheBook", "{http://www.example.org/books}Book"); map.put("id", "{http://www.example.org/books}id"); in.setInTransformElements(map); WebClient client = WebClient.create(address); WebClient.getConfig(client).getInInterceptors().add(in); WebClient.getConfig(client).getOutInterceptors().add(out); Book2 book = client.type("application/xml").accept("text/xml").post(new Book2(), Book2.class); assertEquals(124L, book.getId()); }
@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 testGetBookSubresourceParamExtensions() 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"); Book bean = new Book("CXF Rocks", 139L); Book b = bs.getTheBook4(bean, bean, bean, 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()); }
@Test public void testGetBookWithInterceptor() throws Exception { if (!runTests) { return; } WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123"); KerberosAuthOutInterceptor kbInterceptor = new KerberosAuthOutInterceptor(); AuthorizationPolicy policy = new AuthorizationPolicy(); policy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_NEGOTIATE); policy.setAuthorization("alice"); policy.setUserName("alice"); policy.setPassword("alice"); kbInterceptor.setPolicy(policy); kbInterceptor.setCredDelegation(true); WebClient.getConfig(wc).getOutInterceptors().add(new LoggingOutInterceptor()); WebClient.getConfig(wc).getOutInterceptors().add(kbInterceptor); // Required so as to get it working with our KDC kbInterceptor.setServicePrincipalName("bob@service.ws.apache.org"); kbInterceptor.setServiceNameType(GSSName.NT_HOSTBASED_SERVICE); Book b = wc.get(Book.class); Assert.assertEquals(b.getId(), 123); }
private void doTestGetBook123Proxy(String configFile) throws Exception { BookStore bs = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class, configFile); WebClient.getConfig(bs).getOutInterceptors().add(new LoggingOutInterceptor()); SpnegoAuthSupplier authSupplier = new SpnegoAuthSupplier(); authSupplier.setServicePrincipalName("bob@service.ws.apache.org"); authSupplier.setServiceNameType(GSSName.NT_HOSTBASED_SERVICE); WebClient.getConfig(bs).getHttpConduit().setAuthSupplier(authSupplier); // just to verify the interface call goes through CGLIB proxy too Assert.assertEquals("http://localhost:" + PORT, WebClient.client(bs).getBaseURI().toString()); Book b = bs.getBook("123"); Assert.assertEquals(b.getId(), 123); b = bs.getBook("123"); Assert.assertEquals(b.getId(), 123); }
public void doTestEnvelopedSAMLToken(boolean signed, CallbackHandler samlCallbackHandler) throws Exception { String address = "https://localhost:" + PORT + "/samlxml/bookstore/books"; WebClient wc = createWebClient(address, new SamlEnvelopedOutInterceptor(!signed), null, samlCallbackHandler); XmlSigOutInterceptor xmlSig = new XmlSigOutInterceptor(); if (signed) { xmlSig.setStyle(XmlSigOutInterceptor.DETACHED_SIG); } WebClient.getConfig(wc).getOutInterceptors().add(xmlSig); wc.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML); try { Book book = wc.post(new Book("CXF", 125L), Book.class); assertEquals(125L, book.getId()); } catch (WebApplicationException ex) { fail(ex.getMessage()); } catch (ProcessingException ex) { if (ex.getCause() != null && ex.getCause().getMessage() != null) { fail(ex.getCause().getMessage()); } else { fail(ex.getMessage()); } } }