Search in sources :

Example 1 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project jersey by jersey.

the class Jersey2776ITCase method testThatMultipartServerSupportsBoundaryQuotesEvenWithInterferingRuntimeDelegate.

@Test
public void testThatMultipartServerSupportsBoundaryQuotesEvenWithInterferingRuntimeDelegate() {
    final JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(getBaseUri().toString());
    bean.setServiceClass(ApacheCxfMultipartClient.class);
    final ApacheCxfMultipartClient cxfClient = bean.create(ApacheCxfMultipartClient.class);
    final String originalContent = "abc";
    final byte[] content = originalContent.getBytes(StandardCharsets.US_ASCII);
    final Attachment fileAttachment = new AttachmentBuilder().object(content).contentDisposition(new ContentDisposition("form-data; filename=\"abc-file\"; name=\"file_path\"")).build();
    final String fileContentReturnedFromServer = cxfClient.uploadDocument(new MultipartBody(fileAttachment));
    assertThat(fileContentReturnedFromServer, equalTo(originalContent));
}
Also used : JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) AttachmentBuilder(org.apache.cxf.jaxrs.ext.multipart.AttachmentBuilder) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.

the class ApplicationUploadEndpointTest method testApplicationUploadEndpointUpdateFileNotFound.

/**
     * Tests the {@link ApplicationUploadEndpoint#update(MultipartBody, UriInfo)} method
     * for the case where the file can not be found
     *
     * @throws Exception
     */
@Test
public void testApplicationUploadEndpointUpdateFileNotFound() throws Exception {
    when(testMultipartBody.getAllAttachments()).thenReturn(new ArrayList<Attachment>());
    ApplicationUploadEndpoint applicationUploadEndpoint = new ApplicationUploadEndpoint(testAppService);
    applicationUploadEndpoint.setDefaultFileLocation(TEST_FILE_LOCATION);
    Response response = applicationUploadEndpoint.update(testMultipartBody, testUriInfo);
    Response expectedResponse = Response.serverError().build();
    assertThat("Response should indicate server error.", response.getStatus(), is(expectedResponse.getStatus()));
}
Also used : Response(javax.ws.rs.core.Response) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Test(org.junit.Test)

Example 3 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.

the class ApplicationUploadEndpoint method create.

