Search in sources :

Example 16 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart 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 17 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart 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 18 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project registry by hortonworks.

the class AbstractRestIntegrationTest method getMultiPart.

protected MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) {
    MultiPart multiPart = new MultiPart();
    BodyPart filePart = new FileDataBodyPart(resourceToTest.getFileNameHeader(), resourceToTest.getFileToUpload());
    BodyPart entityPart = new FormDataBodyPart(resourceToTest.getEntityNameHeader(), entity, MediaType.APPLICATION_JSON_TYPE);
    multiPart.bodyPart(filePart).bodyPart(entityPart);
    return multiPart;
}
Also used : FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 19 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project batfish by batfish.

the class BfCoordWorkHelper method getConFiguration.

/**
 * Returns a string contains the content of the configuration file {@code configName}, returns
 * null if configuration file {@code configName} does not exist or the api key that is using has
 * no access to the container {@code containerName}
 */
@Nullable
public String getConFiguration(String containerName, String testrigName, String configName) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_CONFIGURATION);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_VERSION, Version.getVersion());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_TESTRIG_NAME, testrigName);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONFIGURATION_NAME, configName);
        Response response = webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(multiPart, multiPart.getMediaType()));
        _logger.debugf("%s %s %s\n", response.getStatus(), response.getStatusInfo(), response);
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            _logger.error("GetConfiguration: Did not get an OK response\n");
            _logger.error(response.readEntity(String.class) + "\n");
            return null;
        }
        String configContent = response.readEntity(String.class);
        return configContent;
    } catch (Exception e) {
        _logger.errorf("Exception in getConfiguration from %s for container %s, testrig %s, configuration %s\n", _coordWorkMgr, containerName, testrigName, configName);
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Example 20 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project batfish by batfish.

the class BfCoordWorkHelper method configureAnalysis.

public boolean configureAnalysis(String containerName, boolean newAnalysis, String analysisName, @Nullable String addQuestionsFileName, @Nullable String delQuestionsStr) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CONFIGURE_ANALYSIS);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_CONTAINER_NAME, containerName);
        if (newAnalysis) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NEW_ANALYSIS, "new");
        }
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName);
        if (addQuestionsFileName != null) {
            addFileMultiPart(multiPart, CoordConsts.SVC_KEY_FILE, addQuestionsFileName);
        }
        if (delQuestionsStr != null) {
            addTextMultiPart(multiPart, CoordConsts.SVC_KEY_DEL_ANALYSIS_QUESTIONS, delQuestionsStr);
        }
        return postData(webTarget, multiPart) != null;
    } catch (Exception e) {
        if (e.getMessage().contains("FileNotFoundException")) {
            _logger.errorf("File not found: %s (addQuestionsFile file)\n", addQuestionsFileName);
        } else {
            _logger.errorf("Exception when configuring analysis to %s using (%s, %s, %s, %s, %s): %s\n", _coordWorkMgr, containerName, newAnalysis, analysisName, addQuestionsFileName, delQuestionsStr, ExceptionUtils.getStackTrace(e));
        }
        return false;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

MultiPart (org.glassfish.jersey.media.multipart.MultiPart)63 WebTarget (javax.ws.rs.client.WebTarget)42 ProcessingException (javax.ws.rs.ProcessingException)28 BatfishException (org.batfish.common.BatfishException)28 JSONObject (org.codehaus.jettison.json.JSONObject)20 Test (org.junit.Test)16 Nullable (javax.annotation.Nullable)15 MediaType (javax.ws.rs.core.MediaType)15 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)11 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)11 Response (javax.ws.rs.core.Response)10 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)10 Client (javax.ws.rs.client.Client)7 StreamDataBodyPart (org.glassfish.jersey.media.multipart.file.StreamDataBodyPart)7 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)5 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)3 File (java.io.File)3 Path (javax.ws.rs.Path)3