Example usage for com.liferay.portal.kernel.scripting ScriptingHelperUtil getPortletObjects

List of usage examples for com.liferay.portal.kernel.scripting ScriptingHelperUtil getPortletObjects

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.scripting ScriptingHelperUtil getPortletObjects.

Prototype

public static Map<String, Object> getPortletObjects(PortletConfig portletConfig, PortletContext portletContext,
            PortletRequest portletRequest, PortletResponse portletResponse) 

Source Link

Usage

From source file:com.liferay.server.admin.web.internal.portlet.action.EditServerMVCActionCommand.java

License:Open Source License

protected void runScript(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    String language = ParamUtil.getString(actionRequest, "language");
    String script = ParamUtil.getString(actionRequest, "script");

    PortletConfig portletConfig = getPortletConfig(actionRequest);

    PortletContext portletContext = portletConfig.getPortletContext();

    Map<String, Object> portletObjects = ScriptingHelperUtil.getPortletObjects(portletConfig, portletContext,
            actionRequest, actionResponse);

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(unsyncByteArrayOutputStream);

    portletObjects.put("out", unsyncPrintWriter);

    try {/*from w w w  .j  a v a  2s  . co  m*/
        SessionMessages.add(actionRequest, "language", language);
        SessionMessages.add(actionRequest, "script", script);

        _scripting.exec(null, portletObjects, language, script);

        unsyncPrintWriter.flush();

        SessionMessages.add(actionRequest, "scriptOutput", unsyncByteArrayOutputStream.toString());
    } catch (ScriptingException se) {
        SessionErrors.add(actionRequest, ScriptingException.class.getName(), se);

        Log log = SanitizerLogWrapper.allowCRLF(_log);

        log.error(se.getMessage());
    }
}

From source file:com.permeance.utility.scriptinghelper.portlets.ScriptingHelperPortlet.java

License:Open Source License

