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);
}
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);
}
}
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();
}
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);
}
Aggregations