Search in sources :

Example 31 with Book

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

the class LoadDistributorTest method testSingleAltAddress.

@Test
public void testSingleAltAddress() throws Exception {
    LoadDistributorFeature feature = new LoadDistributorFeature();
    List<String> alternateAddresses = new ArrayList<>();
    alternateAddresses.add(Server.ADDRESS2);
    SequentialStrategy strategy = new SequentialStrategy();
    strategy.setAlternateAddresses(alternateAddresses);
    feature.setStrategy(strategy);
    BookStore bookStore = getBookStore(Server.ADDRESS1, feature);
    Book book = bookStore.getBook("123");
    assertEquals("unexpected id", 123L, book.getId());
    book = bookStore.getBook("123");
    assertEquals("unexpected id", 123L, book.getId());
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) SequentialStrategy(org.apache.cxf.clustering.SequentialStrategy) Book(org.apache.cxf.systest.jaxrs.Book) LoadDistributorFeature(org.apache.cxf.clustering.LoadDistributorFeature) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with Book

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

the class JAXRSJmsTest method testGetBookFromWebClient.

@Test
public void testGetBookFromWebClient() throws Exception {
    // setup the the client
    String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?replyToName=dynamicQueues/test.jmstransport.response" + "&jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&jndiURL=tcp://localhost:" + JMS_PORT;
    WebClient client = WebClient.create(endpointAddressUrlEncoded);
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books/123");
    Book book = client.get(Book.class);
    assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus());
    assertEquals("Get a wrong book id.", 123, book.getId());
}
Also used : Book(org.apache.cxf.systest.jaxrs.Book) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 33 with Book

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

the class JAXRSJmsTest method postBook.

private void postBook(Session session, Destination destination, Destination replyTo) throws Exception {
    MessageProducer producer = session.createProducer(destination);
    byte[] payload = writeBook(new Book("JMS", 3L));
    BytesMessage message = session.createBytesMessage();
    message.writeBytes(payload);
    message.setJMSReplyTo(replyTo);
    // or, if oneway,
    // message.setStringProperty("OnewayRequest", "true");
    // we could've set this header in JMSDestination if no replyTo were set
    // but in CXF one could also provide the replyTo in the configuration
    // so it is just simpler to set this header if needed to avoid some
    // complex logic on the server side
    // all these properties are optional
    // CXF JAXRS and JMS Transport will default to
    // Content-Type : text/xml
    // Accept : */*
    // POST
    // Message.REQUEST_URI : "/"
    message.setStringProperty("Content-Type", "application/xml");
    message.setStringProperty("Accept", "text/xml");
    message.setStringProperty(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books");
    message.setStringProperty(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD, "POST");
    message.setStringProperty("custom.protocol.header", "custom.value");
    producer.send(message);
    producer.close();
}
Also used : Book(org.apache.cxf.systest.jaxrs.Book) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer)

Example 34 with Book

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

the class JAXRSJmsTest method testPutBookOneWayWithWebClient.

@Test
public void testPutBookOneWayWithWebClient() throws Exception {
    // setup the the client
    String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text" + "?replyToName=dynamicQueues/test.jmstransport.response" + "&jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory" + "&jndiURL=tcp://localhost:" + JMS_PORT;
    WebClient client = WebClient.create(endpointAddressUrlEncoded);
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/oneway");
    client.header("OnewayRequest", "true");
    Response r = client.type("application/xml").put(new Book("OneWay From WebClient", 129L));
    assertEquals(202, r.getStatus());
    assertFalse(r.hasEntity());
    Context ctx = getContext();
    ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    Destination replyToDestination = (Destination) ctx.lookup("dynamicQueues/test.jmstransport.response");
    Connection connection = null;
    try {
        connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        checkBookInResponse(session, replyToDestination, 129L, "OneWay From WebClient");
        session.close();
    } finally {
        close(connection);
    }
}
Also used : Response(javax.ws.rs.core.Response) Context(javax.naming.Context) JAXBContext(javax.xml.bind.JAXBContext) InitialContext(javax.naming.InitialContext) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) Book(org.apache.cxf.systest.jaxrs.Book) Connection(javax.jms.Connection) WebClient(org.apache.cxf.jaxrs.client.WebClient) Session(javax.jms.Session) Test(org.junit.Test)

Example 35 with Book

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

the class JAXRSJmsTest method postOneWayBook.

private void postOneWayBook(Session session, Destination destination) throws Exception {
    MessageProducer producer = session.createProducer(destination);
    byte[] payload = writeBook(new Book("JMS OneWay", 125L));
    BytesMessage message = session.createBytesMessage();
    message.writeBytes(payload);
    message.setStringProperty("Content-Type", "application/xml");
    message.setStringProperty(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/oneway");
    message.setStringProperty(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD, "PUT");
    producer.send(message);
    producer.close();
}
Also used : Book(org.apache.cxf.systest.jaxrs.Book) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer)

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