Search in sources :

Example 1 with StreamDataBodyPart

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);
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with StreamDataBodyPart

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());
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) Client(javax.ws.rs.client.Client) File(com.hortonworks.streamline.streams.catalog.File) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 3 with StreamDataBodyPart

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()));
    }
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) NotFoundException(javax.ws.rs.NotFoundException) CustomProcessorInfo(com.hortonworks.streamline.streams.catalog.processor.CustomProcessorInfo) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Ignore(org.junit.Ignore) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 4 with StreamDataBodyPart

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);
        }
    });
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart)

Example 5 with StreamDataBodyPart

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;
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) InputStream(java.io.InputStream) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) HTTPContext(org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) ProtocolMetaData(org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData) PayaraClientException(fish.payara.arquillian.container.payara.clientutils.PayaraClientException)

Aggregations

StreamDataBodyPart (org.glassfish.jersey.media.multipart.file.StreamDataBodyPart)7 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)6 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)4 IntegrationTest (com.hortonworks.streamline.common.test.IntegrationTest)2 Client (javax.ws.rs.client.Client)2 WebTarget (javax.ws.rs.client.WebTarget)2 MediaType (javax.ws.rs.core.MediaType)2 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)2 Test (org.junit.Test)2 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)1 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)1 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)1 File (com.hortonworks.streamline.streams.catalog.File)1 CustomProcessorInfo (com.hortonworks.streamline.streams.catalog.processor.CustomProcessorInfo)1 PayaraClientException (fish.payara.arquillian.container.payara.clientutils.PayaraClientException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 NotFoundException (javax.ws.rs.NotFoundException)1 Response (javax.ws.rs.core.Response)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1