Refine search
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 <T> T getAttachmentObject(String contentId, Class<T> cls) { Attachment att = getAttachment(contentId); if (att != null) { return att.getObject(cls); } return null; } }
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(MultipartBody body) throws UnsupportedEncodingException { try { for(Attachment attachment : body.getAllAttachments()){ System.out.println(attachment); } return Response.ok("file uploaded").build(); } catch (Exception ex) { logger.error("uploadFile.error():", ex); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }
@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 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")); }
@Path("/") public ConsumerRegistration register(MultipartBody body) { String appName = body.getAttachmentObject("appName", String.class); String appURI = body.getAttachmentObject("appURI", String.class); String appDesc = body.getAttachmentObject("appDescription", String.class); Attachment att = body.getAttachment("appLogo"); if (att != null) { InputStream logoStream = att.getObject(InputStream.class); CachedOutputStream cos = new CachedOutputStream(); try { ub.path("logo").path(appName.toLowerCase()); ContentDisposition cd = att.getContentDisposition(); if (cd != null && cd.getParameter("filename") != null) { ub.path(cd.getParameter("filename"));
Message m, boolean decode) { List<Attachment> atts = body.getAllAttachments(); checkNumberOfParts(m, atts.size()); for (Attachment a : atts) { ContentDisposition cd = a.getContentDisposition(); if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) { continue; String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", ""); if (StringUtils.isEmpty(name)) { String value = IOUtils.toString(a.getDataHandler().getInputStream()); params.add(HttpUtils.urlDecode(name), decode ? HttpUtils.urlDecode(value) : value);
Attachment file = multipartBody.getAttachment( "files[]" ); String fileName = file.getContentDisposition().getParameter( "filename" ); IOUtils.copy( file.getDataHandler().getInputStream(), new FileOutputStream( tmpFile.toFile() ) ); FileMetadata fileMetadata = new FileMetadata( fileName, Files.size(tmpFile), "theurl" ); fileMetadata.setServerFileName( tmpFile.toString() );
Book jaxbBook = body.getAttachmentObject("book1", Book.class); Book jsonBook = body.getAttachmentObject("book2", Book.class); Attachment imageAtt = body.getAttachment("image"); return createMultipartBody(jaxbBook, jsonBook, imageAtt.getDataHandler());
parts = CastUtils.cast(((MultipartBody)rootEntity).getAllAttachments()); } else { parts = new ArrayList<>(); useJwsJsonSignatureFormat); Attachment jwsPart = new Attachment("signature", JoseConstants.MEDIA_TYPE_JOSE, jws); parts.add(jwsPart); return parts;
private String getStringValue( MultipartBody multipartBody, String attachmentId ) throws IOException { Attachment attachment = multipartBody.getAttachment( attachmentId ); return attachment == null ? "" : IOUtils.toString( attachment.getDataHandler().getInputStream() ); }
@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")); }
@POST @Consumes("multipart/form-data") public Response addBook(final MultipartBody body) throws Exception { for (final Attachment attachment: body.getAllAttachments()) { final DataHandler handler = attachment.getDataHandler(); if (handler != null) { final String source = handler.getName(); final LuceneDocumentMetadata metadata = new LuceneDocumentMetadata() .withSource(source) .withField("modified", Date.class); final Document document = extractor.extract(handler.getInputStream(), metadata); if (document != null) { final IndexWriter writer = getIndexWriter(); try { writer.addDocument(document); writer.commit(); } finally { writer.close(); } } } } return Response.ok().build(); }
Class<?> actual = getActualType(t, 1); for (Attachment a : infos) { map.put(a.getContentType().toString(), fromAttachment(a, actual, actual, anns)); return new MultipartBody(infos); if (multipart != null) { if (collectionExpected && !mediaTypeSupported(multipart.getContentType()) && !PropertyUtils.isTrue(mc.getContextualProperty(SINGLE_PART_IS_COLLECTION))) { List<Attachment> allMultiparts = AttachmentUtils.getMatchingAttachments(id, infos);
private void doTestGetBookAsPlainContent(String address) throws Exception { WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000); wc.accept("multipart/mixed"); MultipartBody book = wc.get(MultipartBody.class); Book b = book.getRootAttachment().getObject(Book.class); assertEquals(888L, b.getId()); }
private void verifyMultipartResponse(MultipartBody bodyResponse) throws Exception { Book jaxbBook = bodyResponse.getAttachmentObject("book1", Book.class); Book jsonBook = bodyResponse.getAttachmentObject("book2", Book.class); byte[] receivedImageBytes = bodyResponse.getAttachmentObject("image", byte[].class); InputStream is = getClass().getResourceAsStream("/java.jpg"); byte[] imageBytes = IOUtils.readBytesFromStream(is); if ("JAXB".equals(jaxbBook.getName()) && 1L == jaxbBook.getId() && "JSON".equals(jsonBook.getName()) && 2L == jsonBook.getId() && Arrays.equals(imageBytes, receivedImageBytes)) { System.out.println(); System.out.println("Book attachments have been successfully received"); } else { throw new RuntimeException("Received Book attachment is corrupted"); } }
public T readFrom( Class<T> clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException { if (is == null) { return null; } try { if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) { MultipartBody body = AttachmentUtils.getMultipartBody(mc); if (MultipartBody.class.isAssignableFrom(clazz)) { return clazz.cast(body); } else if (Attachment.class.isAssignableFrom(clazz)) { return clazz.cast(body.getRootAttachment()); } } MultivaluedMap<String, String> params = createMap(clazz); populateMap(params, annotations, is, mt, !keepEncoded(annotations)); validateMap(params); persistParamsOnMessage(params); return getFormObject(clazz, params); } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw ExceptionUtils.toBadRequestException(e, null); } }
public static List<Attachment> getChildAttachments(MessageContext mc) { return getMultipartBody(mc).getChildAttachments(); }
@Path("/") public ConsumerRegistration register(MultipartBody body) { String appName = body.getAttachmentObject("appName", String.class); String appURI = body.getAttachmentObject("appURI", String.class); String appRedirectURI = body.getAttachmentObject("appRedirectURI", String.class); String appDesc = body.getAttachmentObject("appDescription", String.class); Attachment att = body.getAttachment("appLogo"); if (att != null) { InputStream logoStream = att.getObject(InputStream.class); CachedOutputStream cos = new CachedOutputStream(); try { ub.path("logo").path(appName.toLowerCase()); ContentDisposition cd = att.getContentDisposition(); if (cd != null && cd.getParameter("filename") != null) { ub.path(cd.getParameter("filename"));
Message m, boolean decode) { List<Attachment> atts = body.getAllAttachments(); checkNumberOfParts(m, atts.size()); for (Attachment a : atts) { ContentDisposition cd = a.getContentDisposition(); if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) { continue; String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", ""); if (StringUtils.isEmpty(name)) { String value = IOUtils.toString(a.getDataHandler().getInputStream()); params.add(HttpUtils.urlDecode(name), decode ? HttpUtils.urlDecode(value) : value);