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);
}
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();
}
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;
}
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;
}
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());
}
Aggregations