Search in sources :

Example 11 with MultiPart

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

the class FileUploadResourceTest method checkDictionaryUpload.

// TODO:: We are getting 400. However, it works with front-end. So we need to fix this test case.
@Test
@Ignore
public void checkDictionaryUpload() throws Exception {
    Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client");
    client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
    client.property(ClientProperties.READ_TIMEOUT, 5000);
    client.register(MultiPartFeature.class);
    final MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
    File testDictionaryFile = new File(ResourceHelpers.resourceFilePath("test_dictionary.txt"));
    final FileDataBodyPart filePart = new FileDataBodyPart("file", testDictionaryFile);
    multiPart.bodyPart(filePart);
    Response response = client.target(String.format("http://localhost:%d/api/upload/dictionary", RULE.getLocalPort())).request(MediaType.MULTIPART_FORM_DATA_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()));
    assertThat(response.getStatus()).isEqualTo(200);
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) Client(javax.ws.rs.client.Client) File(java.io.File) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project atsd-api-test by axibase.

the class CSVUploadMethod method importParser.

public static boolean importParser(File configPath) {
    Response response = executeRootRequest(webTarget -> {
        Invocation.Builder builder = webTarget.path("/csv/configs/import").request();
        MultiPart multiPart = new MultiPart();
        FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", configPath, getMediaTypeFromFile(configPath));
        multiPart.bodyPart(fileDataBodyPart);
        return builder.post(Entity.entity(multiPart, Boundary.addBoundary(MediaType.MULTIPART_FORM_DATA_TYPE)));
    });
    response.bufferEntity();
    return response.getStatus() == OK.getStatusCode();
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) Invocation(javax.ws.rs.client.Invocation) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 13 with MultiPart

use of org.glassfish.jersey.media.multipart.MultiPart in project atsd-api-test by axibase.

the class CSVUploadMethod method multipartCsvUpload.

public static Response multipartCsvUpload(File file, String parserName) {
    MultiPart multiPart = new MultiPart();
    MediaType mediaType = getMediaTypeFromFile(file);
    if (mediaType.equals(MediaType.APPLICATION_OCTET_STREAM_TYPE)) {
        mediaType = new MediaType("text", "csv");
    }
    FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("filedata", file, mediaType);
    multiPart.bodyPart(fileDataBodyPart);
    Response response = executeApiRequest(webTarget -> webTarget.path("csv").queryParam("config", parserName).queryParam("wait", true).request().post(Entity.entity(multiPart, Boundary.addBoundary(MediaType.MULTIPART_FORM_DATA_TYPE))));
    response.bufferEntity();
    return response;
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 14 with MultiPart

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

the class IntegrationTest method multipartPost.

private Response multipartPost(String path, File resource, String mediaType, Map<String, String> arguments) throws ParseException {
    MultiPart formdata = new MultiPart();
    arguments.forEach((key, value) -> formdata.bodyPart(new FormDataBodyPart(key, value)));
    formdata.bodyPart(new FormDataBodyPart(new FormDataContentDisposition("form-data; name=\"file\"; filename=\"" + resource.getName().replace("\"", "") + "\""), resource, MediaType.valueOf(mediaType)));
    Response result = call(path).post(Entity.entity(formdata, "multipart/form-data; boundary=Boundary_1_498293219_1483974344746"));
    return result;
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition)

Example 15 with MultiPart

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

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