Search in sources :

Example 1 with HttpOutputHandler

use of org.pentaho.platform.web.http.HttpOutputHandler in project pentaho-platform by pentaho.

the class UIServlet method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    PentahoSystem.systemEntryPoint();
    try {
        OutputStream outputStream = response.getOutputStream();
        String path = request.getContextPath();
        IPentahoSession userSession = getPentahoSession(request);
        HttpSession session = request.getSession();
        // $NON-NLS-1$
        String type = MediaType.valueOf(request.getParameter("type")).toString();
        if (type == null) {
            // $NON-NLS-1$
            type = "text/html";
        }
        // find out which component is going to fulfill this request
        // $NON-NLS-1$
        String componentName = StringEscapeUtils.escapeHtml(request.getParameter("component"));
        if (componentName == null) {
            // $NON-NLS-1$
            response.setContentType("text/html");
            StringBuffer buffer = new StringBuffer();
            formatErrorMessage(userSession, buffer, "UIServlet.ERROR_0001_COMPONENT_NOT_SPECIFIED");
            outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
            return;
        }
        response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
        // TODO switch this to the interface once stable
        IUIComponent component = (IUIComponent) session.getAttribute(componentName);
        if (component == null) {
            component = getComponent(componentName);
            if (component == null) {
                // $NON-NLS-1$
                response.setContentType("text/html");
                StringBuffer buffer = new StringBuffer();
                formatErrorMessage(userSession, buffer, "UIServlet.ERROR_0002_COMPONENT_INVALID");
                outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
                return;
            }
            session.setAttribute(componentName, component);
        }
        if (!component.validate()) {
            // TODO need an error here
            return;
        }
        String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/content?type=" + type + "&component=" + componentName + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
        "&";
        response.setContentType(type);
        HttpOutputHandler outputHandler = new HttpOutputHandler(response, outputStream, true);
        SimpleUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
        HttpServletRequestHandler requestHandler = new HttpServletRequestHandler(userSession, null, request, outputHandler, urlFactory);
        requestHandler.handleUIRequest(component, type);
    } finally {
        PentahoSystem.systemExitPoint();
    }
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) HttpSession(javax.servlet.http.HttpSession) OutputStream(java.io.OutputStream) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler) IUIComponent(org.pentaho.platform.api.ui.IUIComponent) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 2 with HttpOutputHandler

use of org.pentaho.platform.web.http.HttpOutputHandler in project pentaho-platform by pentaho.

the class SolutionEngineInteractivityService method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    PentahoSystem.systemEntryPoint();
    try {
        IPentahoSession userSession = getPentahoSession(request);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // $NON-NLS-1$
        String solutionName = request.getParameter("solution");
        // $NON-NLS-1$
        String actionPath = request.getParameter("path");
        // $NON-NLS-1$
        String actionName = request.getParameter("action");
        IActionSequence actionSequence = new ActionSequenceJCRHelper().getActionSequence(ActionInfo.buildSolutionPath(solutionName, actionPath, actionName), PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
        String fileName = null;
        if (actionSequence != null) {
            String title = actionSequence.getTitle();
            if ((title != null) && (title.length() > 0)) {
                fileName = title;
            } else {
                String sequenceName = actionSequence.getSequenceName();
                if ((sequenceName != null) && (sequenceName.length() > 0)) {
                    fileName = sequenceName;
                } else {
                    List actionDefinitionsList = actionSequence.getActionDefinitionsAndSequences();
                    int i = 0;
                    boolean done = false;
                    while ((actionDefinitionsList.size() > i) && !done) {
                        IActionDefinition actionDefinition = (IActionDefinition) actionDefinitionsList.get(i);
                        String componentName = actionDefinition.getComponentName();
                        if ((componentName != null) && (componentName.length() > 0)) {
                            fileName = componentName;
                            done = true;
                        } else {
                            i++;
                        }
                    }
                }
            }
        }
        IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
        HttpOutputHandler outputHandler = createOutputHandler(response, outputStream);
        outputHandler.setSession(userSession);
        IMimeTypeListener listener = new HttpMimeTypeListener(request, response);
        listener.setName(fileName);
        outputHandler.setMimeTypeListener(listener);
        SimpleUrlFactory urlFactory = new SimpleUrlFactory(requestContext.getContextPath() + // $NON-NLS-1$
        "SolutionEngineInteractivityService?");
        IParameterProvider requestParameters = new HttpRequestParameterProvider(request);
        setupOutputHandler(outputHandler, requestParameters);
        HttpServletRequestHandler requestHandler = getRequestHandler(request, response, userSession, requestParameters, outputStream, outputHandler, urlFactory);
        handleActionRequest(request, response, outputHandler, requestHandler, requestParameters, outputStream, null);
    } finally {
        PentahoSystem.systemExitPoint();
    }
}
Also used : IMimeTypeListener(org.pentaho.platform.api.engine.IMimeTypeListener) IActionSequence(org.pentaho.platform.api.engine.IActionSequence) IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) List(java.util.List) ActionSequenceJCRHelper(org.pentaho.platform.engine.services.ActionSequenceJCRHelper) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 3 with HttpOutputHandler

