/** * 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 ); }
FormDataMultiPart multipart = new FormDataMultiPart();
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(); }
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()
public AtlasImportResult importData(AtlasImportRequest request, String absoluteFilePath) throws AtlasServiceException { FileDataBodyPart filePart = new FileDataBodyPart("data", new File(absoluteFilePath)); MultiPart multipartEntity = new FormDataMultiPart() .field("request", AtlasType.toJson(request), MediaType.APPLICATION_JSON_TYPE) .bodyPart(filePart); return callAPI(IMPORT, AtlasImportResult.class, multipartEntity); }
private AtlasImportResult performImportData(BodyPart requestPart, BodyPart filePart) throws AtlasServiceException { MultiPart multipartEntity = new FormDataMultiPart() .bodyPart(requestPart) .bodyPart(filePart); return callAPI(IMPORT, AtlasImportResult.class, multipartEntity); }
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); }
public void undeploy(Archive<?> archive) throws DeploymentException { if (archive == null) { throw new IllegalArgumentException("archive must not be null"); } else { deploymentName = createDeploymentName(archive.getName()); try { // Build up the POST form to send to Glassfish final FormDataMultiPart form = new FormDataMultiPart(); form.field("target", this.configuration.getTarget(), MediaType.TEXT_PLAIN_TYPE); form.field("operation", DELETE_OPERATION, MediaType.TEXT_PLAIN_TYPE); glassFishClient.doUndeploy(this.deploymentName, form); } catch (GlassFishClientException e) { throw new DeploymentException("Could not undeploy " + archive.getName(), e); } } }
/** * upload from the given inputstream * * @param fileToUpload * @param filename * @param contents * @param comment * @throws Exception */ public synchronized void upload(InputStream fileToUpload, String filename, String contents, String comment) throws Exception { TokenResult token = getEditToken("File:" + filename, "edit"); final FormDataMultiPart multiPart = new FormDataMultiPart(); // http://stackoverflow.com/questions/5772225/trying-to-upload-a-file-to-a-jax-rs-jersey-server multiPart.bodyPart(new StreamDataBodyPart("file", fileToUpload)); multiPart.field("filename", filename); multiPart.field("ignorewarnings", "true"); multiPart.field("text", contents); if (!comment.isEmpty()) multiPart.field("comment", comment); String params = ""; Api api = this.getActionResult("upload", params, token, multiPart); handleError(api); }
@Override public IOsKeyResponse unlockKey(WebResource resource, ResourcePath<Key> keyResourcePath, IOsKeyUnlockRequest iOsKeyUnlockRequest) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FormDataBodyPart("data", iOsKeyUnlockRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(keyResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).put( IOsKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AndroidKeyResponse unlockKey(WebResource resource, ResourcePath<Key> keyResourcePath, AndroidKeyUnlockRequest androidKeyUnlockRequest) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FormDataBodyPart("data", androidKeyUnlockRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(keyResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).put( AndroidKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AppResponse updateAppDetails(final WebResource resource, final ResourcePath<App> keyResourcePath, final AppDetailsRequest appDetailsRequest) { try { final FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FormDataBodyPart("data", appDetailsRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(keyResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).put( AppResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException { if (archive == null) { throw new IllegalArgumentException("archive must not be null"); } final String archiveName = archive.getName(); final ProtocolMetaData protocolMetaData = new ProtocolMetaData(); try { InputStream deployment = archive.as(ZipExporter.class).exportAsInputStream(); // Build up the POST form to send to Glassfish final FormDataMultiPart form = new FormDataMultiPart(); form.bodyPart(new StreamDataBodyPart("id", deployment, archiveName)); deploymentName = createDeploymentName(archiveName); addDeployFormFields(deploymentName, form); // Do Deploy the application on the remote GlassFish HTTPContext httpContext = glassFishClient.doDeploy(deploymentName, form); protocolMetaData.addContext(httpContext); } catch (GlassFishClientException e) { throw new DeploymentException("Could not deploy " + archiveName, e); } return protocolMetaData; }
@Override public AppResponse postNewApp(WebResource resource, ResourcePath<Apps> appsResponsePath, AppDetailsRequest appsRequest, File file) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("file", file)); multiPart.bodyPart(new FormDataBodyPart("data", appsRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(appsResponsePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post(AppResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AndroidKeyResponse postNewKey(WebResource resource, ResourcePath<PlatformKeys> platformResourcePath, AndroidKeyRequest androidKeyRequest, File keystore) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("keystore", keystore)); multiPart.bodyPart(new FormDataBodyPart("data", androidKeyRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(platformResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post( AndroidKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AppResponse putApp(WebResource resource, ResourcePath<App> appResourcePath, AppDetailsRequest appsRequest, File file) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("file", file)); if (appsRequest != null) { multiPart.bodyPart(new FormDataBodyPart("data", appsRequest, MediaType.APPLICATION_JSON_TYPE)); } return resource.path(appResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).put(AppResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public IOsKeyResponse postNewKey(WebResource resource, ResourcePath<PlatformKeys> platformResourcePath, IOsKeyRequest iOsKeyRequest, File cert, File profile) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("cert", cert)); multiPart.bodyPart(new FileDataBodyPart("profile", profile)); multiPart.bodyPart(new FormDataBodyPart("data", iOsKeyRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(platformResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post( IOsKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Test public void test() throws Exception { UploadCredentials credentials = new GcsUploadCredentialBuilder(new TestingIdentityService(PRIVATE_KEY_FILE_PATH), "file.png"). setCreatorId(CuidAdapter.userId(1)). setOwnerId(ResourceId.generateId()). setBucket("ai-dev-field-blob-test"). setKey(BlobId.generate().asString()). setMaxContentLengthInMegabytes(10). expireAfter(Duration.standardMinutes(5)). build(); FormDataMultiPart form = new FormDataMultiPart(); for (Map.Entry<String, String> entry : credentials.getFormFields().entrySet()) { form.field(entry.getKey(), entry.getValue()); } form.field("file", Resources.asByteSource(Resources.getResource(getClass(), "goabout.png")).read(), MediaType.valueOf(PNG.toString())); Client.create() .resource(credentials.getUrl()) .entity(form, MediaType.MULTIPART_FORM_DATA_TYPE) .post(); }