Search in sources :

Example 1 with IMondrianCatalogService

use of org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService in project pentaho-platform by pentaho.

the class RepositoryImportResource method doPostImport.

/**
 * Attempts to import all files from the zip archive or single file. A log file is produced at the end of import.
 *
 * <p><b>Example Request:</b><br />
 *    POST pentaho/api/repo/files/import
 *    <br /><b>POST data:</b>
 *    <pre function="syntax.xml">
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="importDir"
 *
 *      /public
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="fileUpload"; filename="test.csv"
 *      Content-Type: application/vnd.ms-excel
 *
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="overwriteFile"
 *
 *      true
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="overwriteAclPermissions"
 *
 *      true
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="applyAclPermissions"
 *
 *      true
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="retainOwnership"
 *
 *      true
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="charSet"
 *
 *      UTF-8
 *      ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
 *      Content-Disposition: form-data; name="logLevel"
 *
 *      INFO
 *      ------WebKitFormBoundaryd1z6iZhXyx12RYxV
 *      Content-Disposition: form-data; name="fileNameOverride"
 *
 *      fileNameOverriden.csv
 *      ------WebKitFormBoundaryd1z6iZhXyx12RYxV--
 *    </pre>
 * </p>
 *
 * @param importDir               JCR Directory to which the zip structure or single file will be uploaded to.
 * @param fileUpload              Input stream for the file.
 * @param overwriteFile           The flag indicates ability to overwrite existing file.
 * @param overwriteAclPermissions The flag indicates ability to overwrite Acl permissions.
 * @param applyAclPermissions     The flag indicates ability to apply Acl permissions.
 * @param retainOwnership         The flag indicates ability to retain ownership.
 * @param charSet                 The charset for imported file.
 * @param logLevel                The level of logging.
 * @param fileNameOverride        If present and the content represents a single file, this parameter contains the filename to use
 *                                when storing the file in the repository. If not present, the fileInfo.getFileName will be used.
 *                                Note that the later cannot reliably handle foreign character sets.
 *
 * @return A jax-rs Response object with the appropriate header and body.
 *
 * <p><b>Example Response:</b></p>
 * <pre function="syntax.xml">
 *   &lt;html&gt;
 *   &lt;head&gt;
 *   &lt;title&gt;Repository Import Log&lt;/title&gt;
 *   &lt;/head&gt;
 *   &lt;body bgcolor="#FFFFFF" topmargin="6" leftmargin="6" style="font-family: arial,sans-serif; font-size: x-small"&gt;
 *   &lt;hr size="1" noshade&gt;
 *   Log session start time Thu Feb 26 11:04:19 BRT 2015&lt;br&gt;
 *   &lt;br&gt;
 *   &lt;table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%"&gt;
 *   &lt;tr style="background: #336699; color: #FFFFFF; text-align: left"&gt;
 *   &lt;th&gt;Import File&lt;/th&gt;
 *   &lt;th&gt;Level&lt;/th&gt;
 *   &lt;th&gt;Message&lt;/th&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;Start Import Job&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public/fileNameOverriden.csv&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;Start File Import&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public/fileNameOverriden.csv&lt;/td&gt;
 *   &lt;td title="Level"&gt;&lt;font color="#993300"&gt;&lt;strong&gt;WARN&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
 *   &lt;td title="Message"&gt;fileNameOverriden.csv&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;End Import Job&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;/table&gt;
 *   &lt;br&gt;
 *   &lt;/body&gt;&lt;/html&gt;
 * </pre>
 */
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
@Facet(name = "Unsupported")
public Response doPostImport(@FormDataParam("importDir") String importDir, @FormDataParam("fileUpload") InputStream fileUpload, @FormDataParam("overwriteFile") String overwriteFile, @FormDataParam("overwriteAclPermissions") String overwriteAclPermissions, @FormDataParam("applyAclPermissions") String applyAclPermission, @FormDataParam("retainOwnership") String retainOwnership, @FormDataParam("charSet") String charSet, @FormDataParam("logLevel") String logLevel, @FormDataParam("fileUpload") FormDataContentDisposition fileInfo, @FormDataParam("fileNameOverride") String fileNameOverride) {
    IRepositoryImportLogger importLogger = null;
    ByteArrayOutputStream importLoggerStream = new ByteArrayOutputStream();
    boolean logJobStarted = false;
    if (StringUtils.isBlank(charSet)) {
        charSet = DEFAULT_CHAR_SET;
    }
    try {
        validateAccess(importDir);
        boolean overwriteFileFlag = ("false".equals(overwriteFile) ? false : true);
        boolean overwriteAclSettingsFlag = ("true".equals(overwriteAclPermissions) ? true : false);
        boolean applyAclSettingsFlag = ("true".equals(applyAclPermission) ? true : false);
        boolean retainOwnershipFlag = ("true".equals(retainOwnership) ? true : false);
        // If logLevel is null then we will default to ERROR
        if (logLevel == null || logLevel.length() <= 0) {
            logLevel = "ERROR";
        }
        // Non-admins cannot process a manifest
        FileService fileService = new FileService();
        if (!fileService.doCanAdminister()) {
            applyAclSettingsFlag = false;
            retainOwnershipFlag = true;
        }
        Level level = Level.toLevel(logLevel);
        ImportSession.getSession().setAclProperties(applyAclSettingsFlag, retainOwnershipFlag, overwriteAclSettingsFlag);
        // The fileNameOverride was added because the formDataContentDispositionfile object cannot reliable
        // contain non US-ASCII characters.  See RFC283 section 2.3 for details
        String fileName = fileNameOverride != null ? fileNameOverride : fileInfo.getFileName();
        RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
        bundleBuilder.input(fileUpload);
        bundleBuilder.charSet(charSet);
        bundleBuilder.path(importDir);
        bundleBuilder.overwriteFile(overwriteFileFlag);
        bundleBuilder.applyAclSettings(applyAclSettingsFlag);
        bundleBuilder.overwriteAclSettings(overwriteAclSettingsFlag);
        bundleBuilder.retainOwnership(retainOwnershipFlag);
        bundleBuilder.name(fileName);
        IPlatformImportBundle bundle = bundleBuilder.build();
        IPlatformMimeResolver mimeResolver = PentahoSystem.get(IPlatformMimeResolver.class);
        String mimeTypeFromFile = mimeResolver.resolveMimeForFileName(fileName);
        if (mimeTypeFromFile == null) {
            return Response.ok("INVALID_MIME_TYPE", MediaType.TEXT_HTML).build();
        }
        bundleBuilder.mime(mimeTypeFromFile);
        IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
        importLogger = importer.getRepositoryImportLogger();
        final String mimeType = bundle.getMimeType() != null ? bundle.getMimeType() : mimeResolver.resolveMimeForBundle(bundle);
        if (mimeType == null) {
            return Response.ok("INVALID_MIME_TYPE", MediaType.TEXT_HTML).build();
        }
        logJobStarted = true;
        importLogger.startJob(importLoggerStream, importDir, level);
        importer.importFile(bundle);
        // Flush the Mondrian cache to show imported data-sources.
        IMondrianCatalogService mondrianCatalogService = PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", PentahoSessionHolder.getSession());
        mondrianCatalogService.reInit(PentahoSessionHolder.getSession());
    } catch (PentahoAccessControlException e) {
        return Response.serverError().entity(e.toString()).build();
    } catch (Exception e) {
        return Response.serverError().entity(e.toString()).build();
    } finally {
        ImportSession.clearSession();
        if (logJobStarted == true) {
            importLogger.endJob();
        }
    }
    String responseBody;
    try {
        responseBody = importLoggerStream.toString(charSet);
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("Encoding of response body is failed. (charSet=" + charSet + ")", e);
        responseBody = importLoggerStream.toString();
    }
    return Response.ok(responseBody, MediaType.TEXT_HTML).build();
}
Also used : FileService(org.pentaho.platform.web.http.api.resources.services.FileService) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) Level(org.apache.log4j.Level) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Facet(org.codehaus.enunciate.Facet)

