001: /**
002: *
003: */package org.enhydra.util.chiba;
004:
005: import java.io.File;
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.parsers.ParserConfigurationException;
011:
012: import org.chiba.adapter.ChibaAdapter;
013: import org.chiba.tools.xslt.UIGenerator;
014: import org.chiba.tools.xslt.XSLTGenerator;
015: import org.chiba.xml.xforms.config.XFormsConfigException;
016: import org.chiba.xml.xforms.exception.XFormsException;
017: import org.enhydra.util.ResponsePostProcessor;
018: import org.enhydra.xml.io.OutputOptions;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Node;
021:
022: import com.lutris.appserver.server.session.Session;
023: import com.lutris.logging.LogChannel;
024: import com.lutris.logging.Logger;
025: import com.lutris.util.Config;
026: import com.lutris.util.ConfigException;
027: import com.lutris.util.KeywordValueException;
028:
029: /**
030: * @author slobodan
031: */
032: public class ChibaResponsePostProcessor implements
033: ResponsePostProcessor {
034:
035: /*
036: * Name of the post processor
037: */
038: private String name = null;
039:
040: /**
041: * LogChannel used by ResponsePostProcessor
042: */
043: private LogChannel logChannel;
044:
045: /*
046: * Name of the post processor supported mime type
047: */
048: public static final String mimeType = "text/xhtml+xforms";
049:
050: /*
051: * Name of the post processor generated mime type
052: */
053: public static final String outMimeType = "text/html";
054:
055: // Chiba Adaptor attributes
056: private static final String ADAPTER_CONTAINER_PARAM_NAME = "AdapterContainerParamName";
057:
058: private static final String ADAPTER_PARAM_NAME = "AdapterParamName";
059:
060: private static final String GENERATOR_PARAM_NAME = "GeneratorParamName";
061:
062: private static final String SESSION_PARAM_NAME = "SessionParamName";
063:
064: private static final String ENCODING_PARAM_NAME = "ResponseEncoding";
065:
066: private static final String CONFIG_PARAM_NAME = "ConfigPath";
067:
068: private static final String UPLOAD_PARAM_NAME = "UploadPath";
069:
070: private static final String DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE = "chiba.adapter.container";
071:
072: private static final String DEFAULT_ADAPTER_PARAM_VALUE = "chiba.adapter";
073:
074: private static final String DEFAULT_GENERATOR_PARAM_VALUE = "chiba.generator";
075:
076: private static final String DEFAULT_SESSION_PARAM_VALUE = "session";
077:
078: private static final String DEFAULT_ENCODING_PARAM_VALUE = "UTF-8";
079:
080: private static final String DEFAULT_CONFIG_PARAM_VALUE = "default.xml";
081:
082: private static final String DEFAULT_UPLOAD_PARAM_VALUE = "";
083:
084: private String adapterContainerParam = null;
085:
086: private String adapterParam = null;
087:
088: private String generatorParam = null;
089:
090: private String sessionParam = null;
091:
092: private String encodingParam = null;
093:
094: private String configPath = null;
095:
096: private String uploadPath = null;
097:
098: // Chiba Generator attributes
099: private static final String STYLES_DIR_PARAM_NAME = "StylesDir";
100:
101: private static final String STYLES_FILE_PARAM_NAME = "StylesFile";
102:
103: private static final String DEBUG_PARAM_NAME = "Debug";
104:
105: private static final String CSS_FILE_PARAM_NAME = "CssFile";
106:
107: private static final String USER_AGENT_PARAM_NAME = "UserAgent";
108:
109: private static final String SCRIPTED_PARAM_NAME = "Scripted";
110:
111: private static final String CONTEXT_PATH_PARAM_NAME = "ContextPath";
112:
113: private static final String DEFAULT_STYLES_DIR_PARAM_VALUE = null;
114:
115: private static final String DEFAULT_STYLES_FILE_PARAM_VALUE = null;
116:
117: private static final String DEFAULT_DEBUG_PARAM_VALUE = "false";
118:
119: private static final String DEFAULT_CSS_FILE_PARAM_VALUE = null;
120:
121: private static final String DEFAULT_USER_AGENT_PARAM_VALUE = null;
122:
123: private static final String DEFAULT_SCRIPTED_PARAM_VALUE = "false";
124:
125: private static final String DEFAULT_CONTEXT_PATH_PARAM_VALUE = ".";
126:
127: private String stylesDir = null;
128:
129: private String stylesFile = null;
130:
131: private String debug = null;
132:
133: private String cssFile = null;
134:
135: private String userAgent = null;
136:
137: private String scripted = null;
138:
139: private String contextPath = null;
140:
141: public ChibaResponsePostProcessor() {
142: super ();
143: }
144:
145: public ChibaResponsePostProcessor(LogChannel logChannel) {
146: super ();
147: this .logChannel = logChannel;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.enhydra.util.ResponsePostProcessor#configure(com.lutris.util.Config)
154: */
155: public void configure(Config config) {
156: // do some property initialization
157: // action is triggered on application startup
158: try {
159: adapterContainerParam = config.getString(
160: ADAPTER_CONTAINER_PARAM_NAME,
161: DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE);
162: adapterParam = config.getString(ADAPTER_PARAM_NAME,
163: DEFAULT_ADAPTER_PARAM_VALUE);
164: generatorParam = config.getString(GENERATOR_PARAM_NAME,
165: DEFAULT_GENERATOR_PARAM_VALUE);
166: sessionParam = config.getString(SESSION_PARAM_NAME,
167: DEFAULT_SESSION_PARAM_VALUE);
168: encodingParam = config.getString(ENCODING_PARAM_NAME,
169: DEFAULT_ENCODING_PARAM_VALUE);
170: // configPath = config.getString(CONFIG_PARAM_NAME,
171: // DEFAULT_CONFIG_PARAM_VALUE);
172: configPath = getAbsolutePath(CONFIG_PARAM_NAME, config,
173: DEFAULT_CONFIG_PARAM_VALUE, false);
174: // uploadPath = config.getString(UPLOAD_PARAM_NAME,
175: // DEFAULT_UPLOAD_PARAM_VALUE);
176: uploadPath = getAbsolutePath(UPLOAD_PARAM_NAME, config,
177: DEFAULT_UPLOAD_PARAM_VALUE, true);
178:
179: // stylesDir = config.getString(STYLES_DIR_PARAM_NAME,
180: // DEFAULT_STYLES_DIR_PARAM_VALUE);
181: stylesDir = getAbsolutePath(STYLES_DIR_PARAM_NAME, config,
182: DEFAULT_STYLES_DIR_PARAM_VALUE, true);
183: // stylesFile = config.getString(STYLES_FILE_PARAM_NAME,
184: // DEFAULT_STYLES_FILE_PARAM_VALUE);
185: stylesFile = getAbsolutePath(STYLES_FILE_PARAM_NAME,
186: config, DEFAULT_STYLES_FILE_PARAM_VALUE, false);
187: debug = config.getString(DEBUG_PARAM_NAME,
188: DEFAULT_DEBUG_PARAM_VALUE);
189: // cssFile = config.getString(CSS_FILE_PARAM_NAME,
190: // DEFAULT_CSS_FILE_PARAM_VALUE);
191: cssFile = getAbsolutePath(CSS_FILE_PARAM_NAME, config,
192: DEFAULT_CSS_FILE_PARAM_VALUE, false);
193: userAgent = config.getString(USER_AGENT_PARAM_NAME,
194: DEFAULT_USER_AGENT_PARAM_VALUE);
195: scripted = config.getString(SCRIPTED_PARAM_NAME,
196: DEFAULT_SCRIPTED_PARAM_VALUE);
197: contextPath = config.getString(CONTEXT_PATH_PARAM_NAME,
198: DEFAULT_CONTEXT_PATH_PARAM_VALUE);
199:
200: if (logChannel != null) {
201: logChannel.write(Logger.DEBUG, "Chiba Post Processor ("
202: + name + ") - Initialized successfully!");
203: }
204: } catch (ConfigException ce) {
205: if (logChannel != null) {
206: logChannel
207: .write(
208: Logger.WARNING,
209: "Chiba Post Processor ("
210: + name
211: + ") - Initialized with default configuration settings!",
212: ce);
213: }
214:
215: // Applying default values
216: adapterContainerParam = DEFAULT_ADAPTER_CONTAINER_PARAM_VALUE;
217: adapterParam = DEFAULT_ADAPTER_PARAM_VALUE;
218: generatorParam = DEFAULT_GENERATOR_PARAM_VALUE;
219: sessionParam = DEFAULT_SESSION_PARAM_VALUE;
220: encodingParam = DEFAULT_ENCODING_PARAM_VALUE;
221: configPath = DEFAULT_CONFIG_PARAM_VALUE;
222: uploadPath = DEFAULT_UPLOAD_PARAM_VALUE;
223:
224: stylesDir = DEFAULT_STYLES_DIR_PARAM_VALUE;
225: stylesFile = DEFAULT_STYLES_FILE_PARAM_VALUE;
226: debug = DEFAULT_DEBUG_PARAM_VALUE;
227: cssFile = DEFAULT_CSS_FILE_PARAM_VALUE;
228: userAgent = DEFAULT_USER_AGENT_PARAM_VALUE;
229: scripted = DEFAULT_SCRIPTED_PARAM_VALUE;
230: contextPath = DEFAULT_CONTEXT_PATH_PARAM_VALUE;
231: }
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see org.enhydra.util.ResponsePostProcessor#process(java.util.Map,
238: * org.w3c.dom.Node)
239: */
240: public Node process(OutputOptions oo, Node document) {
241: try {
242: if (logChannel != null) {
243: logChannel.write(Logger.DEBUG, "Chiba Post Processor ("
244: + name + ") - Processing parameters (if any)!");
245: }
246:
247: Session session = (Session) oo
248: .getFreeformOption(sessionParam);
249:
250: ChibaAdapterContainer cac = null;
251: BaseAdapter adapter = null;
252: UIGenerator uiGenerator = null;
253:
254: // Chiba configuration path setting
255: if (oo.getFreeformOption(ADAPTER_PARAM_NAME) != null)
256: adapterParam = (String) oo
257: .getFreeformOption(ADAPTER_PARAM_NAME);
258:
259: if (oo.getFreeformOption(GENERATOR_PARAM_NAME) != null)
260: generatorParam = (String) oo
261: .getFreeformOption(GENERATOR_PARAM_NAME);
262:
263: if (logChannel != null) {
264: logChannel.write(Logger.DEBUG, "Chiba Post Processor ("
265: + name + ") - Processing response!");
266: }
267:
268: try {
269: cac = (ChibaAdapterContainer) session.getSessionData()
270: .get(adapterContainerParam);
271: if (cac != null) {
272: adapter = (BaseAdapter) cac
273: .getChibaAdapter(adapterParam);
274: }
275: uiGenerator = (UIGenerator) session.getSessionData()
276: .get(generatorParam);
277: } catch (KeywordValueException kwe) {
278: if (logChannel != null) {
279: logChannel
280: .write(
281: Logger.WARNING,
282: "Chiba Post Processor ("
283: + name
284: + ") - Problem occured while getting chiba adapter and generator instances from session!",
285: kwe);
286: }
287: // kwe.printStackTrace();
288: }
289:
290: // ChibaAdapter initialization
291: if (adapter == null || adapter.isClean()) {
292:
293: if (logChannel != null) {
294: logChannel
295: .write(
296: Logger.DEBUG,
297: "Chiba Post Processor ("
298: + name
299: + ") - Chiba adapter not found - Creating one!");
300: }
301:
302: if (adapter == null) {
303: if (!"true".equalsIgnoreCase(scripted)) {
304: adapter = new ServletAdapter();
305:
306: } else {
307: adapter = new ScriptAdapter();
308: }
309:
310: // Chiba configuration path setting
311: if (oo.getFreeformOption(CONFIG_PARAM_NAME) != null)
312: configPath = (String) oo
313: .getFreeformOption(CONFIG_PARAM_NAME);
314: adapter.setConfigPath(configPath);
315:
316: try {
317: if (cac == null) {
318: cac = new ChibaAdapterContainer();
319: }
320:
321: if (cac.putChibaAdapter(adapterParam, adapter))
322: session.getSessionData().set(
323: adapterContainerParam, cac);
324: } catch (Exception ex) {
325: if (logChannel != null) {
326: logChannel
327: .write(
328: Logger.WARNING,
329: "Chiba Post Processor ("
330: + name
331: + ") - Problem occured while setting chiba adapter session instance!",
332: ex);
333: }
334: // ex.printStackTrace();
335: }
336: } else {
337: adapter.createXFormsProcessor();
338: adapter.setContext(new HashMap());
339: }
340:
341: // Chiba upload path setting
342: if (oo.getFreeformOption(UPLOAD_PARAM_NAME) != null)
343: uploadPath = (String) oo
344: .getFreeformOption(UPLOAD_PARAM_NAME);
345: adapter.setUploadDestination(uploadPath);
346:
347: // base URI setting (not used any more)
348: adapter.setBaseURI("");
349:
350: adapter.setXForms(document);
351:
352: Map servletMap = new HashMap();
353: servletMap.put(ChibaAdapter.SESSION_ID, session
354: .getSessionKey());
355: adapter.setContextParam(
356: ChibaAdapter.SUBMISSION_RESPONSE, servletMap);
357:
358: adapter.init();
359:
360: document = adapter.getXForms();
361:
362: }
363:
364: // XSLT Generator initialization
365: if (uiGenerator == null) {
366:
367: if (logChannel != null) {
368: logChannel
369: .write(
370: Logger.DEBUG,
371: "Chiba Post Processor ("
372: + name
373: + ") - XSL Generator not found - Creating one!");
374: }
375:
376: if (oo.getFreeformOption(STYLES_DIR_PARAM_NAME) != null)
377: stylesDir = (String) oo
378: .getFreeformOption(STYLES_DIR_PARAM_NAME);
379: StylesheetLoader stylesheetLoader = new StylesheetLoader(
380: stylesDir);
381:
382: if (oo.getFreeformOption(STYLES_FILE_PARAM_NAME) != null)
383: stylesFile = (String) oo
384: .getFreeformOption(STYLES_FILE_PARAM_NAME);
385:
386: if (stylesFile != null) {
387: stylesheetLoader.setStylesheetFile(stylesFile);
388: }
389:
390: uiGenerator = new XSLTGenerator(stylesheetLoader);
391:
392: if (oo.getFreeformOption(DEBUG_PARAM_NAME) != null)
393: debug = (String) oo
394: .getFreeformOption(DEBUG_PARAM_NAME);
395: uiGenerator.setParameter("debug-enabled", debug);
396:
397: if (oo.getFreeformOption(CSS_FILE_PARAM_NAME) != null)
398: cssFile = (String) oo
399: .getFreeformOption(CSS_FILE_PARAM_NAME);
400: uiGenerator.setParameter("css-file", cssFile);
401:
402: if (oo.getFreeformOption(USER_AGENT_PARAM_NAME) != null)
403: userAgent = (String) oo
404: .getFreeformOption(USER_AGENT_PARAM_NAME);
405: uiGenerator.setParameter("user-agent", userAgent);
406:
407: if (oo.getFreeformOption(SCRIPTED_PARAM_NAME) != null)
408: scripted = (String) oo
409: .getFreeformOption(SCRIPTED_PARAM_NAME);
410: if ("true".equalsIgnoreCase(scripted))
411: uiGenerator.setParameter("scripted", "true");
412:
413: if (oo.getFreeformOption(CONTEXT_PATH_PARAM_NAME) != null)
414: contextPath = (String) oo
415: .getFreeformOption(CONTEXT_PATH_PARAM_NAME);
416: uiGenerator.setParameter("contextroot", contextPath);
417:
418: uiGenerator.setParameter("action-url", "");
419:
420: uiGenerator
421: .setParameter(
422: "selector-prefix",
423: (String) org.chiba.xml.xforms.config.Config
424: .getInstance()
425: .getProperty(
426: HttpRequestHandler.SELECTOR_PREFIX_PROPERTY,
427: HttpRequestHandler.SELECTOR_PREFIX_DEFAULT));
428: uiGenerator
429: .setParameter(
430: "remove-upload-prefix",
431: (String) org.chiba.xml.xforms.config.Config
432: .getInstance()
433: .getProperty(
434: HttpRequestHandler.REMOVE_UPLOAD_PREFIX_PROPERTY,
435: HttpRequestHandler.REMOVE_UPLOAD_PREFIX_DEFAULT));
436: uiGenerator.setParameter("data-prefix",
437: (String) org.chiba.xml.xforms.config.Config
438: .getInstance().getProperty(
439: "chiba.web.dataPrefix"));
440: uiGenerator.setParameter("trigger-prefix",
441: (String) org.chiba.xml.xforms.config.Config
442: .getInstance().getProperty(
443: "chiba.web.triggerPrefix"));
444:
445: try {
446: session.getSessionData().set(generatorParam,
447: uiGenerator);
448: } catch (KeywordValueException kwe) {
449: if (logChannel != null) {
450: logChannel
451: .write(
452: Logger.WARNING,
453: "Chiba Post Processor ("
454: + name
455: + ") - Problem occured while setting generator session instance!",
456: kwe);
457: }
458: // kwe.printStackTrace();
459: }
460: }
461:
462: // set additional transformation parameters
463: Object value = null;
464:
465: try {
466: value = session.getSessionData().get(
467: "xform.embedingNode");
468: } catch (KeywordValueException e) {
469: value = null;
470: }
471:
472: if (oo.getFreeformOption("xform.embedingNode") != null)
473: value = oo.getFreeformOption("xform.embedingNode");
474:
475: uiGenerator.setParameter("embedingNode", value);
476: // end
477:
478: uiGenerator.setInputNode(document);
479:
480: Document doc = null;
481: try {
482: doc = DocumentBuilderFactory.newInstance()
483: .newDocumentBuilder().newDocument();
484: } catch (ParserConfigurationException pce) {
485: pce.printStackTrace();
486: }
487:
488: if (logChannel != null) {
489: logChannel.write(Logger.DEBUG, "Chiba Post Processor ("
490: + name + ") - Generating output!");
491: }
492:
493: uiGenerator.setOutput(doc);
494: uiGenerator.generate();
495:
496: oo.setEnableXHTMLCompatibility(true);
497: oo.setUseAposEntity(false);
498:
499: oo.setMIMEType("text/html");
500:
501: uiGenerator.setParameter("VirginForm", "true");
502:
503: return doc;
504: } catch (XFormsConfigException xfce) {
505: xfce.printStackTrace();
506: } catch (XFormsException xfe) {
507: xfe.printStackTrace();
508: }
509: // Failed to additionally process this document.
510: // Therefore, we chose to return original one!
511: return document;
512:
513: }
514:
515: /*
516: * (non-Javadoc)
517: *
518: * @see org.enhydra.util.ResponsePostProcessor#process(byte [],
519: * java.lang.String, java.lang.String)
520: */
521: public byte[] process(byte[] buteArray, String mimeEncoding,
522: String mimeType) {
523: return buteArray;
524: }
525:
526: /*
527: * (non-Javadoc)
528: *
529: * @see org.enhydra.util.ResponsePostProcessor#setName(java.lang.String)
530: */
531: public void setName(String name) {
532: this .name = name;
533: }
534:
535: /*
536: * (non-Javadoc)
537: *
538: * @see org.enhydra.util.ResponsePostProcessor#getName()
539: */
540: public String getName() {
541: return name;
542: }
543:
544: /*
545: * (non-Javadoc)
546: *
547: * @see org.enhydra.util.RequestPreProcessor#setLogChannel()
548: */
549: public void setLogChannel(LogChannel logChannel) {
550: this .logChannel = logChannel;
551: }
552:
553: /*
554: * (non-Javadoc)
555: *
556: * @see org.enhydra.util.RequestPreProcessor#getLogChannel()
557: */
558: public LogChannel getLogChannel() {
559: return logChannel;
560: }
561:
562: /*
563: * (non-Javadoc)
564: *
565: * @see org.enhydra.util.ResponsePostProcessor#shouldProcess(java.lang.String)
566: */
567: public boolean shouldProcess(String mimeType) {
568: if (mimeType != null
569: && mimeType
570: .startsWith(ChibaResponsePostProcessor.mimeType))
571: return true;
572: return false;
573: }
574:
575: /*
576: * (non-Javadoc)
577: *
578: * @see org.enhydra.util.ResponsePostProcessor#getOutputMimeType()
579: */
580: public String getOutputMimeType() {
581: return this .outMimeType;
582: }
583:
584: /**
585: * Method returns absolute path to file (directory)!
586: *
587: * @param Property
588: * Name
589: * @param Configuration
590: * Object
591: * @param Default
592: * Property Value
593: * @param If
594: * This Is Directory
595: * @return Absolute File Path
596: */
597: private String getAbsolutePath(String propName, Config config,
598: String defaultValue, boolean isDirectory) {
599: String absoultePath = null;
600: try {
601: absoultePath = config.getString(propName, defaultValue);
602:
603: if (absoultePath != null) {
604: File temp = new File(absoultePath);
605:
606: if ((!temp.isDirectory() && isDirectory)
607: || !temp.exists()) {
608: temp = new File(config.getConfigFile().getFile()
609: .getParentFile().getAbsolutePath()
610: + File.separator + absoultePath);
611:
612: if ((!temp.isDirectory() && isDirectory)
613: || !temp.exists()) {
614: if (logChannel != null)
615: logChannel
616: .write(
617: Logger.WARNING,
618: propName
619: + " application parameter isn't properly initialized!");
620:
621: } else {
622: try {
623: absoultePath = temp.getCanonicalPath();
624: } catch (Exception e) {
625: }
626: }
627: } else {
628: try {
629: absoultePath = temp.getCanonicalPath();
630: } catch (Exception e) {
631: }
632: }
633: }
634: } catch (ConfigException e) {
635: if (logChannel != null) {
636: logChannel
637: .write(
638: Logger.WARNING,
639: propName
640: + " application parameter isn't properly initialized!");
641:
642: }
643: }
644:
645: return absoultePath;
646: }
647:
648: public Object clone() {
649: ChibaResponsePostProcessor crpp = new ChibaResponsePostProcessor(
650: this.getLogChannel());
651:
652: crpp.adapterContainerParam = this.adapterContainerParam;
653: crpp.adapterParam = this.adapterParam;
654: crpp.generatorParam = this.generatorParam;
655: crpp.sessionParam = this.sessionParam;
656: crpp.encodingParam = this.encodingParam;
657: crpp.configPath = this.configPath;
658: crpp.uploadPath = this.uploadPath;
659:
660: crpp.stylesDir = this.stylesDir;
661: crpp.stylesFile = this.stylesFile;
662: crpp.debug = this.debug;
663: crpp.cssFile = this.cssFile;
664: crpp.userAgent = this.userAgent;
665: crpp.scripted = this.scripted;
666: crpp.contextPath = this.contextPath;
667: crpp.name = this.name;
668:
669: return crpp;
670: }
671:
672: }
|