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");
}
}
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));
}
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));
}
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());
}
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()));
}
Aggregations