final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }
/** * POST an object with multiplepart. For example Uploading a file. * Below is a sample on how to create a Multipart from a string and post it * String xml = "some string"; * final FormDataBodyPart templatePart = new FormDataBodyPart("template", xml, MediaType.APPLICATION_OCTET_STREAM_TYPE); * <p> * FormDataContentDisposition.FormDataContentDispositionBuilder disposition = FormDataContentDisposition.name(templatePart.getName()); * disposition.fileName("fileName"); * templatePart.setFormDataContentDisposition(disposition.build()); * // Combine parts * MultiPart multiPart = new MultiPart(); * multiPart.bodyPart(templatePart); * multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); * * @param path the path to access * @param object the multiplart object to post * @param returnType the type to return * @return returns the response of the type T */ public <T> T postMultiPart(String path, MultiPart object, Class<T> returnType) { WebTarget target = buildTarget(path, null); MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE; contentType = Boundary.addBoundary(contentType); return target.request().post(Entity.entity(object, contentType), returnType); }
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }
/** * POST a multipart object streaming * * @param path the path to access * @param name the name of the param the endpoint is expecting * @param fileName the name of the file * @param stream the stream itself * @param returnType the type to return from the post * @return the response of type T */ public <T> T postMultiPartStream(String path, String name, String fileName, InputStream stream, Class<T> returnType) { WebTarget target = buildTarget(path, null); MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE); StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart(name, stream, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.getBodyParts().add(streamDataBodyPart); MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE; contentType = Boundary.addBoundary(contentType); return target.request().post( Entity.entity(multiPart, contentType), returnType); }
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }
/** * Perform a file upload using multipart/form-data using the HTTP PUT method, returning * a ClientResponse instance with the data returned from the endpoint. * * @param name the name for the form field that contains the file name * @param fileToUpload a File instance pointing to the file to upload * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws IOException if an error occurs while constructing the URL */ protected Response putUpload(String name, File fileToUpload, URL url) throws IOException { try (MultiPart multiPart = new FormDataMultiPart()) { multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).put(entity)); } }
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }
/** * Perform a file upload using multipart/form-data using the HTTP PUT method, returning * a ClientResponse instance with the data returned from the endpoint. * * @param name the name for the form field that contains the file name * @param fileToUpload a File instance pointing to the file to upload * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws IOException if an error occurs while constructing the URL */ protected Response putUpload(String name, File fileToUpload, URL url) throws IOException { try (MultiPart multiPart = new FormDataMultiPart()) { multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).put(entity)); } }
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }
new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).post(entity));
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }