public static UploadResponse uploadFile(WebTarget target, Path file, Path tagsFile) { assertThat(Files.exists(file), is(true)); // MediaType of the body part will be derived from the file. FileDataBodyPart filePart = new FileDataBodyPart("file", file.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(filePart); if (tagsFile != null) { FileDataBodyPart tagsFilePart = new FileDataBodyPart("tags", tagsFile.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); mp.bodyPart(tagsFilePart); } if (staticLogger.isDebugEnabled()) { staticLogger.debug("Rest response: {}", restCall(target, "/_upload", mp, String.class, debugOption)); } return restCall(target, "/_upload", mp, UploadResponse.class, Collections.emptyMap()); } }
/** * Adds an attachment from a {@link File}. * * @param file a file to attach * @return this builder */ public MultipartBuilder attachment(File file) { return bodyPart(new FileDataBodyPart(ATTACHMENT_NAME, file)); }
public void attachment(String attachment) { File file = file(attachment); this.attachments.bodyPart(new FileDataBodyPart(file.getName(), file, APPLICATION_OCTET_STREAM_TYPE)); }
public void attachment(String attachment) { File file = file(attachment); this.attachments.bodyPart(new FileDataBodyPart(file.getName(), file, APPLICATION_OCTET_STREAM_TYPE)); }
public Response uploadFile(final String file_path) { final FileDataBodyPart filePart = new FileDataBodyPart("file", new File(file_path)); final FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(filePart); final Response resp = fileUploadService.uploadFile(multiPart); return resp; } }
public boolean deploy(String archiveFile) { FileDataBodyPart archive = new FileDataBodyPart("id", new File(archiveFile)); MultiPart form = new FormDataMultiPart(). field("force", "true").bodyPart(archive); Response response = applicationTarget.request("application/json"). header("X-Requested-By", "loadr"). post(Entity.entity(form, form.getMediaType())); return (response.getStatus() == 200); }
@Override public void uploadFunction(String sourceFile, String path) throws PulsarAdminException { try { final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(new FileDataBodyPart("data", new File(sourceFile), MediaType.APPLICATION_OCTET_STREAM_TYPE)); mp.bodyPart(new FormDataBodyPart("path", path, MediaType.TEXT_PLAIN_TYPE)); request(functions.path("upload")) .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), ErrorData.class); } catch (Exception e) { throw getApiException(e); } }
public void uploadFile(String appName, File file) { FileDataBodyPart applicationPart = new FileDataBodyPart("file", file); MultiPart multipart = new FormDataMultiPart().bodyPart(applicationPart); Response response = post(uri, String.format(APPLICATIONS_FILES_PATH, appName), Entity.entity(multipart, multipart.getMediaType())); if (response.getStatus() != 200) { throw new ApiException(response); } }
@Override public String triggerFunction(String tenant, String namespace, String functionName, String topic, String triggerValue, String triggerFile) throws PulsarAdminException { try { final FormDataMultiPart mp = new FormDataMultiPart(); if (triggerFile != null) { mp.bodyPart(new FileDataBodyPart("dataStream", new File(triggerFile), MediaType.APPLICATION_OCTET_STREAM_TYPE)); } if (triggerValue != null) { mp.bodyPart(new FormDataBodyPart("data", triggerValue, MediaType.TEXT_PLAIN_TYPE)); } if (topic != null && !topic.isEmpty()) { mp.bodyPart(new FormDataBodyPart("topic", topic, MediaType.TEXT_PLAIN_TYPE)); } return request(functions.path(tenant).path(namespace).path(functionName).path("trigger")) .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), String.class); } catch (Exception e) { throw getApiException(e); } }
@Override public void createFunction(FunctionDetails functionDetails, String fileName) throws PulsarAdminException { try { final FormDataMultiPart mp = new FormDataMultiPart(); if (fileName != null && !fileName.startsWith("builtin://")) { // If the function code is built in, we don't need to submit here mp.bodyPart(new FileDataBodyPart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM_TYPE)); } mp.bodyPart(new FormDataBodyPart("functionDetails", printJson(functionDetails), MediaType.APPLICATION_JSON_TYPE)); request(functions.path(functionDetails.getTenant()).path(functionDetails.getNamespace()).path(functionDetails.getName())) .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), ErrorData.class); } catch (Exception e) { throw getApiException(e); } }
@Override public void updateFunction(FunctionDetails functionDetails, String fileName) throws PulsarAdminException { try { final FormDataMultiPart mp = new FormDataMultiPart(); if (fileName != null && !fileName.startsWith("builtin://")) { // If the function code is built in, we don't need to submit here mp.bodyPart(new FileDataBodyPart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM_TYPE)); } mp.bodyPart(new FormDataBodyPart("functionDetails", printJson(functionDetails), MediaType.APPLICATION_JSON_TYPE)); request(functions.path(functionDetails.getTenant()).path(functionDetails.getNamespace()).path(functionDetails.getName())) .put(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), ErrorData.class); } catch (Exception e) { throw getApiException(e); } }
public Application deployApplication(File app, String appName, TargetType targetType, String target) { String id = getId(targetType, target); FileDataBodyPart applicationPart = new FileDataBodyPart("file", app); MultiPart multipart = new FormDataMultiPart() .field("artifactName", appName) .field("targetId", id) .bodyPart(applicationPart); Response response = post(uri, APPLICATIONS, Entity.entity(multipart, multipart.getMediaType())); validateStatusSuccess(response); return response.readEntity(Application.class); }
new FileDataBodyPart(name, fileToUpload, mediaType) : new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType()));
new FileDataBodyPart(name, fileToUpload, mediaType) : new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType()));
/** * 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)); } }
/** * 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)); } }
form.bodyPart(new FileDataBodyPart(MULTIPART_DEPLOYMENT_PART_NAME, deploymentArchive, MediaType.APPLICATION_OCTET_STREAM_TYPE));
multipart.bodyPart(mp, MediaType.MULTIPART_FORM_DATA_TYPE); multipart.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { FormDataMultiPart mp = new FormDataMultiPart();
@Test @Ignore public void checkDictionaryUpload() throws Exception { Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client"); client.property(ClientProperties.CONNECT_TIMEOUT, 5000); client.property(ClientProperties.READ_TIMEOUT, 5000); client.register(MultiPartFeature.class); final MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); File testDictionaryFile = new File(ResourceHelpers.resourceFilePath("test_dictionary.txt")); final FileDataBodyPart filePart = new FileDataBodyPart("file", testDictionaryFile); multiPart.bodyPart(filePart); Response response = client.target( String.format("http://localhost:%d/api/upload/dictionary", RULE.getLocalPort())) .request(MediaType.MULTIPART_FORM_DATA_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType())); assertThat(response.getStatus()).isEqualTo(200); } }
protected MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) { MultiPart multiPart = new MultiPart(); BodyPart filePart = new FileDataBodyPart(resourceToTest.getFileNameHeader(), resourceToTest.getFileToUpload()); BodyPart entityPart = new FormDataBodyPart(resourceToTest.getEntityNameHeader(), entity, MediaType.APPLICATION_JSON_TYPE); multiPart.bodyPart(filePart).bodyPart(entityPart); return multiPart; }