Search in sources :

Example 26 with SimpleUrlFactory

use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.

the class SimpleUrlTest method testSimpleUrl.

public void testSimpleUrl() {
    // $NON-NLS-1$
    String baseUrl = "http://localhost:8080/pentaho";
    // $NON-NLS-1$
    SimpleUrlFactory urlFactory = new SimpleUrlFactory("/testurl?");
    urlFactory.getActionUrlBuilder();
    SimpleUrl url = new SimpleUrl(baseUrl);
    Assert.assertEquals(url.getUrl(), baseUrl);
    // $NON-NLS-1$  //$NON-NLS-2$
    url.setParameter("action", "doSomething");
    // $NON-NLS-1$
    Assert.assertEquals(url.getUrl(), baseUrl + "&action=doSomething");
}
Also used : SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) SimpleUrl(org.pentaho.platform.util.web.SimpleUrl)

Example 27 with SimpleUrlFactory

use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.

the class DefaultChartBeansGenerator method runActionSequence.

/**
 * Executes an action sequence from an <code>ActionSequenceDocument</code>.
 *
 * @param pentahoSession
 *          current <code>IPentahoSession</code>
 * @param parameterProviders
 *          map of parameter providers; there should a single entry with "request" as the key
 * @param outputHandler
 *          output handler
 * @param doc
 *          action sequence document
 * @throws RuntimeException
 *           if anything goes wrong
 */