Example 2 with IMondrianCatalogService

use of org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService in project pentaho-platform by pentaho.

the class SystemRefreshResource method flushMondrianSchemaCache.

@GET
@Path("/mondrianSchemaCache")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
@Facet(name = "Unsupported")
public Response flushMondrianSchemaCache() {
    if (canAdminister()) {
        IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
        if (canAdminister()) {
            // Flush the catalog helper (legacy)
            IMondrianCatalogService mondrianCatalogService = // $NON-NLS-1$
            PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", pentahoSession);
            mondrianCatalogService.reInit(pentahoSession);
            // Flush the IOlapService
            IOlapService olapService = // $NON-NLS-1$
            PentahoSystem.get(IOlapService.class, "IOlapService", pentahoSession);
            olapService.flushAll(pentahoSession);
        }
        return Response.ok().type(MediaType.TEXT_PLAIN).build();
    } else {
        return Response.status(UNAUTHORIZED).build();
    }
}
Also used : IOlapService(org.pentaho.platform.plugin.action.olap.IOlapService) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Facet(org.codehaus.enunciate.Facet)

Example 3 with IMondrianCatalogService

use of org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService in project pentaho-platform by pentaho.

the class PentahoXmlaServletTest method createConnectionFactory.

@Test
public void createConnectionFactory() throws Exception {
    ISecurityHelper securityHelper = mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(securityHelper);
    when(securityHelper.runAsSystem(any((Callable.class)))).thenReturn(DATASOURCE_XML);
    IMondrianCatalogService catalogService = mock(MondrianCatalogHelper.class);
    MondrianCatalog mondrianCatalog = mock(MondrianCatalog.class);
    when(mondrianCatalog.getDataSourceInfo()).thenReturn("DataSource=foo");
    doReturn(mondrianCatalog).when(catalogService).getCatalog(anyString(), anyObject());
    PowerMockito.mockStatic(DriverManager.class);
    when(DriverManager.getConnection(anyString(), anyObject())).thenReturn(mock(RolapConnection.class));
    PentahoSystem.registerObject(catalogService);
    PentahoXmlaServlet xmlaServlet = spy(new PentahoXmlaServlet());
    XmlaHandler.ConnectionFactory connectionFactory = xmlaServlet.createConnectionFactory(mock(ServletConfig.class));
    Properties properties = new Properties();
    properties.put("DataSource", "bogus");
    try {
        connectionFactory.getConnection("SampleData", "SampleData", "baz", properties);
    } catch (MondrianException exception) {
    // ignored
    }
    try {
        connectionFactory.getConnection("SampleData", "SampleData", "baz", properties);
    } catch (MondrianException exception) {
    // ignored
    }
    // We verify that only one Catalog Locator is created for multiple requests
    verify(xmlaServlet, times(1)).makeCatalogLocator(anyObject());
}
Also used : RolapConnection(mondrian.rolap.RolapConnection) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) XmlaHandler(mondrian.xmla.XmlaHandler) ServletConfig(javax.servlet.ServletConfig) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) Properties(java.util.Properties) MondrianException(mondrian.olap.MondrianException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with IMondrianCatalogService

use of org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService in project pentaho-platform by pentaho.

the class MDXBaseComponent method getConnection.

protected IPentahoConnection getConnection() {
    // first attempt to get the connection metadata from the catalog service. if that is not successful,
    // get the connection using the original approach.
    MdxConnectionAction connAction = (MdxConnectionAction) getActionDefinition();
    String catalogName = connAction.getCatalog().getStringValue();
    IMondrianCatalogService mondrianCatalogService = // $NON-NLS-1$
    PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", PentahoSessionHolder.getSession());
    MondrianCatalog catalog = mondrianCatalogService.getCatalog(catalogName, PentahoSessionHolder.getSession());
    if (catalog == null) {
        return getConnectionOrig();
    }
    Util.PropertyList connectProperties = Util.parseConnectString(catalog.getDataSourceInfo());
    Properties properties = new Properties();
    Iterator<Pair<String, String>> iter = connectProperties.iterator();
    while (iter.hasNext()) {
        Pair<String, String> pair = iter.next();
        properties.put(pair.getKey(), pair.getValue());
    }
    properties.put("Catalog", catalog.getDefinition());
    properties.put("Provider", "mondrian");
    properties.put("PoolNeeded", "false");
    properties.put(RolapConnectionProperties.Locale.name(), LocaleHelper.getLocale().toString());
    debug("Mondrian Connection Properties: " + properties.toString());
    MDXConnection mdxConnection = (MDXConnection) PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, properties, PentahoSessionHolder.getSession(), this);
    if (connAction != null) {
        if ((connAction.getExtendedColumnNames() != ActionInputConstant.NULL_INPUT)) {
            mdxConnection.setUseExtendedColumnNames(connAction.getExtendedColumnNames().getBooleanValue());
        }
    }
    return mdxConnection;
}
Also used : MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) TemplateUtil(org.pentaho.platform.engine.services.runtime.TemplateUtil) Util(mondrian.olap.Util) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) Properties(java.util.Properties) Pair(mondrian.util.Pair)