use of org.pentaho.platform.web.http.HttpOutputHandler in project pentaho-platform by pentaho.

the class GenericServlet method getOutputHandler.

protected IOutputHandler getOutputHandler(HttpServletResponse response, boolean allowFeedback) throws IOException {
    OutputStream out = response.getOutputStream();
    HttpOutputHandler handler = new HttpOutputHandler(response, out, allowFeedback);
    return handler;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler)

Example 4 with HttpOutputHandler

use of org.pentaho.platform.web.http.HttpOutputHandler in project pentaho-platform by pentaho.

the class ViewAction method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    PentahoSystem.systemEntryPoint();
    try {
        IPentahoSession userSession = getPentahoSession(request);
        if (!doBackgroundExecution(request, response, userSession)) {
            OutputStream outputStream = getOutputStream(response, doMessages(request));
            ActionSequenceJCRHelper actionHelper = new ActionSequenceJCRHelper(userSession);
            // $NON-NLS-1$
            String actionPath = request.getParameter("path");
            IActionSequence actionSequence = actionHelper.getActionSequence(actionPath, PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
            String fileName = null;
            if (actionSequence != null) {
                String title = actionSequence.getTitle();
                if ((title != null) && (title.length() > 0)) {
                    fileName = title;
                } else {
                    String sequenceName = actionSequence.getSequenceName();
                    if ((sequenceName != null) && (sequenceName.length() > 0)) {
                        fileName = sequenceName;
                    } else {
                        List actionDefinitionsList = actionSequence.getActionDefinitionsAndSequences();
                        int i = 0;
                        boolean done = false;
                        while ((actionDefinitionsList.size() > i) && !done) {
                            IActionDefinition actionDefinition = (IActionDefinition) actionDefinitionsList.get(i);
                            String componentName = actionDefinition.getComponentName();
                            if ((componentName != null) && (componentName.length() > 0)) {
                                fileName = componentName;
                                done = true;
                            } else {
                                i++;
                            }
                        }
                    }
                }
            }
            IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
            HttpOutputHandler outputHandler = createOutputHandler(response, outputStream);
            outputHandler.setSession(userSession);
            IMimeTypeListener listener = new HttpMimeTypeListener(request, response, fileName);
            outputHandler.setMimeTypeListener(listener);
            // $NON-NLS-1$
            SimpleUrlFactory urlFactory = new SimpleUrlFactory(requestContext.getContextPath() + "ViewAction?");
            IParameterProvider requestParameters = new HttpRequestParameterProvider(request);
            HttpServletRequestHandler requestHandler = getRequestHandler(request, response, userSession, requestParameters, outputStream, outputHandler, urlFactory);
            handleActionRequest(request, response, outputHandler, requestHandler, outputStream, null);
        }
    } finally {
        PentahoSystem.systemExitPoint();
    }
}
Also used : IMimeTypeListener(org.pentaho.platform.api.engine.IMimeTypeListener) IActionSequence(org.pentaho.platform.api.engine.IActionSequence) IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) List(java.util.List) ActionSequenceJCRHelper(org.pentaho.platform.engine.services.ActionSequenceJCRHelper) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 5 with HttpOutputHandler

use of org.pentaho.platform.web.http.HttpOutputHandler 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

HttpOutputHandler (org.pentaho.platform.web.http.HttpOutputHandler)5 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 List (java.util.List)2 IActionDefinition (org.pentaho.actionsequence.dom.IActionDefinition)2 IActionSequence (org.pentaho.platform.api.engine.IActionSequence)2 IMimeTypeListener (org.pentaho.platform.api.engine.IMimeTypeListener)2 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)2 ActionSequenceJCRHelper (org.pentaho.platform.engine.services.ActionSequenceJCRHelper)2 HttpRequestParameterProvider (org.pentaho.platform.web.http.request.HttpRequestParameterProvider)2 HashMap (java.util.HashMap)1 HttpSession (javax.servlet.http.HttpSession)1 IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)1 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)1 IUIComponent (org.pentaho.platform.api.ui.IUIComponent)1 HttpMimeTypeListener (org.pentaho.platform.web.http.HttpMimeTypeListener)1