Search in sources :

Example 1 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project pentaho-kettle by pentaho.

the class RepositoryCleanupUtil method purge.

/**
 * Create parameters and send HTTP request to purge REST endpoint
 *
 * @param options
 */
public void purge(String[] options) {
    FormDataMultiPart form = null;
    try {
        Map<String, String> parameters = parseExecutionOptions(options);
        validateParameters(parameters);
        authenticateLoginCredentials();
        String serviceURL = createPurgeServiceURL();
        form = createParametersForm();
        WebResource resource = client.resource(serviceURL);
        ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
        if (response != null && response.getStatus() == 200) {
            String resultLog = response.getEntity(String.class);
            String logName = writeLog(resultLog);
            writeOut(Messages.getInstance().getString("REPOSITORY_CLEANUP_UTIL.INFO_0001.OP_SUCCESS", logName), false);
        } else {
            writeOut(Messages.getInstance().getString("REPOSITORY_CLEANUP_UTIL.ERROR_0001.OP_FAILURE"), true);
        }
    } catch (Exception e) {
        if (e.getMessage() != null) {
            System.out.println(e.getMessage());
        } else {
            if (!(e instanceof NormalExitException)) {
                e.printStackTrace();
            }
        }
    } finally {
        if (client != null) {
            client.destroy();
        }
        if (form != null) {
            form.cleanup();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) ParseException(java.text.ParseException)

Example 2 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project coprhd-controller by CoprHD.

the class StorageDriver method upgradeDriver.

public ClientResponse upgradeDriver(String driverName, File driverFile, boolean force) {
    FormDataMultiPart multiPart = new FormDataMultiPart();
    multiPart.bodyPart(new FileDataBodyPart(DRIVER_KEY_NAME, driverFile, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    multiPart.field("force", Boolean.toString(force));
    return client.postMultiPart(ClientResponse.class, multiPart, PathConstants.STORAGE_DRIVER_UPGRADE_URL, driverName);
}
Also used : FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FileDataBodyPart(com.sun.jersey.multipart.file.FileDataBodyPart)

Example 3 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project coprhd-controller by CoprHD.

the class StorageDriver method installDriver.

public ClientResponse installDriver(File f) {
    FormDataMultiPart multiPart = new FormDataMultiPart();
    multiPart.bodyPart(new FileDataBodyPart(DRIVER_KEY_NAME, f, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    return client.postMultiPart(ClientResponse.class, multiPart, PathConstants.STORAGE_DRIVER_INSTALL_URL);
}
Also used : FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FileDataBodyPart(com.sun.jersey.multipart.file.FileDataBodyPart)

Example 4 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project pentaho-platform by pentaho.

the class CommandLineProcessor method performMetadataDatasourceImport.

/**
 * --import --url=http://localhost:8080/pentaho --username=admin --password=password --file-path=metadata.xmi
 * --resource-type=DATASOURCE --datasource-type=METADATA --overwrite=true --metadata-domain-id=steel-wheels
 *
 * @param contextURL
 * @param metadataDatasourceFile
 * @param overwrite
 * @throws ParseException
 * @throws IOException
 */
private void performMetadataDatasourceImport(String contextURL, File metadataDatasourceFile, String overwrite) throws ParseException, IOException {
    File metadataFileInZip = null;
    InputStream metadataFileInZipInputStream = null;
    String metadataImportURL = contextURL + METADATA_DATASOURCE_IMPORT;
    String domainId = getOptionValue(INFO_OPTION_METADATA_DOMAIN_ID_NAME, true, false);
    WebResource resource = client.resource(metadataImportURL);
    FormDataMultiPart part = new FormDataMultiPart();
    final String name = RepositoryFilenameUtils.separatorsToRepository(metadataDatasourceFile.getName());
    final String ext = RepositoryFilenameUtils.getExtension(name);
    try {
        if (ext.equals(ZIP_EXT)) {
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(metadataDatasourceFile));
            ZipEntry entry = zipInputStream.getNextEntry();
            while (entry != null) {
                final String entryName = RepositoryFilenameUtils.separatorsToRepository(entry.getName());
                final String extension = RepositoryFilenameUtils.getExtension(entryName);
                File tempFile = null;
                boolean isDir = entry.getSize() == 0;
                if (!isDir) {
                    tempFile = File.createTempFile("zip", null);
                    tempFile.deleteOnExit();
                    FileOutputStream fos = new FileOutputStream(tempFile);
                    IOUtils.copy(zipInputStream, fos);
                    fos.close();
                }
                if (extension.equals(METADATA_DATASOURCE_EXT)) {
                    if (metadataFileInZip == null) {
                        metadataFileInZip = new File(entryName);
                        metadataFileInZipInputStream = new FileInputStream(metadataFileInZip);
                    }
                }
                zipInputStream.closeEntry();
                entry = zipInputStream.getNextEntry();
            }
            zipInputStream.close();
            part.field("overwrite", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
            part.field("domainId", domainId, MediaType.MULTIPART_FORM_DATA_TYPE).field("metadataFile", metadataFileInZipInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
            // If the import service needs the file name do the following.
            part.getField("metadataFile").setContentDisposition(FormDataContentDisposition.name("metadataFile").fileName(metadataFileInZip.getName()).build());
            // Response response
            ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, part);
            if (response != null) {
                String message = response.getEntity(String.class);
                System.out.println(Messages.getInstance().getString("CommandLineProcessor.INFO_REST_RESPONSE_RECEIVED", message));
            }
        } else {
            FileInputStream metadataDatasourceInputStream = new FileInputStream(metadataDatasourceFile);
            part.field("overwrite", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
            part.field("domainId", domainId, MediaType.MULTIPART_FORM_DATA_TYPE).field("metadataFile", metadataDatasourceInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
            // If the import service needs the file name do the following.
            part.getField("metadataFile").setContentDisposition(FormDataContentDisposition.name("metadataFile").fileName(metadataDatasourceFile.getName()).build());
            // Response response
            ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, part);
            if (response != null) {
                String message = response.getEntity(String.class);
                System.out.println(Messages.getInstance().getString("CommandLineProcessor.INFO_REST_RESPONSE_RECEIVED", message));
            }
            metadataDatasourceInputStream.close();
        }
    } finally {
        metadataFileInZipInputStream.close();
        part.cleanup();
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) WebResource(com.sun.jersey.api.client.WebResource) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project pentaho-platform by pentaho.

the class RepositoryPublishResourceIT method testImportsSuccessfully.

private void testImportsSuccessfully(String path, String filename) throws Exception {
    String full = path + '/' + filename;
    FormDataMultiPart part = new FormDataMultiPart();
    part.field("importPath", URLEncoder.encode(full, "UTF-8"), MULTIPART_FORM_DATA_TYPE);
    part.field("fileUpload", new ByteArrayInputStream(new byte[0]), MULTIPART_FORM_DATA_TYPE);
    part.field("overwriteFile", "true", MULTIPART_FORM_DATA_TYPE);
    part.getField("fileUpload").setContentDisposition(FormDataContentDisposition.name("fileUpload").fileName(URLEncoder.encode(filename, "UTF-8")).build());
    ClientResponse response = resource().path("repo/publish/file").type(MediaType.MULTIPART_FORM_DATA).accept(TEXT_PLAIN).post(ClientResponse.class, part);
    assertResponse(response, ClientResponse.Status.OK, MediaType.TEXT_PLAIN);
    ArgumentCaptor<IPlatformImportBundle> captor = ArgumentCaptor.forClass(IPlatformImportBundle.class);
    verify(importer).importFile(captor.capture());
    IPlatformImportBundle bundle = captor.getValue();
    assertEquals(path, bundle.getPath());
    assertEquals(filename, bundle.getName());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) ByteArrayInputStream(java.io.ByteArrayInputStream) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart)

Aggregations

FormDataMultiPart (com.sun.jersey.multipart.FormDataMultiPart)23 ClientResponse (com.sun.jersey.api.client.ClientResponse)14 WebResource (com.sun.jersey.api.client.WebResource)14 FileInputStream (java.io.FileInputStream)10 MultiPart (com.sun.jersey.multipart.MultiPart)7 Test (org.junit.Test)7 FormDataBodyPart (com.sun.jersey.multipart.FormDataBodyPart)6 FileDataBodyPart (com.sun.jersey.multipart.file.FileDataBodyPart)6 JerseyTest (com.sun.jersey.test.framework.JerseyTest)6 File (java.io.File)6 InputStream (java.io.InputStream)5 Client (com.sun.jersey.api.client.Client)3 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)3 IOException (java.io.IOException)3 ZipInputStream (java.util.zip.ZipInputStream)3 RepositoryFileAclDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto)3 MalformedURLException (java.net.MalformedURLException)2 ParseException (org.apache.commons.cli.ParseException)2 StreamDataBodyPart (com.sun.jersey.multipart.file.StreamDataBodyPart)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1