Source code: com/aendvari/satyr/servlet/gateway/tethys/MessageGateway.java
1 /*
2 * MessageGateway.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.satyr.servlet.gateway.tethys;
11
12 import java.util.Enumeration;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.StringTokenizer;
16 import java.util.Iterator;
17
18 import javax.servlet.*;
19 import javax.servlet.http.*;
20
21 import com.aendvari.common.util.UrlUtil;
22
23 import com.aendvari.hermes.broker.*;
24
25 import com.aendvari.satyr.servlet.gateway.HttpFormGateway;
26 import com.aendvari.satyr.servlet.gateway.HttpFormGateway.Processor;
27 import com.aendvari.satyr.servlet.gateway.HttpFormGateway.ContextModifier;
28 import com.aendvari.satyr.servlet.gateway.HttpFormGateway.MessageModifier;
29
30 import com.aendvari.tethys.tag.TagConstants;
31
32 /**
33 * An HTTP form POST to message gateway. This supports various tethys features
34 * for message handling and field parsing.
35 *
36 * @author Scott Milne
37 *
38 */
39
40 public class MessageGateway
41 {
42 /* Variables */
43
44
45 /** Processing extension. */
46 protected static Processor httpProcessor = new Processor();
47
48
49 /* Extensions */
50
51
52 /**
53 * Extracts tethys specific information from the HTTP POST.
54 * This is called after the messages have been extracted from the HTTP POST.
55 *
56 */
57
58 public static class Processor implements HttpFormGateway.Processor
59 {
60 /** Allows extension of this {@link HttpFormGateway.Processor} implementation. */
61 protected HttpFormGateway.Processor extension;
62
63 /**
64 * Constructs a normal processor, with no extension.
65 *
66 */
67
68 public Processor()
69 {
70 extension = null;
71 }
72
73 /**
74 * Constructs a processor with an extension.
75 *
76 * @param setExtension The {@link HttpFormGateway.Processor} extension.
77 *
78 */
79
80 public Processor(HttpFormGateway.Processor setExtension)
81
82 {
83 extension = setExtension;
84 }
85
86 /**
87 * Extracts tethys specific information from the HTTP POST.
88 *
89 * @param messages The {@link Message Message} from the HTTP POST.
90 * @param servletConfig The servlet configuration being used.
91 * @param request The servlet request we are processing.
92 * @param response The servlet response we are creating.
93 *
94 * @exception ServletException If a servlet exception occurs.
95 *
96 */
97
98 public void process(
99 HashMap messages,
100 ServletConfig servletConfig,
101 HttpServletRequest request, HttpServletResponse response)
102 throws ServletException
103 {
104 // get the parameters from the reaqest
105 Enumeration parameterNames = request.getParameterNames();
106
107 // parse normal and <copyform> fields
108
109 // for each field sent from the form submission
110 while (parameterNames.hasMoreElements())
111 {
112 String name = (String)parameterNames.nextElement();
113 String parameterValues[] = request.getParameterValues(name);
114
115 int valueIndex;
116 for (valueIndex = 0; valueIndex < parameterValues.length; valueIndex++)
117 {
118 // process "copyform" fields
119 if ( (name.indexOf(TagConstants.Fields.SatyrCopyFormFieldName) != -1) )
120 {
121 // get the name of the form
122 int index = name.indexOf( ":" );
123 String formName = name.substring( index+1 );
124
125 // get the name/value pairs from the data string
126 HashMap nameValuePairs =
127 UrlUtil.getNameValuePairs(parameterValues[valueIndex], ";", "=", true );
128
129 // go through each name/value pair and add them to all the messages
130 Iterator nameValuePairIterator = nameValuePairs.keySet().iterator();
131 while (nameValuePairIterator.hasNext())
132 {
133 String fieldName = (String)nameValuePairIterator.next();
134 String fieldValue = (String)nameValuePairs.get(fieldName);
135
136 if ( fieldName.indexOf(TagConstants.Fields.SatyrMessage) == -1 )
137 {
138 // add the form name to the front of the field name
139 fieldName = formName + "/" + fieldName;
140
141 // add this field to the http post message
142 addFieldToMessages( messages, fieldName, fieldValue );
143 }
144 }
145 }
146 // skip any <message> fields
147 else if ( name.indexOf(TagConstants.Fields.SatyrMessage) != -1 )
148 {
149 continue;
150 }
151 // process main form fields
152 else
153 {
154 // add this field to the http post message
155 addFieldToMessages( messages, name, parameterValues[valueIndex] );
156 }
157 }
158 }
159
160 // call processing extension if provided
161 if (extension != null)
162 {
163 extension.process(messages, servletConfig, request, response);
164 }
165 }
166 }
167
168 /**
169 * Adds the given field and value to all messages in the given map.
170 *
171 * @param messagesMap A HashMap of Message instances.
172 * @param fieldName The name of the field to add.
173 * @param fieldValue The value of the field to add.
174 *
175 */
176
177 static protected void addFieldToMessages( HashMap messagesMap, String fieldName, String fieldValue )
178 {
179 Iterator messagesIterator = messagesMap.keySet().iterator();
180 while (messagesIterator.hasNext())
181 {
182 Message message = (Message)messagesIterator.next();
183 message.getProperties().setString(fieldName, fieldValue);
184 }
185 }
186
187
188 /* Processing */
189
190
191 /**
192 * Processes an HTTP request.
193 *
194 * @param broker The {@link MessageBroker} for publishing.
195 * @param servletConfig The servlet configuration being used.
196 * @param request The servlet request we are processing.
197 * @param response The servlet response we are creating.
198 * @param processor A {@link HttpFormGateway.Processor} hook in. May be null.
199 * @param contextModifier A {@link HttpFormGateway.ContextModifier} hook in. May be null.
200 * @param messageModifier A {@link Message} hook in. May be null.
201 *
202 * @exception ServletException If a servlet exception occurs.
203 *
204 */
205
206 public static void process(
207 MessageBroker broker, ServletConfig servletConfig, HttpServletRequest request, HttpServletResponse response,
208 HttpFormGateway.Processor processor, HttpFormGateway.ContextModifier contextModifier, HttpFormGateway.MessageModifier messageModifier)
209 throws ServletException
210 {
211 // create new Processor to handle different extension, use previous instance if possible
212 if (httpProcessor.extension != processor)
213 {
214 httpProcessor = new Processor(processor);
215 }
216
217 // forward to normal processing
218 HttpFormGateway.process(broker, servletConfig, request, response, httpProcessor, contextModifier, messageModifier);
219 }
220 }
221