001: /**
002: * Chiba Request Pre Processor implementation
003: */package org.enhydra.util.chiba;
004:
005: import java.io.UnsupportedEncodingException;
006: import java.util.Map;
007:
008: import org.chiba.adapter.ChibaAdapter;
009: import org.chiba.adapter.ChibaEvent;
010: import org.chiba.adapter.DefaultChibaEventImpl;
011: import org.chiba.tools.xslt.UIGenerator;
012: import org.chiba.xml.xforms.exception.XFormsException;
013: import org.enhydra.util.RequestPreProcessor;
014: import org.enhydra.xml.io.OutputOptions;
015:
016: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
017: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
018: import com.lutris.appserver.server.httpPresentation.PageRedirectException;
019: import com.lutris.appserver.server.httpPresentation.ServerPageRedirectException;
020: import com.lutris.appserver.server.session.Session;
021: import com.lutris.logging.LogChannel;
022: import com.lutris.logging.Logger;
023: import com.lutris.util.Config;
024: import com.lutris.util.ConfigException;
025: import com.lutris.util.KeywordValueException;
026:
027: /**
028: * @author Slobodan Vujasinovic
029: *
030: */
031: public class ChibaRequestPreProcessor implements RequestPreProcessor {
032:
033: /*
034: * Pre Processor Name
035: */
036: private String name = null;
037:
038: /**
039: * LogChannel used by ResponsePostProcessor
040: */
041: private LogChannel logChannel;
042:
043: private static final String ADAPTER_CONTAINER_PARAM_NAME = "AdapterContainerParamName";
044:
045: private static final String GENERATOR_PARAM_NAME = "GeneratorParamName";
046:
047: private static final String SESSION_PARAM_NAME = "SessionParamName";
048:
049: private static final String ENCODING_PARAM_NAME = "RedirectEncoding";
050:
051: private static final String DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE = "chiba.adapter.container";
052:
053: private static final String DEFAULT_GENERATOR_PARAM_VALUE = "chiba.generator";
054:
055: private static final String DEFAULT_SESSION_PARAM_VALUE = "session";
056:
057: private static final String DEFAULT_ENCODING_PARAM_VALUE = "UTF-8";
058:
059: private String adapterContainerParam = null;
060:
061: private String generatorParam = null;
062:
063: private String sessionParam = null;
064:
065: private String encodingParam = null;
066:
067: public ChibaRequestPreProcessor() {
068: super ();
069: }
070:
071: public ChibaRequestPreProcessor(LogChannel logChannel) {
072: super ();
073: this .logChannel = logChannel;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.enhydra.util.RequestPreProcessor#configure(com.lutris.util.Config)
080: */
081: public void configure(Config config) {
082: // initialize object properties
083: // action is triggered on application startup
084: try {
085: adapterContainerParam = config.getString(
086: ADAPTER_CONTAINER_PARAM_NAME,
087: DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE);
088: generatorParam = config.getString(GENERATOR_PARAM_NAME,
089: DEFAULT_GENERATOR_PARAM_VALUE);
090: sessionParam = config.getString(SESSION_PARAM_NAME,
091: DEFAULT_SESSION_PARAM_VALUE);
092: encodingParam = config.getString(ENCODING_PARAM_NAME,
093: DEFAULT_ENCODING_PARAM_VALUE);
094: if (logChannel != null) {
095: logChannel.write(Logger.DEBUG, "Chiba Pre Processor ("
096: + name + ") - Initialized successfully!");
097: }
098: } catch (ConfigException ce) {
099: if (logChannel != null) {
100: logChannel
101: .write(
102: Logger.WARNING,
103: "Chiba Pre Processor ("
104: + name
105: + ") - Initialized with default configuration settings!",
106: ce);
107: }
108: // Applying default values
109: adapterContainerParam = DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE;
110: generatorParam = DEFAULT_GENERATOR_PARAM_VALUE;
111: sessionParam = DEFAULT_SESSION_PARAM_VALUE;
112: encodingParam = DEFAULT_ENCODING_PARAM_VALUE;
113: }
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see org.enhydra.util.RequestPreProcessor#process(com.lutris.appserver.server.httpPresentation.HttpPresentationComms)
120: */
121: public boolean process(HttpPresentationComms comms)
122: throws PageRedirectException {
123: ChibaAdapterContainer cac = null;
124: BaseAdapter adapter = null;
125: String adapterParam = null;
126: boolean clearCache = false;
127: Session session = comms.session;
128:
129: try {
130: adapterParam = comms.request
131: .getPresentationObjectRelativePath();
132:
133: if (adapterParam != null && !adapterParam.endsWith(".po")) {
134: return false;
135: }
136: try {
137: if (!"GET".equalsIgnoreCase(comms.request.getMethod())) {
138: clearCache = false;
139: } else {
140: clearCache = true;
141: }
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: clearCache = false;
145: }
146: } catch (HttpPresentationException hpe) {
147: return false;
148: }
149:
150: if (logChannel != null) {
151: logChannel.write(Logger.DEBUG, "Chiba Pre Processor ("
152: + name + ") - Processing request!");
153: }
154:
155: if (session != null && session.getSessionData() != null) {
156: try {
157: cac = (ChibaAdapterContainer) session.getSessionData()
158: .get(adapterContainerParam);
159: if (cac != null) {
160: if (clearCache) {
161: // cac.clear();
162: cac.removeChibaAdapter(adapterParam);
163: }
164: adapter = (BaseAdapter) cac
165: .getChibaAdapter(adapterParam);
166: }
167: } catch (KeywordValueException kwe) {
168: // problem while retrieving chiba adapter instance
169: kwe.printStackTrace();
170: }
171: if (adapter != null && !adapter.isClean()) {
172: if (logChannel != null) {
173: logChannel.write(Logger.DEBUG,
174: "Chiba Pre Processor (" + name
175: + ") - Chiba adapter found!");
176: }
177:
178: // Chiba Event Handling
179: ChibaEvent chibaEvent = new DefaultChibaEventImpl();
180: try {
181: comms.request.getHttpServletRequest()
182: .setCharacterEncoding(encodingParam);
183: } catch (UnsupportedEncodingException uee) {
184: // Work with default request encoding
185: }
186:
187: chibaEvent.initEvent("http-request", null, comms);
188: try {
189: if (logChannel != null) {
190: logChannel
191: .write(
192: Logger.DEBUG,
193: "Chiba Pre Processor ("
194: + name
195: + ") - Dispatching chiba event!");
196: }
197: adapter.dispatch(chibaEvent);
198: } catch (XFormsException xfe) {
199: // problem while dispatching event
200: if (logChannel != null) {
201: logChannel
202: .write(
203: Logger.WARNING,
204: "Chiba Pre Processor ("
205: + name
206: + ") - Problem while dispatching chiba event!",
207: xfe);
208: }
209: // xfe.printStackTrace();
210: }
211:
212: if (adapter
213: .getContextParam(ChibaAdapter.SUBMISSION_RESPONSE) != null) {
214: Map forwardMap = (Map) adapter
215: .removeContextParam(ChibaAdapter.SUBMISSION_RESPONSE);
216:
217: if (logChannel != null) {
218: logChannel.write(Logger.DEBUG,
219: "Chiba Pre Processor (" + name
220: + ") - Submission occured!");
221: }
222:
223: if (forwardMap
224: .containsKey(ChibaAdapter.SUBMISSION_RESPONSE_STREAM)) {
225: String response = (String) forwardMap
226: .remove(ChibaAdapter.SUBMISSION_RESPONSE_STREAM);
227: try {
228: if (logChannel != null) {
229: logChannel
230: .write(
231: Logger.DEBUG,
232: "Chiba Pre Processor ("
233: + name
234: + ") - Chiba adapter shutdown!");
235: }
236: adapter.shutdown();
237: } catch (XFormsException xfe) {
238: // Problem detected during adapter shutdown
239: }
240:
241: // try {
242: // if (logChannel != null) {
243: // logChannel
244: // .write(
245: // Logger.DEBUG,
246: // "Chiba Pre Processor ("
247: // + name
248: // + ") - Removing chiba adapter from adapter
249: // container!");
250: // }
251: // cac.removeChibaAdapter(adapterParam);
252: // session.getSessionData().remove(generatorParam);
253: // } catch (KeywordValueException kwe) {
254: // Problem occured during adaptor/generator removal
255: // from session data
256: // }
257:
258: String redirectUrl = (String) forwardMap
259: .remove(ChibaAdapter.LOAD_URI);
260:
261: ServerPageRedirectException spre = new ServerPageRedirectException(
262: redirectUrl, encodingParam);
263: // spre.addArgument(responseParam, response);
264:
265: try {
266: ChibaRequest cr = new ChibaRequest(
267: comms.request
268: .getHttpServletRequest());
269: cr.setBody(response);
270: cr.setCharacterEncoding(encodingParam);
271:
272: spre.setHttpServletRequest(cr);
273: } catch (Exception ex) {
274: ex.printStackTrace();
275: }
276:
277: if (logChannel != null) {
278: logChannel
279: .write(
280: Logger.DEBUG,
281: "Chiba Pre Processor ("
282: + name
283: + ") - Redirecting submission!");
284: }
285: try {
286: ((UIGenerator) session.getSessionData()
287: .get(generatorParam)).setParameter(
288: "VirginForm", "true");
289: } catch (Exception ex) {
290:
291: }
292:
293: throw spre;
294: }
295: }
296:
297: OutputOptions oo = new OutputOptions();
298: oo.addFreeformOption(sessionParam, session);
299: oo.setMIMEType(ChibaResponsePostProcessor.mimeType);
300: oo.setEncoding(encodingParam);
301: oo.addFreeformOption("AdapterParamName", adapterParam);
302:
303: try {
304: ((UIGenerator) session.getSessionData().get(
305: generatorParam)).setParameter("VirginForm",
306: "false");
307: } catch (Exception ex) {
308:
309: }
310:
311: try {
312: if (logChannel != null) {
313: logChannel
314: .write(
315: Logger.DEBUG,
316: "Chiba Pre Processor ("
317: + name
318: + ") - Writing current DOM object!");
319: }
320: comms.response.writeDOM(oo, adapter.getXForms());
321: } catch (XFormsException xfe) {
322: // Problem occured while retrieving Document from
323: // adapter
324: if (logChannel != null) {
325: logChannel
326: .write(
327: Logger.WARNING,
328: "Chiba Pre Processor ("
329: + name
330: + ") - Problem occured during DOM object write!",
331: xfe);
332: }
333: // xfe.printStackTrace();
334: } catch (HttpPresentationException hpe) {
335: // Problems occured during DOM object write
336: if (logChannel != null) {
337: logChannel
338: .write(
339: Logger.WARNING,
340: "Chiba Pre Processor ("
341: + name
342: + ") - Problem occured during DOM object write!",
343: hpe);
344: }
345: // hpe.printStackTrace();
346: }
347:
348: return true;
349: }
350: }
351: return false;
352: }
353:
354: /*
355: * (non-Javadoc)
356: *
357: * @see org.enhydra.util.RequestPreProcessor#setName(java.lang.String)
358: */
359: public void setName(String name) {
360: // set processor name
361: this .name = name;
362: }
363:
364: /*
365: * (non-Javadoc)
366: *
367: * @see org.enhydra.util.RequestPreProcessor#getName()
368: */
369: public String getName() {
370: // return processor name
371: return name;
372: }
373:
374: /*
375: * (non-Javadoc)
376: *
377: * @see org.enhydra.util.RequestPreProcessor#setLogChannel()
378: */
379: public void setLogChannel(LogChannel logChannel) {
380: this .logChannel = logChannel;
381: }
382:
383: /*
384: * (non-Javadoc)
385: *
386: * @see org.enhydra.util.RequestPreProcessor#getLogChannel()
387: */
388: public LogChannel getLogChannel() {
389: return logChannel;
390: }
391:
392: public Object clone() {
393: ChibaRequestPreProcessor crpp = new ChibaRequestPreProcessor(
394: this.getLogChannel());
395: crpp.adapterContainerParam = this.adapterContainerParam;
396: crpp.generatorParam = this.generatorParam;
397: crpp.sessionParam = this.sessionParam;
398: crpp.encodingParam = this.encodingParam;
399: crpp.name = this.name;
400: return crpp;
401: }
402:
403: }
|