Search in sources :

Example 6 with HttpRequestParameterProvider

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

the class GenericServlet method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    if (showDeprecationMessage) {
        String deprecationMessage = "GenericServlet is deprecated and should no longer be handling requests. More detail below..." + "\n | You have issued a {0} request to {1} from referer {2} " + "\n | Please consider using one of the following REST services instead:" + "\n | * GET /api/repos/<pluginId>/<path> to read files from a plugin public dir" + "\n | * POST|GET /api/repos/<pathId>/generatedContent to create content resulting from execution of a " + "repo file" + "\n | * POST|GET /api/repos/<pluginId>/<contentGeneratorId> to execute a content generator by name (RPC " + "compatibility service)" + "\n \\ To turn this message off, set init-param 'showDeprecationMessage' to false in the GenericServlet " + "declaration" + "";
        String referer = StringUtils.defaultString(request.getHeader("Referer"), "");
        logger.warn(MessageFormat.format(deprecationMessage, request.getMethod(), request.getRequestURL(), referer));
    }
    PentahoSystem.systemEntryPoint();
    IOutputHandler outputHandler = null;
    // BISERVER-2767 - grabbing the current class loader so we can replace it at the end
    ClassLoader origContextClassloader = Thread.currentThread().getContextClassLoader();
    try {
        String servletPath = request.getServletPath();
        String pathInfo = request.getPathInfo();
        // $NON-NLS-1$
        String contentGeneratorId = "";
        // $NON-NLS-1$
        String urlPath = "";
        SimpleParameterProvider pathParams = new SimpleParameterProvider();
        if (StringUtils.isEmpty(pathInfo)) {
            logger.error(// $NON-NLS-1$
            Messages.getInstance().getErrorString("GenericServlet.ERROR_0005_NO_RESOURCE_SPECIFIED"));
            response.sendError(403);
            return;
        }
        String path = pathInfo.substring(1);
        int slashPos = path.indexOf('/');
        if (slashPos != -1) {
            // $NON-NLS-1$
            pathParams.setParameter("path", pathInfo.substring(slashPos + 1));
            contentGeneratorId = path.substring(0, slashPos);
        } else {
            contentGeneratorId = path;
        }
        // $NON-NLS-1$
        urlPath = "content/" + contentGeneratorId;
        IParameterProvider requestParameters = new HttpRequestParameterProvider(request);
        // $NON-NLS-1$
        pathParams.setParameter("query", request.getQueryString());
        // $NON-NLS-1$
        pathParams.setParameter("contentType", request.getContentType());
        InputStream in = request.getInputStream();
        // $NON-NLS-1$
        pathParams.setParameter("inputstream", in);
        // $NON-NLS-1$
        pathParams.setParameter("httpresponse", response);
        // $NON-NLS-1$
        pathParams.setParameter("httprequest", request);
        // $NON-NLS-1$
        pathParams.setParameter("remoteaddr", request.getRemoteAddr());
        if (PentahoSystem.debug) {
            // $NON-NLS-1$
            debug("GenericServlet contentGeneratorId=" + contentGeneratorId);
            // $NON-NLS-1$
            debug("GenericServlet urlPath=" + urlPath);
        }
        IPentahoSession session = getPentahoSession(request);
        IPluginManager pluginManager = PentahoSystem.get(IPluginManager.class, session);
        if (pluginManager == null) {
            OutputStream out = response.getOutputStream();
            String message = Messages.getInstance().getErrorString("GenericServlet.ERROR_0001_BAD_OBJECT", // $NON-NLS-1$
            IPluginManager.class.getSimpleName());
            error(message);
            out.write(message.getBytes());
            return;
        }
        // TODO make doing the HTTP headers configurable per content generator
        SimpleParameterProvider headerParams = new SimpleParameterProvider();
        Enumeration names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = request.getHeader(name);
            headerParams.setParameter(name, value);
        }
        String pluginId = pluginManager.getServicePlugin(pathInfo);
        if (pluginId != null && pluginManager.isStaticResource(pathInfo)) {
            boolean cacheOn = "true".equals(pluginManager.getPluginSetting(pluginId, "settings/cache", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
            "false"));
            // $NON-NLS-1$
            String maxAge = (String) pluginManager.getPluginSetting(pluginId, "settings/max-age", null);
            allowBrowserCache(maxAge, pathParams);
            String mimeType = MimeHelper.getMimeTypeFromFileName(pathInfo);
            if (mimeType != null) {
                response.setContentType(mimeType);
            }
            OutputStream out = response.getOutputStream();
            // do we have this resource cached?
            ByteArrayOutputStream byteStream = null;
            if (cacheOn) {
                byteStream = (ByteArrayOutputStream) cache.getFromRegionCache(CACHE_FILE, pathInfo);
            }
            if (byteStream != null) {
                IOUtils.write(byteStream.toByteArray(), out);
                return;
            }
            InputStream resourceStream = pluginManager.getStaticResource(pathInfo);
            if (resourceStream != null) {
                try {
                    byteStream = new ByteArrayOutputStream();
                    IOUtils.copy(resourceStream, byteStream);
                    // if cache is enabled, drop file in cache
                    if (cacheOn) {
                        cache.putInRegionCache(CACHE_FILE, pathInfo, byteStream);
                    }
                    // write it out
                    IOUtils.write(byteStream.toByteArray(), out);
                    return;
                } finally {
                    IOUtils.closeQuietly(resourceStream);
                }
            }
            logger.error(Messages.getInstance().getErrorString("GenericServlet.ERROR_0004_RESOURCE_NOT_FOUND", pluginId, // $NON-NLS-1$
            pathInfo));
            response.sendError(404);
            return;
        }
        // content generators defined in plugin.xml are registered with 2 aliases, one is the id, the other is type
        // so, we can still retrieve a content generator by id, even though this is not the correct way to find
        // it. the correct way is to look up a content generator by pluginManager.getContentGenerator(type,
        // perspectiveName)
        IContentGenerator contentGenerator = (IContentGenerator) pluginManager.getBean(contentGeneratorId);
        if (contentGenerator == null) {
            OutputStream out = response.getOutputStream();
            String message = Messages.getInstance().getErrorString("GenericServlet.ERROR_0002_BAD_GENERATOR", // $NON-NLS-1$
            Encode.forHtml(contentGeneratorId));
            error(message);
            out.write(message.getBytes());
            return;
        }
        // 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
        Thread.currentThread().setContextClassLoader(pluginManager.getClassLoader(pluginId));
        // String proxyClass = PentahoSystem.getSystemSetting( module+"/plugin.xml" ,
        // "plugin/content-generators/"+contentGeneratorId,
        // "content generator not found");
        // see if this is an upload
        // File uploading is a service provided by UploadFileServlet where appropriate protections
        // are in place to prevent uploads that are too large.
        // boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        // if (isMultipart) {
        // requestParameters = new SimpleParameterProvider();
        // // Create a factory for disk-based file items
        // FileItemFactory factory = new DiskFileItemFactory();
        // 
        // // Create a new file upload handler
        // ServletFileUpload upload = new ServletFileUpload(factory);
        // 
        // // Parse the request
        // List<?> /* FileItem */items = upload.parseRequest(request);
        // Iterator<?> iter = items.iterator();
        // while (iter.hasNext()) {
        // FileItem item = (FileItem) iter.next();
        // 
        // if (item.isFormField()) {
        // ((SimpleParameterProvider) requestParameters).setParameter(item.getFieldName(), item.getString());
        // } else {
        // String name = item.getName();
        // ((SimpleParameterProvider) requestParameters).setParameter(name, item.getInputStream());
        // }
        // }
        // }
        response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
        IMimeTypeListener listener = new HttpMimeTypeListener(request, response);
        outputHandler = getOutputHandler(response, true);
        outputHandler.setMimeTypeListener(listener);
        IParameterProvider sessionParameters = new HttpSessionParameterProvider(session);
        IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
        Map<String, IParameterProvider> parameterProviders = new HashMap<String, IParameterProvider>();
        parameterProviders.put(IParameterProvider.SCOPE_REQUEST, requestParameters);
        parameterProviders.put(IParameterProvider.SCOPE_SESSION, sessionParameters);
        // $NON-NLS-1$
        parameterProviders.put("headers", headerParams);
        // $NON-NLS-1$
        parameterProviders.put("path", pathParams);
        SimpleUrlFactory urlFactory = // $NON-NLS-1$ //$NON-NLS-2$
        new SimpleUrlFactory(requestContext.getContextPath() + urlPath + "?");
        List<String> messages = new ArrayList<String>();
        contentGenerator.setOutputHandler(outputHandler);
        contentGenerator.setMessagesList(messages);
        contentGenerator.setParameterProviders(parameterProviders);
        contentGenerator.setSession(session);
        contentGenerator.setUrlFactory(urlFactory);
        // String contentType = request.getContentType();
        // contentGenerator.setInput(input);
        contentGenerator.createContent();
        if (PentahoSystem.debug) {
            // $NON-NLS-1$
            debug("Generic Servlet content generate successfully");
        }
    } catch (Exception e) {
        StringBuffer buffer = new StringBuffer();
        error(Messages.getInstance().getErrorString("GenericServlet.ERROR_0002_BAD_GENERATOR", request.getQueryString()), // $NON-NLS-1$
        e);
        List errorList = new ArrayList();
        String msg = e.getMessage();
        errorList.add(msg);
        // $NON-NLS-1$
        MessageFormatUtils.formatFailureMessage("text/html", null, buffer, errorList);
        response.getOutputStream().write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
    } finally {
        // reset the classloader of the current thread
        Thread.currentThread().setContextClassLoader(origContextClassloader);
        PentahoSystem.systemExitPoint();
    }
}
Also used : IMimeTypeListener(org.pentaho.platform.api.engine.IMimeTypeListener) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) IContentGenerator(org.pentaho.platform.api.engine.IContentGenerator) ArrayList(java.util.ArrayList) List(java.util.List) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider) Enumeration(java.util.Enumeration) InputStream(java.io.InputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) HttpSessionParameterProvider(org.pentaho.platform.web.http.session.HttpSessionParameterProvider) IPluginManager(org.pentaho.platform.api.engine.IPluginManager) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 7 with HttpRequestParameterProvider

