Example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString

List of usage examples for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.liferay.portlet.admin.action.EditServerAction.java

License:Open Source License

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

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

    PortletContext portletContext = portletConfig.getPortletContext();

    Map<String, Object> portletObjects = ScriptingUtil.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 2  s .  c o m*/
        SessionMessages.add(actionRequest, "language", language);
        SessionMessages.add(actionRequest, "script", script);

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

        unsyncPrintWriter.flush();

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

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

From source file:com.liferay.portlet.xslcontent.util.XSLContentUtil.java

License:Open Source License

public static String transform(URL xmlUrl, URL xslUrl) throws IOException, TransformerException {

    String xml = HttpUtil.URLtoString(xmlUrl);
    String xsl = HttpUtil.URLtoString(xslUrl);

    StreamSource xmlSource = new StreamSource(new UnsyncStringReader(xml));
    StreamSource xslSource = new StreamSource(new UnsyncStringReader(xsl));

    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer(xslSource);

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    transformer.transform(xmlSource, new StreamResult(unsyncByteArrayOutputStream));

    return unsyncByteArrayOutputStream.toString();
}

From source file:com.liferay.rtl.servlet.filters.dynamiccss.DynamicCSSUtil.java

License:Open Source License

private static String _parseSass(ServletContext servletContext, HttpServletRequest request,
        ThemeDisplay themeDisplay, Theme theme, String resourcePath, String content) throws Exception {

    Map<String, Object> inputObjects = new HashMap<String, Object>();

    String cssThemePath = _getCssThemePath(servletContext, request, themeDisplay, theme);

    inputObjects.put("content", content);
    inputObjects.put("cssRealPath", resourcePath);
    inputObjects.put("cssThemePath", cssThemePath);

    File sassTempDir = _getSassTempDir(servletContext);

    inputObjects.put("sassCachePath", sassTempDir.getCanonicalPath());

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(unsyncByteArrayOutputStream);

    inputObjects.put("out", unsyncPrintWriter);

    _rubyExecutor.eval(null, inputObjects, null, _rubyScript);

    unsyncPrintWriter.flush();/*from www.ja  va  2s  .c o  m*/

    return unsyncByteArrayOutputStream.toString();
}

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 .  jav a 2 s  .c  o  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.liferay.util.xml.XMLMergerRunner.java

License:Open Source License

private String _documentToString(Document doc, String docType) throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setIndent("\t");
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(unsyncByteArrayOutputStream, format);

    writer.write(doc);//from ww  w  . j  ava 2s  .c  o  m

    String xml = unsyncByteArrayOutputStream.toString();

    int pos = xml.indexOf("<?");

    String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);

    xml = StringUtil.replace(xml, header, "");
    xml = header + "\n" + docType + "\n" + xml;

    return xml;
}

From source file:com.liferay.xsl.content.web.internal.util.XSLContentUtil.java

License:Open Source License

public static String transform(XSLContentConfiguration xslContentConfiguration, URL xmlUrl, URL xslUrl)
        throws Exception {

    TransformerFactory transformerFactory = getTransformerFactory(xslContentConfiguration);

    DocumentBuilder documentBuilder = getDocumentBuilder(xslContentConfiguration);

    Transformer transformer = transformerFactory.newTransformer(getXslSource(documentBuilder, xslUrl));

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    transformer.transform(getXmlSource(documentBuilder, xmlUrl), new StreamResult(unsyncByteArrayOutputStream));

    return unsyncByteArrayOutputStream.toString();
}

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

License:Open Source License

public void execute(ActionRequest actionRequest, ActionResponse actionResponse) {
    try {//from w  w w  .ja v a  2s.  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());
    }
}