Search in sources :

Example 76 with Attachment

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

the class JAXRSClientServerTikaTest method testUploadIndexAndSearchPdfFileUsingUserDefinedDatePattern.

@Test
public void testUploadIndexAndSearchPdfFileUsingUserDefinedDatePattern() {
    final WebClient wc = createWebClient("/catalog").type(MediaType.MULTIPART_FORM_DATA);
    final ContentDisposition disposition = new ContentDisposition("attachment;filename=testPDF.pdf");
    final Attachment attachment = new Attachment("root", getClass().getResourceAsStream("/files/testPDF.pdf"), disposition);
    wc.post(new MultipartBody(attachment));
    // Use user-defined date pattern
    final Collection<ScoreDoc> hits = search("dcterms:modified=le=2007/09/16");
    assertEquals(hits.size(), 1);
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 77 with Attachment

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

the class SendJmsMessage method putJmsMessage.

@POST
@RolesAllowed({ "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("jms/message")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response putJmsMessage(MultipartBody inputDataMap) throws ApiException {
    String message = null, fileName = null;
    InputStream file = null;
    if (inputDataMap == null) {
        throw new ApiException("Missing post parameters");
    }
    String fileEncoding = resolveTypeFromMap(inputDataMap, "encoding", String.class, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
    String connectionFactory = resolveStringFromMap(inputDataMap, "connectionFactory");
    String destinationName = resolveStringFromMap(inputDataMap, "destination");
    String destinationType = resolveStringFromMap(inputDataMap, "type");
    String replyTo = resolveTypeFromMap(inputDataMap, "replyTo", String.class, "");
    boolean persistent = resolveTypeFromMap(inputDataMap, "persistent", boolean.class, false);
    boolean synchronous = resolveTypeFromMap(inputDataMap, "synchronous", boolean.class, false);
    boolean lookupDestination = resolveTypeFromMap(inputDataMap, "lookupDestination", boolean.class, false);
    String messageProperty = resolveTypeFromMap(inputDataMap, "property", String.class, "");
    JmsSender qms = jmsBuilder(connectionFactory, destinationName, persistent, destinationType, replyTo, synchronous, lookupDestination);
    if (StringUtils.isNotEmpty(messageProperty)) {
        String[] keypair = messageProperty.split(",");
        Parameter p = new Parameter(keypair[0], keypair[1]);
        try {
            p.configure();
        } catch (ConfigurationException e) {
            throw new ApiException("Failed to configure message property [" + p.getName() + "]", e);
        }
        qms.addParameter(p);
    }
    Attachment filePart = inputDataMap.getAttachment("file");
    if (filePart != null) {
        fileName = filePart.getContentDisposition().getParameter("filename");
        if (StringUtils.endsWithIgnoreCase(fileName, ".zip")) {
            try {
                processZipFile(file, qms);
                return Response.status(Response.Status.OK).build();
            } catch (IOException e) {
                throw new ApiException("error processing zip file", e);
            }
        } else {
            try {
                message = XmlUtils.readXml(Misc.streamToBytes(file), fileEncoding, false);
            } catch (UnsupportedEncodingException e) {
                throw new ApiException("unsupported file encoding [" + fileEncoding + "]");
            } catch (IOException e) {
                throw new ApiException("error reading file", e);
            }
        }
    } else {
        message = resolveStringWithEncoding(inputDataMap, "message", fileEncoding);
    }
    if (message != null && message.length() > 0) {
        processMessage(qms, message);
        return Response.status(Response.Status.OK).build();
    } else {
        throw new ApiException("must provide either a message or file", 400);
    }
}
Also used : ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Parameter(nl.nn.adapterframework.parameters.Parameter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) JmsSender(nl.nn.adapterframework.jms.JmsSender) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 78 with Attachment

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

the class TestPipeline method postTestPipeLine.

@POST
@RolesAllowed("IbisTester")
@Path("/test-pipeline")
@Relation("pipeline")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postTestPipeLine(MultipartBody inputDataMap) throws ApiException {
    Map<String, Object> result = new HashMap<>();
    IbisManager ibisManager = getIbisManager();
    if (ibisManager == null) {
        throw new ApiException("Config not found!");
    }
    String message = null;
    InputStream file = null;
    boolean isZipFile = false;
    String adapterName = resolveStringFromMap(inputDataMap, "adapter");
    // Make sure the adapter exists!
    IAdapter adapter = ibisManager.getRegisteredAdapter(adapterName);
    if (adapter == null) {
        throw new ApiException("Adapter [" + adapterName + "] not found");
    }
    // resolve session keys
    String sessionKeys = resolveTypeFromMap(inputDataMap, "sessionKeys", String.class, "");
    Map<String, String> sessionKeyMap = null;
    if (StringUtils.isNotEmpty(sessionKeys)) {
        try {
            sessionKeyMap = Stream.of(new ObjectMapper().readValue(sessionKeys, PostedSessionKey[].class)).collect(Collectors.toMap(item -> item.key, item -> item.value));
        } catch (Exception e) {
            throw new ApiException("An exception occurred while parsing session keys", e);
        }
    }
    String fileEncoding = resolveTypeFromMap(inputDataMap, "encoding", String.class, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
    Attachment filePart = inputDataMap.getAttachment("file");
    if (filePart != null) {
        String fileName = filePart.getContentDisposition().getParameter("filename");
        if (StringUtils.endsWithIgnoreCase(fileName, ".zip")) {
            try {
                isZipFile = true;
                file = filePart.getObject(InputStream.class);
                processZipFile(result, file, fileEncoding, adapter, sessionKeyMap, secLogMessage);
            } catch (Exception e) {
                throw new ApiException("An exception occurred while processing zip file", e);
            }
        } else {
            message = resolveStringWithEncoding(inputDataMap, "file", fileEncoding);
        }
    } else {
        message = resolveStringWithEncoding(inputDataMap, "message", fileEncoding);
    }
    if (!isZipFile) {
        result.put("message", message);
        try {
            PipeLineResult plr = processMessage(adapter, message, sessionKeyMap, secLogMessage);
            try {
                result.put(PIPELINE_RESULT_STATE, plr.getState());
                result.put(PIPELINE_RESULT, plr.getResult().asString());
            } catch (Exception e) {
                String msg = "An exception occurred while extracting the result of the PipeLine with exit state [" + plr.getState() + "]";
                log.warn(msg, e);
                result.put(PIPELINE_RESULT_STATE, PIPELINE_RESULT_STATE_ERROR);
                result.put(PIPELINE_RESULT, msg + ": (" + e.getClass().getTypeName() + ") " + e.getMessage());
            }
        } catch (Exception e) {
            String msg = "An exception occurred while processing the message";
            log.warn(msg, e);
            result.put(PIPELINE_RESULT_STATE, PIPELINE_RESULT_STATE_ERROR);
            result.put(PIPELINE_RESULT, msg + ": (" + e.getClass().getTypeName() + ") " + e.getMessage());
        }
    }
    return Response.status(Response.Status.CREATED).entity(result).build();
}
Also used : IbisManager(nl.nn.adapterframework.configuration.IbisManager) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) IAdapter(nl.nn.adapterframework.core.IAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 79 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project tesb-rt-se by Talend.

the class RESTClient method createMultipartBody.

/**
 * Creates MultipartBody. It contains 3 parts, "book1", "book2" and "image".
 * These individual parts have their Content-Type set to application/xml,
 * application/json and application/octet-stream
 *
 * MultipartBody will use the Content-Type value of the individual
 * part to write its data by delegating to a matching JAX-RS MessageBodyWriter
 * provider
 *
 * @return
 * @throws Exception
 */
private MultipartBody createMultipartBody() throws Exception {
    List<Attachment> atts = new LinkedList<Attachment>();
    atts.add(new Attachment("book1", "application/xml", new Book("JAXB", 1L)));
    atts.add(new Attachment("book2", "application/json", new Book("JSON", 2L)));
    atts.add(new Attachment("image", "application/octet-stream", getClass().getResourceAsStream("/java.jpg")));
    return new MultipartBody(atts, true);
}
Also used : Book(common.attachment.Book) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedList(java.util.LinkedList)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)79 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)29 InputStream (java.io.InputStream)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)25 ByteArrayInputStream (java.io.ByteArrayInputStream)23 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)23 Response (javax.ws.rs.core.Response)17 IOException (java.io.IOException)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)15 CatalogFramework (ddf.catalog.CatalogFramework)10 LinkedHashMap (java.util.LinkedHashMap)9 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 Path (javax.ws.rs.Path)8 Metacard (ddf.catalog.data.Metacard)7 InputTransformer (ddf.catalog.transform.InputTransformer)7 LinkedList (java.util.LinkedList)7