use of org.pentaho.platform.web.http.request.HttpRequestParameterProvider 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 8 with HttpRequestParameterProvider

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

the class ViewAction method doBackgroundExecution.

protected boolean doBackgroundExecution(final HttpServletRequest request, final HttpServletResponse response, final IPentahoSession userSession) throws ServletException, IOException {
    if ("true".equals(request.getParameter("background"))) {
        // $NON-NLS-1$ //$NON-NLS-2$
        IBackgroundExecution backgroundExecutionHandler = PentahoSystem.get(IBackgroundExecution.class, userSession);
        if (backgroundExecutionHandler != null) {
            HttpRequestParameterProvider parameterProvider = new HttpRequestParameterProvider(request);
            // $NON-NLS-1$
            String intro = "";
            // $NON-NLS-1$
            String footer = "";
            IUITemplater templater = PentahoSystem.get(IUITemplater.class, userSession);
            if (templater != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                String[] sections = templater.breakTemplate("template-dialog.html", "", userSession);
                if ((sections != null) && (sections.length > 0)) {
                    intro = sections[0];
                }
                if ((sections != null) && (sections.length > 1)) {
                    footer = sections[1];
                }
            } else {
                // $NON-NLS-1$
                intro = Messages.getInstance().getString("ViewAction.ERROR_0002_BAD_TEMPLATE_OBJECT");
            }
            response.getWriter().print(intro);
            String backgroundResponse = null;
            try {
                backgroundResponse = backgroundExecutionHandler.backgroundExecuteAction(userSession, parameterProvider);
            } catch (BackgroundExecutionException bex) {
                backgroundResponse = bex.getLocalizedMessage();
                response.getWriter().print(backgroundResponse);
                response.getWriter().print(footer);
                error(Messages.getInstance().getErrorString("ViewAction.ERROR_0004_UNABLE_TO_PERFORM_BACKGROUND_EXECUTION"), // $NON-NLS-1$
                bex);
                return false;
            }
            response.setHeader("background_execution", "true");
            response.getWriter().print(backgroundResponse);
            response.getWriter().print(footer);
            return true;
        } else {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("ViewAction.ERROR_0001_BACKGROUND_EXECUTE_NOT_SUPPORTED"));
        }
    }
    return false;
}
Also used : BackgroundExecutionException(org.pentaho.platform.api.scheduler.BackgroundExecutionException) IUITemplater(org.pentaho.platform.api.engine.IUITemplater) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) IBackgroundExecution(org.pentaho.platform.api.engine.IBackgroundExecution)

Example 9 with HttpRequestParameterProvider

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

Aggregations

HttpRequestParameterProvider (org.pentaho.platform.web.http.request.HttpRequestParameterProvider)9 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)8 List (java.util.List)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 HttpSessionParameterProvider (org.pentaho.platform.web.http.session.HttpSessionParameterProvider)5 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 ServletException (javax.servlet.ServletException)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)4 Map (java.util.Map)3 IMimeTypeListener (org.pentaho.platform.api.engine.IMimeTypeListener)3 IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)3 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)3 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)3 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)3 Document (org.dom4j.Document)2 IActionDefinition (org.pentaho.actionsequence.dom.IActionDefinition)2