Search in sources :

Example 31 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project Payara by payara.

the class CommonPayaraManager method deploy.

public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
    if (archive == null) {
        throw new IllegalArgumentException("archive must not be null");
    }
    final String archiveName = archive.getName();
    final ProtocolMetaData protocolMetaData = new ProtocolMetaData();
    try {
        InputStream deployment = archive.as(ZipExporter.class).exportAsInputStream();
        // Build up the POST form to send to Payara
        final FormDataMultiPart form = new FormDataMultiPart();
        form.bodyPart(new StreamDataBodyPart("id", deployment, archiveName));
        deploymentName = createDeploymentName(archiveName);
        addDeployFormFields(deploymentName, form);
        // Do Deploy the application on the remote Payara
        HTTPContext httpContext = payaraClient.doDeploy(deploymentName, form);
        protocolMetaData.addContext(httpContext);
    } catch (PayaraClientException e) {
        throw new DeploymentException("Could not deploy " + archiveName, e);
    }
    return protocolMetaData;
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) InputStream(java.io.InputStream) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) HTTPContext(org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) ProtocolMetaData(org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData) PayaraClientException(fish.payara.arquillian.container.payara.clientutils.PayaraClientException)

Example 32 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project Payara by payara.

the class TemplateCommandPostResource method createDataBasedOnForm.

private static ParameterMap createDataBasedOnForm(FormDataMultiPart formData) {
    ParameterMap data = new ParameterMap();
    if (formData == null) {
        formData = new FormDataMultiPart();
    }
    try {
        /* data passed to the generic command running
             *
             * */
        Map<String, List<FormDataBodyPart>> m1 = formData.getFields();
        Set<String> ss = m1.keySet();
        for (String fieldName : ss) {
            for (FormDataBodyPart bodyPart : formData.getFields(fieldName)) {
                if (bodyPart.getContentDisposition().getFileName() != null) {
                    // we have a file
                    // save it and mark it as delete on exit.
                    InputStream fileStream = bodyPart.getValueAs(InputStream.class);
                    String mimeType = bodyPart.getMediaType().toString();
                    // Use just the filename without complete path. File creation
                    // in case of remote deployment failing because fo this.
                    String fileName = bodyPart.getContentDisposition().getFileName();
                    if (fileName.contains("/")) {
                        fileName = Util.getName(fileName, '/');
                    } else {
                        if (fileName.contains("\\")) {
                            fileName = Util.getName(fileName, '\\');
                        }
                    }
                    File f = Util.saveFile(fileName, mimeType, fileStream);
                    f.deleteOnExit();
                    // put only the local path of the file in the same field.
                    data.add(fieldName, f.getAbsolutePath());
                } else {
                    data.add(fieldName, bodyPart.getValue());
                }
            }
        }
    } catch (Exception ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    } finally {
        if (formData != null) {
            formData.cleanup();
        }
    }
    return data;
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) InputStream(java.io.InputStream) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) ParameterMap(org.glassfish.api.admin.ParameterMap) List(java.util.List) File(java.io.File) JsonException(javax.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)32 Test (org.junit.Test)25 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)16 Response (javax.ws.rs.core.Response)10 Invocation (javax.ws.rs.client.Invocation)9 WebTarget (javax.ws.rs.client.WebTarget)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 MediaType (javax.ws.rs.core.MediaType)6 File (java.io.File)5 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)5 Client (javax.ws.rs.client.Client)3 PayaraClientException (fish.payara.arquillian.container.payara.clientutils.PayaraClientException)2 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DeploymentException (org.jboss.arquillian.container.spi.client.container.DeploymentException)2 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 FileOutputStream (java.io.FileOutputStream)1 Map (java.util.Map)1