protected void runActionSequence(final IPentahoSession pentahoSession, final Map<String, IParameterProvider> parameterProviders, final IOutputHandler outputHandler, final ActionSequenceDocument doc) throws RuntimeException {
    // Get the solution engine
    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, pentahoSession);
    if (solutionEngine == null) {
        // $NON-NLS-1$
        throw new RuntimeException("solutionEngine is null");
    }
    solutionEngine.setLoggingLevel(ILogger.DEBUG);
    solutionEngine.init(pentahoSession);
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    String contextPath = requestContext.getContextPath();
    // $NON-NLS-1$
    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(contextPath);
    IRuntimeContext runtime;
    IParameterProvider requestParmProvider = parameterProviders.get("request");
    if (requestParmProvider.hasParameter("obj_id")) {
        final String obj_id = (String) requestParmProvider.getParameter("obj_id");
        final String msg_name = (String) requestParmProvider.getParameter("message_name");
        final String job_id = (String) requestParmProvider.getParameter("job_id");
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        obj_id, // $NON-NLS-1$ //$NON-NLS-2$
        job_id, // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, msg_name, true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    } else {
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        "chartbeans_mql", // $NON-NLS-1$ //$NON-NLS-2$
        "myprocessid", // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, "myinstanceid", true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    }
    if ((runtime != null) && (runtime.getStatus() != IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
        StringBuilder buf = new StringBuilder();
        boolean firstIteration = true;
        for (Object /* String */
        message : runtime.getMessages()) {
            if (message instanceof Exception) {
                Exception ex = (Exception) message;
                if (ex.getCause() instanceof RuntimeException) {
                    throw (RuntimeException) ex.getCause();
                }
            }
            if (!firstIteration) {
                // $NON-NLS-1$
                buf.append(" \\\\ ");
            }
            buf.append(message);
        }
        String errorStr;
        if (buf.indexOf("action_sequence_failed") > -1 && buf.indexOf("MQLRelationalDataComponent") > -1) {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0001_SECURITY_ERROR");
        } else {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0002_UNKNOWN_ERROR");
        }
        throw new RuntimeException(errorStr);
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext)

Example 28 with SimpleUrlFactory

use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.

the class GeneratorStreamingOutput method generateContent.

protected void generateContent(OutputStream outputStream, final MimeTypeCallback callback) throws Exception {
    try {
        httpServletResponse.setCharacterEncoding(LocaleHelper.getSystemEncoding());
    } catch (Throwable t) {
        // $NON-NLS-1$
        logger.warn("could not set encoding, servlet-api is likely too old.  are we in a unit test?");
    }
    IOutputHandler outputHandler = new HttpOutputHandler(httpServletResponse, outputStream, true);
    outputHandler.setMimeTypeListener(new HttpMimeTypeListener(httpServletRequest, httpServletResponse) {

        /*
       * This content generator is setting the mimeType
       */
        @Override
        public void setMimeType(String mimeType) {
            try {
                if (!MIMETYPE_MUTABLE && GeneratorStreamingOutput.this.getMimeType() != null) {
                    mimeTrace(// $NON-NLS-1$
                    "Content generator is trying to set response mime type to [{0}], but mime type [{1}] has already been imposed. Content generator request to change mime type will be ignored.", mimeType, GeneratorStreamingOutput.this.getMimeType());
                    return;
                } else {
                    // $NON-NLS-1$
                    mimeTrace("Content generator is setting response mime type to [{0}]", mimeType);
                    if (callback != null) {
                        callback.setMimeType(mimeType);
                    }
                    GeneratorStreamingOutput.this.setMimeType(mimeType);
                }
            } catch (Throwable th) {
                mimeTrace("Failed to set mime type: {0}", th.getMessage());
                // $NON-NLS-1$
                logger.error(MessageFormat.format("Failed to set mime type: {0}", th.getMessage()));
            }
            super.setMimeType(mimeType);
        }
    });
    Map<String, IParameterProvider> parameterProviders = new HashMap<String, IParameterProvider>();
    parameterProviders.put(IParameterProvider.SCOPE_REQUEST, createRequestParamProvider());
    parameterProviders.put(IParameterProvider.SCOPE_SESSION, createSessionParameterProvider());
    // $NON-NLS-1$
    parameterProviders.put("headers", createHeaderParamProvider());
    // $NON-NLS-1$
    parameterProviders.put("path", createPathParamProvider());
    String pluginId = contentGeneratorDescriptor.getPluginId();
    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(PentahoRequestContextHolder.getRequestContext().getContextPath() + "api/repos/" + pluginId + "/" + contentGeneratorID + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "?");
    // set the classloader of the current thread to the class loader of
    // the plugin so that it can load its libraries
    // Note: we cannot ask the contentGenerator class for it's classloader,
    // since the cg may
    // actually be a proxy object loaded by main the WebAppClassloader
    ClassLoader origContextClassloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(pluginMgr.getClassLoader(pluginId));
    try {
        contentGenerator.setOutputHandler(outputHandler);
        contentGenerator.setMessagesList(new ArrayList<String>());
        contentGenerator.setParameterProviders(parameterProviders);
        contentGenerator.setSession(PentahoSessionHolder.getSession());
        if (urlFactory != null) {
            contentGenerator.setUrlFactory(urlFactory);
        }
        contentGenerator.createContent();
    } finally {
        Thread.currentThread().setContextClassLoader(origContextClassloader);
    }
}
Also used : IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) HashMap(java.util.HashMap) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) HttpMimeTypeListener(org.pentaho.platform.web.servlet.HttpMimeTypeListener) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 29 with SimpleUrlFactory

use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.

the class PentahoSystem method sessionStartup.

