001: /*
002: WikiForms - a WikiPage FORM handler for JSPWiki.
003:
004: Copyright (C) 2003 BaseN.
005:
006: JSPWiki Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
007:
008: This program is free software; you can redistribute it and/or modify
009: it under the terms of the GNU Lesser General Public License as published
010: by the Free Software Foundation; either version 2.1 of the License, or
011: (at your option) any later version.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public License
019: along with this program; if not, write to the Free Software
020: */
021: package com.ecyrd.jspwiki.forms;
022:
023: import java.text.MessageFormat;
024: import java.util.Map;
025: import java.util.ResourceBundle;
026:
027: import com.ecyrd.jspwiki.WikiContext;
028: import com.ecyrd.jspwiki.plugin.PluginException;
029: import com.ecyrd.jspwiki.plugin.PluginManager;
030: import com.ecyrd.jspwiki.plugin.WikiPlugin;
031: import com.ecyrd.jspwiki.util.FormUtil;
032:
033: /**
034: */
035: public class FormOutput extends FormElement {
036: /**
037: * Executes the FormHandler specified in a Form 'output' plugin,
038: * using entries provided in the HttpRequest as FormHandler
039: * parameters.
040: * <p>
041: * If the parameter 'populate' was given, the WikiPlugin it names
042: * is used to get default values. (It probably makes a lot of
043: * sense for this to be the same plugin as the handler.)
044: * Information for the populator can be given with the FormSet
045: * plugin. If 'populate' is not specified, the form is not
046: * displayed.
047: * <p>
048: * Should there be no HTTP request associated with this request,
049: * the method will return immediately with an empty string.
050: */
051: public String execute(WikiContext ctx, Map params)
052: throws PluginException {
053: //
054: // If there is no HTTP request, returns immediately.
055: //
056: if (ctx.getHttpRequest() == null) {
057: return "";
058: }
059: ResourceBundle rb = ctx
060: .getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
061:
062: // If we are NOT here due to this form being submitted, we do nothing.
063: // The submitted form MUST have parameter 'formname' equal to the name
064: // parameter of this Form plugin.
065:
066: String formName = (String) params.get(PARAM_FORM);
067: String submitForm = ctx.getHttpParameter(PARAM_FORMNAMEHIDDEN);
068: String populator = (String) params.get(PARAM_POPULATE);
069:
070: if (submitForm == null || formName == null
071: || !formName.equals(submitForm)) {
072: // No submitForm -> this was not a submission from the
073: // generated form. If populate is specified, we'll go
074: // ahead and let the handler (populator) put stuff into
075: // the context, otherwise we'll just hide.
076: if (populator == null || !PARAM_HANDLER.equals(populator))
077: return "";
078: // If population was allowed, we should first
079: }
080:
081: String handler = (String) params.get(PARAM_HANDLER);
082: if (handler == null || handler.length() == 0) {
083: Object[] args = { PARAM_HANDLER };
084: // Need to print out an error here as this form is misconfigured
085: return "<p class=\"error\">"
086: + MessageFormat.format(rb
087: .getString("formoutput.missingargument"),
088: args) + "</p>";
089: }
090:
091: String sourcePage = ctx.getPage().getName();
092: String submitServlet = ctx.getURL(WikiContext.VIEW, sourcePage);
093:
094: // If there is previous FormInfo available - say, from a
095: // FormSet plugin - use it.
096: FormInfo info = getFormInfo(ctx);
097: if (info == null) {
098: // Reconstruct the form info from post data
099: info = new FormInfo();
100: info.setName(formName);
101: }
102: // Force override of handler and submit.
103: info.setHandler(handler);
104: info.setAction(submitServlet);
105:
106: // Sift out all extra parameters, leaving only those submitted
107: // in the HTML FORM.
108: Map handlerParams = FormUtil.requestToMap(ctx.getHttpRequest(),
109: HANDLERPARAM_PREFIX);
110: // Previous submission info may be available from FormSet
111: // plugin - add, don't replace.
112: info.addSubmission(handlerParams);
113:
114: // Pass the _body parameter from FormOutput on to the handler
115: info.getSubmission().put(PluginManager.PARAM_BODY,
116: params.get(PluginManager.PARAM_BODY));
117:
118: String handlerOutput = null;
119: String error = null;
120: try {
121: // The plugin _can_ modify the parameters, so we make sure
122: // they stay with us.
123: handlerOutput = ctx.getEngine().getPluginManager().execute(
124: ctx, handler, info.getSubmission());
125: info.setResult(handlerOutput);
126: info.setStatus(FormInfo.EXECUTED);
127: } catch (PluginException pe) {
128: error = "<p class=\"error\">" + pe.getMessage() + "</p>";
129: info.setError(error);
130: info.setStatus(FormInfo.ERROR);
131: }
132:
133: // We store the forminfo, so following Form plugin invocations on this
134: // page can decide what to do based on its values.
135: storeFormInfo(ctx, info);
136:
137: if (error != null)
138: return error;
139:
140: return handlerOutput != null ? handlerOutput : "";
141: }
142:
143: }
|