Search in sources :

Example 6 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class SnippetInvoker method invoke.

/** Invoke a single snippet.
     *
     * @param snippet the snippet to run
     * @return the content generated by the snippet
     * @throws IOException if an error was encountered
     */
public String invoke(SnippetInstanceTO snippet) throws IOException {
    if (!test(snippet))
        return null;
    SnippetDefinition defn = snippet.getDefinition();
    if (XMLUtils.hasValue(mode) && !"view".equalsIgnoreCase(mode) && !defn.getModes().contains(mode)) {
        snippet.setStatus(SnippetInvoker.UNSUPPORTED_MODE);
        return null;
    }
    String uri = defn.getUri(mode, action);
    if (uri == null) {
        snippet.setStatus(SnippetInvoker.UNSUPPORTED_MODE);
        return null;
    }
    String namespace = snippet.getNamespace();
    Map extraEnvironment = new HashMap();
    extraEnvironment.put(SNIPPET_ID, snippet.getSnippetID());
    extraEnvironment.put(SNIPPET_VERSION, snippet.getSnippetVersion());
    extraEnvironment.put(PERSISTED_TEXT, snippet.getPersistedText());
    extraEnvironment.put(RESOURCES, defn.getResources());
    extraEnvironment.put(HTMLPreprocessor.REPLACEMENTS_PARAM, Collections.singletonMap("$$$_", namespace));
    for (int i = 0; i < PROPAGATE_TO_ENV.length; i++) extraEnvironment.put(PROPAGATE_TO_ENV[i], parentParameters.get(PROPAGATE_TO_ENV[i]));
    StringBuffer queryString = new StringBuffer(this.queryString);
    addNamespacedParameters(parentParameters, namespace, queryString);
    if (defn.shouldParsePersistedText())
        addParsedParameters(snippet.getPersistedText(), queryString);
    StringBuffer fullUri = new StringBuffer();
    fullUri.append(WebServer.urlEncodePath(prefix)).append("/").append(uri);
    HTMLUtils.appendQuery(fullUri, queryString.toString());
    WebServer webServer = (WebServer) parentEnv.get(TinyCGI.TINY_WEB_SERVER);
    try {
        String results = webServer.getRequestAsString(fullUri.toString(), extraEnvironment);
        snippet.setStatus(SnippetInvoker.STATUS_OK);
        snippet.setUri(fullUri.toString());
        return results;
    } catch (IOException ioe) {
        snippet.setStatus(SnippetInvoker.STATUS_INTERNAL_ERROR);
        snippet.setInvocationException(ioe);
        throw ioe;
    }
}
Also used : HashMap(java.util.HashMap) WebServer(net.sourceforge.processdash.net.http.WebServer) IOException(java.io.IOException) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class ProcessAdvisor method getAdvisorSnippets.

/**
     * @param pageRegion
     * @return a list of all snippets in the "advice" category
     */
private static List getAdvisorSnippets(int pageRegion) {
    List result = new ArrayList();
    Set snippets = SnippetDefinitionManager.getAllSnippets();
    for (Iterator i = snippets.iterator(); i.hasNext(); ) {
        SnippetDefinition snipDef = (SnippetDefinition) i.next();
        if (snipDef.matchesCategory(ADVISOR_SNIPPET_CATEGORY)) {
            SnippetInstanceTO snip = new SnippetInstanceTO();
            snip.setSnippetID(snipDef.getId());
            snip.setDefinition(snipDef);
            snip.setPageRegion(pageRegion);
            result.add(snip);
        }
    }
    return result;
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) SnippetInstanceTO(net.sourceforge.processdash.net.cms.SnippetInstanceTO) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition)

Example 8 with SnippetDefinition

use of net.sourceforge.processdash.ui.snippet.SnippetDefinition in project processdash by dtuma.

the class CmsContentDispatcher method applySnippetFilters.

private void applySnippetFilters(PageContentTO page, String mode) {
    Map<SnippetInstanceTO, SnippetPageFilter> filters = null;
    for (Iterator i = page.getSnippets().iterator(); i.hasNext(); ) {
        SnippetInstanceTO snip = (SnippetInstanceTO) i.next();
        SnippetDefinition def = snip.getDefinition();
        SnippetPageFilter filter = null;
        try {
            if (def != null)
                filter = def.getFilter(mode, null);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Unable to create page filter for snippet '" + snip.getSnippetID() + "'", e);
        }
        if (filter != null) {
            if (filters == null)
                filters = new LinkedHashMap();
            filters.put(snip, filter);
        }
    }
    if (filters != null) {
        // if any filters were found, run them all against the page.
        for (Map.Entry<SnippetInstanceTO, SnippetPageFilter> e : filters.entrySet()) {
            SnippetInstanceTO snip = e.getKey();
            SnippetPageFilter f = e.getValue();
            try {
                f.filter(page, snip, mode);
            } catch (Exception ex) {
                logger.log(Level.SEVERE, "Snippet page filter '" + snip.getSnippetID() + "' encountered an exception while running.", ex);
            }
        }
        // the filters may have added new snippet instances to the page.
        // resolve those snippets if necessary.
        lookupSnippets(page);
    }
}
Also used : Iterator(java.util.Iterator) SnippetPageFilter(net.sourceforge.processdash.ui.snippet.SnippetPageFilter) SnippetDefinition(net.sourceforge.processdash.ui.snippet.SnippetDefinition) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IOException(java.io.IOException) TinyCGIException(net.sourceforge.processdash.net.http.TinyCGIException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SnippetDefinition (net.sourceforge.processdash.ui.snippet.SnippetDefinition)8 Iterator (java.util.Iterator)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SnippetInstanceTO (net.sourceforge.processdash.net.cms.SnippetInstanceTO)2 LinkedHashMap (java.util.LinkedHashMap)1 TreeSet (java.util.TreeSet)1 DataContext (net.sourceforge.processdash.data.DataContext)1 SimpleDataContext (net.sourceforge.processdash.data.util.SimpleDataContext)1 EVTaskList (net.sourceforge.processdash.ev.EVTaskList)1 PersistenceException (net.sourceforge.processdash.ev.ui.TaskScheduleChartSettings.PersistenceException)1 TinyCGIException (net.sourceforge.processdash.net.http.TinyCGIException)1 WebServer (net.sourceforge.processdash.net.http.WebServer)1 SnippetPageFilter (net.sourceforge.processdash.ui.snippet.SnippetPageFilter)1