Search in sources :

Example 26 with Book

use of org.apache.cxf.systest.jaxrs.Book in project cxf by apache.

the class FailoverWebClientTest method testFailover.

@Test
public void testFailover() throws Exception {
    String address = "http://localhost:" + PORT1 + "/bookstore";
    FailoverFeature failoverFeature = new FailoverFeature();
    SequentialStrategy strategy = new SequentialStrategy();
    List<String> addresses = new ArrayList<>();
    addresses.add("http://localhost:" + PORT2 + "/bookstore");
    addresses.add("http://localhost:" + PORT3 + "/bookstore");
    strategy.setAlternateAddresses(addresses);
    failoverFeature.setStrategy(strategy);
    WebClient webClient = WebClient.create(address, null, Collections.singletonList(failoverFeature), null).accept("application/xml");
    // Should hit PORT1
    Book b = webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals("http://localhost:" + PORT1 + "/bookstore", webClient.getBaseURI().toString());
    // Should failover to PORT2
    webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals("http://localhost:" + PORT2 + "/bookstore", webClient.getBaseURI().toString());
    // Should failover to PORT3
    webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals("http://localhost:" + PORT3 + "/bookstore", webClient.getBaseURI().toString());
}
Also used : SequentialStrategy(org.apache.cxf.clustering.SequentialStrategy) Book(org.apache.cxf.systest.jaxrs.Book) FailoverFeature(org.apache.cxf.clustering.FailoverFeature) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 27 with Book

use of org.apache.cxf.systest.jaxrs.Book in project cxf by apache.

the class BookServerWebSocket method run.

protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStoreWebSocket.class, BookStorePerRequest.class);
    sf.setProvider(new StreamingResponseProvider<Book>());
    sf.setResourceProvider(BookStoreWebSocket.class, new SingletonResourceProvider(new BookStoreWebSocket(), true));
    sf.setAddress("ws://localhost:" + port + "/websocket");
    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
Also used : Bus(org.apache.cxf.Bus) Book(org.apache.cxf.systest.jaxrs.Book) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 28 with Book

use of org.apache.cxf.systest.jaxrs.Book in project cxf by apache.

the class BookStoreWebSocket method getBookStream.

@GET
@Path("/bookstream")
@Produces("application/json")
public StreamingResponse<Book> getBookStream() {
    return new StreamingResponse<Book>() {

        public void writeTo(final StreamingResponse.Writer<Book> out) throws IOException {
            out.write(new Book("WebSocket1", 1));
            executor.execute(new Runnable() {

                public void run() {
                    try {
                        for (int i = 2; i <= 5; i++) {
                            Thread.sleep(500);
                            out.write(new Book("WebSocket" + i, i));
                            out.getEntityStream().flush();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    };
}
Also used : StreamingResponse(org.apache.cxf.jaxrs.ext.StreamingResponse) Book(org.apache.cxf.systest.jaxrs.Book) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 29 with Book

use of org.apache.cxf.systest.jaxrs.Book in project cxf by apache.

the class JAXRSClientConduitWebSocketTest method testBookWithWebSocket.

@Test
public void testBookWithWebSocket() throws Exception {
    String address = "ws://localhost:" + getPort() + "/websocket";
    BookStoreWebSocket resource = JAXRSClientFactory.create(address, BookStoreWebSocket.class);
    Client client = WebClient.client(resource);
    client.header(HttpHeaders.USER_AGENT, JAXRSClientConduitWebSocketTest.class.getName());
    // call the GET service
    assertEquals("CXF in Action", new String(resource.getBookName()));
    // call the GET service in text mode
    // TODO add some way to control the client to switch between the bytes and text modes
    assertEquals("CXF in Action", new String(resource.getBookName()));
    // call another GET service
    Book book = resource.getBook(123);
    assertEquals("CXF in Action", book.getName());
    // call the POST service
    assertEquals(Long.valueOf(123), resource.echoBookId(123));
    // call the same POST service in the text mode
    // TODO add some way to control the client to switch between the bytes and text modes
    assertEquals(Long.valueOf(123), resource.echoBookId(123));
// call the GET service returning a continous stream output
// TODO there is no way to get the continuous stream at the moment
// resource.getBookBought();
}
Also used : Book(org.apache.cxf.systest.jaxrs.Book) WebClient(org.apache.cxf.jaxrs.client.WebClient) Client(org.apache.cxf.jaxrs.client.Client) Test(org.junit.Test)

Example 30 with Book

use of org.apache.cxf.systest.jaxrs.Book in project cxf by apache.

the class JAXRSClientServerWebSocketSpringWebAppTest method testGetBookHTTP.

@Test
public void testGetBookHTTP() throws Exception {
    String address = "http://localhost:" + getPort() + getContext() + "/http/web/bookstore/books/1";
    WebClient wc = WebClient.create(address);
    wc.accept("application/xml");
    Book book = wc.get(Book.class);
    assertEquals(1L, book.getId());
}
Also used : Book(org.apache.cxf.systest.jaxrs.Book) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

Book (org.apache.cxf.systest.jaxrs.Book)46 Test (org.junit.Test)31 WebClient (org.apache.cxf.jaxrs.client.WebClient)22 BookStore (org.apache.cxf.systest.jaxrs.BookStore)8 JMSBookStore (org.apache.cxf.systest.jaxrs.JMSBookStore)6 SequentialStrategy (org.apache.cxf.clustering.SequentialStrategy)5 Response (javax.ws.rs.core.Response)4 ArrayList (java.util.ArrayList)3 BytesMessage (javax.jms.BytesMessage)3 JAXBContext (javax.xml.bind.JAXBContext)3 InputStream (java.io.InputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 MessageProducer (javax.jms.MessageProducer)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 ProcessingException (javax.ws.rs.ProcessingException)2 Client (javax.ws.rs.client.Client)2 ClientBuilder (javax.ws.rs.client.ClientBuilder)2 WebTarget (javax.ws.rs.client.WebTarget)2 Form (javax.ws.rs.core.Form)2