use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project tika by apache.
the class GrobidRESTParser method parse.
public void parse(String filePath, ContentHandler handler, Metadata metadata, ParseContext context) throws FileNotFoundException {
File pdfFile = new File(filePath);
ContentDisposition cd = new ContentDisposition("form-data; name=\"input\"; filename=\"" + pdfFile.getName() + "\"");
Attachment att = new Attachment("input", new FileInputStream(pdfFile), cd);
MultipartBody body = new MultipartBody(att);
Response response = WebClient.create(restHostUrlStr + GROBID_PROCESSHEADER_PATH).accept(MediaType.APPLICATION_XML).type(MediaType.MULTIPART_FORM_DATA).post(body);
try {
String resp = response.readEntity(String.class);
Metadata teiMet = new TEIParser().parse(resp);
for (String key : teiMet.names()) {
metadata.add("grobid:header_" + key, teiMet.get(key));
}
} catch (Exception e) {
LOG.warn("Couldn't read response", e);
}
}
use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project opencast by opencast.
the class IngestServiceImpl method getContentDispositionFileName.
private String getContentDispositionFileName(HttpResponse response) {
if (response == null)
return null;
Header header = response.getFirstHeader("Content-Disposition");
ContentDisposition contentDisposition = new ContentDisposition(header.getValue());
return contentDisposition.getParameter("filename");
}
use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project ddf by codice.
the class AbstractCatalogService method parseParts.
@Override
public Map.Entry<AttachmentInfo, Metacard> parseParts(Collection<Part> contentParts, String transformerParam) {
if (contentParts.size() == 1) {
Part part = Iterables.get(contentParts, 0);
try (InputStream inputStream = part.getInputStream()) {
ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
return new ImmutablePair<>(attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)), null);
} catch (IOException e) {
LOGGER.debug("IOException reading stream from file attachment in multipart body.", e);
}
}
Metacard metacard = null;
AttachmentInfo attachmentInfo = null;
Map<String, AttributeImpl> attributeMap = new HashMap<>();
for (Part part : contentParts) {
String name = part.getName();
String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
try (InputStream inputStream = part.getInputStream()) {
ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
switch(name) {
case "parse.resource":
attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME));
break;
case "parse.metadata":
metacard = parseMetacard(transformerParam, metacard, part, inputStream);
break;
default:
parseOverrideAttributes(attributeMap, parsedName, inputStream);
break;
}
} catch (IOException e) {
LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
}
}
if (attachmentInfo == null) {
throw new IllegalArgumentException("No parse.resource specified in request.");
}
if (metacard == null) {
metacard = new MetacardImpl();
}
Set<AttributeDescriptor> missingDescriptors = new HashSet<>();
for (Attribute attribute : attributeMap.values()) {
if (metacard.getMetacardType().getAttributeDescriptor(attribute.getName()) == null) {
attributeRegistry.lookup(attribute.getName()).ifPresent(missingDescriptors::add);
}
metacard.setAttribute(attribute);
}
if (!missingDescriptors.isEmpty()) {
MetacardType original = metacard.getMetacardType();
MetacardImpl newMetacard = new MetacardImpl(metacard);
newMetacard.setType(new MetacardTypeImpl(original.getName(), original, missingDescriptors));
metacard = newMetacard;
}
return new ImmutablePair<>(attachmentInfo, metacard);
}
use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project ddf by codice.
the class CatalogServiceImplTest method testGetMetacardAsXml.
/**
* Tests that a geojson input has its InputTransformer invoked by the REST endpoint to create a
* metacard that is then converted to XML and returned from the REST endpoint.
*/
@Test
@SuppressWarnings({ "unchecked" })
public void testGetMetacardAsXml() throws Exception {
CatalogFramework framework = givenCatalogFramework();
String metacardXml = "<metacard ns2:id=\"assigned-when-ingested\">\r\n" + "<type>type.metacard</type>\r\n" + "<string name=\"title\">\r\n" + "<value>Title goes here ...</value>\r\n" + "</string>\r\n" + "<string name=\"metadata\">\r\n" + "<value>metadata goes here ...</value>\r\n" + "</metacard>";
// Mock XmlMetacardTransformer that CatalogFramework will call to convert generated
// metacard into XML to be returned from REST endpoint.
final BinaryContent content = mock(BinaryContent.class);
InputStream inputStream = new ByteArrayInputStream(metacardXml.getBytes(GET_OUTPUT_TYPE));
when(content.getInputStream()).thenReturn(inputStream);
when(content.getMimeTypeValue()).thenReturn("application/json;id=geojson");
when(framework.transform(isA(Metacard.class), anyString(), isNull())).thenReturn(content);
CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
// Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
catalogService.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
catalogService.setFilterBuilder(filterBuilder);
String json = "{\r\n" + " \"properties\": {\r\n" + " \"title\": \"myTitle\",\r\n" + " \"thumbnail\": \"CA==\",\r\n" + " \"resource-uri\": \"http://example.com\",\r\n" + " \"created\": \"2012-09-01T00:09:19.368+0000\",\r\n" + " \"metadata-content-type-version\": \"myVersion\",\r\n" + " \"metadata-content-type\": \"myType\",\r\n" + " \"metadata\": \"<xml>metadata goes here ...</xml>\",\r\n" + " \"modified\": \"2012-09-01T00:09:19.368+0000\"\r\n" + " },\r\n" + " \"type\": \"Feature\",\r\n" + " \"geometry\": {\r\n" + " \"type\": \"Point\",\r\n" + " \"coordinates\": [\r\n" + " 30.0,\r\n" + " 10.0\r\n" + " ]\r\n" + " }\r\n" + "} ";
// Sample headers for a multipart body specifying a geojson file to have a metacard created for:
// Content-Disposition: form-data; name="file"; filename="C:\DDF\geojson_valid.json"
// Content-Type: application/json;id=geojson
InputStream is = IOUtils.toInputStream(json);
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=file; filename=C:\\DDF\\geojson_valid.json");
Attachment attachment = new Attachment("file_part", is, contentDisposition);
attachments.add(attachment);
MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, "id=geojson");
MultipartBody multipartBody = new MultipartBody(attachments, mediaType, true);
BinaryContent binaryContent = catalogService.createMetacard(multipartBody, AbstractCatalogService.DEFAULT_METACARD_TRANSFORMER);
InputStream responseEntity = binaryContent.getInputStream();
String responseXml = IOUtils.toString(responseEntity);
assertEquals(metacardXml, responseXml);
}
use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project ddf by codice.
the class CatalogServiceImplTest method testParseAttachmentsWithAttributeOverrides.
@Test
public void testParseAttachmentsWithAttributeOverrides() throws IngestException, SourceUnavailableException {
CatalogFramework framework = givenCatalogFramework();
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
when(attributeRegistry.lookup(Topic.KEYWORD)).thenReturn(Optional.of(new TopicAttributes().getAttributeDescriptor(Topic.KEYWORD)));
when(attributeRegistry.lookup(Core.LOCATION)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.LOCATION)));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=/path/to/metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.location");
Attachment attachment1 = new Attachment("parse.location", new ByteArrayInputStream("POINT(0 0)".getBytes()), contentDisposition1);
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=parse.topic.keyword");
Attachment attachment2 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword1".getBytes()), contentDisposition2);
ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=parse.topic.keyword");
Attachment attachment3 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword2".getBytes()), contentDisposition3);
attachments.add(attachment);
attachments.add(attachment1);
attachments.add(attachment2);
attachments.add(attachment3);
Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, null);
Metacard metacard = attachmentInfoAndMetacard.getValue();
assertThat(metacard.getAttribute(Core.LOCATION).getValues(), hasItem("POINT(0 0)"));
assertThat(metacard.getAttribute(Topic.KEYWORD).getValues(), hasItems("keyword1", "keyword2"));
}
Aggregations