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 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); } }
@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")); }
@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")); }
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()); }