Java Code Examples for org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setAddress()
The following examples show how to use
org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestClientBuilder.java From microshed-testing with Apache License 2.0 | 6 votes |
public <T> T build(Class<T> clazz) { // Apply default values if unspecified if (appContextRoot == null) appContextRoot = ApplicationEnvironment.Resolver.load().getApplicationURL(); if (jaxrsPath == null) jaxrsPath = locateApplicationPath(clazz); if (providers == null) providers = Collections.singletonList(JsonBProvider.class); JAXRSClientFactoryBean bean = new org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean(); String basePath = join(appContextRoot, jaxrsPath); LOG.info("Building rest client for " + clazz + " with base path: " + basePath + " and providers: " + providers); bean.setResourceClass(clazz); bean.setProviders(providers); bean.setAddress(basePath); bean.setHeaders(headers); return bean.create(clazz); }
Example 2
Source File: JAXRSSoapBookTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testClientFaultOutInterceptor() throws Exception { //testing faults created by client out interceptor chain handled correctly String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(baseAddress); bean.setResourceClass(BookStoreJaxrsJaxws.class); final boolean addBadOutInterceptor = true; TestFeature testFeature = new TestFeature(addBadOutInterceptor); List<AbstractFeature> features = new ArrayList<>(); features.add(testFeature); bean.setFeatures(features); BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create(); try { //321 is special case - causes error code of 525 proxy.getBook(Long.valueOf("123")); fail("Method should have thrown an exception"); } catch (Exception e) { assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled()); assertFalse("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled()); assertTrue("Wrong exception caught", "fault from bad interceptor".equals(e.getCause().getMessage())); assertTrue("Client In Fault In Interceptor was invoked", testFeature.faultInInterceptorCalled()); } }
Example 3
Source File: JAXRSClientMetricsTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Exception { final JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean(); factory.setResourceClass(Library.class); factory.setAddress("http://localhost:" + wireMockRule.port() + "/"); factory.setFeatures(Arrays.asList(new MetricsFeature(provider))); factory.setProvider(JacksonJsonProvider.class); stubFor(get(urlEqualTo("/books/10")) .willReturn(aResponse() .withStatus(404))); try { final Library client = factory.create(Library.class); expectedException.expect(NotFoundException.class); client.getBook(10); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); Mockito.verify(endpointContext, times(1)).start(any(Exchange.class)); Mockito.verify(endpointContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); Mockito.verifyNoInteractions(operationContext); } }
Example 4
Source File: JAXRSSamlTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testInvalidSAMLTokenAsHeader() throws Exception { String address = "https://localhost:" + PORT + "/samlheader/bookstore/books/123"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSSamlTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); WebClient wc = bean.createWebClient(); wc.header("Authorization", "SAML invalid_grant"); Response r = wc.get(); assertEquals(401, r.getStatus()); }
Example 5
Source File: JAXRSJwsMultipartTest.java From cxf with Apache License 2.0 | 6 votes |
private JAXRSClientFactoryBean createJAXRSClientFactoryBean(String address, boolean bufferPayload, boolean useJwsJsonSignatureFormat) throws Exception { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSJwsMultipartTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); bean.setServiceClass(BookStore.class); bean.setAddress(address); List<Object> providers = new LinkedList<>(); JwsMultipartClientRequestFilter outFilter = new JwsMultipartClientRequestFilter(); outFilter.setUseJwsJsonSignatureFormat(useJwsJsonSignatureFormat); providers.add(outFilter); JwsMultipartClientResponseFilter inFilter = new JwsMultipartClientResponseFilter(); inFilter.setBufferPayload(bufferPayload); providers.add(inFilter); providers.add(new JwsDetachedSignatureProvider()); bean.setProviders(providers); return bean; }
Example 6
Source File: JAXRSSoapBookTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testAddFeatureToClient() throws Exception { String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(baseAddress); bean.setResourceClass(BookStoreJaxrsJaxws.class); TestFeature testFeature = new TestFeature(); List<AbstractFeature> features = new ArrayList<>(); features.add(testFeature); bean.setFeatures(features); BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create(); Book b = proxy.getBook(Long.valueOf("123")); assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled()); assertTrue("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled()); assertEquals(123, b.getId()); assertEquals("CXF in Action", b.getName()); }
Example 7
Source File: JAXRSJweJwsTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testJwsJwkEC() throws Exception { String address = "https://localhost:" + PORT + "/jwsjwkec"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); bean.setServiceClass(BookStore.class); bean.setAddress(address); List<Object> providers = new LinkedList<>(); JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); jwsWriter.setUseJwsOutputStream(true); providers.add(jwsWriter); providers.add(new JwsClientResponseFilter()); bean.setProviders(providers); bean.getProperties(true).put("rs.security.signature.out.properties", "org/apache/cxf/systest/jaxrs/security/jws.ec.private.properties"); bean.getProperties(true).put("rs.security.signature.in.properties", "org/apache/cxf/systest/jaxrs/security/jws.ec.public.properties"); BookStore bs = bean.create(BookStore.class); String text = bs.echoText("book"); assertEquals("book", text); }
Example 8
Source File: AmbariClientBuilder.java From components with Apache License 2.0 | 5 votes |
/** * Build a client proxy, for a specific proxy type. * * @param proxyType proxy type class * @return client proxy stub */ protected <T> T build(Class<T> proxyType) { String address = generateAddress(); T rootResource; // Synchronized on the class to correlate with the scope of clientStaticResources // We want to ensure that the shared bean isn't set concurrently in multiple callers synchronized (AmbariClientBuilder.class) { JAXRSClientFactoryBean bean = cleanFactory(clientStaticResources.getUnchecked(proxyType)); bean.setAddress(address); if (username != null) { bean.setUsername(username); bean.setPassword(password); } if (enableLogging) { bean.setFeatures(Arrays.<AbstractFeature> asList(new LoggingFeature())); } rootResource = bean.create(proxyType); } boolean isTlsEnabled = address.startsWith("https://"); ClientConfiguration config = WebClient.getConfig(rootResource); HTTPConduit conduit = (HTTPConduit) config.getConduit(); if (isTlsEnabled) { TLSClientParameters tlsParams = new TLSClientParameters(); if (!validateCerts) { tlsParams.setTrustManagers(new TrustManager[] { new AcceptAllTrustManager() }); } else if (trustManagers != null) { tlsParams.setTrustManagers(trustManagers); } tlsParams.setDisableCNCheck(!validateCn); conduit.setTlsClientParameters(tlsParams); } HTTPClientPolicy policy = conduit.getClient(); policy.setConnectionTimeout(connectionTimeoutUnits.toMillis(connectionTimeout)); policy.setReceiveTimeout(receiveTimeoutUnits.toMillis(receiveTimeout)); return rootResource; }
Example 9
Source File: JAXRSJweJwsTest.java From cxf with Apache License 2.0 | 5 votes |
private BookStore createJweJwsBookStore(String address, JwsSignatureProvider jwsSigProvider, List<?> mbProviders) throws Exception { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); bean.setServiceClass(BookStore.class); bean.setAddress(address); List<Object> providers = new LinkedList<>(); JweWriterInterceptor jweWriter = new JweWriterInterceptor(); jweWriter.setUseJweOutputStream(true); providers.add(jweWriter); providers.add(new JweClientResponseFilter()); JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); if (jwsSigProvider != null) { jwsWriter.setSignatureProvider(jwsSigProvider); } jwsWriter.setUseJwsOutputStream(true); providers.add(jwsWriter); providers.add(new JwsClientResponseFilter()); if (mbProviders != null) { providers.addAll(mbProviders); } bean.setProviders(providers); bean.getProperties(true).put("rs.security.encryption.out.properties", SERVER_JWEJWS_PROPERTIES); bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES); bean.getProperties(true).put("rs.security.encryption.in.properties", CLIENT_JWEJWS_PROPERTIES); bean.getProperties(true).put("rs.security.signature.in.properties", SERVER_JWEJWS_PROPERTIES); PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl(); bean.getProperties(true).put("rs.security.signature.key.password.provider", provider); bean.getProperties(true).put("rs.security.decryption.key.password.provider", provider); return bean.create(BookStore.class); }
Example 10
Source File: JAXRSJweJwsTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testJweRsaJwsRsaEncryptThenSign() throws Exception { String address = "https://localhost:" + PORT + "/jwejwsrsaencrsign"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); bean.setServiceClass(BookStore.class); bean.setAddress(address); List<Object> providers = new LinkedList<>(); JweWriterInterceptor jweWriter = new EncrSignJweWriterInterceptor(); jweWriter.setUseJweOutputStream(true); providers.add(jweWriter); JwsWriterInterceptor jwsWriter = new EncrSignJwsWriterInterceptor(); jwsWriter.setUseJwsOutputStream(true); providers.add(jwsWriter); bean.setProviders(providers); bean.getProperties(true).put("rs.security.encryption.out.properties", SERVER_JWEJWS_PROPERTIES); bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES); PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl(); bean.getProperties(true).put("rs.security.signature.key.password.provider", provider); BookStore bs = bean.create(BookStore.class); String text = bs.echoText("book"); assertEquals("book", text); }
Example 11
Source File: JAXRSClientServerBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testGetPrimitiveDoubleArray() throws Exception { String address = "http://localhost:" + PORT; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setProvider(new BookStore.PrimitiveDoubleArrayReaderWriter()); bean.setAddress(address); bean.setResourceClass(BookStore.class); BookStore store = bean.create(BookStore.class); double[] arr = store.getBookIndexAsDoubleArray(); assertEquals(3, arr.length); assertEquals(1, arr[0], 0.0); assertEquals(2, arr[1], 0.0); assertEquals(3, arr[2], 0.0); }
Example 12
Source File: JAXRSSamlTest.java From cxf with Apache License 2.0 | 5 votes |
private WebClient createWebClient(String address, Interceptor<Message> outInterceptor, Object provider, CallbackHandler samlCallbackHandler) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSSamlTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); Map<String, Object> properties = new HashMap<>(); properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback"); properties.put(SecurityConstants.SAML_CALLBACK_HANDLER, samlCallbackHandler); properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice"); properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties"); bean.setProperties(properties); bean.getOutInterceptors().add(outInterceptor); if (provider != null) { bean.setProvider(provider); } return bean.createWebClient(); }
Example 13
Source File: JAXRSSoapBookTest.java From cxf with Apache License 2.0 | 5 votes |
private void serverFaultInInterceptorTest(String param) { String baseAddress = "http://localhost:" + PORT + "/test/services/rest"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(baseAddress); bean.setResourceClass(BookStoreJaxrsJaxws.class); TestFeature testFeature = new TestFeature(); List<AbstractFeature> features = new ArrayList<>(); features.add(testFeature); bean.setFeatures(features); BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create(); WebClient.getConfig(proxy).getRequestContext().put("org.apache.cxf.transport.no_io_exceptions", false); try { //321 is special case - causes error code of 525 proxy.getBook(Long.valueOf(param)); fail("Method should have thrown an exception"); } catch (Exception e) { assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled()); if ("322".equals(param)) { //In interceptors not called when checked exception thrown from server assertTrue("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled()); } else { assertFalse("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled()); } assertTrue("Client In Fault In Interceptor not invoked", testFeature.faultInInterceptorCalled()); } }
Example 14
Source File: JAXRSClientServerBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testEmptyPostProxy2() throws Exception { String address = "http://localhost:" + PORT; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); bean.setResourceClass(BookStore.class); BookStore store = bean.create(BookStore.class); store.emptypostNoPath(); assertEquals(204, WebClient.client(store).getResponse().getStatus()); }
Example 15
Source File: JAXRSOAuth2TlsTest.java From cxf with Apache License 2.0 | 5 votes |
private WebClient createDynRegWebClient(String address) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); bean.setProvider(new JsonMapObjectProvider()); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSOAuth2TlsTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); WebClient wc = bean.createWebClient(); wc.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); return wc; }
Example 16
Source File: JAXRSOAuth2Test.java From cxf with Apache License 2.0 | 5 votes |
private WebClient createWebClient(String address) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSOAuth2Test.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); WebClient wc = bean.createWebClient(); wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON); return wc; }
Example 17
Source File: CxfTest.java From java-specialagent with Apache License 2.0 | 5 votes |
@Test public void testRs(final MockTracer tracer) { System.setProperty(Configuration.INTERCEPTORS_CLIENT_IN, ClientSpanTagInterceptor.class.getName()); System.setProperty(Configuration.INTERCEPTORS_SERVER_OUT, ServerSpanTagInterceptor.class.getName()); final String msg = "hello"; // prepare server final JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean(); serverFactory.setAddress(BASE_URI); serverFactory.setServiceBean(new EchoImpl()); final Server server = serverFactory.create(); // prepare client final JAXRSClientFactoryBean clientFactory = new JAXRSClientFactoryBean(); clientFactory.setServiceClass(Echo.class); clientFactory.setAddress(BASE_URI); final Echo echo = clientFactory.create(Echo.class); final String response = echo.echo(msg); assertEquals(msg, response); assertEquals(2, tracer.finishedSpans().size()); final WebClient client = WebClient.create(BASE_URI); final String result = client.post(msg, String.class); assertEquals(msg, result); assertEquals(4, tracer.finishedSpans().size()); final List<MockSpan> spans = tracer.finishedSpans(); for (final MockSpan span : spans) { assertEquals(AbstractSpanTagInterceptor.SPAN_TAG_VALUE, span.tags().get(AbstractSpanTagInterceptor.SPAN_TAG_KEY)); } server.destroy(); serverFactory.getBus().shutdown(true); }
Example 18
Source File: AbstractFailoverTest.java From cxf with Apache License 2.0 | 5 votes |
private static JAXRSClientFactoryBean createBean(String address, FailoverFeature feature) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); bean.setFeatures(Arrays.asList(feature)); return bean; }
Example 19
Source File: JAXRSMultipartTest.java From cxf with Apache License 2.0 | 4 votes |
@Test public void testXopWebClient() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/xop"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); bean.setProperties(Collections.singletonMap(org.apache.cxf.message.Message.MTOM_ENABLED, (Object)"true")); WebClient client = bean.createWebClient(); WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor()); WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor()); WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true"); client.type("multipart/related").accept("multipart/related"); XopType xop = new XopType(); xop.setName("xopName"); InputStream is = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd"); byte[] data = IOUtils.readBytesFromStream(is); xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"))); xop.setAttachInfoRef(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"))); String bookXsd = IOUtils.readStringFromStream(getClass().getResourceAsStream( "/org/apache/cxf/systest/jaxrs/resources/book.xsd")); xop.setAttachinfo2(bookXsd.getBytes()); xop.setImage(getImage("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); XopType xop2 = client.post(xop, XopType.class); String bookXsdOriginal = IOUtils.readStringFromStream(getClass().getResourceAsStream( "/org/apache/cxf/systest/jaxrs/resources/book.xsd")); String bookXsd2 = IOUtils.readStringFromStream(xop2.getAttachinfo().getInputStream()); assertEquals(bookXsdOriginal, bookXsd2); String bookXsdRef = IOUtils.readStringFromStream(xop2.getAttachInfoRef().getInputStream()); assertEquals(bookXsdOriginal, bookXsdRef); String ctString = client.getResponse().getMetadata().getFirst("Content-Type").toString(); MediaType mt = MediaType.valueOf(ctString); Map<String, String> params = mt.getParameters(); assertEquals(4, params.size()); assertNotNull(params.get("boundary")); assertNotNull(params.get("type")); assertNotNull(params.get("start")); assertNotNull(params.get("start-info")); }
Example 20
Source File: JAXRSXmlSecTest.java From cxf with Apache License 2.0 | 4 votes |
@Test public void testSignatureNoEncryption() throws Exception { if (test.streaming) { // Only testing the endpoints, not the clients here return; } String address = "https://localhost:" + test.port + "/xmlsec-validate"; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); SpringBusFactory bf = new SpringBusFactory(); URL busFile = JAXRSXmlSecTest.class.getResource("client.xml"); Bus springBus = bf.createBus(busFile.toString()); bean.setBus(springBus); Map<String, Object> properties = new HashMap<>(); properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback"); properties.put(SecurityConstants.ENCRYPT_USERNAME, "bob"); properties.put(SecurityConstants.ENCRYPT_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/bob.properties"); properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties"); bean.setProperties(properties); XmlSigOutInterceptor sigInterceptor = new XmlSigOutInterceptor(); bean.getOutInterceptors().add(sigInterceptor); bean.getInInterceptors().add(new XmlEncInInterceptor()); bean.getInInterceptors().add(new XmlSigInInterceptor()); bean.setServiceClass(BookStore.class); BookStore store = bean.create(BookStore.class); try { store.addBook(new Book("CXF", 126L)); fail("Failure expected on no Encryption"); } catch (WebApplicationException ex) { // expected } }