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); } }
private MultipartBody createMultipartBody(Book jaxbBook, Book jsonBook, DataHandler imageHandler) { List<Attachment> atts = new LinkedList<Attachment>(); // One of the ways to create an individual part is to use // a Content-Id, Content-Type and the object triple. // Multipart Provider will delegate to a matching JAX-RS // MessageBodyWriter atts.add(new Attachment("book1", "application/xml", jaxbBook)); atts.add(new Attachment("book2", "application/json", jsonBook)); try { atts.add(new Attachment("image", "application/octet-stream", imageHandler.getInputStream())); } catch (IOException ex) { throw new RuntimeException(ex); } return new MultipartBody(atts, true); } }
/** * 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); }
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 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) { e.printStackTrace(); } }
@POST @Path("/books/formimage") @Consumes("multipart/form-data") @Produces("multipart/form-data") public MultipartBody addBookFormImage(MultipartBody image) throws Exception { List<Attachment> atts = image.getAllAttachments(); if (atts.size() != 1) { throw new WebApplicationException(); } List<Attachment> newAtts = new ArrayList<>(); Attachment at = atts.get(0); MultivaluedMap<String, String> headers = at.getHeaders(); if (!"http://host/bar".equals(headers.getFirst("Content-Location"))) { throw new WebApplicationException(); } if (!"custom".equals(headers.getFirst("Custom-Header"))) { throw new WebApplicationException(); } headers.putSingle("Content-Location", "http://host/location"); newAtts.add(new Attachment(at.getContentId(), at.getDataHandler(), headers)); return new MultipartBody(newAtts); }
@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("modified=le=2007/09/16"); assertEquals(hits.size(), 1); }
return new MultipartBody(infos);
return new MultipartBody(infos);
@Test public void testUploadIndexAndSearchPdfFile() { 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)); final Collection<ScoreDoc> hits = search("modified=le=2007-09-16T09:00:00"); assertEquals(hits.size(), 1); }
@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); }
? (MediaType)inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType(); MultipartBody body = new MultipartBody(newAttachments, mt, false); inMessage.put(propertyName, body); return body;
@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")); }
? (MediaType)inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType(); MultipartBody body = new MultipartBody(newAttachments, mt, false); inMessage.put(propertyName, body); return body;