Search in sources :

Example 16 with FormDataMultiPart

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

the class DataSourcePublishIT method testMetadata_ACL.

@Test
public void testMetadata_ACL() throws Exception {
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final String domainID = "domainID.xmi";
    final FileInputStream metadataFile = new FileInputStream("target/test-classes/Sample_SQL_Query.xmi");
    final String overwrite = "true";
    MultiPart part = new FormDataMultiPart().field("domainId", domainID).field("overwrite", String.valueOf(overwrite)).bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("metadataFile").fileName("Sample_SQL_Query.xmi").size(metadataFile.available()).build(), metadataFile, MediaType.TEXT_XML_TYPE));
    WebResource webResource = resource();
    final ClientResponse noMetadata = webResource.path(DATA_ACCESS_API_DATASOURCE_METADATA + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.CONFLICT.getStatusCode(), noMetadata.getStatus());
    ClientResponse postAnalysis = webResource.path("data-access/api/metadata/import").type(MediaType.MULTIPART_FORM_DATA_TYPE).put(ClientResponse.class, part);
    assertEquals(3, postAnalysis.getStatus());
    final ClientResponse noACL = webResource.path(DATA_ACCESS_API_DATASOURCE_METADATA + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.NOT_FOUND.getStatusCode(), noACL.getStatus());
    checkMetadata(webResource, domainID, true);
    repositoryBase.login(USERNAME_SUZY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkMetadata(webResource, domainID, true);
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final ClientResponse changeACL = webResource.path(DATA_ACCESS_API_DATASOURCE_METADATA + domainID + "/acl").put(ClientResponse.class, generateACL(USERNAME_SUZY, RepositoryFileSid.Type.USER));
    assertEquals(Response.Status.OK.getStatusCode(), changeACL.getStatus());
    repositoryBase.login(USERNAME_SUZY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkMetadata(webResource, domainID, true);
    final ClientResponse noAccessACL = webResource.path(DATA_ACCESS_API_DATASOURCE_METADATA + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), noAccessACL.getStatus());
    final ClientResponse noAccessACLNoDS = webResource.path(DATA_ACCESS_API_DATASOURCE_METADATA + domainID + "_not_exist/acl").get(ClientResponse.class);
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), noAccessACLNoDS.getStatus());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) MultiPart(com.sun.jersey.multipart.MultiPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) FileInputStream(java.io.FileInputStream) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 17 with FormDataMultiPart

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

the class CommandLineProcessor method performImport.

/*
   * --import --url=http://localhost:8080/pentaho - -username=admin --password=password --charset=UTF-8 --path=:public
   * --file-path=C:/Users/tband/Downloads/pentaho-solutions.zip --logfile=c:/Users/tband/Desktop/logfile.log
   * --permission=true --overwrite=true --retainOwnership=true (required fields- default is false)
   */
