|
@@ -1,368 +1,369 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package InterModuleComm; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
5 |
|
- |
|
6 |
|
- |
import java.util.*; |
7 |
|
- |
|
8 |
|
- |
public final class ToMessage implements Message { |
9 |
|
- |
protected static final String PREFIX = Constants.PROTOCOL_PREFIX + ModuleMsgLifecycleForm.To + Constants.NV_SEP; |
10 |
|
- |
protected static final String FROM_PREFIX = "From" + Constants.NV_SEP; |
11 |
|
- |
protected static final String ERROR_PREFIX = "Error" + Constants.NV_SEP; |
12 |
|
- |
protected static final String TERMINATOR_PREFIX = "Terminator" + Constants.NV_SEP; |
13 |
|
- |
protected static final String ONELINE_PREFIX = "OneLine" + Constants.NV_SEP; |
14 |
|
- |
protected static final String NOLINES = "NoLines"; |
15 |
|
- |
protected static final String INITIAL_TERMINATOR = "-----"; |
16 |
|
- |
|
17 |
|
- |
private ToMsgForm mForm; |
18 |
|
- |
private String mFrom; |
19 |
|
- |
private String mTo; |
20 |
|
- |
private String mId; |
21 |
|
- |
private String mError; |
22 |
|
- |
private String mTerminatingMsgLine; |
23 |
|
- |
|
24 |
|
- |
private final String[] mMsgLines; |
25 |
|
- |
|
26 |
|
- |
protected ToMessage( ToMsgForm pForm, String pFrom, String pTo, String pId, String pError, |
27 |
|
- |
String pTerminatingMsgLine, String[] pMsgLines ) { |
28 |
|
- |
mForm = pForm; |
29 |
|
- |
mFrom = pFrom; |
30 |
|
- |
mTo = pTo; |
31 |
|
- |
mId = pId; |
32 |
|
- |
mError = pError; |
33 |
|
- |
mTerminatingMsgLine = pTerminatingMsgLine; |
34 |
|
- |
mMsgLines = pMsgLines; |
35 |
|
- |
} |
36 |
|
- |
|
37 |
|
- |
public String[] getMsgLines() { |
38 |
|
- |
return (mMsgLines != null) ? mMsgLines.clone() : null; |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
public String getMsgBody() { |
42 |
|
- |
return (mError != null) || (mMsgLines == null) || (mMsgLines.length == 0) ? null : |
43 |
|
- |
LowLevelBuildMsgBody(); |
44 |
|
- |
} |
45 |
|
- |
|
46 |
|
- |
private String LowLevelBuildMsgBody() { |
47 |
|
- |
StringBuilder sb = new StringBuilder(); |
48 |
|
- |
for ( String line : mMsgLines ) { |
49 |
|
- |
sb.append( line ).append( '\n' ); |
50 |
|
- |
} |
51 |
|
- |
sb.append( mTerminatingMsgLine ); |
52 |
|
- |
return sb.toString(); |
53 |
|
- |
} |
54 |
|
- |
|
55 |
|
- |
@Override |
56 |
|
- |
public String toString() { |
57 |
|
- |
return CombinedHeaderAndMsgBody(); |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
private String CombinedHeaderAndMsgBody() { |
61 |
|
- |
String rv = getHeader(); |
62 |
|
- |
if ( (mMsgLines != null) && (mMsgLines.length > 1) ) { |
63 |
|
- |
rv += "\n" + LowLevelBuildMsgBody(); |
64 |
|
- |
} |
65 |
|
- |
return rv; |
66 |
|
- |
} |
67 |
|
- |
|
68 |
|
- |
public String ToSendText() { |
69 |
|
- |
return CombinedHeaderAndMsgBody() + "\n"; |
70 |
|
- |
} |
71 |
|
- |
|
72 |
|
- |
public String getHeader() { |
73 |
|
- |
String rv = PREFIX + mTo + " " + FROM_PREFIX + mFrom + " " + mForm + Constants.NV_SEP + mId; |
74 |
|
- |
if ( mError != null ) { |
75 |
|
- |
rv += " " + ERROR_PREFIX + mError; |
76 |
|
- |
} else if ( (mMsgLines == null) || (mMsgLines.length == 0) ) { |
77 |
|
- |
rv += " " + NOLINES; |
78 |
|
- |
} else if ( mMsgLines.length == 1 ) { |
79 |
|
- |
rv += " " + ONELINE_PREFIX + mMsgLines[0]; |
80 |
|
- |
} else if ( !Strings.isNullOrEmpty( mTerminatingMsgLine ) ) { |
81 |
|
- |
rv += " " + TERMINATOR_PREFIX + mTerminatingMsgLine; |
82 |
|
- |
} |
83 |
|
- |
return rv; |
84 |
|
- |
} |
85 |
|
- |
|
86 |
|
- |
public ToMsgForm getForm() { |
87 |
|
- |
return mForm; |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
public String getFrom() { |
91 |
|
- |
return mFrom; |
92 |
|
- |
} |
93 |
|
- |
|
94 |
|
- |
public String getTo() { |
95 |
|
- |
return mTo; |
96 |
|
- |
} |
97 |
|
- |
|
98 |
|
- |
public String getId() { |
99 |
|
- |
return mId; |
100 |
|
- |
} |
101 |
|
- |
|
102 |
|
- |
public String getError() { |
103 |
|
- |
return mError; |
104 |
|
- |
} |
105 |
|
- |
|
106 |
|
- |
public String getTerminatingMsgLine() { |
107 |
|
- |
return mTerminatingMsgLine; |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
public static class Builder { |
111 |
|
- |
public static Builder FromHeader( String pHeader ) { |
112 |
|
- |
if ( (pHeader != null) && pHeader.startsWith( PREFIX ) ) { |
113 |
|
- |
//String zRest = Utils.trimLeadingSpaces( pHeader.substring( PREFIX.length() )); |
114 |
|
- |
RestOfMsg vRestOfMsg = |
115 |
|
- |
new RestOfMsg( Strings.trimLeadingSpaces( pHeader.substring( PREFIX.length() ) ) ); |
116 |
|
- |
String zTo = thru( ' ', vRestOfMsg ); // ref is reference. Create zRest object |
117 |
|
- |
if ( vRestOfMsg.getRest().startsWith( FROM_PREFIX ) ) { |
118 |
|
- |
vRestOfMsg.setRest( Strings.trimLeadingSpaces( |
119 |
|
- |
vRestOfMsg.getRest().substring( FROM_PREFIX.length() ) ) ); |
120 |
|
- |
String zFrom = thru( ' ', vRestOfMsg ); |
121 |
|
- |
ToMsgForm zForm = convertToMsgForm( thru( Constants.NV_SEP, vRestOfMsg ) ); |
122 |
|
- |
if ( zForm != null ) { |
123 |
|
- |
vRestOfMsg.setRest( Strings.trimLeadingSpaces( vRestOfMsg.getRest().substring( 1 ) ) ); |
124 |
|
- |
String zId = thru( ' ', vRestOfMsg ); |
125 |
|
- |
String zError = null; |
126 |
|
- |
String zTerminator = ""; |
127 |
|
- |
boolean zNoLines = false; |
128 |
|
- |
String zOneLine = null; |
129 |
|
- |
if ( vRestOfMsg.getRest().startsWith( ERROR_PREFIX ) ) { |
130 |
|
- |
zError = vRestOfMsg.getRest().substring( ERROR_PREFIX.length() ).trim(); |
131 |
|
- |
} else if ( vRestOfMsg.getRest().equals( NOLINES ) ) { |
132 |
|
- |
zNoLines = true; |
133 |
|
- |
} else if ( vRestOfMsg.getRest().startsWith( ONELINE_PREFIX ) ) { |
134 |
|
- |
zOneLine = vRestOfMsg.getRest().substring( ONELINE_PREFIX.length() ); |
135 |
|
- |
} else if ( vRestOfMsg.getRest().startsWith( TERMINATOR_PREFIX ) ) { |
136 |
|
- |
zTerminator = vRestOfMsg.getRest().substring( TERMINATOR_PREFIX.length() ).trim(); |
137 |
|
- |
} |
138 |
|
- |
Builder rv = fromParams( zForm, zFrom, zTo, zId, zError ); |
139 |
|
- |
if ( rv != null ) { |
140 |
|
- |
rv.mLinesTerminatorFromHeader = zTerminator; |
141 |
|
- |
if ( zNoLines || (zOneLine != null) ) { |
142 |
|
- |
if ( zOneLine != null ) { |
143 |
|
- |
if ( !rv.LowLevelAddMsgLine( zOneLine ) ) { |
144 |
|
- |
return null; |
145 |
|
- |
} |
146 |
|
- |
} |
147 |
|
- |
rv.mMsgLinesTerminated = true; |
148 |
|
- |
} |
149 |
|
- |
return rv; |
150 |
|
- |
} |
151 |
|
- |
} |
152 |
|
- |
} |
153 |
|
- |
} |
154 |
|
- |
return null; |
155 |
|
- |
} |
156 |
|
- |
|
157 |
|
- |
public static class RestOfMsg { |
158 |
|
- |
private String mRest; |
159 |
|
- |
|
160 |
|
- |
public RestOfMsg( String pRest ) { |
161 |
|
- |
mRest = pRest; |
162 |
|
- |
} |
163 |
|
- |
|
164 |
|
- |
public String getRest() { |
165 |
|
- |
return mRest; |
166 |
|
- |
} |
167 |
|
- |
|
168 |
|
- |
public void setRest( String pRest ) { |
169 |
|
- |
mRest = pRest; |
170 |
|
- |
} |
171 |
|
- |
} |
172 |
|
- |
|
173 |
|
- |
private static ToMsgForm convertToMsgForm( String pStr ) { |
174 |
|
- |
try { |
175 |
|
- |
return ToMsgForm.valueOf( pStr ); |
176 |
|
- |
} |
177 |
|
- |
catch ( IllegalArgumentException ex ) { |
178 |
|
- |
return null; |
179 |
|
- |
} |
180 |
|
- |
} |
181 |
|
- |
|
182 |
|
- |
//private static String thru( char pSep, ref String pLine ) |
183 |
|
- |
private static String thru( char pSep, RestOfMsg pLine ) { |
184 |
|
- |
String rv; |
185 |
|
- |
int zAt = pLine.getRest().indexOf( pSep ); |
186 |
|
- |
if ( zAt == -1 ) { |
187 |
|
- |
rv = pLine.getRest(); |
188 |
|
- |
pLine.setRest( "" ); |
189 |
|
- |
} else { |
190 |
|
- |
rv = pLine.getRest().substring( 0, zAt ); |
191 |
|
- |
pLine.setRest( Strings.trimLeadingSpaces( pLine.getRest().substring( zAt ) ) ); |
192 |
|
- |
} |
193 |
|
- |
return rv; |
194 |
|
- |
} |
195 |
|
- |
|
196 |
|
- |
private Builder( ToMsgForm pForm, String pFrom, String pTo, String pId, String pError ) { |
197 |
|
- |
mForm = pForm; |
198 |
|
- |
mFrom = pFrom; |
199 |
|
- |
mTo = pTo; |
200 |
|
- |
mId = pId; |
201 |
|
- |
mError = pError; |
202 |
|
- |
} |
203 |
|
- |
|
204 |
|
- |
private static Builder fromParams( ToMsgForm pForm, String pFrom, String pTo, String pId, |
205 |
|
- |
String pError ) { |
206 |
|
- |
pFrom = Strings.noEmpty( pFrom ); |
207 |
|
- |
pTo = Strings.noEmpty( pTo ); |
208 |
|
- |
pId = Strings.noEmpty( pId ); |
209 |
|
- |
|
210 |
|
- |
if ( Strings.isAsciiIdentifier( pFrom ) && Strings.isAsciiIdentifier( pTo ) && Strings.isNoSpaceAscii( pId ) ) { |
211 |
|
- |
return new Builder( pForm, pFrom, pTo, pId, Strings.noEmpty( pError ) ); |
212 |
|
- |
} |
213 |
|
- |
return null; |
214 |
|
- |
} |
215 |
|
- |
|
216 |
|
- |
public static Builder Notify( String pFrom, String pTo, String pId, String pError ) { |
217 |
|
- |
return fromParams( ToMsgForm.Notify, pFrom, pTo, pId, pError ); |
218 |
|
- |
} |
219 |
|
- |
|
220 |
|
- |
public static Builder Notify( String pFrom, String pTo, String pId ) { |
221 |
|
- |
return Notify( pFrom, pTo, pId, null ); |
222 |
|
- |
} |
223 |
|
- |
|
224 |
|
- |
public static Builder Request( String pFrom, String pTo, String pId ) { |
225 |
|
- |
return fromParams( ToMsgForm.Request, pFrom, pTo, pId, null ); |
226 |
|
- |
} |
227 |
|
- |
|
228 |
|
- |
public static Builder Reply( ToMessage pReplyTo, String pError ) { |
229 |
|
- |
if ( pReplyTo == null ) { |
230 |
|
- |
throw new NullPointerException( "pReplyTo" ); |
231 |
|
- |
} |
232 |
|
- |
return fromParams( ToMsgForm.Reply, pReplyTo.mTo, pReplyTo.mFrom, pReplyTo.mId, pError ); |
233 |
|
- |
} |
234 |
|
- |
|
235 |
|
- |
public static Builder Reply( ToMessage pReplyTo ) { |
236 |
|
- |
return Reply( pReplyTo, null ); |
237 |
|
- |
} |
238 |
|
- |
|
239 |
|
- |
public void AddMsgLine( String pLine ) { |
240 |
|
- |
if ( pLine == null ) { |
241 |
|
- |
throw new IllegalStateException( "May Not add a Null line" ); |
242 |
|
- |
} |
243 |
|
- |
if ( mMsgLinesTerminated ) { |
244 |
|
- |
throw new IllegalStateException( |
245 |
|
- |
"May Not add any more lines after the the Message Lines have been Terminated" ); |
246 |
|
- |
} |
247 |
|
- |
if ( mError != null ) { |
248 |
|
- |
throw new IllegalStateException( "May Not add lines to an Error'd Message" ); |
249 |
|
- |
} |
250 |
|
- |
mAnyEmptyMsgLines |= (pLine.length() == 0); |
251 |
|
- |
while ( pLine.startsWith( mLinesTerminatorEvolving ) ) { |
252 |
|
- |
mLinesTerminatorEvolving += "-"; |
253 |
|
- |
} |
254 |
|
- |
if ( pLine.equals( mLinesTerminatorFromHeader ) ) { |
255 |
|
- |
mMsgLinesTerminated = true; |
256 |
|
- |
return; |
257 |
|
- |
} |
258 |
|
- |
if ( !LowLevelAddMsgLine( pLine ) ) { |
259 |
|
- |
throw new IllegalStateException( |
260 |
|
- |
"May Not add lines with Control Characters (< ' ') to a Message" ); |
261 |
|
- |
} |
262 |
|
- |
} |
263 |
|
- |
|
264 |
|
- |
// return true if line is empty || consists of characters which are >= ' ' |
265 |
|
- |
protected boolean LowLevelAddMsgLine( String pLine ) { |
266 |
|
- |
for ( int i = 0; i < pLine.length(); i++ ) { |
267 |
|
- |
if ( pLine.charAt( i ) < ' ' ) { |
268 |
|
- |
return false; |
269 |
|
- |
} |
270 |
|
- |
} |
271 |
|
- |
mMsgLines.add( pLine ); |
272 |
|
- |
return true; |
273 |
|
- |
} |
274 |
|
- |
|
275 |
|
- |
private ToMsgForm mForm; |
276 |
|
- |
private String mFrom; |
277 |
|
- |
private String mTo; |
278 |
|
- |
private String mId; |
279 |
|
- |
private String mError; |
280 |
|
- |
|
281 |
|
- |
private String mLinesTerminatorFromHeader; |
282 |
|
- |
|
283 |
|
- |
private String mLinesTerminatorEvolving = INITIAL_TERMINATOR; |
284 |
|
- |
private boolean mAnyEmptyMsgLines; |
285 |
|
- |
|
286 |
|
- |
private boolean mMsgLinesTerminated; |
287 |
|
- |
|
288 |
|
- |
private final List<String> mMsgLines = new ArrayList<String>(); |
289 |
|
- |
|
290 |
|
- |
public List<String> getMsgLines() { |
291 |
|
- |
return mMsgLines; |
292 |
|
- |
} |
293 |
|
- |
|
294 |
|
- |
public ToMessage ToMsg() { |
295 |
|
- |
String zTerminator = null; |
296 |
|
- |
String[] zMsgLines = null; |
297 |
|
- |
if ( mError == null ) { |
298 |
|
- |
zMsgLines = mMsgLines.toArray( new String[mMsgLines.size()] ); |
299 |
|
- |
mMsgLinesTerminated = true; |
300 |
|
- |
if ( null == (zTerminator = mLinesTerminatorFromHeader) ) // Not Header Terminated |
301 |
|
- |
{ |
302 |
|
- |
zTerminator = mAnyEmptyMsgLines ? mLinesTerminatorEvolving : ""; |
303 |
|
- |
} |
304 |
|
- |
} |
305 |
|
- |
return new ToMessage( mForm, mFrom, mTo, mId, mError, zTerminator, zMsgLines ); |
306 |
|
- |
} |
307 |
|
- |
|
308 |
|
- |
public boolean ExpectsMoreLines() { |
309 |
|
- |
return ((mError == null) && !mMsgLinesTerminated); |
310 |
|
- |
} |
311 |
|
- |
|
312 |
|
- |
public ToMsgForm getForm() { |
313 |
|
- |
return mForm; |
314 |
|
- |
} |
315 |
|
- |
|
316 |
|
- |
public void setForm( ToMsgForm pForm ) { |
317 |
|
- |
mForm = pForm; |
318 |
|
- |
} |
319 |
|
- |
|
320 |
|
- |
public String getFrom() { |
321 |
|
- |
return mFrom; |
322 |
|
- |
} |
323 |
|
- |
|
324 |
|
- |
public void setFrom( String pFrom ) { |
325 |
|
- |
mFrom = pFrom; |
326 |
|
- |
} |
327 |
|
- |
|
328 |
|
- |
public String getTo() { |
329 |
|
- |
return mTo; |
330 |
|
- |
} |
331 |
|
- |
|
332 |
|
- |
public void setTo( String pTo ) { |
333 |
|
- |
mTo = pTo; |
334 |
|
- |
} |
335 |
|
- |
|
336 |
|
- |
public String getId() { |
337 |
|
- |
return mId; |
338 |
|
- |
} |
339 |
|
- |
|
340 |
|
- |
public void setId( String pId ) { |
341 |
|
- |
mId = pId; |
342 |
|
- |
} |
343 |
|
- |
|
344 |
|
- |
public String getError() { |
345 |
|
- |
return mError; |
346 |
|
- |
} |
347 |
|
- |
|
348 |
|
- |
public void setError( String pError ) { |
349 |
|
- |
mError = pError; |
350 |
|
- |
} |
351 |
|
- |
|
352 |
|
- |
public String getLinesTerminatorFromHeader() { |
353 |
|
- |
return mLinesTerminatorFromHeader; |
354 |
|
- |
} |
355 |
|
- |
|
356 |
|
- |
public void setLinesTerminatorFromHeader( String pLinesTerminatorFromHeader ) { |
357 |
|
- |
mLinesTerminatorFromHeader = pLinesTerminatorFromHeader; |
358 |
|
- |
} |
359 |
|
- |
|
360 |
|
- |
public boolean isMsgLinesTerminated() { |
361 |
|
- |
return mMsgLinesTerminated; |
362 |
|
- |
} |
363 |
|
- |
|
364 |
|
- |
public void setMsgLinesTerminated( boolean pMsgLinesTerminated ) { |
365 |
|
- |
mMsgLinesTerminated = pMsgLinesTerminated; |
366 |
|
- |
} |
367 |
|
- |
} |
368 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package InterModuleComm; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
|
6 |
+ |
|
|
7 |
+ |
import java.util.*; |
|
8 |
+ |
|
|
9 |
+ |
public final class ToMessage implements Message { |
|
10 |
+ |
protected static final String PREFIX = Constants.PROTOCOL_PREFIX + ModuleMsgLifecycleForm.To + Constants.NV_SEP; |
|
11 |
+ |
protected static final String FROM_PREFIX = "From" + Constants.NV_SEP; |
|
12 |
+ |
protected static final String ERROR_PREFIX = "Error" + Constants.NV_SEP; |
|
13 |
+ |
protected static final String TERMINATOR_PREFIX = "Terminator" + Constants.NV_SEP; |
|
14 |
+ |
protected static final String ONELINE_PREFIX = "OneLine" + Constants.NV_SEP; |
|
15 |
+ |
protected static final String NOLINES = "NoLines"; |
|
16 |
+ |
protected static final String INITIAL_TERMINATOR = "-----"; |
|
17 |
+ |
|
|
18 |
+ |
private ToMsgForm mForm; |
|
19 |
+ |
private String mFrom; |
|
20 |
+ |
private String mTo; |
|
21 |
+ |
private String mId; |
|
22 |
+ |
private String mError; |
|
23 |
+ |
private String mTerminatingMsgLine; |
|
24 |
+ |
|
|
25 |
+ |
private final String[] mMsgLines; |
|
26 |
+ |
|
|
27 |
+ |
protected ToMessage( ToMsgForm pForm, String pFrom, String pTo, String pId, String pError, |
|
28 |
+ |
String pTerminatingMsgLine, String[] pMsgLines ) { |
|
29 |
+ |
mForm = pForm; |
|
30 |
+ |
mFrom = pFrom; |
|
31 |
+ |
mTo = pTo; |
|
32 |
+ |
mId = pId; |
|
33 |
+ |
mError = pError; |
|
34 |
+ |
mTerminatingMsgLine = pTerminatingMsgLine; |
|
35 |
+ |
mMsgLines = pMsgLines; |
|
36 |
+ |
} |
|
37 |
+ |
|
|
38 |
+ |
public String[] getMsgLines() { |
|
39 |
+ |
return (mMsgLines != null) ? mMsgLines.clone() : null; |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
public String getMsgBody() { |
|
43 |
+ |
return (mError != null) || (mMsgLines == null) || (mMsgLines.length == 0) ? null : |
|
44 |
+ |
LowLevelBuildMsgBody(); |
|
45 |
+ |
} |
|
46 |
+ |
|
|
47 |
+ |
private String LowLevelBuildMsgBody() { |
|
48 |
+ |
StringBuilder sb = new StringBuilder(); |
|
49 |
+ |
for ( String line : mMsgLines ) { |
|
50 |
+ |
sb.append( line ).append( '\n' ); |
|
51 |
+ |
} |
|
52 |
+ |
sb.append( mTerminatingMsgLine ); |
|
53 |
+ |
return sb.toString(); |
|
54 |
+ |
} |
|
55 |
+ |
|
|
56 |
+ |
@Override |
|
57 |
+ |
public String toString() { |
|
58 |
+ |
return CombinedHeaderAndMsgBody(); |
|
59 |
+ |
} |
|
60 |
+ |
|
|
61 |
+ |
private String CombinedHeaderAndMsgBody() { |
|
62 |
+ |
String rv = getHeader(); |
|
63 |
+ |
if ( (mMsgLines != null) && (mMsgLines.length > 1) ) { |
|
64 |
+ |
rv += "\n" + LowLevelBuildMsgBody(); |
|
65 |
+ |
} |
|
66 |
+ |
return rv; |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
public String ToSendText() { |
|
70 |
+ |
return CombinedHeaderAndMsgBody() + "\n"; |
|
71 |
+ |
} |
|
72 |
+ |
|
|
73 |
+ |
public String getHeader() { |
|
74 |
+ |
String rv = PREFIX + mTo + " " + FROM_PREFIX + mFrom + " " + mForm + Constants.NV_SEP + mId; |
|
75 |
+ |
if ( mError != null ) { |
|
76 |
+ |
rv += " " + ERROR_PREFIX + mError; |
|
77 |
+ |
} else if ( (mMsgLines == null) || (mMsgLines.length == 0) ) { |
|
78 |
+ |
rv += " " + NOLINES; |
|
79 |
+ |
} else if ( mMsgLines.length == 1 ) { |
|
80 |
+ |
rv += " " + ONELINE_PREFIX + mMsgLines[0]; |
|
81 |
+ |
} else if ( !Currently.isNullOrEmpty( mTerminatingMsgLine ) ) { |
|
82 |
+ |
rv += " " + TERMINATOR_PREFIX + mTerminatingMsgLine; |
|
83 |
+ |
} |
|
84 |
+ |
return rv; |
|
85 |
+ |
} |
|
86 |
+ |
|
|
87 |
+ |
public ToMsgForm getForm() { |
|
88 |
+ |
return mForm; |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
public String getFrom() { |
|
92 |
+ |
return mFrom; |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
public String getTo() { |
|
96 |
+ |
return mTo; |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
public String getId() { |
|
100 |
+ |
return mId; |
|
101 |
+ |
} |
|
102 |
+ |
|
|
103 |
+ |
public String getError() { |
|
104 |
+ |
return mError; |
|
105 |
+ |
} |
|
106 |
+ |
|
|
107 |
+ |
public String getTerminatingMsgLine() { |
|
108 |
+ |
return mTerminatingMsgLine; |
|
109 |
+ |
} |
|
110 |
+ |
|
|
111 |
+ |
public static class Builder { |
|
112 |
+ |
public static Builder FromHeader( String pHeader ) { |
|
113 |
+ |
if ( (pHeader != null) && pHeader.startsWith( PREFIX ) ) { |
|
114 |
+ |
//String zRest = Utils.trimLeadingSpaces( pHeader.substring( PREFIX.length() )); |
|
115 |
+ |
RestOfMsg vRestOfMsg = |
|
116 |
+ |
new RestOfMsg( Strings.trimLeadingSpaces( pHeader.substring( PREFIX.length() ) ) ); |
|
117 |
+ |
String zTo = thru( ' ', vRestOfMsg ); // ref is reference. Create zRest object |
|
118 |
+ |
if ( vRestOfMsg.getRest().startsWith( FROM_PREFIX ) ) { |
|
119 |
+ |
vRestOfMsg.setRest( Strings.trimLeadingSpaces( |
|
120 |
+ |
vRestOfMsg.getRest().substring( FROM_PREFIX.length() ) ) ); |
|
121 |
+ |
String zFrom = thru( ' ', vRestOfMsg ); |
|
122 |
+ |
ToMsgForm zForm = convertToMsgForm( thru( Constants.NV_SEP, vRestOfMsg ) ); |
|
123 |
+ |
if ( zForm != null ) { |
|
124 |
+ |
vRestOfMsg.setRest( Strings.trimLeadingSpaces( vRestOfMsg.getRest().substring( 1 ) ) ); |
|
125 |
+ |
String zId = thru( ' ', vRestOfMsg ); |
|
126 |
+ |
String zError = null; |
|
127 |
+ |
String zTerminator = ""; |
|
128 |
+ |
boolean zNoLines = false; |
|
129 |
+ |
String zOneLine = null; |
|
130 |
+ |
if ( vRestOfMsg.getRest().startsWith( ERROR_PREFIX ) ) { |
|
131 |
+ |
zError = vRestOfMsg.getRest().substring( ERROR_PREFIX.length() ).trim(); |
|
132 |
+ |
} else if ( vRestOfMsg.getRest().equals( NOLINES ) ) { |
|
133 |
+ |
zNoLines = true; |
|
134 |
+ |
} else if ( vRestOfMsg.getRest().startsWith( ONELINE_PREFIX ) ) { |
|
135 |
+ |
zOneLine = vRestOfMsg.getRest().substring( ONELINE_PREFIX.length() ); |
|
136 |
+ |
} else if ( vRestOfMsg.getRest().startsWith( TERMINATOR_PREFIX ) ) { |
|
137 |
+ |
zTerminator = vRestOfMsg.getRest().substring( TERMINATOR_PREFIX.length() ).trim(); |
|
138 |
+ |
} |
|
139 |
+ |
Builder rv = fromParams( zForm, zFrom, zTo, zId, zError ); |
|
140 |
+ |
if ( rv != null ) { |
|
141 |
+ |
rv.mLinesTerminatorFromHeader = zTerminator; |
|
142 |
+ |
if ( zNoLines || (zOneLine != null) ) { |
|
143 |
+ |
if ( zOneLine != null ) { |
|
144 |
+ |
if ( !rv.LowLevelAddMsgLine( zOneLine ) ) { |
|
145 |
+ |
return null; |
|
146 |
+ |
} |
|
147 |
+ |
} |
|
148 |
+ |
rv.mMsgLinesTerminated = true; |
|
149 |
+ |
} |
|
150 |
+ |
return rv; |
|
151 |
+ |
} |
|
152 |
+ |
} |
|
153 |
+ |
} |
|
154 |
+ |
} |
|
155 |
+ |
return null; |
|
156 |
+ |
} |
|
157 |
+ |
|
|
158 |
+ |
public static class RestOfMsg { |
|
159 |
+ |
private String mRest; |
|
160 |
+ |
|
|
161 |
+ |
public RestOfMsg( String pRest ) { |
|
162 |
+ |
mRest = pRest; |
|
163 |
+ |
} |
|
164 |
+ |
|
|
165 |
+ |
public String getRest() { |
|
166 |
+ |
return mRest; |
|
167 |
+ |
} |
|
168 |
+ |
|
|
169 |
+ |
public void setRest( String pRest ) { |
|
170 |
+ |
mRest = pRest; |
|
171 |
+ |
} |
|
172 |
+ |
} |
|
173 |
+ |
|
|
174 |
+ |
private static ToMsgForm convertToMsgForm( String pStr ) { |
|
175 |
+ |
try { |
|
176 |
+ |
return ToMsgForm.valueOf( pStr ); |
|
177 |
+ |
} |
|
178 |
+ |
catch ( IllegalArgumentException ex ) { |
|
179 |
+ |
return null; |
|
180 |
+ |
} |
|
181 |
+ |
} |
|
182 |
+ |
|
|
183 |
+ |
//private static String thru( char pSep, ref String pLine ) |
|
184 |
+ |
private static String thru( char pSep, RestOfMsg pLine ) { |
|
185 |
+ |
String rv; |
|
186 |
+ |
int zAt = pLine.getRest().indexOf( pSep ); |
|
187 |
+ |
if ( zAt == -1 ) { |
|
188 |
+ |
rv = pLine.getRest(); |
|
189 |
+ |
pLine.setRest( "" ); |
|
190 |
+ |
} else { |
|
191 |
+ |
rv = pLine.getRest().substring( 0, zAt ); |
|
192 |
+ |
pLine.setRest( Strings.trimLeadingSpaces( pLine.getRest().substring( zAt ) ) ); |
|
193 |
+ |
} |
|
194 |
+ |
return rv; |
|
195 |
+ |
} |
|
196 |
+ |
|
|
197 |
+ |
private Builder( ToMsgForm pForm, String pFrom, String pTo, String pId, String pError ) { |
|
198 |
+ |
mForm = pForm; |
|
199 |
+ |
mFrom = pFrom; |
|
200 |
+ |
mTo = pTo; |
|
201 |
+ |
mId = pId; |
|
202 |
+ |
mError = pError; |
|
203 |
+ |
} |
|
204 |
+ |
|
|
205 |
+ |
private static Builder fromParams( ToMsgForm pForm, String pFrom, String pTo, String pId, |
|
206 |
+ |
String pError ) { |
|
207 |
+ |
pFrom = ConstrainTo.significantOrNull( pFrom ); |
|
208 |
+ |
pTo = ConstrainTo.significantOrNull( pTo ); |
|
209 |
+ |
pId = ConstrainTo.significantOrNull( pId ); |
|
210 |
+ |
|
|
211 |
+ |
if ( Strings.isAsciiIdentifier( pFrom ) && Strings.isAsciiIdentifier( pTo ) && Strings.isNoSpaceAscii( pId ) ) { |
|
212 |
+ |
return new Builder( pForm, pFrom, pTo, pId, ConstrainTo.significantOrNull( pError ) ); |
|
213 |
+ |
} |
|
214 |
+ |
return null; |
|
215 |
+ |
} |
|
216 |
+ |
|
|
217 |
+ |
public static Builder Notify( String pFrom, String pTo, String pId, String pError ) { |
|
218 |
+ |
return fromParams( ToMsgForm.Notify, pFrom, pTo, pId, pError ); |
|
219 |
+ |
} |
|
220 |
+ |
|
|
221 |
+ |
public static Builder Notify( String pFrom, String pTo, String pId ) { |
|
222 |
+ |
return Notify( pFrom, pTo, pId, null ); |
|
223 |
+ |
} |
|
224 |
+ |
|
|
225 |
+ |
public static Builder Request( String pFrom, String pTo, String pId ) { |
|
226 |
+ |
return fromParams( ToMsgForm.Request, pFrom, pTo, pId, null ); |
|
227 |
+ |
} |
|
228 |
+ |
|
|
229 |
+ |
public static Builder Reply( ToMessage pReplyTo, String pError ) { |
|
230 |
+ |
if ( pReplyTo == null ) { |
|
231 |
+ |
throw new NullPointerException( "pReplyTo" ); |
|
232 |
+ |
} |
|
233 |
+ |
return fromParams( ToMsgForm.Reply, pReplyTo.mTo, pReplyTo.mFrom, pReplyTo.mId, pError ); |
|
234 |
+ |
} |
|
235 |
+ |
|
|
236 |
+ |
public static Builder Reply( ToMessage pReplyTo ) { |
|
237 |
+ |
return Reply( pReplyTo, null ); |
|
238 |
+ |
} |
|
239 |
+ |
|
|
240 |
+ |
public void AddMsgLine( String pLine ) { |
|
241 |
+ |
if ( pLine == null ) { |
|
242 |
+ |
throw new IllegalStateException( "May Not add a Null line" ); |
|
243 |
+ |
} |
|
244 |
+ |
if ( mMsgLinesTerminated ) { |
|
245 |
+ |
throw new IllegalStateException( |
|
246 |
+ |
"May Not add any more lines after the the Message Lines have been Terminated" ); |
|
247 |
+ |
} |
|
248 |
+ |
if ( mError != null ) { |
|
249 |
+ |
throw new IllegalStateException( "May Not add lines to an Error'd Message" ); |
|
250 |
+ |
} |
|
251 |
+ |
mAnyEmptyMsgLines |= (pLine.length() == 0); |
|
252 |
+ |
while ( pLine.startsWith( mLinesTerminatorEvolving ) ) { |
|
253 |
+ |
mLinesTerminatorEvolving += "-"; |
|
254 |
+ |
} |
|
255 |
+ |
if ( pLine.equals( mLinesTerminatorFromHeader ) ) { |
|
256 |
+ |
mMsgLinesTerminated = true; |
|
257 |
+ |
return; |
|
258 |
+ |
} |
|
259 |
+ |
if ( !LowLevelAddMsgLine( pLine ) ) { |
|
260 |
+ |
throw new IllegalStateException( |
|
261 |
+ |
"May Not add lines with Control Characters (< ' ') to a Message" ); |
|
262 |
+ |
} |
|
263 |
+ |
} |
|
264 |
+ |
|
|
265 |
+ |
// return true if line is empty || consists of characters which are >= ' ' |
|
266 |
+ |
protected boolean LowLevelAddMsgLine( String pLine ) { |
|
267 |
+ |
for ( int i = 0; i < pLine.length(); i++ ) { |
|
268 |
+ |
if ( pLine.charAt( i ) < ' ' ) { |
|
269 |
+ |
return false; |
|
270 |
+ |
} |
|
271 |
+ |
} |
|
272 |
+ |
mMsgLines.add( pLine ); |
|
273 |
+ |
return true; |
|
274 |
+ |
} |
|
275 |
+ |
|
|
276 |
+ |
private ToMsgForm mForm; |
|
277 |
+ |
private String mFrom; |
|
278 |
+ |
private String mTo; |
|
279 |
+ |
private String mId; |
|
280 |
+ |
private String mError; |
|
281 |
+ |
|
|
282 |
+ |
private String mLinesTerminatorFromHeader; |
|
283 |
+ |
|
|
284 |
+ |
private String mLinesTerminatorEvolving = INITIAL_TERMINATOR; |
|
285 |
+ |
private boolean mAnyEmptyMsgLines; |
|
286 |
+ |
|
|
287 |
+ |
private boolean mMsgLinesTerminated; |
|
288 |
+ |
|
|
289 |
+ |
private final List<String> mMsgLines = new ArrayList<String>(); |
|
290 |
+ |
|
|
291 |
+ |
public List<String> getMsgLines() { |
|
292 |
+ |
return mMsgLines; |
|
293 |
+ |
} |
|
294 |
+ |
|
|
295 |
+ |
public ToMessage ToMsg() { |
|
296 |
+ |
String zTerminator = null; |
|
297 |
+ |
String[] zMsgLines = null; |
|
298 |
+ |
if ( mError == null ) { |
|
299 |
+ |
zMsgLines = mMsgLines.toArray( new String[mMsgLines.size()] ); |
|
300 |
+ |
mMsgLinesTerminated = true; |
|
301 |
+ |
if ( null == (zTerminator = mLinesTerminatorFromHeader) ) // Not Header Terminated |
|
302 |
+ |
{ |
|
303 |
+ |
zTerminator = mAnyEmptyMsgLines ? mLinesTerminatorEvolving : ""; |
|
304 |
+ |
} |
|
305 |
+ |
} |
|
306 |
+ |
return new ToMessage( mForm, mFrom, mTo, mId, mError, zTerminator, zMsgLines ); |
|
307 |
+ |
} |
|
308 |
+ |
|
|
309 |
+ |
public boolean ExpectsMoreLines() { |
|
310 |
+ |
return ((mError == null) && !mMsgLinesTerminated); |
|
311 |
+ |
} |
|
312 |
+ |
|
|
313 |
+ |
public ToMsgForm getForm() { |
|
314 |
+ |
return mForm; |
|
315 |
+ |
} |
|
316 |
+ |
|
|
317 |
+ |
public void setForm( ToMsgForm pForm ) { |
|
318 |
+ |
mForm = pForm; |
|
319 |
+ |
} |
|
320 |
+ |
|
|
321 |
+ |
public String getFrom() { |
|
322 |
+ |
return mFrom; |
|
323 |
+ |
} |
|
324 |
+ |
|
|
325 |
+ |
public void setFrom( String pFrom ) { |
|
326 |
+ |
mFrom = pFrom; |
|
327 |
+ |
} |
|
328 |
+ |
|
|
329 |
+ |
public String getTo() { |
|
330 |
+ |
return mTo; |
|
331 |
+ |
} |
|
332 |
+ |
|
|
333 |
+ |
public void setTo( String pTo ) { |
|
334 |
+ |
mTo = pTo; |
|
335 |
+ |
} |
|
336 |
+ |
|
|
337 |
+ |
public String getId() { |
|
338 |
+ |
return mId; |
|
339 |
+ |
} |
|
340 |
+ |
|
|
341 |
+ |
public void setId( String pId ) { |
|
342 |
+ |
mId = pId; |
|
343 |
+ |
} |
|
344 |
+ |
|
|
345 |
+ |
public String getError() { |
|
346 |
+ |
return mError; |
|
347 |
+ |
} |
|
348 |
+ |
|
|
349 |
+ |
public void setError( String pError ) { |
|
350 |
+ |
mError = pError; |
|
351 |
+ |
} |
|
352 |
+ |
|
|
353 |
+ |
public String getLinesTerminatorFromHeader() { |
|
354 |
+ |
return mLinesTerminatorFromHeader; |
|
355 |
+ |
} |
|
356 |
+ |
|
|
357 |
+ |
public void setLinesTerminatorFromHeader( String pLinesTerminatorFromHeader ) { |
|
358 |
+ |
mLinesTerminatorFromHeader = pLinesTerminatorFromHeader; |
|
359 |
+ |
} |
|
360 |
+ |
|
|
361 |
+ |
public boolean isMsgLinesTerminated() { |
|
362 |
+ |
return mMsgLinesTerminated; |
|
363 |
+ |
} |
|
364 |
+ |
|
|
365 |
+ |
public void setMsgLinesTerminated( boolean pMsgLinesTerminated ) { |
|
366 |
+ |
mMsgLinesTerminated = pMsgLinesTerminated; |
|
367 |
+ |
} |
|
368 |
+ |
} |
|
369 |
+ |
} |