Search in sources :

Example 21 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty.

@Test
public void shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty() 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, emptyList());
    when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
    when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
    when(inputPart.getBody(InputStream.class, null)).thenReturn(inputStream);
    try {
        fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
    } catch (final BadRequestException expected) {
        assertThat(expected.getMessage(), is("The list of input parts named 'myFieldName' is empty"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) InputStream(java.io.InputStream) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) 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 22 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractorTest method shouldExtractTheFileNameFromTheContentDispositionHeader.

@Test
public void shouldExtractTheFileNameFromTheContentDispositionHeader() throws Exception {
    final String headerName = "Content-Disposition";
    final String headerValue = "form-data; name=\"file\"; filename=\"your_file.zip\"";
    final Map<String, String> headers = of(headerName, headerValue);
    final InputPart inputPart = mock(InputPart.class);
    when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>(headers));
    assertThat(inputPartFileNameExtractor.extractFileName(inputPart), is("your_file.zip"));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) Test(org.junit.Test)

Example 23 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound.

@Test
public void shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound() throws Exception {
    final String headerName = "Some-Other-Header-Name";
    final String headerValue = "form-data; name=\"file\"; filename=\"your_file.zip\"";
    final Map<String, String> headers = of(headerName, headerValue);
    final InputPart inputPart = mock(InputPart.class);
    when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>(headers));
    try {
        inputPartFileNameExtractor.extractFileName(inputPart);
    } catch (final BadRequestException expected) {
        assertThat(expected.getMessage(), is("No header found named 'Content-Disposition'"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 24 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project scheduling by ow2-proactive.

the class SchedulerStateRest method submit.

@Override
public JobIdData submit(String sessionId, PathSegment pathSegment, MultipartFormDataInput multipart, UriInfo contextInfos) throws JobCreationRestException, NotConnectedRestException, PermissionRestException, SubmissionClosedRestException, IOException {
    Scheduler scheduler = checkAccess(sessionId, "submit");
    SchedulerSpaceInterface space = getSpaceInterface(sessionId);
    try {
        // In multipart, we can find the "variables" key for job variables, AND/OR ...
        // ... "job.xml" for a job submitted from the studio OR "file" for a job submitted by job planner
        // take and remove the "variables" key before
        Map<String, String> variablesFromMultipart = null;
        if (multipart.getFormDataMap().containsKey(VARIABLES_KEY)) {
            variablesFromMultipart = multipart.getFormDataPart(VARIABLES_KEY, new GenericType<Map<String, String>>() {
            });
            multipart.getFormDataMap().remove(VARIABLES_KEY);
        }
        // Get job from multipart
        InputPart part1 = multipart.getFormDataMap().values().iterator().next().get(0);
        String fileType = part1.getMediaType().toString().toLowerCase();
        if (!fileType.contains(MediaType.APPLICATION_XML.toLowerCase()) && !fileType.contains(MediaType.TEXT_XML.toLowerCase())) {
            throw new JobCreationRestException("Unknown job descriptor type: " + fileType);
        }
        JobId jobId;
        // is the name of the browser's input field
        try (InputStream tmpWorkflowStream = part1.getBody(new GenericType<InputStream>() {
        })) {
            // Get the job submission variables
            Map<String, String> jobVariables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
            // Add multipart variables to variables
            if (variablesFromMultipart != null) {
                if (jobVariables != null) {
                    jobVariables.putAll(variablesFromMultipart);
                } else {
                    jobVariables = variablesFromMultipart;
                }
            }
            // Get the job submission generic infos
            Map<String, String> genericInfos = null;
            if (contextInfos != null)
                genericInfos = getMapWithFirstValues(contextInfos.getQueryParameters());
            WorkflowSubmitter workflowSubmitter = new WorkflowSubmitter(scheduler, space, sessionId);
            jobId = workflowSubmitter.submit(tmpWorkflowStream, jobVariables, genericInfos);
        }
        return mapper.map(jobId, JobIdData.class);
    } finally {
        if (multipart != null) {
            multipart.close();
        }
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart)

Example 25 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project keycloak by keycloak.

the class IdentityProvidersResource method importFrom.

/**
 * Import identity provider from uploaded JSON file
 *
 * @param input
 * @return
 * @throws IOException
 */
@POST
@Path("import-config")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> importFrom(MultipartFormDataInput input) throws IOException {
    this.auth.realm().requireManageIdentityProviders();
    Map<String, List<InputPart>> formDataMap = input.getFormDataMap();
    if (!(formDataMap.containsKey("providerId") && formDataMap.containsKey("file"))) {
        throw new BadRequestException();
    }
    String providerId = formDataMap.get("providerId").get(0).getBodyAsString();
    InputPart file = formDataMap.get("file").get(0);
    InputStream inputStream = file.getBody(InputStream.class, null);
    IdentityProviderFactory providerFactory = getProviderFactorytById(providerId);
    Map<String, String> config = providerFactory.parseConfig(session, inputStream);
    return config;
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) InputStream(java.io.InputStream) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

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