use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project scheduling by ow2-proactive.
the class SchedulerStateRest method validate.
@Override
public JobValidationData validate(PathSegment pathSegment, MultipartFormDataInput multipart) {
File tmpFile = null;
try {
Map<String, List<InputPart>> formDataMap = multipart.getFormDataMap();
String name = formDataMap.keySet().iterator().next();
InputPart part1 = formDataMap.get(name).get(0);
InputStream is = part1.getBody(new GenericType<InputStream>() {
});
tmpFile = File.createTempFile("valid-job", "d");
Map<String, String> jobVariables;
try (OutputStream outputStream = new FileOutputStream(tmpFile)) {
IOUtils.copy(is, outputStream);
jobVariables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
}
return jobValidator.validateJobDescriptor(tmpFile, jobVariables);
} catch (IOException e) {
JobValidationData validation = new JobValidationData();
validation.setErrorMessage("Cannot read from the job validation request.");
validation.setStackTrace(getStackTrace(e));
return validation;
} finally {
if (tmpFile != null) {
FileUtils.deleteQuietly(tmpFile);
}
}
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project keycloak by keycloak.
the class ClientAttributeCertificateResource method getCertFromRequest.
private CertificateRepresentation getCertFromRequest(MultipartFormDataInput input) throws IOException {
auth.clients().requireManage(client);
CertificateRepresentation info = new CertificateRepresentation();
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> keystoreFormatPart = uploadForm.get("keystoreFormat");
if (keystoreFormatPart == null)
throw new BadRequestException();
String keystoreFormat = keystoreFormatPart.get(0).getBodyAsString();
List<InputPart> inputParts = uploadForm.get("file");
if (keystoreFormat.equals(CERTIFICATE_PEM)) {
String pem = StreamUtil.readString(inputParts.get(0).getBody(InputStream.class, null));
pem = PemUtils.removeBeginEnd(pem);
// Validate format
KeycloakModelUtils.getCertificate(pem);
info.setCertificate(pem);
return info;
} else if (keystoreFormat.equals(PUBLIC_KEY_PEM)) {
String pem = StreamUtil.readString(inputParts.get(0).getBody(InputStream.class, null));
// Validate format
KeycloakModelUtils.getPublicKey(pem);
info.setPublicKey(pem);
return info;
} else if (keystoreFormat.equals(JSON_WEB_KEY_SET)) {
InputStream stream = inputParts.get(0).getBody(InputStream.class, null);
JSONWebKeySet keySet = JsonSerialization.readValue(stream, JSONWebKeySet.class);
JWK publicKeyJwk = JWKSUtils.getKeyForUse(keySet, JWK.Use.SIG);
if (publicKeyJwk == null) {
throw new IllegalStateException("Certificate not found for use sig");
} else {
PublicKey publicKey = JWKParser.create(publicKeyJwk).toPublicKey();
String publicKeyPem = KeycloakModelUtils.getPemFromKey(publicKey);
info.setPublicKey(publicKeyPem);
info.setKid(publicKeyJwk.getKeyId());
return info;
}
}
String keyAlias = uploadForm.get("keyAlias").get(0).getBodyAsString();
List<InputPart> keyPasswordPart = uploadForm.get("keyPassword");
char[] keyPassword = keyPasswordPart != null ? keyPasswordPart.get(0).getBodyAsString().toCharArray() : null;
List<InputPart> storePasswordPart = uploadForm.get("storePassword");
char[] storePassword = storePasswordPart != null ? storePasswordPart.get(0).getBodyAsString().toCharArray() : null;
PrivateKey privateKey = null;
X509Certificate certificate = null;
try {
KeyStore keyStore = null;
if (keystoreFormat.equals("JKS"))
keyStore = KeyStore.getInstance("JKS");
else
keyStore = KeyStore.getInstance(keystoreFormat, "BC");
keyStore.load(inputParts.get(0).getBody(InputStream.class, null), storePassword);
try {
privateKey = (PrivateKey) keyStore.getKey(keyAlias, keyPassword);
} catch (Exception e) {
// ignore
}
certificate = (X509Certificate) keyStore.getCertificate(keyAlias);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (privateKey != null) {
String privateKeyPem = KeycloakModelUtils.getPemFromKey(privateKey);
info.setPrivateKey(privateKeyPem);
}
if (certificate != null) {
String certPem = KeycloakModelUtils.getPemFromCertificate(certificate);
info.setCertificate(certPem);
}
return info;
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project teiid by teiid.
the class TeiidRSProvider method convertParameters.
private LinkedHashMap<String, Object> convertParameters(Connection conn, String vdbName, String procedureName, MultipartFormDataInput form) throws SQLException {
Map<String, Class<?>> runtimeTypes = getParameterTypes(conn, vdbName, procedureName);
LinkedHashMap<String, Object> expectedValues = new LinkedHashMap<String, Object>();
Map<String, List<InputPart>> inputParameters = form.getFormDataMap();
for (String columnName : inputParameters.keySet()) {
Class<?> runtimeType = runtimeTypes.get(columnName);
if (runtimeType == null) {
throw new SQLException(RestServicePlugin.Util.gs(RestServicePlugin.Event.TEIID28001, columnName, procedureName));
}
if (runtimeType.isAssignableFrom(Array.class)) {
List<InputPart> valueStreams = inputParameters.get(columnName);
ArrayList<Object> array = new ArrayList<Object>();
try {
for (InputPart part : valueStreams) {
array.add(part.getBodyAsString());
}
} catch (IOException e) {
throw new SQLException(e);
}
expectedValues.put(columnName, array.toArray(new Object[array.size()]));
} else {
final InputPart part = inputParameters.get(columnName).get(0);
try {
expectedValues.put(columnName, convertToRuntimeType(runtimeType, part));
} catch (IOException e) {
throw new SQLException(e);
}
}
}
return expectedValues;
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml.
@Test
public void shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml() 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 = new HashMap<>();
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));
fail();
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("Failed to find input part named 'myFieldName' as specified in the raml"));
}
}
use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowFileStoreFailedExceptionIfGettingFileInputStreamFails.
@Test
public void shouldThrowFileStoreFailedExceptionIfGettingFileInputStreamFails() throws Exception {
final IOException ioException = new IOException("bunnies");
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.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.getMediaType()).thenReturn(TEXT_XML_TYPE);
when(inputPart.getBody(InputStream.class, null)).thenThrow(ioException);
try {
fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
fail();
} catch (final FileStoreFailedException expected) {
assertThat(expected.getCause(), is(ioException));
assertThat(expected.getMessage(), is("Failed to store file 'the-file-name.jpeg'"));
}
}
Aggregations