/** * Create payload to supply with POST to REST API * * @return */ private FormDataMultiPart createParametersForm() { FormDataMultiPart form = new FormDataMultiPart(); if ( verCount != -1 && !purgeRev ) { form.field( VER_COUNT, Integer.toString( verCount ), MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( delFrom != null && !purgeRev ) { form.field( DEL_DATE, delFrom, MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( fileFilter != null ) { form.field( FILE_FILTER, fileFilter, MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( logLevel != null ) { form.field( LOG_LEVEL, logLevel, MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( purgeFiles ) { form.field( PURGE_FILES, Boolean.toString( purgeFiles ), MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( purgeRev ) { form.field( PURGE_REV, Boolean.toString( purgeRev ), MediaType.MULTIPART_FORM_DATA_TYPE ); } if ( purgeShared ) { form.field( PURGE_SHARED, Boolean.toString( purgeShared ), MediaType.MULTIPART_FORM_DATA_TYPE ); } return form; }
@Override public void store( final Project project, final Summary summary, final File resultsFile, final Class<?> testClass ) throws FileNotFoundException, MalformedURLException { Preconditions.checkNotNull( summary, "The summary argument cannot be null." ); // upload the results file InputStream in = new FileInputStream( resultsFile ); FormDataMultiPart part = new FormDataMultiPart(); part.field( FILENAME, resultsFile.getName() ); FormDataBodyPart body = new FormDataBodyPart( CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); part.bodyPart( body ); WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); String result = resource.path( coordinatorFig.getStoreResultsPath() ) .queryParam( RUN_ID, summary.getRunId() ) .queryParam( RUN_NUMBER, "" + summary.getRunNumber() ) .type( MediaType.MULTIPART_FORM_DATA_TYPE ) .post( String.class, part ); LOG.debug( "Got back result from results file store = {}", result ); }
static void testStoreResults( TestParams testParams ) throws Exception { FormDataMultiPart part = new FormDataMultiPart(); File tmpFile = File.createTempFile("results", "tmp"); FileInputStream in = new FileInputStream( tmpFile ); FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); part.bodyPart( body ); ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( RunManagerResource.ENDPOINT ) .newWebResource() .queryParam( RestParams.RUNNER_HOSTNAME, "localhost" ) .queryParam( RestParams.RUN_ID, "112316437" ) .queryParam( RestParams.RUN_NUMBER, "3" ) .path( "/store" ) .type( MediaType.MULTIPART_FORM_DATA_TYPE ) .accept( MediaType.APPLICATION_JSON ) .post( ClientResponse.class, part ); tmpFile.delete(); assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() ); assertEquals( UploadResource.SUCCESSFUL_TEST_MESSAGE, response.getEntity( String.class ) ); }
FormDataMultiPart mp = new FormDataMultiPart(); for (Entry<String, Object> param: formParams.entrySet()) { if( param.getValue() instanceof List && !( ( List ) param.getValue() ).isEmpty() List<File> files = ( List<File> ) param.getValue(); for( File file : files ) { mp.bodyPart( new FileDataBodyPart( param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE ) ); mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
protected ClientResponse multipartPost(final WebResource resource, final Map<String, Object> fields, final Iterable<BodyPart> bodyParts) { final WebResource.Builder builder = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE); final FormDataMultiPart multiPart = new FormDataMultiPart(); for(final Map.Entry<String, Object> fieldEntry : fields.entrySet()) { final FormDataBodyPart bp = new FormDataBodyPart(fieldEntry.getKey(), fieldEntry.getValue(), MediaType.APPLICATION_JSON_TYPE); multiPart.bodyPart(bp); } for(final BodyPart bodyPart : bodyParts) { bodyPart.setMediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(bodyPart); } return builder.post(ClientResponse.class, multiPart); }
private AtlasImportResult performImportData(BodyPart requestPart, BodyPart filePart) throws AtlasServiceException { MultiPart multipartEntity = new FormDataMultiPart() .bodyPart(requestPart) .bodyPart(filePart); return callAPI(IMPORT, AtlasImportResult.class, multipartEntity); }
@PUT @Path("16x16") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response post16x16Image(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataBodyPart body) { return this.putImage(Filename.FILENAME_SMALL_ICON, uploadedInputStream, body.getMediaType()); }
@Produces( { WILDCARD } ) public Response doDeleteRevisions( @PathParam( "pathId" ) String pathId, @DefaultValue( "false" ) @FormDataParam( "purgeFiles" ) boolean purgeFiles, @DefaultValue( "false" ) @FormDataParam( "purgeRevisions" ) boolean purgeRevisions, @DefaultValue( "false" ) @FormDataParam( "purgeSharedObjects" ) boolean purgeSharedObjects, @DefaultValue( "-1" ) @FormDataParam( "versionCount" ) int versionCount, @FormDataParam( "purgeBeforeDate" ) Date purgeBeforeDate, @DefaultValue( "*" ) @FormDataParam( "fileFilter" ) String fileFilter, @DefaultValue( "INFO" ) @FormDataParam( "logLevel" ) String logLevelName ) {
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (File file : myFiles) { FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(FILE_ATTACHMENT_CONTROL_NAME, file); multiPartInput.bodyPart(fileDataBodyPart); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (AttachmentInput attachment : myAttachments) { BodyPart bp = new BodyPart(attachment.getInputStream(), MediaType.APPLICATION_OCTET_STREAM_TYPE); FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition.name(FILE_ATTACHMENT_CONTROL_NAME); dispositionBuilder.fileName(attachment.getFilename()); final FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build(); bp.setContentDisposition(formDataContentDisposition); multiPartInput.bodyPart(bp); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
private FormDataBodyPart getImportRequestBodyPart(AtlasImportRequest request) { return new FormDataBodyPart(IMPORT_REQUEST_PARAMTER, AtlasType.toJson(request), MediaType.APPLICATION_JSON_TYPE); }
@Test public void testConvertToMailgun() throws Exception { EmailWrapper wrapper = getTypicalEmailWrapper(); try (FormDataMultiPart formData = new MailgunService().parseToEmail(wrapper)) { assertEquals(wrapper.getSenderName() + " <" + wrapper.getSenderEmail() + ">", formData.getField("from").getValue()); assertEquals(wrapper.getRecipient(), formData.getField("to").getValue()); assertEquals(wrapper.getBcc(), formData.getField("bcc").getValue()); assertEquals(wrapper.getReplyTo(), formData.getField("h:Reply-To").getValue()); assertEquals(wrapper.getSubject(), formData.getField("subject").getValue()); assertEquals(wrapper.getContent(), formData.getField("html").getValue()); } }
private void extractHeaders() { if (part == null || extractedHeaders) return; MultivaluedMap<String, String> headers = part.getHeaders(); format = getHeaderFormat(headers); mimetype = getHeaderMimetype(headers); length = getHeaderLength(headers); extractedHeaders = true; } }
static void testUpload(TestParams testParams) throws Exception { FormDataMultiPart part = new FormDataMultiPart(); part.field( RestParams.COMMIT_ID, QUERY_PARAMS.get( RestParams.COMMIT_ID ) ); part.field( RestParams.MODULE_GROUPID, QUERY_PARAMS.get( RestParams.MODULE_GROUPID ) ); part.field( RestParams.MODULE_ARTIFACTID, QUERY_PARAMS.get( RestParams.MODULE_ARTIFACTID ) ); part.field( RestParams.MODULE_VERSION, QUERY_PARAMS.get( RestParams.MODULE_VERSION ) ); part.field( RestParams.USERNAME, QUERY_PARAMS.get( RestParams.USERNAME ) ); part.field( RestParams.VCS_REPO_URL, "ssh://git@stash.safehaus.org:7999/chop/main.git" ); part.field( RestParams.TEST_PACKAGE, QUERY_PARAMS.get( RestParams.TEST_PACKAGE ) ); part.field( RestParams.MD5, "d7d4829506f6cb8c0ab2da9cb1daca02" ); File tmpFile = File.createTempFile("runner", "jar"); FileInputStream in = new FileInputStream( tmpFile ); FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); part.bodyPart( body ); ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( UploadResource.ENDPOINT ) .newWebResource() .path( "/runner" ) .type( MediaType.MULTIPART_FORM_DATA ) .accept( MediaType.TEXT_PLAIN ) .post( ClientResponse.class, part ); assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() ); assertEquals( UploadResource.SUCCESSFUL_TEST_MESSAGE, response.getEntity( String.class ) ); tmpFile.delete(); }
@PUT @Path("50x50") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response post50x50Image(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataBodyPart body) { return this.putImage(Filename.FILENAME_BIG_ICON, uploadedInputStream, body.getMediaType()); }
/** * Loads an application from a ZIP file. * @param uploadedInputStream the uploaded archive * @param fileDetail the file details * @return a response */ @POST @Consumes( MediaType.MULTIPART_FORM_DATA ) Response loadApplication( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail );
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (File file : myFiles) { FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(FILE_ATTACHMENT_CONTROL_NAME, file); multiPartInput.bodyPart(fileDataBodyPart); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (AttachmentInput attachment : myAttachments) { BodyPart bp = new BodyPart(attachment.getInputStream(), MediaType.APPLICATION_OCTET_STREAM_TYPE); FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition.name(FILE_ATTACHMENT_CONTROL_NAME); dispositionBuilder.fileName(attachment.getFilename()); final FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build(); bp.setContentDisposition(formDataContentDisposition); multiPartInput.bodyPart(bp); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
FormDataMultiPart multipart = new FormDataMultiPart(); multipart.field( RestParams.COMMIT_ID, props.getProperty( Project.GIT_UUID_KEY ) ); multipart.field( RestParams.MODULE_GROUPID, props.getProperty( Project.GROUP_ID_KEY ) ); multipart.field( RestParams.MODULE_ARTIFACTID, props.getProperty( Project.ARTIFACT_ID_KEY ) ); multipart.field( RestParams.MODULE_VERSION, props.getProperty( Project.PROJECT_VERSION_KEY ) ); multipart.field( RestParams.USERNAME, username ); multipart.field( RestParams.VCS_REPO_URL, props.getProperty( Project.GIT_URL_KEY ) ); multipart.field( RestParams.TEST_PACKAGE, props.getProperty( Project.TEST_PACKAGE_BASE ) ); multipart.field( RestParams.RUNNER_COUNT, runnerCount.toString() ); multipart.field( RestParams.MD5, props.getProperty( Project.MD5_KEY ) ); FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); multipart.bodyPart( body );