use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project scheduling by ow2-proactive.
the class StudioRest method createClass.
@Override
public String createClass(String sessionId, MultipartFormDataInput input) throws NotConnectedRestException, IOException {
try {
String userName = getUserName(sessionId);
File classesDir = new File(getFileStorageSupport().getWorkflowsDir(userName), "classes");
if (!classesDir.exists()) {
logger.info("Creating dir " + classesDir.getAbsolutePath());
classesDir.mkdirs();
}
String fileName = "";
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
String name = uploadForm.keySet().iterator().next();
List<InputPart> inputParts = uploadForm.get(name);
for (InputPart inputPart : inputParts) {
try {
// convert the uploaded file to inputstream
InputStream inputStream = inputPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
// constructs upload file path
fileName = classesDir.getAbsolutePath() + File.separator + name;
FileUtils.writeByteArrayToFile(new File(fileName), bytes);
} catch (IOException e) {
logger.warn("Could not read input part", e);
throw e;
}
}
return fileName;
} finally {
if (input != null) {
input.close();
}
}
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project scheduling by ow2-proactive.
the class SchedulerStateRest method submit.
/**
* Submits a job to the scheduler
*
* @param sessionId
* a valid session id
* @return the <code>jobid</code> of the newly created job
* @throws IOException
* if the job was not correctly uploaded/stored
*/
@Override
@POST
@Path("{path:submit}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public JobIdData submit(@HeaderParam("sessionid") String sessionId, @PathParam("path") PathSegment pathSegment, MultipartFormDataInput multipart) throws JobCreationRestException, NotConnectedRestException, PermissionRestException, SubmissionClosedRestException, IOException {
try {
Scheduler scheduler = checkAccess(sessionId, "submit");
Map<String, List<InputPart>> formDataMap = multipart.getFormDataMap();
String name = formDataMap.keySet().iterator().next();
File tmpJobFile = null;
try {
// "file"
InputPart part1 = multipart.getFormDataMap().get(name).get(0);
String fileType = part1.getMediaType().toString().toLowerCase();
if (!fileType.contains(MediaType.APPLICATION_XML.toLowerCase())) {
throw new JobCreationRestException("Unknown job descriptor type: " + fileType);
}
// is the name of the browser's input field
InputStream is = part1.getBody(new GenericType<InputStream>() {
});
tmpJobFile = File.createTempFile("job", "d");
JobId jobId;
try (OutputStream outputStream = new FileOutputStream(tmpJobFile)) {
IOUtils.copy(is, outputStream);
Map<String, String> jobVariables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
WorkflowSubmitter workflowSubmitter = new WorkflowSubmitter(scheduler);
jobId = workflowSubmitter.submit(tmpJobFile, jobVariables);
}
return mapper.map(jobId, JobIdData.class);
} finally {
if (tmpJobFile != null) {
// clean the temporary file
FileUtils.deleteQuietly(tmpJobFile);
}
}
} catch (IOException e) {
throw new IOException("I/O Error: " + e.getMessage(), e);
}
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project candlepin by candlepin.
the class OwnerResourceTest method testImportManifestAsyncSuccess.
@Test
public void testImportManifestAsyncSuccess() throws IOException, ImporterException {
ManifestManager manifestManager = mock(ManifestManager.class);
EventSink es = mock(EventSink.class);
OwnerResource thisOwnerResource = new OwnerResource(ownerCurator, productCurator, null, null, i18n, es, eventFactory, null, null, manifestManager, null, null, null, null, importRecordCurator, null, null, null, null, null, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
MultipartInput input = mock(MultipartInput.class);
InputPart part = mock(InputPart.class);
File archive = mock(File.class);
List<InputPart> parts = new ArrayList<>();
parts.add(part);
MultivaluedMap<String, String> mm = new MultivaluedMapImpl<>();
List<String> contDis = new ArrayList<>();
contDis.add("form-data; name=\"upload\"; filename=\"test_file.zip\"");
mm.put("Content-Disposition", contDis);
JobDetail job = mock(JobDetail.class);
when(input.getParts()).thenReturn(parts);
when(part.getHeaders()).thenReturn(mm);
when(part.getBody(any(GenericType.class))).thenReturn(archive);
when(manifestManager.importManifestAsync(eq(owner), any(File.class), eq("test_file.zip"), any(ConflictOverrides.class))).thenReturn(job);
JobDetail response = thisOwnerResource.importManifestAsync(owner.getKey(), new String[] {}, input);
assertNotNull(response);
assertEquals(job, response);
verify(manifestManager, never()).importManifest(eq(owner), any(File.class), any(String.class), any(ConflictOverrides.class));
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldCreateFileInputDetailsFromFilePart.
@Test
public void shouldCreateFileInputDetailsFromFilePart() throws Exception {
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.class);
final InputStream inputStream = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = ImmutableMap.of(fieldName, singletonList(inputPart));
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
when(inputPart.getBody(InputStream.class, null)).thenReturn(inputStream);
final List<FileInputDetails> fileInputDetails = fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
final FileInputDetails inputDetails = fileInputDetails.get(0);
assertThat(inputDetails.getFileName(), is(fileName));
assertThat(inputDetails.getFieldName(), is(fieldName));
assertThat(inputDetails.getInputStream(), is(inputStream));
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldHandleMoreThanOneMultipart.
@Test
public void shouldHandleMoreThanOneMultipart() throws Exception {
final String fileName_1 = "the-file-name_1.jpeg";
final String fieldName_1 = "myFieldName_1";
final String fileName_2 = "the-file-name_2.jpeg";
final String fieldName_2 = "myFieldName_2";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart_1 = mock(InputPart.class);
final InputPart inputPart_2 = mock(InputPart.class);
final InputStream inputStream_1 = mock(InputStream.class);
final InputStream inputStream_2 = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = ImmutableMap.of(fieldName_1, singletonList(inputPart_1), fieldName_2, singletonList(inputPart_2));
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart_1)).thenReturn(fileName_1);
when(inputPartFileNameExtractor.extractFileName(inputPart_2)).thenReturn(fileName_2);
when(inputPart_1.getBody(InputStream.class, null)).thenReturn(inputStream_1);
when(inputPart_2.getBody(InputStream.class, null)).thenReturn(inputStream_2);
final List<FileInputDetails> fileInputDetails = fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, asList(fieldName_1, fieldName_2));
final FileInputDetails inputDetails_1 = fileInputDetails.get(0);
assertThat(inputDetails_1.getFileName(), is(fileName_1));
assertThat(inputDetails_1.getFieldName(), is(fieldName_1));
assertThat(inputDetails_1.getInputStream(), is(inputStream_1));
final FileInputDetails inputDetails_2 = fileInputDetails.get(1);
assertThat(inputDetails_2.getFileName(), is(fileName_2));
assertThat(inputDetails_2.getFieldName(), is(fieldName_2));
assertThat(inputDetails_2.getInputStream(), is(inputStream_2));
}
Aggregations