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;
}
}
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;
}
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);
}
}
Aggregations