private JAXRSClientFactoryBean prepareFactoryBean() throws Exception { JAXRSClientFactoryBean factoryBean = new JAXRSClientFactoryBean(); factoryBean.setAddress("locator://some_usefull_information"); factoryBean.setServiceName(new QName("http://common.demo/", "OrderService")); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "META-INF/tesb/locator/beans.xml" }); LocatorFeature feature = (LocatorFeature) context.getBean("locatorFeature"); factoryBean.setFeatures(Collections.singletonList(feature)); return factoryBean; }
private static JAXRSClientFactoryBean cleanFactory(JAXRSClientFactoryBean bean) { bean.setUsername(null); bean.setPassword(null); bean.setFeatures(Arrays.<AbstractFeature> asList()); return bean; }
private static JAXRSClientFactoryBean cleanFactory(JAXRSClientFactoryBean bean) { bean.setUsername(null); bean.setPassword(null); bean.setInitialState(null); bean.setFeatures(Arrays.<AbstractFeature>asList()); return bean; }
protected JAXRSClientFactoryBean defaultRestClientFactoryBean() { JAXRSClientFactoryBean defaultRestClientFactoryBean = new JAXRSClientFactoryBean(); defaultRestClientFactoryBean.setHeaders(new HashMap<>()); if (StringUtils.isBlank(address)) { throw new IllegalArgumentException("Property 'address' is missing"); } defaultRestClientFactoryBean.setAddress(address); if (StringUtils.isNotBlank(domain)) { defaultRestClientFactoryBean.getHeaders().put(RESTHeaders.DOMAIN, Collections.singletonList(domain)); } defaultRestClientFactoryBean.setThreadSafe(true); defaultRestClientFactoryBean.setInheritHeaders(true); List<Feature> features = new ArrayList<>(); features.add(new LoggingFeature()); defaultRestClientFactoryBean.setFeatures(features); List<Object> providers = new ArrayList<>(4); providers.add(new DateParamConverterProvider()); providers.add(getJaxbProvider()); providers.add(getJsonProvider()); providers.add(getExceptionMapper()); defaultRestClientFactoryBean.setProviders(providers); return defaultRestClientFactoryBean; }
protected JAXRSClientFactoryBean createBean(String address, FailoverFeature feature) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); List<Feature> features = new ArrayList<>(); features.add(feature); bean.setFeatures(features); return bean; }
/** * Creates WebClient with a list of custom features * @param baseAddress baseAddress * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource, can be null * @return WebClient instance */ public static WebClient create(String baseAddress, List<?> providers, List<? extends Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.createWebClient(); }
/** * Creates WebClient with a list of custom features * @param baseAddress baseAddress * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource, can be null * @return WebClient instance */ public static WebClient create(String baseAddress, List<?> providers, List<? extends Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.createWebClient(); }
/** * Creates a proxy * @param baseAddress baseAddress * @param cls proxy class, if not interface then a CGLIB proxy will be created * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource * @return typed proxy */ public static <T> T create(String baseAddress, Class<T> cls, List<?> providers, List<Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.create(cls); }
protected JAXRSClientFactoryBean defaultRestClientFactoryBean() { JAXRSClientFactoryBean defaultRestClientFactoryBean = new JAXRSClientFactoryBean(); defaultRestClientFactoryBean.setHeaders(new HashMap<>()); if (StringUtils.isBlank(address)) { throw new IllegalArgumentException("Property 'address' is missing"); } defaultRestClientFactoryBean.setAddress(address); if (StringUtils.isNotBlank(domain)) { defaultRestClientFactoryBean.getHeaders().put(RESTHeaders.DOMAIN, Collections.singletonList(domain)); } defaultRestClientFactoryBean.setThreadSafe(true); defaultRestClientFactoryBean.setInheritHeaders(true); List<Feature> features = new ArrayList<>(); features.add(new LoggingFeature()); defaultRestClientFactoryBean.setFeatures(features); List<Object> providers = new ArrayList<>(4); providers.add(new DateParamConverterProvider()); providers.add(getJaxbProvider()); providers.add(getJsonProvider()); providers.add(getExceptionMapper()); defaultRestClientFactoryBean.setProviders(providers); return defaultRestClientFactoryBean; }
/** * Creates a proxy * @param baseAddress baseAddress * @param cls proxy class, if not interface then a CGLIB proxy will be created * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource * @return typed proxy */ public static <T> T create(String baseAddress, Class<T> cls, List<?> providers, List<Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.create(cls); }
/** * Creates WebClient with a list of custom features * @param baseAddress baseAddress * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource, can be null * @return WebClient instance */ public static WebClient create(String baseAddress, List<?> providers, List<? extends Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.createWebClient(); }
/** * Creates a proxy * @param baseAddress baseAddress * @param cls proxy class, if not interface then a CGLIB proxy will be created * @param providers list of providers * @param features the features which will be applied to the client * @param configLocation classpath location of the configuration resource * @return typed proxy */ public static <T> T create(String baseAddress, Class<T> cls, List<?> providers, List<Feature> features, String configLocation) { JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation); bean.setProviders(providers); bean.setFeatures(features); return bean.create(cls); }
protected JAXRSClientFactoryBean createBean(String address, FailoverFeature feature) { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress(address); List<AbstractFeature> features = new ArrayList<>(); features.add(feature); bean.setFeatures(features); return bean; }
bean.setFeatures(Arrays.<AbstractFeature> asList(new LoggingFeature()));
bean.setFeatures(Arrays.<AbstractFeature>asList(new LoggingFeature()));
@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 testHelloRestValidationFailsIfNameIsNullClient() throws Exception { JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); bean.setAddress("http://localhost:" + PORT + "/bwrest"); bean.setServiceClass(BookWorld.class); bean.setFeatures(Arrays.asList(new JAXRSClientBeanValidationFeature())); BookWorld service = bean.create(BookWorld.class); BookWithValidation bw = service.echoBook(new BookWithValidation("RS", "123")); assertEquals("123", bw.getId()); try { service.echoBook(new BookWithValidation(null, "123")); fail("Validation failure expected"); } catch (ConstraintViolationException ex) { // complete } } @Test