public void execute(ActionRequest actionRequest, ActionResponse actionResponse) {
    try {//  www .  j  a  v  a 2  s  .c o  m
        sCheckPermissions(actionRequest);

        String portletId = "_" + PortalUtil.getPortletId(actionRequest) + "_";

        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(100 * 1024 * 1024);
        PortletFileUpload upload = new PortletFileUpload(factory);

        FileItem fileUploaded = null;
        List<FileItem> items = upload.parseRequest(actionRequest);
        for (FileItem fi : items) {
            if (fi.isFormField()) {
                actionRequest.setAttribute(fi.getFieldName(), fi.getString());
                if (fi.getFieldName().startsWith(portletId)) {
                    actionRequest.setAttribute(fi.getFieldName().substring(portletId.length()), fi.getString());
                }
            } else {
                fileUploaded = fi;
            }
        }

        String cmd = (String) actionRequest.getAttribute("cmd");
        String language = (String) actionRequest.getAttribute("language");
        String script = (String) actionRequest.getAttribute("script");
        String editorheight = (String) actionRequest.getAttribute("editorheight");
        String themesel = (String) actionRequest.getAttribute("themesel");
        if (language == null) {
            language = getDefaultLanguage();
        }
        if (script == null) {
            script = StringPool.BLANK;
        }
        actionResponse.setRenderParameter("language", language);
        actionResponse.setRenderParameter("script", script);
        actionResponse.setRenderParameter("editorheight", editorheight);
        actionResponse.setRenderParameter("themesel", themesel);

        if ("execute".equals(cmd)) {

            Map<String, Object> portletObjects = ScriptingHelperUtil.getPortletObjects(getPortletConfig(),
                    getPortletContext(), actionRequest, actionResponse);

            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

            UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(unsyncByteArrayOutputStream);

            portletObjects.put("out", unsyncPrintWriter);

            _log.info("Executing script");
            ScriptingUtil.exec(null, portletObjects, language, script, StringPool.EMPTY_ARRAY);
            unsyncPrintWriter.flush();
            actionResponse.setRenderParameter("script_output", unsyncByteArrayOutputStream.toString());
        } else if ("save".equals(cmd)) {
            String newscriptname = (String) actionRequest.getAttribute("newscriptname");
            if (newscriptname == null || newscriptname.trim().length() == 0) {
                actionResponse.setRenderParameter("script_trace", "No script name specified to save into!");
                SessionErrors.add(actionRequest, "error");
                return;
            }

            _log.info("Saving new script: " + newscriptname.trim());
            PortletPreferences prefs = actionRequest.getPreferences();
            prefs.setValue("savedscript." + newscriptname.trim(), script);
            prefs.setValue("lang." + newscriptname.trim(), language);
            prefs.store();
        } else if ("saveinto".equals(cmd)) {
            String scriptname = (String) actionRequest.getAttribute("savedscript");
            if (scriptname == null) {
                actionResponse.setRenderParameter("script_trace", "No script specified to save into!");
                SessionErrors.add(actionRequest, "error");
                return;
            }

            _log.info("Saving saved script: " + scriptname);
            PortletPreferences prefs = actionRequest.getPreferences();
            prefs.setValue("savedscript." + scriptname, script);
            prefs.setValue("lang." + scriptname, language);
            prefs.store();
        } else if ("loadfrom".equals(cmd)) {
            String scriptname = (String) actionRequest.getAttribute("savedscript");
            if (scriptname == null) {
                actionResponse.setRenderParameter("script_trace", "No script specified to load from!");
                SessionErrors.add(actionRequest, "error");
                return;
            }
            _log.info("Loading saved script: " + scriptname);
            PortletPreferences prefs = actionRequest.getPreferences();
            language = prefs.getValue("lang." + scriptname, getDefaultLanguage());
            script = prefs.getValue("savedscript." + scriptname, StringPool.BLANK);
            actionResponse.setRenderParameter("language", language);
            actionResponse.setRenderParameter("script", script);
        } else if ("delete".equals(cmd)) {
            String scriptname = (String) actionRequest.getAttribute("savedscript");
            if (scriptname == null) {
                actionResponse.setRenderParameter("script_trace", "No script specified to delete!");
                SessionErrors.add(actionRequest, "error");
                return;
            }
            _log.info("Deleting saved script: " + scriptname);
            PortletPreferences prefs = actionRequest.getPreferences();
            prefs.reset("savedscript." + scriptname);
            prefs.reset("lang." + scriptname);
            prefs.store();
        } else if ("import".equals(cmd)) {
            if (fileUploaded == null) {
                actionResponse.setRenderParameter("script_trace", "No file was uploaded for import!");
                SessionErrors.add(actionRequest, "error");
                return;
            }

            StringBuilder output = new StringBuilder();

            InputStream instream = fileUploaded.getInputStream();
            ZipInputStream zipstream = null;
            try {
                zipstream = new ZipInputStream(instream);
                ZipEntry entry = zipstream.getNextEntry();
                while (entry != null) {
                    String filename = entry.getName();
                    if (filename.contains("/")) {
                        int qs = filename.lastIndexOf("/");
                        if (qs != -1) {
                            filename = filename.substring(qs + 1);
                        }
                    }
                    if (filename.contains("\\")) {
                        int qs = filename.lastIndexOf("\\");
                        if (qs != -1) {
                            filename = filename.substring(qs + 1);
                        }
                    }

                    String ext = StringPool.BLANK;
                    if (filename.length() > 0) {
                        int qs = filename.lastIndexOf(".");
                        if (qs > 0) {
                            ext = filename.substring(qs + 1);
                            filename = filename.substring(0, qs);
                        }
                    }

                    String lang = resolveLanguage(ext);
                    String imscript = getStreamAsString(zipstream, "utf-8", false);

                    if (imscript != null && imscript.length() > 0) {
                        _log.info("Importing script \"" + filename + "\" of type " + lang);
                        output.append("Importing script \"" + filename + "\" of type " + lang + "\n");

                        PortletPreferences prefs = actionRequest.getPreferences();
                        prefs.setValue("savedscript." + filename, imscript);
                        prefs.setValue("lang." + filename, lang);
                        prefs.store();
                    }

                    entry = zipstream.getNextEntry();
                }

                actionResponse.setRenderParameter("script_output", output.toString());
            } finally {
                try {
                    if (zipstream != null) {
                        zipstream.close();
                    }
                } catch (Exception e) {
                }
                try {
                    if (instream != null) {
                        instream.close();
                    }
                } catch (Exception e) {
                }
            }

            _log.info(fileUploaded.getName());
        }
        SessionMessages.add(actionRequest, "success");
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.close();
        actionResponse.setRenderParameter("script_trace", sw.toString());
        _log.error(e);
        SessionErrors.add(actionRequest, e.toString());
    }
}