Search in sources :

Example 16 with InputPart

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();
        }
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 17 with InputPart

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);
    }
}
Also used : Scheduler(org.ow2.proactive.scheduler.common.Scheduler) BufferedInputStream(java.io.BufferedInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) JobCreationRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.JobCreationRestException) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) JobId(org.ow2.proactive.scheduler.common.job.JobId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 18 with InputPart

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));
}
Also used : ConflictOverrides(org.candlepin.sync.ConflictOverrides) GenericType(org.jboss.resteasy.util.GenericType) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Matchers.anyString(org.mockito.Matchers.anyString) ManifestManager(org.candlepin.controller.ManifestManager) JobDetail(org.quartz.JobDetail) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) EventSink(org.candlepin.audit.EventSink) File(java.io.File) Test(org.junit.Test)

Example 19 with InputPart

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));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) InputStream(java.io.InputStream) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 20 with InputPart

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));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) InputStream(java.io.InputStream) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Aggregations

InputPart (org.jboss.resteasy.plugins.providers.multipart.InputPart)26 Test (org.junit.Test)13 InputStream (java.io.InputStream)12 List (java.util.List)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 File (java.io.File)6 MultipartFormDataInput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput)6 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)6 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.singletonList (java.util.Collections.singletonList)5 GenericType (org.jboss.resteasy.util.GenericType)4 BufferedInputStream (java.io.BufferedInputStream)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 EventSink (org.candlepin.audit.EventSink)3 ManifestManager (org.candlepin.controller.ManifestManager)3 ConflictOverrides (org.candlepin.sync.ConflictOverrides)3