Search in sources :

Example 81 with MediaType

use of javax.ws.rs.core.MediaType in project ddf by codice.

the class BodyWriter method writeBody.

<T> void writeBody(T o, Message outMessage, Class<?> cls, Type type, Annotation[] anns, OutputStream os) {
    if (o == null) {
        return;
    }
    @SuppressWarnings("unchecked") MultivaluedMap<String, Object> headers = (MultivaluedMap<String, Object>) outMessage.get(Message.PROTOCOL_HEADERS);
    @SuppressWarnings("unchecked") Class<T> theClass = (Class<T>) cls;
    MediaType contentType = JAXRSUtils.toMediaType(headers.getFirst("Content-Type").toString());
    List<WriterInterceptor> writers = ClientProviderFactory.getInstance(outMessage).createMessageBodyWriterInterceptor(theClass, type, anns, contentType, outMessage, null);
    if (writers != null) {
        try {
            JAXRSUtils.writeMessageBody(writers, o, theClass, type, anns, contentType, headers, outMessage);
            OutputStream realOs = outMessage.get(OutputStream.class);
            if (realOs != null) {
                realOs.flush();
            }
        } catch (Exception ex) {
            LOGGER.debug("Unable to write message body for final ECP response.");
        }
    } else {
        LOGGER.debug("No writers available for final ECP response");
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) OutputStream(java.io.OutputStream) MediaType(javax.ws.rs.core.MediaType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 82 with MediaType

use of javax.ws.rs.core.MediaType in project ddf by codice.

the class TestGetRecordsMessageBodyReader method testReadProductData.

@Test
public void testReadProductData() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    String sampleData = "SampleData";
    byte[] data = sampleData.getBytes();
    ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data);
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
    httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
    MediaType mediaType = new MediaType("text", "plain");
    CswRecordCollection cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
    Resource resource = cswRecords.getResource();
    assertThat(resource, notNullValue());
    assertThat(resource.getName(), is("ResourceName"));
    assertThat(resource.getMimeType().toString(), is(MediaType.TEXT_PLAIN));
    assertThat(resource.getByteArray(), is(data));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Resource(ddf.catalog.resource.Resource) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 83 with MediaType

use of javax.ws.rs.core.MediaType in project ddf by codice.

the class TestGetRecordsMessageBodyReader method testPartialContentNotSupportedHandling.

@Test
public void testPartialContentNotSupportedHandling() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    String sampleData = "SampleData";
    byte[] data = sampleData.getBytes();
    ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data);
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
    httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
    MediaType mediaType = new MediaType("text", "plain");
    CswRecordCollection cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
    Resource resource = cswRecords.getResource();
    // assert that the CswRecordCollection property is not set if the server does not support Partial Content responses
    assertThat(cswRecords.getResourceProperties().get(GetRecordsMessageBodyReader.BYTES_SKIPPED), nullValue());
    // assert that the input stream has not been skipped at this stage. Since AbstractCswSource has the number
    // of bytes that was attempted to be skipped, the stream must be aligned there instead.
    assertThat(resource.getByteArray(), is(data));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Resource(ddf.catalog.resource.Resource) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 84 with MediaType

use of javax.ws.rs.core.MediaType in project cxf by apache.

the class AtomPojoProviderTest method testReadEntryNoBuilders2.

@Test
public void testReadEntryNoBuilders2() throws Exception {
    final String entry = "<!DOCTYPE entry SYSTEM \"entry://entry\"><entry xmlns=\"http://www.w3.org/2005/Atom\">" + "<title type=\"text\">a</title>" + "<content type=\"application/xml\">" + "<book xmlns=\"\">" + "<name>a</name>" + "</book>" + "</content>" + "</entry>";
    AtomPojoProvider provider = new AtomPojoProvider();
    ByteArrayInputStream bis = new ByteArrayInputStream(entry.getBytes());
    MediaType mt = MediaType.valueOf("application/atom+xml;type=entry");
    @SuppressWarnings({ "unchecked", "rawtypes" }) Book book = (Book) provider.readFrom((Class) Book.class, Book.class, new Annotation[] {}, mt, null, bis);
    assertEquals("a", book.getName());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(javax.ws.rs.core.MediaType) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 85 with MediaType

use of javax.ws.rs.core.MediaType in project cxf by apache.

the class AtomPojoProviderTest method doTestReadFeed.

private void doTestReadFeed(AtomPojoProvider provider) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    MediaType mt = MediaType.valueOf("application/atom+xml;type=feed");
    Books books = new Books();
    List<Book> bs = new ArrayList<>();
    bs.add(new Book("a"));
    bs.add(new Book("b"));
    books.setBooks(bs);
    provider.writeTo(books, Books.class, Books.class, new Annotation[] {}, mt, null, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    @SuppressWarnings({ "unchecked", "rawtypes" }) Books books2 = (Books) provider.readFrom((Class) Books.class, Books.class, new Annotation[] {}, mt, null, bis);
    List<Book> list = books2.getBooks();
    assertEquals(2, list.size());
    assertTrue("a".equals(list.get(0).getName()) || "a".equals(list.get(1).getName()));
    assertTrue("b".equals(list.get(0).getName()) || "b".equals(list.get(1).getName()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation)

Aggregations

MediaType (javax.ws.rs.core.MediaType)477 Test (org.junit.Test)184 Path (javax.ws.rs.Path)44 Produces (javax.ws.rs.Produces)44 ByteArrayInputStream (java.io.ByteArrayInputStream)42 WebApplicationException (javax.ws.rs.WebApplicationException)41 IOException (java.io.IOException)40 Response (javax.ws.rs.core.Response)40 InputStream (java.io.InputStream)38 ArrayList (java.util.ArrayList)31 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)31 Type (java.lang.reflect.Type)30 Consumes (javax.ws.rs.Consumes)27 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)27 GET (javax.ws.rs.GET)25 OutputStream (java.io.OutputStream)23 Annotation (java.lang.annotation.Annotation)23 HashSet (java.util.HashSet)22 Locale (java.util.Locale)22 HashMap (java.util.HashMap)21