Search in sources :

Example 1 with InputPart

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);
        }
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BufferedInputStream(java.io.BufferedInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) JobValidationData(org.ow2.proactive_grid_cloud_portal.scheduler.dto.JobValidationData) File(java.io.File)

Example 2 with InputPart

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;
}
Also used : PrivateKey(java.security.PrivateKey) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet) CertificateRepresentation(org.keycloak.representations.idm.CertificateRepresentation) InputStream(java.io.InputStream) PublicKey(java.security.PublicKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) ErrorResponseException(org.keycloak.services.ErrorResponseException) BadRequestException(javax.ws.rs.BadRequestException) NotAcceptableException(javax.ws.rs.NotAcceptableException) IOException(java.io.IOException) NotFoundException(javax.ws.rs.NotFoundException) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) JWK(org.keycloak.jose.jwk.JWK)

Example 3 with InputPart

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

Example 4 with InputPart

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"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) HashMap(java.util.HashMap) 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 5 with InputPart

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'"));
    }
}
Also used : FileStoreFailedException(uk.gov.justice.services.adapter.rest.interceptor.FileStoreFailedException) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) 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