@POST
@Path("/")
public Response create(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
    LOGGER.trace("ENTERING: create");
    Response response;
    List<Attachment> attachmentList = multipartBody.getAllAttachments();
    File newFile = null;
    for (Attachment attachment : attachmentList) {
        newFile = createFileFromAttachement(attachment);
    }
    try {
        if (newFile != null) {
            appService.addApplication(newFile.toURI());
        }
        Response.ResponseBuilder responseBuilder = Response.ok();
        response = responseBuilder.build();
    } catch (ApplicationServiceException e) {
        LOGGER.warn("Unable to add the application to the server: {}", newFile, e);
        Response.ResponseBuilder responseBuilder = Response.serverError();
        response = responseBuilder.build();
    }
    LOGGER.trace("EXITING: create");
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 4 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.

the class ApplicationUploadEndpoint method update.

@POST
@Path("/update")
@Produces("application/json")
public Response update(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
    LOGGER.trace("ENTERING: update");
    Response response;
    List<Attachment> attachmentList = multipartBody.getAllAttachments();
    File newFile = null;
    for (Attachment attachment : attachmentList) {
        newFile = createFileFromAttachement(attachment);
    }
    try {
        if (newFile != null) {
            ZipFileApplicationDetails appDetails = ApplicationFileInstaller.getAppDetails(newFile);
            if (appDetails != null) {
                // lets get the existing app if it exists.
                Application existingApplication = appService.getApplication(appDetails.getName());
                // assume false until proved
                boolean wasExistingAppStarted = false;
                // otherwise.
                if (existingApplication != null) {
                    wasExistingAppStarted = appService.isApplicationStarted(existingApplication);
                    appService.removeApplication(existingApplication);
                }
                appService.addApplication(newFile.toURI());
                // if application was started before it was removed, lets try and start it.
                if (wasExistingAppStarted) {
                    appService.startApplication(appDetails.getName());
                }
            } else {
                throw new ApplicationServiceException("No Application details could be extracted from the provided file.");
            }
        } else {
            throw new ApplicationServiceException("No file attachment provided.");
        }
        // we need to output valid JSON to the client so fileupload can correctly call
        // done/fail callbacks correctly.
        Response.ResponseBuilder responseBuilder = Response.ok("{\"status\":\"success\"}").type("application/json");
        response = responseBuilder.build();
    } catch (ApplicationServiceException e) {
        LOGGER.warn("Unable to update an application on the server: {}", newFile, e);
        Response.ResponseBuilder responseBuilder = Response.serverError();
        response = responseBuilder.build();
    }
    LOGGER.trace("EXITING: update");
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ZipFileApplicationDetails(org.codice.ddf.admin.application.service.impl.ZipFileApplicationDetails) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Application(org.codice.ddf.admin.application.service.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 5 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project tika by apache.

the class RecursiveMetadataResourceTest method testHandlerTypeInMultipartXML.

@Test
public void testHandlerTypeInMultipartXML() throws Exception {
    //default unspecified
    Attachment attachmentPart = new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
    WebClient webClient = WebClient.create(endPoint + META_PATH + FORM_PATH);
    Response response = webClient.type("multipart/form-data").accept("application/json").post(attachmentPart);
    Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    List<Metadata> metadataList = JsonMetadataList.fromJson(reader);
    assertEquals(12, metadataList.size());
    String content = metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).trim();
    assertTrue(content.startsWith("<html xmlns=\"http://www.w3.org/1999/xhtml\">"));
    //unparseable
    attachmentPart = new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
    webClient = WebClient.create(endPoint + META_PATH + FORM_PATH + UNPARSEABLE_PATH);
    response = webClient.type("multipart/form-data").accept("application/json").post(attachmentPart);
    reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    metadataList = JsonMetadataList.fromJson(reader);
    assertEquals(12, metadataList.size());
    content = metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).trim();
    assertTrue(content.startsWith("<html xmlns=\"http://www.w3.org/1999/xhtml\">"));
    //xml
    attachmentPart = new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
    webClient = WebClient.create(endPoint + META_PATH + FORM_PATH + XML_PATH);
    response = webClient.type("multipart/form-data").accept("application/json").post(attachmentPart);
    reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    metadataList = JsonMetadataList.fromJson(reader);
    assertEquals(12, metadataList.size());
    content = metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).trim();
    assertTrue(content.startsWith("<html xmlns=\"http://www.w3.org/1999/xhtml\">"));
    //text
    attachmentPart = new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
    webClient = WebClient.create(endPoint + META_PATH + FORM_PATH + TEXT_PATH);
    response = webClient.type("multipart/form-data").accept("application/json").post(attachmentPart);
    reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    metadataList = JsonMetadataList.fromJson(reader);
    assertEquals(12, metadataList.size());
    content = metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).trim();
    assertTrue(content.startsWith("embed_3"));
    //ignore -- no content
    attachmentPart = new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
    webClient = WebClient.create(endPoint + META_PATH + FORM_PATH + IGNORE_PATH);
    response = webClient.type("multipart/form-data").accept("application/json").query("handler", "ignore").post(attachmentPart);
    reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    metadataList = JsonMetadataList.fromJson(reader);
    assertEquals(12, metadataList.size());
    assertNull(metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT));
}
Also used : Response(javax.ws.rs.core.Response) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)56 Test (org.junit.Test)20 InputStream (java.io.InputStream)19 Response (javax.ws.rs.core.Response)17 ArrayList (java.util.ArrayList)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)14 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)14 IOException (java.io.IOException)10 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 LinkedList (java.util.LinkedList)7 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)7 LinkedHashMap (java.util.LinkedHashMap)6 DataHandler (javax.activation.DataHandler)6 Path (javax.ws.rs.Path)6 QueryResponse (ddf.catalog.operation.QueryResponse)5 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)5 Consumes (javax.ws.rs.Consumes)5