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