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 TEIDOMParser().parse(resp, context); for (String key : teiMet.names()) { metadata.add("grobid:header_" + key, teiMet.get(key)); } } catch (Exception e) { LOG.warn("Couldn't read response", e); } }
public static boolean matchAttachmentId(Attachment at, String value) { if (value.isEmpty()) { return true; } if (at.getContentId().equals(value)) { return true; } ContentDisposition cd = at.getContentDisposition(); return cd != null && value.equals(cd.getParameter("name")); }
public Attachment build() { if (contentDisposition != null) { headers.putSingle("Content-Disposition", contentDisposition.toString()); } return new Attachment(headers, dataHandler, object); }
@Test public void testUploadImageFromForm() throws Exception { InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"); String address = "http://localhost:" + PORT + "/bookstore/books/formimage"; WebClient client = WebClient.create(address); HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit(); conduit.getClient().setReceiveTimeout(1000000); conduit.getClient().setConnectionTimeout(1000000); client.type("multipart/form-data").accept("multipart/form-data"); ContentDisposition cd = new ContentDisposition("attachment;filename=java.jpg"); MultivaluedMap<String, String> headers = new MetadataMap<>(); headers.putSingle("Content-ID", "image"); headers.putSingle("Content-Disposition", cd.toString()); headers.putSingle("Content-Location", "http://host/bar"); headers.putSingle("custom-header", "custom"); Attachment att = new Attachment(is1, headers); MultipartBody body = new MultipartBody(att); MultipartBody body2 = client.post(body, MultipartBody.class); InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream(); byte[] image1 = IOUtils.readBytesFromStream( getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); byte[] image2 = IOUtils.readBytesFromStream(is2); assertTrue(Arrays.equals(image1, image2)); ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition(); assertEquals("attachment;filename=java.jpg", cd2.toString()); assertEquals("java.jpg", cd2.getParameter("filename")); assertEquals("http://host/location", body2.getRootAttachment().getHeader("Content-Location")); }
@Test public void testUploadFileWithSemicolonName() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/file/semicolon"; WebClient client = WebClient.create(address); client.type("multipart/form-data").accept("text/plain"); ContentDisposition cd = new ContentDisposition("attachment;name=\"a\";filename=\"a;txt\""); MultivaluedMap<String, String> headers = new MetadataMap<>(); headers.putSingle("Content-Disposition", cd.toString()); Attachment att = new Attachment(new ByteArrayInputStream("file name with semicolon".getBytes()), headers); MultipartBody body = new MultipartBody(att); String partContent = client.post(body, String.class); assertEquals("file name with semicolon, filename:" + "a;txt", partContent); }
@Test public void testUploadImageFromForm2() throws Exception { File file = new File(getClass().getResource("/org/apache/cxf/systest/jaxrs/resources/java.jpg") .toURI().getPath()); String address = "http://localhost:" + PORT + "/bookstore/books/formimage2"; WebClient client = WebClient.create(address); client.type("multipart/form-data").accept("multipart/form-data"); WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true"); MultipartBody body2 = client.post(file, MultipartBody.class); InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream(); byte[] image1 = IOUtils.readBytesFromStream( getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); byte[] image2 = IOUtils.readBytesFromStream(is2); assertTrue(Arrays.equals(image1, image2)); ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition(); assertEquals("form-data;name=file;filename=java.jpg", cd2.toString()); assertEquals("java.jpg", cd2.getParameter("filename")); }
for (Attachment a : atts) { ContentDisposition cd = a.getContentDisposition(); if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) { continue; String cdName = cd == null ? null : cd.getParameter("name"); String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
for (Attachment a : atts) { ContentDisposition cd = a.getContentDisposition(); if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) { continue; String cdName = cd == null ? null : cd.getParameter("name"); String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
public ContentDisposition getContentDisposition() { String header = getHeader("Content-Disposition"); return header == null ? null : new ContentDisposition(header); }
public Attachment getAttachment(String contentId) { for (Attachment a : atts) { if (contentId.equalsIgnoreCase(a.getContentId())) { return a; } ContentDisposition cd = a.getContentDisposition(); if (cd != null && contentId.equals(cd.getParameter("name"))) { return a; } } return null; }
public Attachment build() { if (contentDisposition != null) { headers.putSingle("Content-Disposition", contentDisposition.toString()); } return new Attachment(headers, dataHandler, object); }
public ContentDisposition getContentDisposition() { String header = getHeader("Content-Disposition"); return header == null ? null : new ContentDisposition(header); }
public static boolean matchAttachmentId(Attachment at, String value) { if (value.isEmpty()) { return true; } if (at.getContentId().equals(value)) { return true; } ContentDisposition cd = at.getContentDisposition(); if (cd != null && value.equals(cd.getParameter("name"))) { return true; } return false; }
public Attachment(String id, InputStream is, ContentDisposition cd) { handler = new DataHandler(new InputStreamDataSource(is, "application/octet-stream")); if (cd != null) { headers.putSingle("Content-Disposition", cd.toString()); } headers.putSingle("Content-ID", id); headers.putSingle("Content-Type", "application/octet-stream"); }
private Attachment buildMediaPart(String name, String fileNameWithExtension, byte[] value) { Attachment a = new Attachment(name, new ByteArrayInputStream(value), new ContentDisposition("form-data; name=\"" + escapeMimeName(name) + "\"; filename=\"" + escapeMimeName(fileNameWithExtension) + "\"")); return a; }
public Attachment getAttachment(String contentId) { for (Attachment a : atts) { if (contentId.equalsIgnoreCase(a.getContentId())) { return a; } ContentDisposition cd = a.getContentDisposition(); if (cd != null && contentId.equals(cd.getParameter("name"))) { return a; } } return null; }
public Attachment(String id, InputStream is, ContentDisposition cd) { handler = new DataHandler(new InputStreamDataSource(is, "application/octet-stream")); if (cd != null) { headers.putSingle("Content-Disposition", cd.toString()); } headers.putSingle("Content-ID", id); headers.putSingle("Content-Type", "application/octet-stream"); }
public UploadFileResponse uploadFile(ProcessDefinitionResponse processDefinition, FileInput file) throws Exception { WebTarget resource = webTarget .path("portal/resource/process/{processName}/{processVersion}/API/formFileUpload") .resolveTemplate("processName", processDefinition.getName()) .resolveTemplate("processVersion", processDefinition.getVersion()); File tempFile = File.createTempFile("tempFile", ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); fos.write(file.getContent()); fos.close(); String dispositionValue = String.format("form-data;filename=%s;name=file", tempFile.getName()); Attachment attachment = new AttachmentBuilder() .object(new ByteArrayInputStream(file.getContent())) .contentDisposition(new ContentDisposition(dispositionValue)) .build(); return resource.request().accept(MediaType.APPLICATION_JSON).post( entity(attachment, MediaType.MULTIPART_FORM_DATA), UploadFileResponse.class); }
private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) { Map<String, Attachment> map = new LinkedHashMap<>(); for (Attachment a : atts) { String contentId = null; if (preferContentDisposition) { ContentDisposition cd = a.getContentDisposition(); if (cd != null) { contentId = cd.getParameter("name"); } } if (contentId == null) { contentId = a.getContentId(); } map.put(contentId, a); } return map; }
File f = (File)obj; ContentDisposition cd = mainMediaType.startsWith(MediaType.MULTIPART_FORM_DATA) ? new ContentDisposition("form-data;name=file;filename=" + f.getName()) : null; return new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, Files.newInputStream(f.toPath()), cd); } else if (Attachment.class.isAssignableFrom(obj.getClass())) {