Search in sources :

Example 11 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource in project pentaho-platform by pentaho.

the class GetResource method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    // TODO perform any authorization here...
    // TODO support caching
    PentahoSystem.systemEntryPoint();
    try {
        IPentahoSession session = getPentahoSession(request);
        // $NON-NLS-1$
        String resource = request.getParameter("resource");
        if ((resource == null) || StringUtil.doesPathContainParentPathSegment(resource)) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("GetResource.ERROR_0001_RESOURCE_PARAMETER_MISSING"));
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        String resLower = resource.toLowerCase();
        String resourcePath;
        if (resLower.endsWith(".xsl")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            resourcePath = "system/custom/xsl/" + resource;
        } else if (resLower.endsWith(".mondrian.xml")) {
            // $NON-NLS-1$
            // Ensure user is authenticated by checking the default role
            // gets defaultRole from pentahoObjects-s-s.x
            String defaultRole = PentahoSystem.get(String.class, "defaultRole", null);
            if (defaultRole != null) {
                if (!SecurityHelper.getInstance().isGranted(session, new SimpleGrantedAuthority(defaultRole))) {
                    response.sendError(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            }
            // If no defaultRole is defined, then just continue action as per normal.
            resourcePath = resource;
        } else if (resLower.endsWith(".jpg") || resLower.endsWith(".jpeg") || resLower.endsWith(".gif") || resLower.endsWith(".png") || resLower.endsWith(".bmp")) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            resourcePath = resource;
        } else {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("GetResource.ERROR_0002_INVALID_FILE", resource));
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        IActionSequenceResource asqr = new // $NON-NLS-1$ //$NON-NLS-2$
        ActionSequenceResource(// $NON-NLS-1$ //$NON-NLS-2$
        "", // $NON-NLS-1$ //$NON-NLS-2$
        IActionSequenceResource.SOLUTION_FILE_RESOURCE, // $NON-NLS-1$ //$NON-NLS-2$
        "", resourcePath);
        InputStream in = asqr.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
        if (in == null) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("GetResource.ERROR_0003_RESOURCE_MISSING", resourcePath));
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        String mimeType = getServletContext().getMimeType(resourcePath);
        String resourceName = resourcePath;
        if (resourcePath.indexOf("/") != -1) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            resourceName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        response.setHeader("content-disposition", "attachment;filename=" + resourceName);
        if ((null == mimeType) || (mimeType.length() <= 0)) {
            // Hard coded to PNG because BIRT does not give us a mime type at
            // all...
            // $NON-NLS-1$
            response.setContentType("image/png");
        } else {
            response.setContentType(mimeType);
        }
        response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
        // $NON-NLS-1$ //$NON-NLS-2$
        response.setHeader("expires", "0");
        // Open the input and output streams
        OutputStream out = response.getOutputStream();
        try {
            // Copy the contents of the file to the output stream
            byte[] buf = new byte[1024];
            int count = 0;
            int totalBytes = 0;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
                totalBytes += count;
            }
            response.setContentLength(totalBytes);
        } finally {
            in.close();
            out.close();
        }
    } finally {
        PentahoSystem.systemExitPoint();
    }
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) ActionSequenceResource(org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 12 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource in project pentaho-platform by pentaho.

the class MDXBaseComponent method getConnectionOrig.

protected IPentahoConnection getConnectionOrig() {
    IPentahoConnection localConnection = null;
    MdxConnectionAction connAction = (MdxConnectionAction) getActionDefinition();
    try {
        String mdxConnectionStr = connAction.getMdxConnectionString().getStringValue();
        Properties mdxConnectionProps = (Properties) connAction.getConnectionProps().getValue();
        String jdbcStr = connAction.getConnection().getStringValue();
        String jndiStr = connAction.getJndi().getStringValue();
        String location = connAction.getLocation().getStringValue();
        String role = connAction.getRole().getStringValue();
        String catalog = connAction.getCatalog().getStringValue();
        if ((catalog == null) && (connAction.getCatalogResource() != null)) {
            IActionSequenceResource resource = getResource(connAction.getCatalogResource().getName());
            catalog = resource.getAddress();
            if (resource.getSourceType() == IActionSequenceResource.URL_RESOURCE) {
                if (!catalog.startsWith("solution:") && !catalog.startsWith("http:")) {
                    // $NON-NLS-1$
                    if (fileExistsInRepository(catalog)) {
                        // About allowed "solution:"
                        // Extra step to make sure that remote mondrian models
                        // fully qualified aren't munged
                        // MB
                        // $NON-NLS-1$
                        catalog = "solution:" + catalog;
                    }
                }
            } else if ((resource.getSourceType() == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resource.getSourceType() == IActionSequenceResource.FILE_RESOURCE)) {
                if (!catalog.startsWith("solution:")) {
                    // $NON-NLS-1$
                    catalog = "solution:" + catalog;
                }
            }
        }
        if (catalog == null) {
            // $NON-NLS-1$
            warn(Messages.getInstance().getString("MDXBaseComponent.ERROR_0007_CATALOG_NOT_DEFINED", getActionName()));
        } else {
            if (mdxConnectionProps != null) {
                mdxConnectionProps.put(MdxConnectionAction.CATALOG_ELEMENT, catalog);
            }
        }
        String userId = connAction.getUserId().getStringValue();
        String password = connAction.getPassword().getStringValue();
        if (mdxConnectionProps != null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionProps, getSession(), this);
        } else {
            if (mdxConnectionStr != null) {
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionStr, getSession(), this);
            } else {
                String connectStr = null;
                if (jdbcStr != null) {
                    // $NON-NLS-1$
                    connectStr = jdbcStr + "; Catalog=" + catalog;
                } else if (jndiStr != null) {
                    IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
                    if (datasourceService.getDataSource(jndiStr) == null) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                        return null;
                    }
                    // $NON-NLS-1$ //$NON-NLS-2$
                    connectStr = "dataSource=" + jndiStr + "; Catalog=" + catalog;
                    // Add extra definitions from platform mondrian metadata
                    MondrianCatalog mc = getMondrianCatalog(catalog);
                    try {
                        connectStr += ";" + mc.getDataSourceInfo();
                    } catch (Exception e) {
                    // Just swallow the exception
                    }
                }
                if (role != null) {
                    // $NON-NLS-1$
                    connectStr += "; Role=" + role;
                }
                Properties props = new Properties();
                props.setProperty(IPentahoConnection.CONNECTION, connectStr);
                props.setProperty(IPentahoConnection.PROVIDER, location);
                if (userId != null) {
                    props.setProperty(IPentahoConnection.USERNAME_KEY, userId);
                }
                if (password != null) {
                    props.setProperty(IPentahoConnection.PASSWORD_KEY, password);
                }
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
            }
            if (localConnection == null) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                return null;
            }
        }
        if (localConnection instanceof MDXConnection) {
            MDXConnection mdxConn = (MDXConnection) localConnection;
            if (connAction != null) {
                if ((connAction.getExtendedColumnNames() != ActionInputConstant.NULL_INPUT)) {
                    mdxConn.setUseExtendedColumnNames(connAction.getExtendedColumnNames().getBooleanValue());
                }
            }
        }
        return localConnection;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 13 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource in project pentaho-platform by pentaho.

