Java Code Examples for org.apache.cxf.jaxrs.client.WebClient#invoke()
The following examples show how to use
org.apache.cxf.jaxrs.client.WebClient#invoke() .
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: JAXRSAsyncClientTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testPatchBookTimeout() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/patch"; WebClient wc = WebClient.create(address); wc.type("application/xml"); ClientConfiguration clientConfig = WebClient.getConfig(wc); clientConfig.getRequestContext().put("use.async.http.conduit", true); HTTPClientPolicy clientPolicy = clientConfig.getHttpConduit().getClient(); clientPolicy.setReceiveTimeout(500); clientPolicy.setConnectionTimeout(500); try { Book book = wc.invoke("PATCH", new Book("Timeout", 123L), Book.class); fail("should throw an exception due to timeout, instead got " + book); } catch (javax.ws.rs.ProcessingException e) { //expected!!! } }
Example 2
Source File: JAXRSAsyncClientTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testRetrieveBookCustomMethodAsyncSync() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); // Setting this property is not needed given that // we have the async conduit loaded in the test module: // WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); wc.type("application/xml").accept("application/xml"); Book book = wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class); assertEquals("Retrieve", book.getName()); wc.close(); }
Example 3
Source File: JAXRSAsyncClientTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testPatchBook() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/patch"; WebClient wc = WebClient.create(address); wc.type("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("PATCH", new Book("Patch", 123L), Book.class); assertEquals("Patch", book.getName()); wc.close(); }
Example 4
Source File: JAXRSAsyncClientTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testPatchBookInputStream() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/patch"; WebClient wc = WebClient.create(address); wc.type("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("PATCH", new ByteArrayInputStream( "<Book><name>Patch</name><id>123</id></Book>".getBytes()), Book.class); assertEquals("Patch", book.getName()); wc.close(); }
Example 5
Source File: JAXRSAsyncClientTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testDeleteWithBody() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/deletebody"; WebClient wc = WebClient.create(address); wc.type("application/xml").accept("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("DELETE", new Book("Delete", 123L), Book.class); assertEquals("Delete", book.getName()); wc.close(); }
Example 6
Source File: JAXRSClientServerSpringBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testPostBookXsiType() throws Exception { String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore/books/xsitype"; JAXBElementProvider<Book> provider = new JAXBElementProvider<>(); provider.setExtraClass(new Class[]{SuperBook.class}); provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName())); WebClient wc = WebClient.create(address, Collections.singletonList(provider)); wc.accept("application/xml"); wc.type("application/xml"); SuperBook book = new SuperBook("SuperBook2", 999L, true); Book book2 = wc.invoke("POST", book, Book.class, Book.class); assertEquals("SuperBook2", book2.getName()); }
Example 7
Source File: JAXRSClientServerSpringBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testReaderWriterFromJaxrsFilters() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/the/thebooksWithStax/bookstore/books/convert2/123"; WebClient wc = WebClient.create(endpointAddress); wc.type("application/xml").accept("application/xml"); Book2 b = new Book2(); b.setId(777L); b.setName("CXF - 777"); Book2 b2 = wc.invoke("PUT", b, Book2.class); assertNotSame(b, b2); assertEquals(777, b2.getId()); assertEquals("CXF - 777", b2.getName()); }
Example 8
Source File: JAXRSClientServerSpringBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testReaderWriterFromInterceptors() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/the/thebooksWithStax/bookstore/books/convert"; WebClient wc = WebClient.create(endpointAddress); wc.type("application/xml").accept("application/xml"); Book2 b = new Book2(); b.setId(777L); b.setName("CXF - 777"); Book2 b2 = wc.invoke("POST", b, Book2.class); assertNotSame(b, b2); assertEquals(777, b2.getId()); assertEquals("CXF - 777", b2.getName()); }
Example 9
Source File: JAXRS20ClientServerBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testReplaceBookMistypedATAndHttpVerb() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books2/mistyped"; WebClient wc = WebClient.create(endpointAddress, Collections.singletonList(new ReplaceBodyFilter())); wc.accept("text/mistypedxml").type("text/xml").header("THEMETHOD", "PUT"); Book book = wc.invoke("DELETE", new Book("book", 555L), Book.class); assertEquals(561L, book.getId()); }
Example 10
Source File: JAXRS20ClientServerBookTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testReplaceBookMistypedATAndHttpVerb2() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books2/mistyped"; WebClient wc = WebClient.create(endpointAddress, Collections.singletonList(new ReplaceBodyFilter())); wc.accept("text/mistypedxml").header("THEMETHOD", "PUT"); Book book = wc.invoke("GET", null, Book.class); assertEquals(561L, book.getId()); }
Example 11
Source File: JAXRSClientServerBookTest.java From cxf with Apache License 2.0 | 5 votes |
private Book doRetrieveBook(boolean useReflection) { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); wc.type("application/xml").accept("application/xml"); if (!useReflection) { WebClient.getConfig(wc).getRequestContext().put("use.httpurlconnection.method.reflection", false); } // CXF RS Client code will set this property to true if the http verb is unknown // and this property is not already set. The async conduit is loaded in the tests module // but we do want to test HTTPUrlConnection reflection hence we set this property to false WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", false); WebClient.getConfig(wc).getRequestContext().put("response.stream.auto.close", true); return wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class); }
Example 12
Source File: BazaarRestClient.java From peer-os with Apache License 2.0 | 4 votes |
private <T> RestResult<T> execute( String httpMethod, String url, Object body, Class<T> clazz, boolean encrypt ) { log.info( "{} {}", httpMethod, url ); WebClient webClient = null; Response response = null; RestResult<T> restResult = new RestResult<>( HttpStatus.SC_INTERNAL_SERVER_ERROR ); try { webClient = configManager.getTrustedWebClientWithAuth( url, configManager.getBazaarIp() ); Object requestBody = encrypt ? encryptBody( body ) : body; response = webClient.invoke( httpMethod, requestBody ); // retry on 503 http code >>> int attemptNo = 1; while ( response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE && attemptNo < MAX_ATTEMPTS ) { attemptNo++; response = webClient.invoke( httpMethod, requestBody ); TaskUtil.sleep( 500 ); } // <<< retry on 503 http code log.info( "response.status: {} - {}", response.getStatus(), response.getStatusInfo().getReasonPhrase() ); restResult = handleResponse( response, clazz, encrypt ); } catch ( Exception e ) { if ( response != null ) { restResult.setReasonPhrase( response.getStatusInfo().getReasonPhrase() ); } Throwable rootCause = ExceptionUtil.getRootCauze( e ); if ( rootCause instanceof ConnectException || rootCause instanceof UnknownHostException || rootCause instanceof BindException || rootCause instanceof NoRouteToHostException || rootCause instanceof PortUnreachableException || rootCause instanceof SocketTimeoutException ) { restResult.setError( CONNECTION_EXCEPTION_MARKER ); } else { restResult.setError( ERROR + e.getMessage() ); } log.error( ERROR + e.getMessage() ); } finally { close( webClient, response ); } return restResult; }
Example 13
Source File: Services.java From olingo-odata4 with Apache License 2.0 | 4 votes |
private Response bodyPartRequest(final MimeBodyPart body, final Map<String, String> references) throws Exception { @SuppressWarnings("unchecked") final Enumeration<Header> en = body.getAllHeaders(); Header header = en.nextElement(); final String request = header.getName() + (StringUtils.isNotBlank(header.getValue()) ? ":" + header.getValue() : ""); final Matcher matcher = REQUEST_PATTERN.matcher(request); final Matcher matcherRef = BATCH_REQUEST_REF_PATTERN.matcher(request); final MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>(); while (en.hasMoreElements()) { header = en.nextElement(); headers.putSingle(header.getName(), header.getValue()); } final Response res; final String url; final String method; if (matcher.find()) { url = matcher.group(2); method = matcher.group(1); } else if (matcherRef.find()) { url = references.get(matcherRef.group(2)) + matcherRef.group(3); method = matcherRef.group(1); } else { url = null; method = null; } if (url == null) { res = null; } else { final WebClient client = WebClient.create(url, "odatajclient", "odatajclient", null); client.headers(headers); if ("DELETE".equals(method)) { res = client.delete(); } else { final InputStream is = body.getDataHandler().getInputStream(); String content = IOUtils.toString(is); IOUtils.closeQuietly(is); final Matcher refs = REF_PATTERN.matcher(content); while (refs.find()) { content = content.replace(refs.group(1), references.get(refs.group(1))); } if ("PATCH".equals(method) || "MERGE".equals(method)) { client.header("X-HTTP-METHOD", method); res = client.invoke("POST", IOUtils.toInputStream(content)); } else { res = client.invoke(method, IOUtils.toInputStream(content)); } } // When updating to CXF 3.0.1, uncomment the following line, see CXF-5865 // client.close(); } return res; }