01: package org.enhydra.util.chiba;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.OutputStream;
05: import java.util.Hashtable;
06: import java.util.Map;
07:
08: import org.chiba.adapter.ChibaAdapter;
09: import org.chiba.xml.xforms.Submission;
10: import org.chiba.xml.xforms.connector.InstanceSerializer;
11: import org.chiba.xml.xforms.connector.SubmissionHandler;
12: import org.chiba.xml.xforms.connector.http.AbstractHTTPConnector;
13: import org.chiba.xml.xforms.exception.XFormsException;
14: import org.w3c.dom.Node;
15:
16: /**
17: * The HTTP submission handler serializes instance data simulating HTTP submission.
18: *
19: * @author Slobodan Vujasinovic;
20: */
21: public class HTTPSubmissionHandler extends AbstractHTTPConnector
22: implements SubmissionHandler {
23:
24: /**
25: * Serializes the specified instance data.
26: *
27: * @param submission
28: * the submission issuing the request.
29: * @param instance
30: * the instance data to be serialized and submitted.
31: * @return a map holding response stream.
32: * @throws XFormsException
33: * if any error occurred during serialization.
34: */
35: public Map submit(Submission submission, Node instance)
36: throws XFormsException {
37: try {
38: String encoding = submission.getEncoding();
39: if (submission.getEncoding() == null) {
40: encoding = getDefaultEncoding();
41: }
42:
43: ByteArrayOutputStream stream = new ByteArrayOutputStream();
44: serialize(submission, instance, stream, encoding);
45:
46: String url = getURI();
47: Map response = new Hashtable();
48: response.put(ChibaAdapter.LOAD_URI, url);
49: response.put(ChibaAdapter.SUBMISSION_RESPONSE_STREAM,
50: stream.toString(encoding));
51:
52: return response;
53: } catch (Exception e) {
54: throw new XFormsException(e);
55: }
56: }
57:
58: protected final void serialize(Submission submission,
59: Node instance, OutputStream stream, String encoding)
60: throws Exception {
61:
62: if (encoding == null) {
63: encoding = getDefaultEncoding();
64: }
65:
66: String method = submission.getMethod();
67: if (method == null) {
68: // oops ...
69: throw new XFormsException("Submission method not defined.");
70: }
71:
72: String scheme = "http";
73:
74: String mediatype = submission.getMediatype();
75: if (mediatype == null) {
76: mediatype = "application/xml";
77: }
78:
79: InstanceSerializer serializer = getSerializer(scheme, method,
80: mediatype);
81: if (serializer == null) {
82: // is exception the right way to go ?
83: throw new XFormsException(
84: "No instance serializer defined for scheme '"
85: + scheme + "', method '" + method
86: + "' and mediatype '" + mediatype + "'");
87: }
88:
89: serializer.serialize(submission, instance, stream, encoding);
90: }
91:
92: }
93:
94: // end of class
|