the class JFreeChartEngine method getImage.

/**
 * @param imageName
 *          String that contains a path to the image
 * @return the image
 */
public static Image getImage(final String imageName, final IPentahoSession session) {
    Image image = null;
    try {
        IActionSequenceResource resource = new // $NON-NLS-1$ //$NON-NLS-2$
        ActionSequenceResource(// $NON-NLS-1$ //$NON-NLS-2$
        "", // $NON-NLS-1$ //$NON-NLS-2$
        IActionSequenceResource.SOLUTION_FILE_RESOURCE, // $NON-NLS-1$ //$NON-NLS-2$
        "", imageName);
        InputStream is = resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
        image = ImageIO.read(is);
    } catch (IOException e) {
        JFreeChartEngine.log.error(null, e);
    }
    return image;
}
Also used : InputStream(java.io.InputStream) ActionSequenceResource(org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) IOException(java.io.IOException) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 14 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource in project pentaho-platform by pentaho.

the class RuntimeContext method addXFormHeader.

private void addXFormHeader() {
    XForm.createXFormHeader(RuntimeContext.PARAMETER_FORM, xformHeader);
    IActionSequenceResource resource = paramManager.getCurrentResource(parameterXsl);
    if (!parameterXsl.endsWith(".xsl") && (resource != null)) {
        // load the parameter page template
        try {
            parameterTemplate = getResourceAsString(resource);
        } catch (Exception e) {
        // TODO log this
        }
    }
}
Also used : ActionExecutionException(org.pentaho.platform.api.engine.ActionExecutionException) InvalidParameterException(org.pentaho.platform.api.engine.InvalidParameterException) ActionValidationException(org.pentaho.platform.api.engine.ActionValidationException) ActionSequenceException(org.pentaho.platform.api.engine.ActionSequenceException) ActionInitializationException(org.pentaho.platform.api.engine.ActionInitializationException) ActionSequencePromptException(org.pentaho.platform.api.engine.ActionSequencePromptException) FileNotFoundException(java.io.FileNotFoundException) XmlParseException(org.pentaho.platform.api.util.XmlParseException) UnresolvedParameterException(org.pentaho.platform.api.engine.UnresolvedParameterException) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) IOException(java.io.IOException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 15 with IActionSequenceResource

use of org.pentaho.platform.api.engine.IActionSequenceResource in project pentaho-platform by pentaho.

the class XQueryBaseComponent method runQuery.

protected boolean runQuery(final IPentahoConnection localConnection, String rawQuery) {
    XQueryAction xQueryAction = (XQueryAction) getActionDefinition();
    try {
        if (localConnection == null) {
            return false;
        }
        if (ComponentBase.debug) {
            // $NON-NLS-1$
            debug(Messages.getInstance().getString("XQueryBaseComponent.DEBUG_RUNNING_QUERY", rawQuery));
        }
        String documentPath = null;
        int resourceType = -1;
        String srcXml = xQueryAction.getSourceXml().getStringValue();
        org.pentaho.actionsequence.dom.IActionResource xmlResource = xQueryAction.getXmlDocument();
        InputStream inputStream = null;
        URL url = null;
        if (srcXml != null) {
            inputStream = new FileInputStream(new File(createTempXMLFile(srcXml)));
        } else if (xmlResource != null) {
            // we have a local document to use as the data source
            IActionSequenceResource resource = getResource(xmlResource.getName());
            resourceType = resource.getSourceType();
            if ((resourceType == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resourceType == IActionSequenceResource.FILE_RESOURCE)) {
                inputStream = resource.getInputStream(RepositoryFilePermission.READ);
            } else if (resourceType == IActionSequenceResource.XML) {
                inputStream = new FileInputStream(new File(createTempXMLFile(resource.getAddress())));
            } else {
                url = new URL(documentPath);
            }
        }
        // Retrieve the column types
        String[] columnTypes = null;
        if (retrieveColumnTypes()) {
            try {
                SAXReader reader = XMLParserFactoryProducer.getSAXReader(null);
                Document document;
                if (url != null) {
                    document = reader.read(url);
                } else {
                    document = reader.read(inputStream);
                }
                // $NON-NLS-1$
                Node commentNode = document.selectSingleNode("/result-set/comment()");
                if (commentNode != null) {
                    String commentString = commentNode.getText();
                    // $NON-NLS-1$
                    StringTokenizer st = new StringTokenizer(commentString, ",");
                    List columnTypesList = new LinkedList();
                    while (st.hasMoreTokens()) {
                        String token = st.nextToken().trim();
                        columnTypesList.add(token);
                    }
                    columnTypes = (String[]) columnTypesList.toArray(new String[0]);
                }
            } catch (Exception e) {
                getLogger().warn(Messages.getInstance().getString("XQueryBaseComponent.ERROR_0009_ERROR_BUILDING_COLUMN_TYPES"), // $NON-NLS-1$
                e);
            }
        }
        if (rawQuery != null) {
            if (rawQuery.indexOf("{" + XQueryBaseComponent.XML_DOCUMENT_TAG + "}") >= 0) {
                // $NON-NLS-1$//$NON-NLS-2$
                rawQuery = TemplateUtil.applyTemplate(rawQuery, XQueryBaseComponent.XML_DOCUMENT_TAG, documentPath);
            } else {
                Calendar now = Calendar.getInstance();
                File temp = File.createTempFile("tempXQuery" + now.getTimeInMillis(), ".xml");
                temp.deleteOnExit();
                OutputStream out = new FileOutputStream(temp);
                IActionSequenceResource resource = getResource(xmlResource.getName());
                inputStream = resource.getInputStream(RepositoryFilePermission.READ);
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                inputStream.close();
                documentPath = temp.getAbsolutePath();
                documentPath = FilenameUtils.separatorsToUnix(documentPath);
                // $NON-NLS-1$ //$NON-NLS-2$
                rawQuery = "doc(\"" + documentPath + "\")" + rawQuery;
            }
        }
        if (xQueryAction.getOutputPreparedStatement() != null) {
            return prepareFinalQuery(rawQuery, columnTypes);
        } else {
            return runFinalQuery(localConnection, rawQuery, columnTypes);
        }
    } catch (Exception e) {
        getLogger().error(Messages.getInstance().getString("XQueryBaseComponent.ERROR_0010_ERROR_RUNNING_QUERY"), // $NON-NLS-1$
        e);
        return false;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Calendar(java.util.Calendar) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XQueryAction(org.pentaho.actionsequence.dom.actions.XQueryAction) Document(org.dom4j.Document) URL(java.net.URL) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) XPathException(net.sf.saxon.trans.XPathException) IOException(java.io.IOException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) StringTokenizer(java.util.StringTokenizer) FileOutputStream(java.io.FileOutputStream) LinkedList(java.util.LinkedList) List(java.util.List) File(java.io.File)

Aggregations

IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)28 InputStream (java.io.InputStream)10 IOException (java.io.IOException)9 ActionSequenceResource (org.pentaho.platform.engine.services.actionsequence.ActionSequenceResource)7 OutputStream (java.io.OutputStream)5 FileInputStream (java.io.FileInputStream)4 Set (java.util.Set)4 Document (org.dom4j.Document)4 Node (org.dom4j.Node)4 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)4 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)4 MasterReport (org.pentaho.reporting.engine.classic.core.MasterReport)4 ReportGenerator (org.pentaho.reporting.engine.classic.core.modules.parser.base.ReportGenerator)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URL (java.net.URL)3 Iterator (java.util.Iterator)3