001: package org.enhydra.util.chiba;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import org.chiba.adapter.AbstractChibaAdapter;
007: import org.chiba.adapter.ChibaEvent;
008: import org.chiba.xml.xforms.events.XFormsEvent;
009: import org.chiba.xml.xforms.events.XFormsEventFactory;
010: import org.chiba.xml.xforms.exception.XFormsException;
011: import org.w3c.dom.Element;
012: import org.w3c.dom.events.Event;
013: import org.w3c.dom.events.EventListener;
014:
015: /**
016: * integrates XForms Processor into Web-applications and handles request
017: * processing. This is the default implementation of ChibaAdapter and besides
018: * handling the interaction it also manages a UIGenerator to build the rendered
019: * output for the browser.
020: *
021: * @author joern turner
022: * @version $Id: ServletAdapter.java,v 1.2 2007-05-17 12:52:55 sinisa Exp $
023: */
024: public class ServletAdapter extends BaseAdapter implements
025: EventListener {
026:
027: public static final String HTTP_SERVLET_REQUEST = "chiba.web.request";
028: public static final String HTTP_UPLOAD_DIR = "chiba.web.uploadDir";
029:
030: public static final String USERAGENT = "chiba.useragent";
031: private HttpRequestHandler handler;
032: public static final Object XSLT_PATH = "xslt-path";
033:
034: /**
035: * Creates a new ServletAdapter object.
036: */
037: public ServletAdapter() {
038: createXFormsProcessor();
039: this .context = new HashMap();
040: chibaBean.setContext(this .context);
041: }
042:
043: /**
044: * place to put application-specific params or configurations before
045: * actually starting off the XFormsProcessor. It's the responsibility of
046: * this method to call chibaBean.init() to finish up the processor setup.
047: *
048: * @throws XFormsException If an error occurs
049: */
050: public void init() throws XFormsException {
051: this .chibaBean.init();
052: this .handler = getNewInteractionHandler();
053: setClean(false);
054: }
055:
056: /**
057: * ServletAdapter knows and executes only one ChibaEvent: 'http-request'
058: * which will contain the HttpServletRequest as contextInfo.
059: *
060: * @param event only events of type 'http-request' will be handled
061: * @throws XFormsException
062: */
063: public void dispatch(ChibaEvent event) throws XFormsException {
064: if (event.getEventName().equals("http-request")) {
065: this .handler.execute(event);
066: } else {
067: // Log.log("unknown event: '" + event.getEventName() + "' - ignoring");
068: }
069:
070: }
071:
072: /**
073: * terminates the XForms processing. right place to do cleanup of
074: * resources.
075: *
076: * @throws org.chiba.xml.xforms.exception.XFormsException
077: */
078: public void shutdown() throws XFormsException {
079: if (isClean())
080: return;
081: this .chibaBean.shutdown();
082: this .chibaBean = null;
083: setClean(true);
084: }
085:
086: /**
087: * Instructs the application environment to forward the given response.
088: *
089: * @param response a map containing at least a response stream and optional
090: * header information.
091: */
092: public void forward(Map response) {
093: this .chibaBean.getContext().put(SUBMISSION_RESPONSE, response);
094: }
095:
096: // todo: should be set by servlet
097:
098: /**
099: * return a new InteractionHandler.
100: * <p/>
101: * This method returns a new HttpRequestHandler.
102: *
103: * @return returns a new
104: */
105: protected HttpRequestHandler getNewInteractionHandler()
106: throws XFormsException {
107: return new HttpRequestHandler(this .chibaBean);
108: }
109:
110: public void setUploadDestination(String uploadDir) {
111: super .setUploadDestination(uploadDir);
112: //HttpRequestHandler uses this
113: // todo: should be a member of request handler and set directly
114: setContextParam(HTTP_UPLOAD_DIR, uploadDir);
115: }
116:
117: // event handling
118: // todo: should be moved up to base class
119:
120: /**
121: * This method is called whenever an event occurs of the type for which the
122: * <code> EventListener</code> interface was registered.
123: *
124: * @param event The <code>Event</code> contains contextual information about
125: * the event. It also contains the <code>stopPropagation</code> and
126: * <code>preventDefault</code> methods which are used in determining the
127: * event's flow and default action.
128: */
129: public void handleEvent(Event event) {
130: String type = event.getType();
131: String targetId = ((Element) event.getTarget()).getAttributeNS(
132: null, "id");
133: XFormsEvent xformsEvent = (XFormsEvent) event;
134:
135: if (XFormsEventFactory.CHIBA_LOAD_URI.equals(type)) {
136: handleLoadURI(targetId, (String) xformsEvent
137: .getContextInfo("uri"), (String) xformsEvent
138: .getContextInfo("show"));
139: return;
140: }
141: if (XFormsEventFactory.CHIBA_RENDER_MESSAGE.equals(type)) {
142: handleMessage(targetId, (String) xformsEvent
143: .getContextInfo("message"), (String) xformsEvent
144: .getContextInfo("level"));
145: return;
146: }
147: if (XFormsEventFactory.CHIBA_REPLACE_ALL.equals(type)) {
148: handleReplaceAll(targetId, (Map) xformsEvent
149: .getContextInfo("header"), xformsEvent
150: .getContextInfo("body"));
151: return;
152: }
153:
154: // unknown event ignored
155: }
156:
157: // todo: *either* move up these three methods as abstract template methods *or* use event log ?
158: public void handleLoadURI(String targetId, String uri, String show) {
159: // todo
160: }
161:
162: public void handleMessage(String targetId, String message,
163: String level) {
164: // todo
165: }
166:
167: public void handleReplaceAll(String targetId, Map header,
168: Object body) {
169: //FIXME
170:
171: /*if (getContextParam(ChibaAdapter.SUBMISSION_RESPONSE) != null) {
172: Map forwardMap = (Map) removeContextParam(ChibaAdapter.SUBMISSION_RESPONSE);
173: if (forwardMap.containsKey(ChibaAdapter.SUBMISSION_RESPONSE_STREAM)) {
174: forwardResponse(forwardMap,this. response);
175: shutdown();
176:
177: }
178: }*/
179:
180: }
181:
182: public void setBaseURI(String aURI) {
183: super .setBaseURI(aURI);
184: }
185:
186: /**
187: * passes Map containing arbitrary context parameters to the Adapter.
188: *
189: * @param contextParams Map of arbitrary params passed to the processor
190: */
191: public void setContext(Map contextParams) {
192: this.context = contextParams;
193: chibaBean.setContext(contextParams);
194: }
195:
196: }
|