public void writeTo(Map<String, Object> map, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { PartType partType = FindAnnotation.findAnnotation(annotations, PartType.class); MediaType partMediaType = MediaType.valueOf(partType.value()); MultipartFormDataOutput output = new MultipartFormDataOutput(); for (Map.Entry<String, Object> entry : map.entrySet()) { output.addFormData(entry.getKey(), entry.getValue(), partMediaType); } write(output, mediaType, httpHeaders, entityStream); } }
public OutputPart addFormData(String key, Object entity, GenericType<?> type, MediaType mediaType) { OutputPart part = super.addPart(entity, type, mediaType); formData.put(key, part); addFormDataMap(key, part); return part; }
private void addFormDataMap(String key, OutputPart part) { List<OutputPart> list = getFormDataMap().get(key); if (list == null) { list = new LinkedList<OutputPart>(); formDataMap.put(key, list); } list.add(part); }
protected void getFields(Class<?> type, MultipartFormDataOutput output, Object obj) throws IOException { for (Field field : type.getDeclaredFields()) { if (field.isAnnotationPresent(FormParam.class) && field.isAnnotationPresent(PartType.class)) { AccessController.doPrivileged(new FieldEnablerPrivilegedAction(field)); FormParam param = field.getAnnotation(FormParam.class); Object value = null; try { value = field.get(obj); } catch (IllegalAccessException e) { throw new WriterException(e); } PartType partType = field.getAnnotation(PartType.class); String filename = getFilename(field); output.addFormData(param.value(), value, field.getType(), field.getGenericType(), MediaType.valueOf(partType.value()), filename); } } }
protected void getFields(Class<?> type, MultipartFormDataOutput output, Object obj) throws IOException { for (Field field : type.getDeclaredFields()) { if (field.isAnnotationPresent(FormParam.class) && field.isAnnotationPresent(PartType.class)) { AccessController.doPrivileged(new FieldEnablerPrivilegedAction(field)); FormParam param = field.getAnnotation(FormParam.class); Object value = null; try { value = field.get(obj); } catch (IllegalAccessException e) { throw new WriterException(e); } PartType partType = field.getAnnotation(PartType.class); String filename = getFilename(field); output.addFormData(param.value(), value, field.getType(), field.getGenericType(), MediaType.valueOf(partType.value()), filename); } } }
public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException MultipartFormDataOutput multipart = new MultipartFormDataOutput(); String filename = getFilename(method); multipart.addFormData(param.value(), value, method.getReturnType(), method.getGenericReturnType(), MediaType.valueOf(partType.value()), filename);
public OutputPart addFormData(String key, Object entity, GenericType<?> type, MediaType mediaType, String filename) { OutputPart part = super.addPart(entity, type, mediaType, filename); formData.put(key, part); addFormDataMap(key, part); return part; }
@Override protected void writeParts(MultipartOutput multipartOutput, OutputStream entityStream, byte[] boundaryBytes) throws IOException { if (!(multipartOutput instanceof MultipartFormDataOutput)) throw new IllegalArgumentException(Messages.MESSAGES.hadToWriteMultipartOutput(multipartOutput, this, MultipartFormDataOutput.class)); MultipartFormDataOutput form = (MultipartFormDataOutput) multipartOutput; for (Map.Entry<String, List<OutputPart>> entry : form.getFormDataMap().entrySet()) { for (OutputPart outputPart : entry.getValue()) { if (outputPart.getEntity() == null) { continue; } MultivaluedMap<String, Object> headers = new MultivaluedMapImpl<String, Object>(); headers.putSingle("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\"" + getFilename(outputPart)); writePart(entityStream, boundaryBytes, outputPart, headers); } } }
MultipartFormDataOutput output = new MultipartFormDataOutput(); // file (below) doesn't have to be a `byte[]` // It can be a `File` object and work just the same output.addFormData("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE); Response response = target.request() .post(Entity.entity(output, MediaType.MULTIPART_FORM_DATA));
public OutputPart addFormData(String key, Object entity, MediaType mediaType, String filename) { OutputPart part = super.addPart(entity, mediaType, filename); formData.put(key, part); addFormDataMap(key, part); return part; }
private void addFormDataMap(String key, OutputPart part) { List<OutputPart> list = getFormDataMap().get(key); if (list == null) { list = new LinkedList<OutputPart>(); formDataMap.put(key, list); } list.add(part); }
WebTarget target = client.target("/some-resource"); MultipartFormDataOutput formData = new MultipartFormDataOutput(); formData.addFormData("file", fileContent, MediaType.TEXT_PLAIN_TYPE.withCharset("utf-8")); Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA); Response response = target.request().post(entity);
public OutputPart addFormData(String key, Object entity, MediaType mediaType) { OutputPart part = super.addPart(entity, mediaType); formData.put(key, part); addFormDataMap(key, part); return part; }
@Override protected void writeParts(MultipartOutput multipartOutput, OutputStream entityStream, byte[] boundaryBytes) throws IOException { if (!(multipartOutput instanceof MultipartFormDataOutput)) throw new IllegalArgumentException(Messages.MESSAGES.hadToWriteMultipartOutput(multipartOutput, this, MultipartFormDataOutput.class)); MultipartFormDataOutput form = (MultipartFormDataOutput) multipartOutput; for (Map.Entry<String, List<OutputPart>> entry : form.getFormDataMap().entrySet()) { for (OutputPart outputPart : entry.getValue()) { if (outputPart.getEntity() == null) { continue; } MultivaluedMap<String, Object> headers = new MultivaluedMapImpl<String, Object>(); headers.putSingle("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\"" + getFilename(outputPart)); writePart(entityStream, boundaryBytes, outputPart, headers); } } }
WebTarget target = ClientBuilder.newClient().target("some/url"); MultipartFormDataOutput formData = new MultipartFormDataOutput(); Map<String, Object> json = new HashMap<>(); json.put("foo", "bar"); formData.addFormData("json", json, MediaType.APPLICATION_JSON_TYPE); FileInputStream fis = new FileInputStream(new File("/path/to/image")); formData.addFormData("image", fis, MediaType.APPLICATION_OCTET_STREAM_TYPE); Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA); Response response = target.request().post(entity);
public OutputPart addFormData(String key, Object entity, Class<?> type, Type genericType, MediaType mediaType) { OutputPart part = super.addPart(entity, type, genericType, mediaType); formData.put(key, part); addFormDataMap(key, part); return part; }
ResteasyClient client = (ResteasyClient) this.client; ResteasyWebTarget target = client.target("http://localhost:8080").path("path"); MultipartFormDataOutput mdo = new MultipartFormDataOutput(); mdo.addFormData("firstPart", new ByteArrayInputStream("firstContent".getBytes()), MediaType.TEXT_PLAIN_TYPE); mdo.addFormData("secondPart", new ByteArrayInputStream("secondContent".getBytes()), MediaType.TEXT_PLAIN_TYPE); GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) { }; Response response = target.request().put(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE)); response.close();
public OutputPart addFormData(String key, Object entity, Class<?> type, Type genericType, MediaType mediaType, String filename) { OutputPart part = super.addPart(entity, type, genericType, mediaType, filename); formData.put(key, part); addFormDataMap(key, part); return part; }
public void writeTo(Map<String, Object> map, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { PartType partType = FindAnnotation.findAnnotation(annotations, PartType.class); MediaType partMediaType = MediaType.valueOf(partType.value()); MultipartFormDataOutput output = new MultipartFormDataOutput(); for (Map.Entry<String, Object> entry : map.entrySet()) { output.addFormData(entry.getKey(), entry.getValue(), partMediaType); } write(output, mediaType, httpHeaders, entityStream); } }
public OutputPart addFormData(String key, Object entity, Class<?> type, Type genericType, MediaType mediaType, String filename) { OutputPart part = super.addPart(entity, type, genericType, mediaType, filename); formData.put(key, part); addFormDataMap(key, part); return part; }