Example 5 with IMondrianCatalogService

use of org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService in project pentaho-platform by pentaho.

the class MondrianModelComponent method getInitialQuery.

public static String getInitialQuery(final Properties properties, final String cubeName, IPentahoSession session) throws Throwable {
    // Apply any properties for this catalog specified in datasource.xml
    IMondrianCatalogService mondrianCatalogService = PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", PentahoSessionHolder.getSession());
    List<MondrianCatalog> catalogs = mondrianCatalogService.listCatalogs(PentahoSessionHolder.getSession(), true);
    String propCat = properties.getProperty(RolapConnectionProperties.Catalog.name());
    for (MondrianCatalog cat : catalogs) {
        if (cat.getDefinition().equalsIgnoreCase(propCat)) {
            Util.PropertyList connectProperties = Util.parseConnectString(cat.getDataSourceInfo());
            Iterator<Pair<String, String>> iter = connectProperties.iterator();
            while (iter.hasNext()) {
                Pair<String, String> pair = iter.next();
                if (// Only set if not set already
                !properties.containsKey(pair.getKey())) {
                    properties.put(pair.getKey(), pair.getValue());
                }
            }
            break;
        }
    }
    MDXConnection mdxConnection = (MDXConnection) PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, properties, session, null);
    // mdxConnection.setProperties( properties );
    Connection connection = mdxConnection.getConnection();
    if (connection == null) {
        Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0001_INVALID_CONNECTION", // $NON-NLS-1$ //$NON-NLS-2$
        properties.toString()));
        return null;
    }
    try {
        return MondrianModelComponent.getInitialQuery(connection, cubeName);
    } catch (Throwable t) {
        if (t instanceof MondrianException) {
            // pull the cause out, otherwise it never gets logged
            Throwable cause = ((MondrianException) t).getCause();
            if (cause != null) {
                throw cause;
            } else {
                throw t;
            }
        } else {
            throw t;
        }
    }
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) Connection(mondrian.olap.Connection) RolapConnection(mondrian.rolap.RolapConnection) Util(mondrian.olap.Util) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianException(mondrian.olap.MondrianException) Pair(mondrian.util.Pair)

Aggregations

IMondrianCatalogService (org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)13 MondrianCatalog (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog)10 Produces (javax.ws.rs.Produces)4 Facet (org.codehaus.enunciate.Facet)4 Properties (java.util.Properties)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 MondrianException (mondrian.olap.MondrianException)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 MDXConnection (org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Connection (mondrian.olap.Connection)2 Util (mondrian.olap.Util)2 RolapConnection (mondrian.rolap.RolapConnection)2 Pair (mondrian.util.Pair)2 OlapConnection (org.olap4j.OlapConnection)2 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)2