Search in sources :

Example 31 with JacksonJsonProvider

use of com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider in project cxf by apache.

the class AbstractSseTest method testBooksStreamIsReturnedFromInboundSseEventsWithPOST.

@SuppressWarnings("unchecked")
@Test
public void testBooksStreamIsReturnedFromInboundSseEventsWithPOST() throws InterruptedException, IOException {
    final WebTarget target = createWebTarget("/rest/api/bookstore/sse/0");
    final Collection<Book> books = new ArrayList<>();
    @SuppressWarnings("rawtypes") MessageBodyReader mbr = new JacksonJsonProvider();
    Response response = target.request(MediaType.SERVER_SENT_EVENTS).post(Entity.entity(42, MediaType.TEXT_PLAIN));
    try (BufferedReader br = new BufferedReader(new InputStreamReader(response.readEntity(InputStream.class)))) {
        String s;
        Integer id = null;
        Book book = null;
        while ((s = br.readLine()) != null) {
            if (s.trim().isEmpty()) {
                if (id == null && book == null) {
                    continue;
                } else if (id != null && book != null) {
                    books.add(book);
                    id = null;
                    book = null;
                    continue;
                }
                fail("The event did not contain both an id " + id + " and a book " + book);
            }
            if (s.startsWith("event:")) {
                assertEquals("Not a book event", "event: book", s.trim());
                continue;
            }
            if (s.startsWith("id:")) {
                assertNull("There was an existing id " + id, id);
                id = Integer.parseInt(s.substring(3).trim());
                continue;
            }
            if (s.startsWith("data:")) {
                assertNull("There was an existing book " + book, book);
                book = (Book) mbr.readFrom(Book.class, Book.class, null, MediaType.APPLICATION_JSON_TYPE, null, new ByteArrayInputStream(s.substring(5).trim().getBytes(StandardCharsets.UTF_8)));
                continue;
            }
            fail("Unexpected String content returned by SSE POST " + s);
        }
    }
    // Easing the test verification here, it does not work well for Atm + Jetty
    assertThat(books, hasItems(new Book("New Book #43", 43), new Book("New Book #44", 44), new Book("New Book #45", 45), new Book("New Book #46", 46)));
}
Also used : InputStreamReader(java.io.InputStreamReader) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Response(javax.ws.rs.core.Response) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 32 with JacksonJsonProvider

use of com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider in project cxf by apache.

the class BookStoreCustomApplication method getSingletons.

@Override
public Set<Object> getSingletons() {
    Set<Object> singletons = new HashSet<>();
    singletons.add(new JacksonJsonProvider());
    singletons.add(new ValidationExceptionMapper());
    singletons.add(new JAXRSBeanValidationFeature());
    singletons.add(new LoggingFilter());
    return singletons;
}
Also used : JAXRSBeanValidationFeature(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationFeature) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) LoggingFilter(org.apache.cxf.systests.cdi.base.bindings.LoggingFilter) ValidationExceptionMapper(org.apache.cxf.jaxrs.validation.ValidationExceptionMapper) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 33 with JacksonJsonProvider

use of com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider in project cxf by apache.

the class JAXRSClientServerTikaTest method createWebClient.

private WebClient createWebClient(final String url) {
    WebClient wc = WebClient.create("http://localhost:" + PORT + url, Arrays.asList(new MultipartProvider(), new JacksonJsonProvider()));
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
    return wc;
}
Also used : JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) MultipartProvider(org.apache.cxf.jaxrs.provider.MultipartProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 34 with JacksonJsonProvider

use of com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider in project cxf by apache.

the class JweJwsReferenceTest method testSignatureIncludeCertSha1.

@org.junit.Test
public void testSignatureIncludeCertSha1() throws Exception {
    URL busFile = JweJwsReferenceTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new JwsWriterInterceptor());
    String address = "http://localhost:" + PORT + "/jwsincludecertsha1/bookstore/books";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jks");
    properties.put("rs.security.keystore.alias", "alice");
    properties.put("rs.security.keystore.password", "password");
    properties.put("rs.security.key.password", "password");
    properties.put("rs.security.keystore.file", "keys/alice.jks");
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    // First test that it fails without adding a cert (reference). This is because
    // the service side does not have an alias configured
    Response response = client.post(new Book("book", 123L));
    assertNotEquals(response.getStatus(), 200);
    // Now it should work
    properties.put("rs.security.signature.include.cert.sha1", "true");
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    response = client.post(new Book("book", 123L));
    assertEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) Book(org.apache.cxf.systest.jaxrs.security.Book) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) JwsWriterInterceptor(org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 35 with JacksonJsonProvider

use of com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider in project cxf by apache.

the class JWTAlgorithmTest method testBadSignatureCertificateTest.

// Include the cert in the "x5c" header
@org.junit.Test
public void testBadSignatureCertificateTest() throws Exception {
    URL busFile = JWTAlgorithmTest.class.getResource("client.xml");
    List<Object> providers = new ArrayList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new JwtAuthenticationClientFilter());
    String address = "https://localhost:" + PORT + "/signedjwtincludecert/bookstore/books";
    WebClient client = WebClient.create(address, providers, busFile.toString());
    client.type("application/json").accept("application/json");
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject("alice");
    claims.setIssuer("DoubleItSTSIssuer");
    claims.setIssuedAt(Instant.now().getEpochSecond());
    claims.setAudiences(toList(address));
    JwtToken token = new JwtToken(claims);
    Map<String, Object> properties = new HashMap<>();
    properties.put("rs.security.keystore.type", "jks");
    properties.put("rs.security.keystore.password", "password");
    properties.put("rs.security.key.password", "password");
    properties.put("rs.security.keystore.alias", "bethal");
    properties.put("rs.security.keystore.file", "keys/Bethal.jks");
    properties.put("rs.security.signature.algorithm", "RS256");
    properties.put("rs.security.signature.include.cert", "true");
    properties.put(JwtConstants.JWT_TOKEN, token);
    WebClient.getConfig(client).getRequestContext().putAll(properties);
    Response response = client.post(new Book("book", 123L));
    assertNotEquals(response.getStatus(), 200);
}
Also used : JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) HashMap(java.util.HashMap) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.security.Book) JwtAuthenticationClientFilter(org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)

Aggregations

JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)217 WebClient (org.apache.cxf.jaxrs.client.WebClient)149 Response (javax.ws.rs.core.Response)127 ArrayList (java.util.ArrayList)109 HashMap (java.util.HashMap)104 URL (java.net.URL)103 Book (org.apache.cxf.systest.jaxrs.security.Book)76 Test (org.junit.Test)66 JwtAuthenticationClientFilter (org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)50 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)50 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)45 ClientBuilder (javax.ws.rs.client.ClientBuilder)28 Number (org.apache.coheigea.cxf.jaxrs.json.common.Number)28 List (java.util.List)27 JwsWriterInterceptor (org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor)27 LinkedList (java.util.LinkedList)26 GenericType (javax.ws.rs.core.GenericType)26 MediaType (javax.ws.rs.core.MediaType)26 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)26 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)26