use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project cxf by apache.
the class JAXRSMultipartTest method doTestGetBookAsPlainContent.
private void doTestGetBookAsPlainContent(String address) throws Exception {
WebClient wc = WebClient.create(address);
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
wc.accept("multipart/mixed");
MultipartBody book = wc.get(MultipartBody.class);
Book b = book.getRootAttachment().getObject(Book.class);
assertEquals(888L, b.getId());
}
use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project karaf by apache.
the class BookingServiceRest method traceMultipart.
@Path("/all")
@Consumes("multipart/mixed")
@Produces("multipart/mixed")
@POST
public MultipartBody traceMultipart(List<Attachment> atts) {
final List<Attachment> results = new ArrayList<>();
final Attachment att1 = new Attachment("text", MediaType.TEXT_HTML, "Hello World!");
results.add(att1);
Booking b1 = new Booking();
b1.setCustomer("me");
b1.setFlight("far far away");
b1.setId(42L);
final Attachment att2 = new Attachment("json", MediaType.APPLICATION_JSON, b1);
results.add(att2);
final Attachment att3 = new Attachment("stream", MediaType.APPLICATION_OCTET_STREAM, new ByteArrayInputStream("important information".getBytes()));
results.add(att3);
results.addAll(atts);
return new MultipartBody(results, true);
}
use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project ddf by codice.
the class CatalogServiceImplTest method testAddDocumentWithAttributeOverrides.
@Test
@SuppressWarnings({ "unchecked" })
public void testAddDocumentWithAttributeOverrides() throws Exception {
CatalogFramework framework = givenCatalogFramework();
AttributeDescriptor descriptor = new AttributeDescriptorImpl("custom.attribute", true, true, false, false, BasicTypes.STRING_TYPE);
HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
BundleContext bundleContext = mock(BundleContext.class);
Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
ServiceReference serviceReference = mock(ServiceReference.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(any())).thenReturn(new MetacardImpl());
when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
serviceReferences.add(serviceReference);
when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
when(attributeRegistry.lookup("custom.attribute")).thenReturn(Optional.of(descriptor));
CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
UuidGenerator uuidGenerator = mock(UuidGenerator.class);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
catalogService.setUuidGenerator(uuidGenerator);
addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
attachments.add(attachment);
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=custom.attribute; ");
Attachment attachment2 = new Attachment(descriptor.getName(), new ByteArrayInputStream("CustomValue".getBytes()), contentDisposition2);
attachments.add(attachment2);
MultipartBody multipartBody = new MultipartBody(attachments);
String response = catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
LOGGER.debug(ToStringBuilder.reflectionToString(response));
ArgumentCaptor<CreateStorageRequest> captor = ArgumentCaptor.forClass(CreateStorageRequest.class);
verify(framework, times(1)).create(captor.capture());
assertThat(captor.getValue().getContentItems().get(0).getMetacard().getMetacardType().getAttributeDescriptor(descriptor.getName()), equalTo(descriptor));
}
use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project ddf by codice.
the class CatalogServiceImplTest method mcardIdTest.
private String mcardIdTest(Metacard metacard, UuidGenerator uuidGenerator) throws Exception {
CatalogFramework framework = mock(CatalogFramework.class);
when(framework.create(isA(CreateStorageRequest.class))).thenAnswer(args -> {
ContentItem item = ((CreateStorageRequest) args.getArguments()[0]).getContentItems().get(0);
item.getMetacard().setAttribute(new AttributeImpl(Core.ID, item.getId()));
return new CreateResponseImpl(null, new HashMap<>(), Collections.singletonList(item.getMetacard()));
});
HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
BundleContext bundleContext = mock(BundleContext.class);
Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
ServiceReference serviceReference = mock(ServiceReference.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(any())).thenReturn(metacard);
when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
serviceReferences.add(serviceReference);
when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
String generatedMcardId = UUID.randomUUID().toString();
when(uuidGenerator.generateUuid()).thenReturn(generatedMcardId);
catalogService.setUuidGenerator(uuidGenerator);
when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
addMatchingService(catalogService, Collections.singletonList(inputTransformer));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
attachments.add(attachment);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
attachments.add(attachment1);
MultipartBody multipartBody = new MultipartBody(attachments);
return catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
}
use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tesb-rt-se by Talend.
the class MultipartsServiceImpl method createMultipartBody.
private MultipartBody createMultipartBody(Book jaxbBook, Book jsonBook, DataHandler imageHandler) {
List<Attachment> atts = new LinkedList<Attachment>();
// One of the ways to create an individual part is to use
// a Content-Id, Content-Type and the object triple.
// Multipart Provider will delegate to a matching JAX-RS
// MessageBodyWriter
atts.add(new Attachment("book1", "application/xml", jaxbBook));
atts.add(new Attachment("book2", "application/json", jsonBook));
try {
atts.add(new Attachment("image", "application/octet-stream", imageHandler.getInputStream()));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return new MultipartBody(atts, true);
}
Aggregations