private void performImport() throws ParseException, IOException {
    String contextURL = getOptionValue(INFO_OPTION_URL_NAME, true, false);
    String filePath = getOptionValue(INFO_OPTION_FILEPATH_NAME, true, false);
    String resourceType = getOptionValue(INFO_OPTION_RESOURCE_TYPE_NAME, false, true);
    // We are importing datasources
    if (resourceType != null && resourceType.equals(ResourceType.DATASOURCE.name())) {
        performDatasourceImport();
    } else {
        String charSet = getOptionValue(INFO_OPTION_CHARSET_NAME, false, true);
        String logFile = getOptionValue(INFO_OPTION_LOGFILE_NAME, false, true);
        String path = getOptionValue(INFO_OPTION_PATH_NAME, true, false);
        String importURL = contextURL + API_REPO_FILES_IMPORT;
        File fileIS = new File(filePath);
        InputStream in = new FileInputStream(fileIS);
        FormDataMultiPart part = new FormDataMultiPart();
        /*
       * wrap in a try/finally to ensure input stream is closed properly
       */
        try {
            initRestService();
            WebResource resource = client.resource(importURL);
            String overwrite = getOptionValue(INFO_OPTION_OVERWRITE_NAME, false, true);
            String retainOwnership = getOptionValue(INFO_OPTION_RETAIN_OWNERSHIP_NAME, false, true);
            String permission = getOptionValue(INFO_OPTION_PERMISSION_NAME, false, true);
            part.field("importDir", path, MediaType.MULTIPART_FORM_DATA_TYPE);
            part.field("overwriteAclPermissions", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
            part.field("retainOwnership", "true".equals(retainOwnership) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
            part.field("charSet", charSet == null ? "UTF-8" : charSet);
            part.field("applyAclPermissions", "true".equals(permission) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE).field("fileUpload", in, MediaType.MULTIPART_FORM_DATA_TYPE);
            // If the import service needs the file name do the following.
            part.field("fileNameOverride", fileIS.getName(), MediaType.MULTIPART_FORM_DATA_TYPE);
            part.getField("fileUpload").setContentDisposition(FormDataContentDisposition.name("fileUpload").fileName(fileIS.getName()).build());
            // Response response
            ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, part);
            if (response != null) {
                if (response.getStatus() == 200) {
                    errorMessage = Messages.getInstance().getString("CommandLineProcessor.INFO_IMPORT_SUCCESSFUL");
                    System.out.println(errorMessage);
                    return;
                }
                if (response.getStatus() == 403) {
                    errorMessage = Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0007_FORBIDDEN", path);
                    System.out.println(errorMessage);
                    return;
                }
                if (response.getStatus() == 404) {
                    errorMessage = Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0004_UNKNOWN_SOURCE", path);
                    System.out.println(errorMessage);
                    return;
                }
                String message = response.getEntity(String.class);
                System.out.println(Messages.getInstance().getString("CommandLineProcessor.INFO_REST_RESPONSE_RECEIVED", message));
                if (logFile != null && !"".equals(logFile)) {
                    writeFile(message, logFile);
                }
                response.close();
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            log.error(e.getMessage());
            writeFile(e.getMessage(), logFile);
        } finally {
            // close input stream and cleanup the jersey resources
            client.destroy();
            part.cleanup();
            in.close();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) File(java.io.File) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 18 with FormDataMultiPart

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

the class CommandLineProcessor method performAnalysisDatasourceImport.

/**
 * --import --url=http://localhost:8080/pentaho --username=admin --password=password
 * --file-path=analysis/steelwheels.mondrian.xml --resource-type=DATASOURCE --datasource-type=ANALYSIS
 * --overwrite=true --analysis-datasource=steelwheels
 *
 * @param contextURL
 * @param analysisDatasourceFile
 * @param overwrite
 * @throws ParseException
 * @throws IOException
 */
private void performAnalysisDatasourceImport(String contextURL, File analysisDatasourceFile, String overwrite) throws ParseException, IOException {
    String analysisImportURL = contextURL + ANALYSIS_DATASOURCE_IMPORT;
    String catalogName = getOptionValue(INFO_OPTION_ANALYSIS_CATALOG_NAME, false, true);
    String datasourceName = getOptionValue(INFO_OPTION_ANALYSIS_DATASOURCE_NAME, false, true);
    String xmlaEnabledFlag = getOptionValue(INFO_OPTION_ANALYSIS_XMLA_ENABLED_NAME, false, true);
    WebResource resource = client.resource(analysisImportURL);
    FileInputStream inputStream = new FileInputStream(analysisDatasourceFile);
    String parms = "Datasource=" + datasourceName + ";overwrite=" + overwrite;
    FormDataMultiPart part = new FormDataMultiPart();
    part.field("overwrite", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
    if (catalogName != null) {
        part.field("catalogName", catalogName, MediaType.MULTIPART_FORM_DATA_TYPE);
    }
    if (datasourceName != null) {
        part.field("datasourceName", datasourceName, MediaType.MULTIPART_FORM_DATA_TYPE);
    }
    part.field("parameters", parms, MediaType.MULTIPART_FORM_DATA_TYPE);
    part.field("xmlaEnabledFlag", "true".equals(xmlaEnabledFlag) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
    part.field("uploadAnalysis", inputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
    // If the import service needs the file name do the following.
    part.getField("uploadAnalysis").setContentDisposition(FormDataContentDisposition.name("uploadAnalysis").fileName(analysisDatasourceFile.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);
        response.close();
        System.out.println(Messages.getInstance().getString("CommandLineProcessor.INFO_REST_RESPONSE_RECEIVED", message));
    }
    inputStream.close();
    part.cleanup();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FileInputStream(java.io.FileInputStream)

Example 19 with FormDataMultiPart

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

the class CommandLineProcessor method performRestore.

/**
 * REST Service Restore
 *
 * @throws ParseException
 *           --restore --url=http://localhost:8080/pentaho --username=admin --password=password --overwrite=true
 *           --logfile=c:/temp/steel-wheels.log --file-path=c:/temp/backup.zip
 * @throws java.io.IOException
 */
private void performRestore() throws ParseException, IOException {
    String contextURL = getOptionValue(INFO_OPTION_URL_NAME, true, false);
    String filePath = getOptionValue(INFO_OPTION_FILEPATH_NAME, true, false);
    String logFile = getOptionValue(INFO_OPTION_LOGFILE_NAME, false, true);
    String importURL = contextURL + "/api/repo/files/systemRestore";
    File fileIS = new File(filePath);
    InputStream in = new FileInputStream(fileIS);
    FormDataMultiPart part = new FormDataMultiPart().field("fileUpload", in, MediaType.MULTIPART_FORM_DATA_TYPE);
    try {
        initRestService();
        // check if the user has permissions to upload/download data
        WebResource authResource = client.resource(contextURL + "/api/authorization/action/isauthorized?authAction=" + AdministerSecurityAction.NAME);
        String authResponse = authResource.get(String.class);
        if (!authResponse.equals("true")) {
            System.err.println(Messages.getInstance().getString("CommandLineProcessor.ERROR_0006_NON_ADMIN_CREDENTIALS"));
        }
        WebResource resource = client.resource(importURL);
        String overwrite = getOptionValue(INFO_OPTION_OVERWRITE_NAME, true, false);
        part.field("overwriteFile", "true".equals(overwrite) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
        String applyAclSettings = getOptionValue(INFO_OPTION_APPLY_ACL_SETTINGS_NAME, false, true);
        part.field("applyAclSettings", !"false".equals(applyAclSettings) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
        String overwriteAclSettings = getOptionValue(INFO_OPTION_OVERWRITE_ACL_SETTINGS_NAME, false, true);
        part.field("overwriteAclSettings", "true".equals(overwriteAclSettings) ? "true" : "false", MediaType.MULTIPART_FORM_DATA_TYPE);
        // 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));
            if (logFile != null && !"".equals(logFile)) {
                writeFile(message, logFile);
            }
            response.close();
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        log.error(e.getMessage());
        writeFile(e.getMessage(), logFile);
    } finally {
        // close input stream and cleanup the jersey resources
        client.destroy();
        part.cleanup();
        in.close();
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) File(java.io.File) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 20 with FormDataMultiPart

use of com.sun.jersey.multipart.FormDataMultiPart in project java-docs-samples by GoogleCloudPlatform.

the class MailgunServlet method sendComplexMessage.

// [END simple]
// [START complex]
private ClientResponse sendComplexMessage(String recipient) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
    FormDataMultiPart formData = new FormDataMultiPart();
    formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
    formData.field("to", recipient);
    formData.field("subject", "Complex Mailgun Example");
    formData.field("html", "<html>HTML <strong>content</strong></html>");
    ClassLoader classLoader = getClass().getClassLoader();
    File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
    formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
    WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
    return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);
}
Also used : FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) File(java.io.File) FileDataBodyPart(com.sun.jersey.multipart.file.FileDataBodyPart)

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