SystemMessage withoutAttachment = new SystemMessage("title", "content"); Attachment attachment = new Attachment(); SystemMessage withAttachment = new SystemMessage("title", "content", attachment);
SystemMessage withoutAttachment = new SystemMessage("title", "content", Optional.empty()); Attachment attachment = new Attachment(); SystemMessage withAttachment = new SystemMessage("title", "content", Optional.ofNullable(attachment));
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); } }
@Test public void testTextMainMultipart() throws Exception { //boilerpipe Attachment attachmentPart = new Attachment("myhtml", "text/html", ClassLoader.getSystemResourceAsStream("testHTML.html")); Response response = WebClient.create(endPoint + TIKA_PATH+"/form/main") .type("multipart/form-data") .accept("text/plain") .post(attachmentPart); String responseMsg = getStringFromInputStream((InputStream) response .getEntity()); assertTrue(responseMsg.contains("Title : Test Indexation Html")); assertFalse(responseMsg.contains("Indexation du fichier")); }
@Test public void testSimpleWordMultipartXML() throws Exception { ClassLoader.getSystemResourceAsStream(TEST_DOC); Attachment attachmentPart = new Attachment("myworddoc", "application/msword", ClassLoader.getSystemResourceAsStream(TEST_DOC)); WebClient webClient = WebClient.create(endPoint + TIKA_PATH + "/form"); Response response = webClient.type("multipart/form-data") .accept("text/xml") .post(attachmentPart); String responseMsg = getStringFromInputStream((InputStream) response .getEntity()); assertTrue(responseMsg.contains("test")); assertContains("<meta name=\"X-TIKA:digest:MD5\" content=\"f8be45c34e8919eedba48cc8d207fbf0\"/>", responseMsg); }
@Test public void testExtractTextAcceptPlainText() throws Exception { //TIKA-2384 Attachment attachmentPart = new Attachment( "my-docx-file", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream("2pic.docx") ); Response response = WebClient.create(endPoint + TIKA_PATH + "/form") .type("multipart/form-data") .accept("text/plain") .post(attachmentPart); String responseMsg = getStringFromInputStream((InputStream) response.getEntity()); assertTrue(responseMsg.contains("P1040893.JPG")); assertNotFound( STREAM_CLOSED_FAULT, responseMsg ); }
new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC)); new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC)); new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC)); new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC)); new Attachment("myworddocx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
Long yesterday = new Date().getTime() - (1000*60*60*24); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String theCsv = "stuff,stuff\n yeah, uhuh\n"; //This sends a normal file Attachment att = new Attachment("myfile.csv", theCsv.getBytes(), "UTF-8"); sendEmailWithAttachment("you@you.com","Me","me@me.com","Stuff for "+sdf.format(yesterday) ,"", att); //This sends one with a trailing hyphen!! String fileName = sdf.format(yesterday)+"-myfile.csv"; fileName = fileName.replaceAll(" ", "").replaceAll("\"", ""); Attachment att = new Attachment( fileName, theCsv.getBytes(), "UTF-8"); sendEmailWithAttachment("you@you.com", "Me","me@me.com","Stuff for "+sdf.format(yesterday),"", att);
public Attachment build() { if (contentDisposition != null) { headers.putSingle("Content-Disposition", contentDisposition.toString()); } return new Attachment(headers, dataHandler, object); }
public Attachment build() { if (contentDisposition != null) { headers.putSingle("Content-Disposition", contentDisposition.toString()); } return new Attachment(headers, dataHandler, object); }
private Attachment buildTextPart(String name, String value) { MultivaluedMap m = new MultivaluedHashMap<>(); m.putSingle("Content-Type", "text/plain"); m.putSingle("Content-Disposition", "form-data; name=\"" + escapeMimeName(name) + "\""); Attachment a = new Attachment(m, value); return a; }
@GET @Path("/content/string") @Produces("multipart/mixed") public Attachment getAttachmentWithStringContent() throws Exception { return new Attachment("Response_XML_Payload", "application/xml", "<Book><id>888</id></Book>"); }
@GET @Path("/content/bytes") @Produces("multipart/mixed") public Attachment getAttachmentWithByteContent() throws Exception { return new Attachment("Response_XML_Payload", "application/xml", "<Book><id>888</id></Book>".getBytes()); }
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; }
@Override public void filter(List<Attachment> atts) { Attachment dataPart = atts.remove(0); Attachment newDataPart = new Attachment(new ByteArrayInputStream("Hi".getBytes()), dataPart.getHeaders()); atts.add(0, newDataPart); } }
@Test public void testNullableParamsPrimitive() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/testnullpartprimitive"; WebClient client = WebClient.create(address); client.type("multipart/form-data").accept("text/plain"); List<Attachment> atts = new LinkedList<>(); atts.add(new Attachment("somepart", "text/plain", "hello there")); Response r = client.postCollection(atts, Attachment.class); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals((Integer)0, Integer.valueOf(IOUtils.readStringFromStream((InputStream)r.getEntity()))); }
private void doTestNullPart(String address) throws Exception { WebClient client = WebClient.create(address); client.type("multipart/form-data").accept("text/plain"); List<Attachment> atts = new LinkedList<>(); atts.add(new Attachment("somepart", "text/plain", "hello there")); Response r = client.postCollection(atts, Attachment.class); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals("nobody home", IOUtils.readStringFromStream((InputStream)r.getEntity())); }
@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); }
@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); }