JAXRSClientFactoryBean sf = new JAXRSClientFactoryBean(); sf.setResourceClass(serviceClass); sf.setAddress("http://" + getServerUrl() + "/cxf/" + serviceName); sf.setProviders(providers); ConduitInitiatorManager cim = sf.getBus().getExtension(ConduitInitiatorManager.class); cim.registerConduitInitiator("http://cxf.apache.org/transports/http", new HTTPTransportFactory(sf.getBus())); service = sf.create(serviceClass);
@Override public JAXRSClientFactoryBean load(Class<?> proxyType) throws Exception { JAXRSClientFactoryBean clientFactoryBean = new JAXRSClientFactoryBean(); clientFactoryBean.setResourceClass(proxyType); clientFactoryBean.setProvider(new TextJacksonJsonProvider(new ApiObjectMapper())); return clientFactoryBean; } });
@Override public JAXRSClientFactoryBean load(Class<?> proxyType) throws Exception { JAXRSClientFactoryBean clientFactoryBean = new JAXRSClientFactoryBean(); clientFactoryBean.setResourceClass(proxyType); // Configuring the deserialization to not fail on unknown properties. // This ensures backward compatibility of APIs especially with BDR. OPSAPS-45062 clientFactoryBean.setProvider(new JacksonJsonProvider(( new ApiObjectMapper()).configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false))); return clientFactoryBean; } });
protected void setupJAXRSClientFactoryBean(JAXRSClientFactoryBean cfb, String address) { if (modelRef != null) { cfb.setModelRef(modelRef); } if (getResourceClasses() != null && !getResourceClasses().isEmpty()) { cfb.setResourceClass(getResourceClasses().get(0)); cfb.getServiceFactory().setResourceClasses(getResourceClasses()); } setupCommonFactoryProperties(cfb); cfb.setThreadSafe(true); getNullSafeCxfRsEndpointConfigurer().configure(cfb); // Add the address could be override by message header if (address != null) { cfb.setAddress(address); } }
/** * This method ... * * @param resourceClass * @return */ private <R> R getServiceProxy(Class<R> resourceClass) { JAXRSClientFactoryBean clientFactory; clientFactory = new JAXRSClientFactoryBean(); clientFactory.setBus(KSBServiceLocator.getCXFBus()); clientFactory.setResourceClass(resourceClass); clientFactory.setAddress(actualEndpointUrl.toString()); BindingFactoryManager bindingFactoryManager = KSBServiceLocator.getCXFBus().getExtension(BindingFactoryManager.class); JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); bindingFactory.setBus(KSBServiceLocator.getCXFBus()); bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); //Set logging interceptors if (LOG.isDebugEnabled()) { clientFactory.getOutInterceptors().add(new LoggingOutInterceptor()); } if (getCredentialsSource() != null) { clientFactory.getOutInterceptors().add(new CredentialsOutHandler(getCredentialsSource(), serviceConfiguration)); } if (LOG.isDebugEnabled()) { clientFactory.getInInterceptors().add(new LoggingInInterceptor()); } Object service = clientFactory.create(); return getServiceProxyWithFailureMode(resourceClass, service, serviceConfiguration); }
@Test public void testGetStringList() throws Exception { String address = "http://localhost:" + PORT; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setProvider(new BookStore.StringListBodyReaderWriter()); bean.setAddress(address); bean.setResourceClass(BookStore.class); BookStore store = bean.create(BookStore.class); List<String> str = store.getBookListArray(); assertEquals("Good book", str.get(0)); }
@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()); }
@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()); assertTrue("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()); } }
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 { assertTrue("In Interceptor not invoked", !testFeature.handleMessageOnInInterceptorCalled()); } assertTrue("Client In Fault In Interceptor not invoked", testFeature.faultInInterceptorCalled()); } }
@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()); }
@Test public void testGetStringArray() throws Exception { String address = "http://localhost:" + PORT; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setProvider(new BookStore.StringArrayBodyReaderWriter()); bean.setAddress(address); bean.setResourceClass(BookStore.class); BookStore store = bean.create(BookStore.class); String[] str = store.getBookStringArray(); assertEquals("Good book", str[0]); }
@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); }
@Test public void testGetPrimitiveIntArray() throws Exception { String address = "http://localhost:" + PORT; JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setProvider(new BookStore.PrimitiveIntArrayReaderWriter()); bean.setAddress(address); bean.setResourceClass(BookStore.class); BookStore store = bean.create(BookStore.class); int[] arr = store.getBookIndexAsIntArray(); assertEquals(3, arr.length); assertEquals(1, arr[0]); assertEquals(2, arr[1]); assertEquals(3, arr[2]); }