public static void sessionStartup(final IPentahoSession session, IParameterProvider sessionParameters) {
    List<ISessionStartupAction> sessionStartupActions = PentahoSystem.getSessionStartupActionsForType(session.getClass().getName());
    if (sessionStartupActions == null) {
        // nothing to do...
        return;
    }
    if (!session.isAuthenticated()) {
        return;
    }
    Boolean startupActionsFired = (Boolean) session.getAttribute("StartupActionsFired");
    if ((startupActionsFired == null) || (!startupActionsFired)) {
        try {
            if (debug) {
                // $NON-NLS-1$
                Logger.debug(PentahoSystem.class, "Process session startup actions");
            }
            if (sessionStartupActions != null) {
                for (ISessionStartupAction sessionStartupAction : sessionStartupActions) {
                    // parse the actionStr out to identify an action
                    // now execute the action...
                    SimpleOutputHandler outputHandler = null;
                    String instanceId = null;
                    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, session);
                    solutionEngine.setLoggingLevel(PentahoSystem.loggingLevel);
                    solutionEngine.init(session);
                    // $NON-NLS-1$
                    String baseUrl = "";
                    HashMap parameterProviderMap = new HashMap();
                    if (sessionParameters == null) {
                        sessionParameters = new PentahoSessionParameterProvider(session);
                    }
                    parameterProviderMap.put(SCOPE_SESSION, sessionParameters);
                    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
                    ArrayList messages = new ArrayList();
                    IRuntimeContext context = null;
                    try {
                        context = solutionEngine.execute(sessionStartupAction.getActionPath(), "Session startup actions", false, true, instanceId, false, parameterProviderMap, outputHandler, null, urlFactory, // $NON-NLS-1$
                        messages);
                        // if context is null, then we cannot check the status
                        if (null == context) {
                            return;
                        }
                        if (context.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
                            // now grab any outputs
                            Iterator outputNameIterator = context.getOutputNames().iterator();
                            while (outputNameIterator.hasNext()) {
                                String attributeName = (String) outputNameIterator.next();
                                IActionParameter output = context.getOutputParameter(attributeName);
                                Object data = output.getValue();
                                if (data != null) {
                                    session.removeAttribute(attributeName);
                                    session.setAttribute(attributeName, data);
                                }
                            }
                        }
                    } catch (Throwable th) {
                        Logger.warn(PentahoSystem.class.getName(), Messages.getInstance().getString("PentahoSystem.WARN_UNABLE_TO_EXECUTE_SESSION_ACTION", th.getLocalizedMessage()), // $NON-NLS-1$
                        th);
                    } finally {
                        if (context != null) {
                            context.dispose();
                        }
                    }
                }
            }
        } finally {
            session.setAttribute("StartupActionsFired", true);
        }
    } else {
        if (debug) {
            Logger.debug(PentahoSystem.class, "Session startup actions already fired");
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) HashMap(java.util.HashMap) PentahoSessionParameterProvider(org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 30 with SimpleUrlFactory

use of org.pentaho.platform.util.web.SimpleUrlFactory in project pentaho-platform by pentaho.

the class ContentGeneratorUtil method getContentAsString.

/**
 * Convenience method for executing a content generator and getting back it's output as a string. Useful for
 * testing.
 *
 * @param cg
 *          the content generator to execute
 * @return the output of the content generator
 * @throws Exception
 *           if there was a problem creating the content
 */
public static String getContentAsString(IContentGenerator cg) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOutputHandler outputHandler = new SimpleOutputHandler(out, false);
    Map<String, IParameterProvider> parameterProviders = new HashMap<String, IParameterProvider>();
    SimpleParameterProvider requestParams = new SimpleParameterProvider();
    parameterProviders.put(IParameterProvider.SCOPE_REQUEST, requestParams);
    // $NON-NLS-1$
    SimpleUrlFactory urlFactory = new SimpleUrlFactory("?");
    List<String> messages = new ArrayList<String>();
    cg.setOutputHandler(outputHandler);
    IMimeTypeListener mimeTypeListener = new IMimeTypeListener() {

        @SuppressWarnings("unused")
        public String mimeType = null;

        @SuppressWarnings("unused")
        public String name = null;

        public void setMimeType(String mimeType) {
            this.mimeType = mimeType;
        }

        public void setName(String name) {
            this.name = name;
        }
    };
    outputHandler.setMimeTypeListener(mimeTypeListener);
    cg.setMessagesList(messages);
    cg.setParameterProviders(parameterProviders);
    cg.setSession(PentahoSessionHolder.getSession());
    cg.setUrlFactory(urlFactory);
    cg.createContent();
    String content = new String(out.toByteArray());
    return content;
}
Also used : IMimeTypeListener(org.pentaho.platform.api.engine.IMimeTypeListener) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Aggregations

SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)51 ArrayList (java.util.ArrayList)39 HashMap (java.util.HashMap)31 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)23 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)20 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)20 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)17 OutputStream (java.io.OutputStream)16 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)16 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)15 IOException (java.io.IOException)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)11 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)11 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)9 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)8 List (java.util.List)6 Document (org.dom4j.Document)6 IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)6 BaseRequestHandler (org.pentaho.platform.engine.services.BaseRequestHandler)5