Search in sources :

Example 11 with IOutputHandler

use of org.pentaho.platform.api.engine.IOutputHandler 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.http.HttpMimeTypeListener) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Aggregations

IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)11 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)6 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)5 List (java.util.List)4 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)4 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)4 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)4 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 ServletException (javax.servlet.ServletException)3 HttpRequestParameterProvider (org.pentaho.platform.web.http.request.HttpRequestParameterProvider)3 HttpSessionParameterProvider (org.pentaho.platform.web.http.session.HttpSessionParameterProvider)3 Map (java.util.Map)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)2 Before (org.junit.Before)2