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