|
原始代码是这样的
- package com.palm.messagingrouter;
% K* Y- Q7 |/ E0 S
& J7 L0 Q5 }/ j, a4 s4 f) b/ `! e$ a- import com.palm.oasis.activerecord.ActiveRecord;& ~. T0 V6 ?& ] }4 c5 Y
- import com.palm.oasis.activerecord.ActiveRecordException;
/ ~, u% u7 l9 b5 U" b# b$ u4 V7 i - import com.palm.oasis.activerecord.Association.Many;
- S* [% ^& o* | - import com.palm.oasis.activerecord.Batch;
5 T5 c: B4 ~5 w - import com.palm.oasis.activerecord.SqlFind; G7 l9 h. z4 c5 k' z% N" a
- import com.palm.oasis.util.Base64;
3 N* z# a' |9 @0 S- P. T: w: ? - import com.palm.oasis.util.ServiceLogger;
1 S8 \9 }5 v/ F8 q - import com.palm.oasis.util.ZoneLogger;
( Y/ j5 }1 I: w* | - import java.io.ByteArrayOutputStream;
1 ^+ [9 z0 M& \+ W+ u8 t% a - import java.io.IOException;
! E# d( u( f! j/ g- F - import java.io.PrintStream;* m1 z) z8 A, O) a$ ^- g, c
- import java.util.Iterator; o N5 q- o$ d& }2 X. Q
- import java.util.List;
( x% c j4 \, u - import org.json.JSONException;# e U/ ]" A" Z% n
- import org.json.JSONObject;
2 U# X' A+ R" {. {* c2 \9 v- p
# ?, \8 u% K; X) [; `2 v: C- `- public class SmsMessageSegmentContainer extends MessageSegmentContainer" ?( y2 l) j( }, ^8 R- m
- implements MessagingRouterJsonConstants, MessagingRouterTilConstants! ?" ^' j c- D w8 S
- {* c7 |( Z0 G$ X# E; r, a I
- private static final long serialVersionUID = 1L;( X: L% G* S8 M- t" A
- private String completeMsgString;$ M5 k4 y) ~% G3 E0 p/ x& V+ I
- private String bodyEncoding;
+ E; |0 E9 O& H5 \( _" Z; u5 N - protected static ZoneLogger logger = MessagingRouterService.getLogger(SmsMessageSegmentContainer.class.getSimpleName());, Q) m+ B. |/ n8 b: e* O7 h
- Association.Many segments;
6 T# n" x7 V3 l2 F6 k - ) k) x" a2 E1 B+ l
- public SmsMessageSegmentContainer()
7 b" B) I% |) c3 v" T - {9 w4 @. k: a+ X; C# m5 C
- this.completeMsgString = null;# o8 c, t Y2 B F, h+ G
- this.bodyEncoding = null;, p' P1 E1 e* i4 K
- this.segments = hasMany(SmsMessageFromTil.class, 1);
9 }' P- B5 }8 `, T d/ q+ L$ z! B - }3 W* l$ o S4 C- |# f2 M) E4 |
/ b' H2 w, G2 u/ g/ t: @# \- public SmsMessageSegmentContainer(long messageId, int totalSegments, String bodyEncoding)
2 E6 i3 e9 |! A( |: c! J) H. ~ - {
: N3 R" F f5 W) j8 p; j$ n - super(System.currentTimeMillis(), messageId, totalSegments);
, Z! g4 X0 \3 i5 ? - this.completeMsgString = null;
1 o. t" v% J# y8 E* D2 l& a0 M - this.bodyEncoding = null;; M& e: `$ ]9 ] W# F
- this.segments = hasMany(SmsMessageFromTil.class, 1);
- W& ~4 V7 i6 w - this.bodyEncoding = bodyEncoding;, O* v+ B- G! m( q p i8 L! `5 z
- }
5 x9 v, z& O6 s; l4 Q
9 y% ~3 O9 i8 j# |3 `; j7 s- SmsMessageSegmentContainer(String messageId, int totalSegments, String bodyEncoding)/ s0 R, L; z( M( S
- {
9 \7 n/ D! w+ U+ _. w) m - super(System.currentTimeMillis(), Integer.parseInt(messageId), totalSegments);
# e! W8 k8 i+ ~5 |) S; [% Z - this.completeMsgString = null;9 j9 Q# S9 m0 `: I& `
- this.bodyEncoding = null;
; K. q& H+ Z: ] R% y7 o - this.segments = hasMany(SmsMessageFromTil.class, 1);
5 d4 ~" G; r, `( J* ^: J8 V* \ - this.bodyEncoding = bodyEncoding;
8 u2 p. @/ G( Z5 Z% t; u2 o! V - }) j7 W: v1 m" V& s
6 M7 G x1 i1 g7 F- public String getBodyEncoding()
9 ^2 x# [1 T; M7 V1 D# q. { - {( }3 W1 W: F7 ` q
- return this.bodyEncoding;: `2 M; u, ?1 t1 q. Y: q) q8 y& C5 u
- }
& b' Y N5 \. b) m" C - # T6 e9 s; G* s9 {0 K$ u z4 U/ T. v
- protected boolean isPrintableChar(char ch)1 b/ u0 O$ @- A5 {! ?
- {& X. N; F* J# T6 W! Z' A/ D
- boolean isItPrintableChar = false;9 E8 W0 O: `* _2 A- i
- if ((ch >= ' ') && (ch <= '~')) {( @% v& O' e" S& q, w
- isItPrintableChar = true;( n1 p3 Z' x8 ?. c8 ?
- }
6 p$ K' g6 L2 I% x+ W2 u+ H1 L( I - else if ((ch >= 128) && (ch <= 168)) {
% A( E$ w) e( {/ \/ R - isItPrintableChar = true;
8 Y" _5 R/ @& j1 G' E - }
1 Y' M( b& v( s4 o; M/ } - else if ((ch >= 224) && (ch <= 247))0 z8 m0 T9 Z. B/ X/ B( E
- isItPrintableChar = true;# W& t" U! }6 p
- return isItPrintableChar;
/ T P" \' F7 f5 G$ d! y$ g - }
: c$ s! q1 ~; V5 q5 B. u) y
4 d9 W x2 K5 A" s5 M9 b! |- public boolean isMessageComplete()
3 L5 ~% a/ k; Z4 d+ F- q - {5 @2 Q, ~9 i3 L
- if (!(this.isMessageComplete))! X! k8 W9 |2 X! [) H
- try
- Q" D9 g1 Z: S& R1 I1 b3 t4 c - {$ b# J; l N% ^5 M- r
- synchronized (SmsMessageSegmentContainer.class)5 N% U6 }$ f' V- ]+ w
- {
# }# ^) k G. `- F4 U I - List listSmsSegments = ActiveRecord.find(this.segments.sqlAll().orderBy("segmentNumber ASC"));4 j1 |3 A! G# I' q$ C6 K
- if ((listSmsSegments != null) && (listSmsSegments.size() == this.totalSegments))' j8 @, A% n" J: m8 o# q3 p
- {" ~& k6 R5 K. U% c7 X2 ?
- this.isMessageComplete = true;/ N/ c1 r8 Z* L; G2 O9 k
- logger.fine("At this point all SMS message segments have been recieved so we are going to put it all together");
" x. z7 h0 s; t/ [$ m; y& \ - StringBuilder msgString = new StringBuilder(160 * this.totalSegments);8 T% A- q# N9 n0 c7 z
- if (msgString != null)
- {1 V% y# s1 V, J* `, h: N3 h - {3 R) A, m7 H( c" k, `
- if ("bbin".equals(this.bodyEncoding))4 Y' R0 O+ V3 B
- {
& m3 o8 j) Z% q - ByteArrayOutputStream byteOut = new ByteArrayOutputStream();9 o" v! [, M+ V$ r
- Iterator i$ = listSmsSegments.iterator();
9 I v. G7 [( R6 o - % q5 ?& ^$ G0 C/ s; U: z
- while (i$.hasNext())7 c/ j; I1 ?; _0 h( B; Q, c
- {% Z0 j. D1 y9 k& w
- SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();6 O/ ^! G0 O, n( |
- if ((msg.getBody() != null) && (msg.getBody().length() != 0));2 p$ I8 _ r; E! Y9 j$ Q+ [: C
- byteOut.write(Base64.decode(msg.getBody()));: p. v' R2 N3 U! R7 V. l7 K( `
- }. k2 r r" _% W
- char[] testArray = byteOut.toString().toCharArray();* n* g5 ^- b5 l4 V
- boolean isAllCharsPrintable = true;& O3 J2 W+ ^ v2 {" O
- int i = 0;$ c. n! m; G7 r
- for (i = 0; (i < testArray.length) && (isAllCharsPrintable); ++i) {
6 Y- W+ I+ Y/ e+ W - if (!(isPrintableChar(testArray[i])))2 C! `' }. E: n2 V0 @4 d
- isAllCharsPrintable = false;. D9 S- P8 Q& @- q: g2 }
- }9 _$ H& |% T. Z
- if (isAllCharsPrintable)
3 S8 C2 y1 m7 o/ | - this.completeMsgString = byteOut.toString() + "";$ x6 Z0 S& i( n( F0 y& z
- else8 D7 h# P; u% P: z2 e% M
- this.completeMsgString = Base64.encodeBytes(byteOut.toByteArray()) + "";
9 r1 L4 Y% t7 t2 { - System.out.println(this.completeMsgString);
) p) n: j) \; J/ h( ^: K; p2 W - }
( |; n/ c1 \2 a - else {- }* O- e) M) g9 `! |
- Iterator i$ = listSmsSegments.iterator();
' B* t' A3 x @ - # U" U6 x+ r% Y! k8 z
- while (i$.hasNext())
- S% `1 W8 D- j$ |- M2 l% | - {
B5 q- K$ r0 B8 C7 P. v6 s0 @( R - SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();
5 X$ `5 P: N1 j7 `% e* \4 A1 c - if ((msg.getBody() != null) && (msg.getBody().length() != 0));& U0 {+ y% X1 G: E7 B9 {
- msgString.append(TilUtils.decodeString(msg.getBodyEncoding(), msg.getBody()));+ {* W+ [! b# J3 o; U9 \
- }$ p6 _/ z, F; ~" E2 B& N
- this.completeMsgString = msgString.toString() + "";( g& h* G7 z9 J! }, m
- }5 Z0 t7 U, a4 m: m3 y* ^$ d5 ~
- }
% g+ M& w" [- x; l - else {
( r9 @; g# b6 j: X6 r - logger.severe("Error allocating memory to put Segmented SMS Message Together");
/ a" D+ c; K8 q - }
( j6 y5 X4 ~7 k( L - }
+ _3 e% v- L1 a0 n - else if ((listSmsSegments != null) && (listSmsSegments.size() > this.totalSegments)) {5 n- D, Y/ R- [
- logger.severe("Too many SMS message segments found so assuming corrupted for " + this.segMsgContainerId + " in DB. We found:" + listSmsSegments.size() + " but we expected: " + this.totalSegments); }, J8 [" M# m( B8 O
- listSmsSegments = null;# |+ W8 ]: P! c8 H% _
- }
. K/ l. O: \0 h3 @0 i6 i - }2 P t* {& J! [6 _
- catch (ActiveRecordException e)
# R q! j1 Q8 o - {( R; e7 ^ @' V! h" J
- logger.warning("SMS Message couldn't be retrieved from the database at this time.");" w3 R6 i4 n) _' Y5 c+ Y/ J
- }
, l6 q# I& R6 x4 Z0 Z- v - catch (NullPointerException e). p- d3 g+ w" Q# V9 @! ~, C5 W
- {2 U3 ~2 v2 h4 p9 K
- logger.severe("Null Pointer Error putting Segmented SMS Message Together");
B q+ M0 i. Y" [ - }
7 U7 t% A$ J9 r1 d( O! s$ i9 ]6 D - catch (IOException e)" Z0 X, F, v9 h6 L% S* n
- {
+ }; R6 R$ @& c* x. C/ T5 y - logger.severe("Error Decoding Binary Message");
( C; f6 V2 f, Q' t; J1 S3 g - }
4 J. `5 I4 m3 H - return this.isMessageComplete;
+ F. N6 L+ W. f+ M - }
: L5 b; i8 V3 N. ?' q
- j4 C% v( u& f4 z1 I; X- public String getCompletedBody()
{) l, k! V D& R% ? - {' z8 Q2 _! |8 v2 I, m# z
- if (!(isMessageComplete()))
1 d, Q4 a( `" u+ l( n* ~: { - logger.warning("Message Not yet Complete so you should call this after testing with isMessageComplete");. w: E3 e0 L! T5 E8 P$ y! Z0 A
- return this.completeMsgString;# ^& j2 q3 G; C- L* s
- }
. M! J) v" A" F" f# Y - , }3 _' i: F% I" P {
- public void addNewSegment(SmsMessageFromTil msgSegment)
4 `. y! ?. H9 p- F, g; O - {
3 m6 y2 f) D* Y" ]3 F7 { - try
: {! T# U3 v! O7 W/ g, |) b7 e - {
0 L% P4 _0 W0 m* [* U2 K - if (logger.getLogger().isFinerLoggable)
3 B- I! I) J8 ~) e: Q% _$ q o - logger.finer("Attempting to Adding new SMS Message with MessageId:" + msgSegment.getMessageId() + " with SegmentNo:" + msgSegment.getSegmentNumber() + " of Total Segments:" + msgSegment.getTotalNumberOfSegments());# G I/ K" _1 g3 B
- synchronized (SmsMessageSegmentContainer.class)6 W3 X7 m s+ K
- {, D. ^! e/ l$ V( `# @
- int numSegmentsAlreadyStoredWithThatSegNumber = ActiveRecord.count(this.segments.sqlAll().where("segmentNumber=?", new Object[] { 1 u H; ~8 }8 ]4 V9 j4 L( Z- X
- Integer.valueOf(msgSegment.getSegmentNumber()) }));
5 {9 w& c7 `: `5 I - / i4 u+ F ^' ?7 c R' B2 @1 g
- if (numSegmentsAlreadyStoredWithThatSegNumber == 0)
& R& M4 ]8 n' l- R, I) x9 X - {
+ R& ?6 v2 u; A - logger.finer("New SMS Segmeted Accepted. Adding it to SMS Container");
5 y$ ~+ U& g: H5 _ - logger.finer("saving segment to message holder");
- h1 Q5 r2 z- z; ~ - Batch batch = new Batch();) y: M& A& \+ f+ {& [9 I' V9 @5 u
- this.segments.push(batch, msgSegment);: T% y* M9 B! g) s+ a! F
- msgSegment.save(batch);
% M3 h0 k( }% C/ T: a" } - if (0L != msgSegment.getTimeStamp())& k. V8 c9 c5 b
- setTimeStampLastReceivedSegment(msgSegment.getTimeStamp());
& g( t- [9 W. u - save(batch);5 p- ]& K/ D. S. a! V4 i. v
- batch.execute();
7 Y7 A6 V: Y% i4 | - }# k8 L R9 _+ F8 x; }3 }, m
- else {
' x3 E5 P5 i' ^+ e+ ^0 {6 R - logger.warning("Found duplicate SMS Message segment already saved");
0 @0 t4 F6 o! |. R - }
% A/ A, A- `0 e9 ^+ |" S6 k% B5 Q. Y - }7 L1 V7 ]7 F8 @! j
- }4 t0 f0 x# ~& I' v* ~: M' S* M2 T
- catch (ActiveRecordException e)
4 \9 j! D6 c# n - {% q3 A6 e6 H/ t# z' V. A. [" L
- logger.severe("Error saving SMS Message Segment. Data will probably be lost.");1 x6 e+ f+ Q" e3 I6 {! T% R
- }
4 o; t8 U4 y; x$ c# p. V9 s8 v# v, r - }
$ k! |! f G' R0 v! [2 S. x9 k
0 G* h9 O5 K9 Y2 w: S- private String convertToJson(boolean doHidePrivateData)
5 v! `! A6 c8 r/ H Q3 v& [ - {
. S- o9 L1 P! v; ~' i$ S; Y - JSONObject smsJson = new JSONObject();
0 V* l s$ r4 w* D% G - SmsMessageFromTil msgSeg = null;
5 E* I. b5 C |) C2 ` - try3 {$ ]: D# c4 B$ j
- {2 S3 }4 `$ |. s" G
- msgSeg = (SmsMessageFromTil)this.segments.findFirst();! X6 T4 r( D& a, t( j' s
- if (msgSeg != null). m, p R1 s/ i5 V# i# t* h* W0 s4 N
- {
' ]+ w' `' D9 ~1 x' R - if (doHidePrivateData)" k y( F9 o& G! O. w% [
- {7 P- Z4 O0 n8 X" q2 ^6 I" G
- smsJson.put("body", "This has been hidden for logging");6 U( w' c: L% l
- smsJson.put("address", "555-333-4444");
# c- a# g4 R8 G: V9 f - smsJson.put("callbackNumber", "555-333-4444");
/ ]! _3 B$ W0 o( \: U, [. m - }! L. x8 z- u1 y& ]
- else {3 e8 `" e8 a: @1 f8 O
- smsJson.put("body", getCompletedBody());4 w; Y+ ?5 y! a
- smsJson.put("address", msgSeg.getWhoIsItFrom());. G: `+ ^$ p* ]: n: N& N
- smsJson.put("callbackNumber", msgSeg.getCallbackNumber());
* s/ ~9 L& N* U' R - }
/ \0 j# K" ?8 n. m/ Q+ d* M - smsJson.put("networkMsgId", msgSeg.getMessageId());/ g: h O- t5 ~7 P* d; K# ]7 Q" v# b Z
- smsJson.put("deliveryReportOptions", msgSeg.getDeliveryReportOptions());" K2 O1 Q2 k; |8 V% Y
- smsJson.put("timeStamp", msgSeg.getTimeStamp());9 {# n; k4 A: G4 t- B
- if (msgSeg.isThisMessageFromAGsmNetwork())
0 i" @& e) x1 h3 g - {. w, j+ x. F5 Q5 ?# h0 U9 O x
- smsJson.put("priority", msgSeg.getSmsClassInfo());
3 g u7 P# r: v( Z0 U - if (msgSeg.isFromSim())8 {' N; U% @1 F0 p8 q( ~: [
- smsJson.put("isFromSim", true);
6 _7 l' Z, d7 Y1 c' | - }' B2 e: {5 M& o* u l" D8 J2 O
- else {1 K% ?4 g, S* M7 P; S* W s; R
- smsJson.put("priority", msgSeg.getPriority());$ w' H8 A. {, C8 G9 s4 R# u) t
- }8 c+ ^/ B: v" |( |
- smsJson.put("displayMode", msgSeg.getDisplayMode());
k0 j; i7 Q1 U+ x; o - smsJson.put("privacyMode", msgSeg.getPrivacyMode()); break label257:- Z* J# r6 ^0 U/ ?6 G: ]/ n! x8 t9 ^- g
- }- J. p" z ?' B- j3 a
- ' j3 a* _* p9 p0 D D9 J2 V+ Q
- logger.severe("Did not find Message Segment to generate JSON string");% [0 J: I6 p0 J9 A
- }2 s; Y5 L( Y0 t7 _7 K$ [- ^
- catch (ActiveRecordException e)
9 j r( Y& P4 u3 ^0 u, L5 n5 D" G - {
7 w& q' i2 p( [; S% } - logger.severe("Caught Exception", e);
% c) q* I8 i$ `# h$ H& i0 O - }% `, o- u1 ?0 w% ?) h
- catch (JSONException e)6 L* L) ~, k+ [0 f+ g* z
- {9 T' R1 i+ { Z& B
- logger.severe("Caught Exception", e);8 @) h+ t, T, c. b7 d! D7 @' c+ {* l
- }+ i. o; X% b+ }3 J& o4 @
- label257: String jsonString = smsJson.toString();
) T9 ~8 q5 Q. B0 u4 | - logger.finer("toJSONString SMS Message: " + jsonString);$ H. Z8 |' a/ u2 P1 N3 Z: U0 J
- return jsonString;6 f- g+ ~( D2 l; g6 S
- }
4 }" n B* H- K( J
* _, S& a' @) Z+ d- public String toJSONString()
+ @" W, f" h2 Y0 l2 O - {
+ w! A# A* K3 o6 T8 V/ p9 F- U - return convertToJson(false);
P0 g* X1 n' O; p3 H. u - }
+ y' @, g8 H I4 y+ M4 {* N& W - " h( X( ]5 p$ i$ ]- f% U* t4 q
- public String toDebugJSONString()
9 Z1 Y7 b" p! d. m# [6 U - {) S1 }2 f9 U9 {, ?3 _
- return convertToJson(true);, V5 b7 J$ i) f( K! c* D8 T) C" ^
- } ~2 K. v: ^. E( x9 o, _( A' ~
- }
复制代码 |
|