use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class DataSourceProviderTest method testWriteDataSource.
@Test
public void testWriteDataSource() throws Exception {
DataSourceProvider<DataSource> p = new DataSourceProvider<DataSource>();
DataSource ds = new InputStreamDataSource(new ByteArrayInputStream("image".getBytes()), "image/png");
MultivaluedMap<String, Object> outHeaders = new MetadataMap<String, Object>();
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
p.writeTo(ds, DataSource.class, DataSource.class, new Annotation[] {}, MediaType.valueOf("image/png"), outHeaders, os);
assertEquals("image", os.toString());
}
assertEquals(0, outHeaders.size());
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class JAXBElementProviderTest method doTestObjectFactoryExtraClass.
private void doTestObjectFactoryExtraClass(JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider) throws Exception {
org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 b = new org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3("CXF in Action", 123L, 124L);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider2 = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
provider2.setExtraClass(new Class[] { org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class });
ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 book = (org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3) provider2.readFrom(org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
assertEquals(124L, book.getSuperId());
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class PrimitiveTextProviderTest method testWriteStringISO.
@Test
public void testWriteStringISO() throws Exception {
MessageBodyWriter<String> p = new PrimitiveTextProvider<String>();
ByteArrayOutputStream os = new ByteArrayOutputStream();
MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
String eWithAcute = "\u00E9";
String helloStringUTF16 = "Hello, my name is F" + eWithAcute + "lix Agn" + eWithAcute + "s";
p.writeTo(helloStringUTF16, String.class, String.class, null, MediaType.valueOf("text/plain;charset=ISO-8859-1"), headers, os);
byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
assertEquals(helloStringISO88591, os.toString("ISO-8859-1"));
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class CustomOutInterceptor method handleMessage.
@SuppressWarnings("unchecked")
public void handleMessage(Message message) throws Fault {
String requestUri = (String) message.getExchange().getInMessage().get(Message.REQUEST_URI);
if (requestUri.endsWith("/outfault")) {
throw new WebApplicationException(403);
}
HttpHeaders requestHeaders = new HttpHeadersImpl(message.getExchange().getInMessage());
if (requestHeaders.getHeaderString("PLAIN-MAP") != null) {
Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
if (headers == null) {
headers = new HashMap<>();
message.put(Message.PROTOCOL_HEADERS, headers);
}
headers.put("BookId", Arrays.asList("321"));
headers.put("MAP-NAME", Arrays.asList(Map.class.getName()));
message.put(Message.PROTOCOL_HEADERS, headers);
} else {
MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
headers.putSingle("BookId", "123");
headers.putSingle("MAP-NAME", MultivaluedMap.class.getName());
message.put(Message.PROTOCOL_HEADERS, headers);
}
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class JAXRSMultipartTest method testUploadImageFromForm.
@Test
public void testUploadImageFromForm() throws Exception {
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
String address = "http://localhost:" + PORT + "/bookstore/books/formimage";
WebClient client = WebClient.create(address);
HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
conduit.getClient().setReceiveTimeout(1000000);
conduit.getClient().setConnectionTimeout(1000000);
client.type("multipart/form-data").accept("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=java.jpg");
MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
headers.putSingle("Content-ID", "image");
headers.putSingle("Content-Disposition", cd.toString());
headers.putSingle("Content-Location", "http://host/bar");
headers.putSingle("custom-header", "custom");
Attachment att = new Attachment(is1, headers);
MultipartBody body = new MultipartBody(att);
MultipartBody body2 = client.post(body, MultipartBody.class);
InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream();
byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
byte[] image2 = IOUtils.readBytesFromStream(is2);
assertTrue(Arrays.equals(image1, image2));
ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition();
assertEquals("attachment;filename=java.jpg", cd2.toString());
assertEquals("java.jpg", cd2.getParameter("filename"));
assertEquals("http://host/location", body2.getRootAttachment().getHeader("Content-Location"));
}
Aggregations