Search in sources :

Example 11 with MultipartBody

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());
}
Also used : MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 12 with MultipartBody

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Booking(org.apache.karaf.examples.rest.api.Booking)

Example 13 with MultipartBody

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));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) BundleContext(org.osgi.framework.BundleContext) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) Test(org.junit.Test)

Example 14 with MultipartBody

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()));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) ContentItem(ddf.catalog.content.data.ContentItem) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl) BundleContext(org.osgi.framework.BundleContext)

Example 15 with MultipartBody

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);
}
Also used : MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Aggregations

MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)35 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 WebClient (org.apache.cxf.jaxrs.client.WebClient)7 CatalogFramework (ddf.catalog.CatalogFramework)6 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 Response (javax.ws.rs.core.Response)5 InputTransformer (ddf.catalog.transform.InputTransformer)4 LinkedHashMap (java.util.LinkedHashMap)4 HttpHeaders (javax.ws.rs.core.HttpHeaders)4 MediaType (javax.ws.rs.core.MediaType)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 Map (java.util.Map)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3