001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.util;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import org.apache.log4j.Logger;
028:
029: import de.schlund.pfixcore.workflow.Context;
030: import de.schlund.pfixcore.workflow.ContextResource;
031: import de.schlund.pfixcore.workflow.ContextResourceManager;
032: import de.schlund.pfixcore.workflow.State;
033: import de.schlund.pfixcore.workflow.StateImpl;
034: import de.schlund.pfixxml.PfixServletRequest;
035: import de.schlund.pfixxml.RequestParam;
036: import de.schlund.pfixxml.ResultDocument;
037: import de.schlund.pfixxml.SPDocument;
038: import de.schlund.pfixxml.XMLException;
039: import de.schlund.pfixxml.perflogging.PerfEvent;
040: import de.schlund.pfixxml.perflogging.PerfEventType;
041:
042: /**
043: *
044: * @author Benjamin Reitzammer <benjamin@schlund.de>
045: */
046: public class StateUtil {
047:
048: private final static Logger LOG = Logger.getLogger(StateImpl.class);
049:
050: private static final String MIMETYPE = "mimetype";
051: private static final String HEADER = "responseheader";
052: private static final String def_mime = "text/html";
053:
054: /**
055: *
056: */
057: public static ResultDocument createDefaultResultDocument(
058: Context context) throws Exception {
059: ResultDocument resdoc = new ResultDocument();
060: renderContextResources(context, resdoc);
061: addResponseHeadersAndType(context, resdoc);
062: return resdoc;
063: }
064:
065: /**
066: *
067: */
068: public static void renderContextResources(Context context,
069: ResultDocument resdoc) throws Exception {
070: ContextResourceManager crm = context
071: .getContextResourceManager();
072: Map<String, Class<? extends ContextResource>> crs = context
073: .getConfigForCurrentPageRequest().getContextResources();
074:
075: for (Iterator<String> i = crs.keySet().iterator(); i.hasNext();) {
076: String nodename = i.next();
077: String classname = crs.get(nodename).getName();
078: if (LOG.isDebugEnabled()) {
079: LOG.debug("*** Auto appending status for " + classname
080: + " at node " + nodename);
081: }
082: ContextResource cr = crm.getResource(classname);
083: if (cr == null) {
084: throw new XMLException("ContextResource not found: "
085: + classname);
086: }
087:
088: PerfEvent pe = new PerfEvent(
089: PerfEventType.CONTEXTRESOURCE_INSERTSTATUS,
090: classname);
091: pe.start();
092: cr.insertStatus(resdoc, resdoc.createNode(nodename));
093: pe.save();
094: }
095: }
096:
097: /**
098: *
099: */
100: public static void addResponseHeadersAndType(Context context,
101: ResultDocument resdoc) {
102: Properties props = context.getPropertiesForCurrentPageRequest();
103: Properties contextprops = context.getProperties();
104:
105: String mime = props.getProperty(MIMETYPE);
106: SPDocument doc = resdoc.getSPDocument();
107:
108: if (mime != null) {
109: doc.setResponseContentType(mime);
110: } else {
111: doc.setResponseContentType(def_mime);
112: }
113:
114: // Set global headers first
115: HashMap<String, String> headers = PropertiesUtils
116: .selectProperties(contextprops, HEADER);
117: if (headers != null && !headers.isEmpty()) {
118: for (Iterator<String> iter = headers.keySet().iterator(); iter
119: .hasNext();) {
120: String key = iter.next();
121: String val = headers.get(key);
122: LOG.debug("* Adding response header: " + key + " => "
123: + val);
124: doc.addResponseHeader(key, val);
125: }
126: }
127:
128: // then set page specific headers
129: headers = PropertiesUtils.selectProperties(props, HEADER);
130: if (headers != null && !headers.isEmpty()) {
131: for (Iterator<String> iter = headers.keySet().iterator(); iter
132: .hasNext();) {
133: String key = iter.next();
134: String val = headers.get(key);
135: LOG.debug("* Adding response header: " + key + " => "
136: + val);
137: doc.addResponseHeader(key, val);
138: }
139: }
140: }
141:
142: /**
143: *
144: */
145: public static boolean isDirectTrigger(Context context,
146: PfixServletRequest preq) {
147: RequestParam sdreq = preq.getRequestParam(State.SENDDATA);
148: return (!context.flowIsRunning() && (context
149: .jumpToPageIsRunning()
150: || sdreq == null || !sdreq.isTrue()));
151: }
152:
153: /**
154: *
155: */
156: public static boolean isSubmitTrigger(Context context,
157: PfixServletRequest preq) {
158: return isSubmitTriggerHelper(context, preq
159: .getRequestParam(State.SENDDATA));
160: }
161:
162: /**
163: *
164: */
165: public static boolean isSubmitAuthTrigger(Context context,
166: PfixServletRequest preq) {
167: return isSubmitTriggerHelper(context, preq
168: .getRequestParam(State.SENDAUTHDATA));
169: }
170:
171: // ============ private Helper methods ============
172:
173: /**
174: *
175: */
176: private static boolean isSubmitTriggerHelper(Context context,
177: RequestParam sdreq) {
178: return (!context.flowIsRunning()
179: && !context.finalPageIsRunning()
180: && !context.jumpToPageIsRunning() && sdreq != null && sdreq
181: .isTrue());
182: }
183: }
|