use of org.glassfish.jersey.media.multipart.file.StreamDataBodyPart in project kylo by Teradata.
the class JerseyRestClient method postMultiPartStream.
/**
* 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);
}
use of org.glassfish.jersey.media.multipart.file.StreamDataBodyPart in project streamline by hortonworks.
the class RestIntegrationTest method testFileResources.
@Test
public void testFileResources() throws Exception {
Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
String response = null;
String url = rootUrl + "files";
// POST
File file = new File();
file.setName("milkyway-jar");
file.setVersion(System.currentTimeMillis());
MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
String initialJarContent = "milkyway-jar-contents";
final InputStream fileStream = new ByteArrayInputStream(initialJarContent.getBytes());
multiPart.bodyPart(new StreamDataBodyPart("file", fileStream, "file"));
multiPart.bodyPart(new FormDataBodyPart("fileInfo", file, MediaType.APPLICATION_JSON_TYPE));
File postedFile = client.target(url).request(MediaType.MULTIPART_FORM_DATA_TYPE, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()), File.class);
// DOWNLOAD
InputStream downloadInputStream = client.target(url + "/download/" + postedFile.getId()).request().get(InputStream.class);
ByteArrayOutputStream downloadedJarOutputStream = new ByteArrayOutputStream();
IOUtils.copy(downloadInputStream, downloadedJarOutputStream);
ByteArrayOutputStream uploadedOutputStream = new ByteArrayOutputStream();
IOUtils.copy(new ByteArrayInputStream(initialJarContent.getBytes()), uploadedOutputStream);
Assert.assertArrayEquals(uploadedOutputStream.toByteArray(), downloadedJarOutputStream.toByteArray());
// GET all
response = client.target(url).request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
List<File> files = getEntities(response, File.class);
Assert.assertEquals(files.size(), 1);
Assert.assertEquals(files.iterator().next().getName(), file.getName());
// GET /files/1
File receivedFile = client.target(url + "/" + postedFile.getId()).request(MediaType.APPLICATION_JSON_TYPE).get(File.class);
Assert.assertEquals(receivedFile.getName(), postedFile.getName());
Assert.assertEquals(receivedFile.getId(), postedFile.getId());
// PUT
postedFile.setName("andromeda-jar");
postedFile.setVersion(System.currentTimeMillis());
multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
InputStream updatedFileStream = new ByteArrayInputStream("andromeda-jar-contents".getBytes());
multiPart.bodyPart(new StreamDataBodyPart("file", updatedFileStream, "file"));
multiPart.bodyPart(new FormDataBodyPart("fileInfo", postedFile, MediaType.APPLICATION_JSON_TYPE));
File updatedFile = client.target(url).request(MediaType.MULTIPART_FORM_DATA_TYPE, MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()), File.class);
Assert.assertEquals(updatedFile.getId(), postedFile.getId());
Assert.assertEquals(updatedFile.getName(), postedFile.getName());
// DELETE
final File deletedFile = client.target(url + "/" + updatedFile.getId()).request().delete(File.class);
Assert.assertEquals(deletedFile.getId(), updatedFile.getId());
// GET
response = client.target(url).request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
files = getEntities(response, File.class);
Assert.assertTrue(files.isEmpty());
}
use of org.glassfish.jersey.media.multipart.file.StreamDataBodyPart in project streamline by hortonworks.
the class RestIntegrationTest method testCustomProcessorInfos.
@Test
@Ignore
public void testCustomProcessorInfos() throws Exception {
// Some issue with sending multi part for requests using this client and hence this test case is ignored for now. Fix later.
String response;
String prefixUrl = rootUrl + "streams/componentbundles/PROCESSOR/custom";
CustomProcessorInfo customProcessorInfo = createCustomProcessorInfo();
String prefixQueryParam = "?streamingEngine=STORM";
List<String> getUrlQueryParms = new ArrayList<String>();
getUrlQueryParms.add(prefixQueryParam + "&name=ConsoleCustomProcessor");
getUrlQueryParms.add(prefixQueryParam + "&jarFileName=streamline-core.jar");
getUrlQueryParms.add(prefixQueryParam + "&customProcessorImpl=" + ConsoleCustomProcessor.class.getCanonicalName());
List<List<CustomProcessorInfo>> getResults = new ArrayList<List<CustomProcessorInfo>>();
getResults.add(Arrays.asList(customProcessorInfo));
getResults.add(Arrays.asList(customProcessorInfo));
getResults.add(Arrays.asList(customProcessorInfo));
getResults.add(Arrays.asList(customProcessorInfo));
List<String> getUrls = new ArrayList<String>();
for (String queryParam : getUrlQueryParms) {
getUrls.add(prefixUrl + queryParam);
}
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MultiPartWriter.class);
Client client = ClientBuilder.newClient(clientConfig);
for (String getUrl : getUrls) {
try {
client.target(getUrl).request().get(String.class);
Assert.fail("Should have thrown NotFoundException.");
} catch (NotFoundException e) {
// pass
}
}
/*FileDataBodyPart imageFileBodyPart = new FileDataBodyPart(TopologyCatalogResource.IMAGE_FILE_PARAM_NAME, getCpImageFile(), MediaType
.APPLICATION_OCTET_STREAM_TYPE);
FileDataBodyPart jarFileBodyPart = new FileDataBodyPart(TopologyCatalogResource.JAR_FILE_PARAM_NAME, getCpJarFile(), MediaType
.APPLICATION_OCTET_STREAM_TYPE);*/
MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
multiPart.bodyPart(new StreamDataBodyPart(TopologyComponentBundleResource.JAR_FILE_PARAM_NAME, JAR_FILE_STREAM));
multiPart.bodyPart(new FormDataBodyPart(TopologyComponentBundleResource.CP_INFO_PARAM_NAME, customProcessorInfo, MediaType.APPLICATION_JSON_TYPE));
client.target(prefixUrl).request(MediaType.MULTIPART_FORM_DATA_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()));
for (int i = 0; i < getUrls.size(); ++i) {
String getUrl = getUrls.get(i);
List<CustomProcessorInfo> expectedResults = getResults.get(i);
response = client.target(getUrl).request().get(String.class);
Assert.assertEquals(expectedResults, getEntities(response, expectedResults.get(i).getClass()));
}
}
use of org.glassfish.jersey.media.multipart.file.StreamDataBodyPart in project registry by hortonworks.
the class SchemaRegistryClient method uploadFile.
@Override
public String uploadFile(InputStream inputStream) {
MultiPart multiPart = new MultiPart();
BodyPart filePart = new StreamDataBodyPart("file", inputStream, "file");
multiPart.bodyPart(filePart);
return Subject.doAs(subject, new PrivilegedAction<String>() {
@Override
public String run() {
return currentSchemaRegistryTargets().filesTarget.request().post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA), String.class);
}
});
}
use of org.glassfish.jersey.media.multipart.file.StreamDataBodyPart in project Payara by payara.
the class CommonPayaraManager method deploy.
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 Payara
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 Payara
HTTPContext httpContext = payaraClient.doDeploy(deploymentName, form);
protocolMetaData.addContext(httpContext);
} catch (PayaraClientException e) {
throw new DeploymentException("Could not deploy " + archiveName, e);
}
return protocolMetaData;
}
Aggregations