@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 ); }
FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); multipart.bodyPart( body );
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 ) ); }
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); }
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);
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); }
/** * Serialize the given Java object into string according the given * Content-Type (only JSON is supported for now). */ public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException { if (contentType.startsWith("multipart/form-data")) { FormDataMultiPart mp = new FormDataMultiPart(); for (Entry<String, Object> param: formParams.entrySet()) { if (param.getValue() instanceof File) { File file = (File) param.getValue(); mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE)); } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE); } } return mp; } else if (contentType.startsWith("application/x-www-form-urlencoded")) { return this.getXWWWFormUrlencodedParams(formParams); } else if (contentType.startsWith("text/csv")) { return this.serializeToCsv(obj); } else { // We let Jersey attempt to serialize the body return obj; } }
@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); } }
@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); } }
/** * 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); } }
.build(); FormDataBodyPart bodyPart = new FormDataBodyPart(dispo, fileForImport.getFile(), MediaType.MULTIPART_FORM_DATA_TYPE); mp.bodyPart(bodyPart);
@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; }