|
原始代码是这样的
- package com.palm.messagingrouter;
9 i4 W& l; W; P+ l' u; O% v - ' ]5 Q) p ?5 B j
- import com.palm.oasis.activerecord.ActiveRecord;: v' }/ G% V2 m4 y9 g" q) v
- import com.palm.oasis.activerecord.ActiveRecordException;
- R+ U0 Y0 \/ ^" \* o0 t) ` - import com.palm.oasis.activerecord.Association.Many;8 E1 p4 s! [! h, T2 n( \* f
- import com.palm.oasis.activerecord.Batch;
& ?# W1 K# U' C# ] - import com.palm.oasis.activerecord.SqlFind;
; Q' E+ a, x0 G+ o, h5 ] - import com.palm.oasis.util.Base64;% N6 R# H0 A8 r2 P8 Y
- import com.palm.oasis.util.ServiceLogger;* a0 k& G) s6 C. V/ ~' x: [( K9 C
- import com.palm.oasis.util.ZoneLogger;8 [9 z/ o! s" [! e
- import java.io.ByteArrayOutputStream;
" `/ A: U' K- I; y7 H( F+ ~: P - import java.io.IOException;
V: M9 z+ Q- w. J4 M* M - import java.io.PrintStream;% E {' ]2 j9 P+ r; R
- import java.util.Iterator;% A H- k* _) L& n/ T1 a, b, T6 P/ Y: Q
- import java.util.List;( V9 L( t1 W: s
- import org.json.JSONException;
/ M% K' B, I4 h5 x9 x - import org.json.JSONObject;1 D+ a/ e8 @8 r( h1 @" Y+ c
5 f) A! j7 T: B/ @- public class SmsMessageSegmentContainer extends MessageSegmentContainer q/ H l( B' k9 e! K; l C" e( y
- implements MessagingRouterJsonConstants, MessagingRouterTilConstants+ O8 f9 ~; ^: p3 b2 s
- {
[# w2 E2 G0 ]/ X3 N% l+ F - private static final long serialVersionUID = 1L;( ^. h! { P) |
- private String completeMsgString;
, F' h6 J2 x' h+ ~ - private String bodyEncoding;
: N- ]* m+ L% N; h/ |# K( N( v2 _1 S6 r - protected static ZoneLogger logger = MessagingRouterService.getLogger(SmsMessageSegmentContainer.class.getSimpleName());
8 u3 r Z$ a; t& Q - Association.Many segments;: m$ N" j' Z' `* `" S
- 2 W9 K6 @# E9 t' Y
- public SmsMessageSegmentContainer()% ]1 M" O& c# T/ x, q
- {
. s2 T, d2 o- [- a$ S - this.completeMsgString = null;! V" p* H) ^4 [' i) U, u
- this.bodyEncoding = null;
) _1 X: e* G Z: N7 B& `6 k$ }+ c - this.segments = hasMany(SmsMessageFromTil.class, 1);- u6 ~* Y8 k: ^3 {
- }8 b9 c# J9 @+ \: ?
& Q9 p3 R; f2 o8 w- public SmsMessageSegmentContainer(long messageId, int totalSegments, String bodyEncoding)! H% z: {5 ?3 e- x2 j
- {+ o+ S2 v! t# p* C; m" d
- super(System.currentTimeMillis(), messageId, totalSegments);1 t) ^. S) t! P! g
- this.completeMsgString = null;# b$ V( Q/ f: `
- this.bodyEncoding = null;
1 @9 ^ |4 Q M$ b - this.segments = hasMany(SmsMessageFromTil.class, 1);9 O# m1 g/ h6 A. k3 ]
- this.bodyEncoding = bodyEncoding;. z6 s8 ?- e1 a/ s! g
- }$ C* Y; j+ I+ U& K7 s& Q
/ p" D/ e& B* |- C- SmsMessageSegmentContainer(String messageId, int totalSegments, String bodyEncoding)+ T; G1 ~4 n7 Q+ T' t7 ?( \
- {
. N0 x+ x: V# |1 C2 s3 v9 g7 c - super(System.currentTimeMillis(), Integer.parseInt(messageId), totalSegments);6 ^+ t. y. G2 h' r% N% I* J
- this.completeMsgString = null;
. n8 a0 z+ f8 Q* t5 }, M$ l6 ~; W b - this.bodyEncoding = null;
2 E! @7 [: c( \; g! F - this.segments = hasMany(SmsMessageFromTil.class, 1);/ s5 ~- _2 |) f- H, z7 X
- this.bodyEncoding = bodyEncoding;! W2 n: m/ s6 v. _ t. z0 A
- }
4 @; O) Z9 ?# ^, ^# {1 D/ A
* j# R6 N" d4 B& {- public String getBodyEncoding()! R4 M. I' Z3 D
- {0 A4 L9 S% S1 X1 v" Q8 Y; p
- return this.bodyEncoding;
: \# f0 H7 c- I! y1 { - }
. `2 O+ N5 j8 [( p+ x - / L- n$ i4 Y$ }' N& V3 T
- protected boolean isPrintableChar(char ch)
9 P) r: G/ u! @3 B* _* E - {
8 D$ _3 t2 \) Z& n1 d% o! [ - boolean isItPrintableChar = false;( @) H$ [2 ^! O" Z
- if ((ch >= ' ') && (ch <= '~')) {
1 }; n6 y2 X+ }, g2 j - isItPrintableChar = true;) G+ }9 w5 ]: ?4 r7 ]" _3 c
- }8 r- E3 _, J. F3 W+ b3 w
- else if ((ch >= 128) && (ch <= 168)) {+ J# F7 T6 \! \( V3 M
- isItPrintableChar = true;+ L1 F' O& i. b {4 }& I; H
- }' f" c, a1 g! \* ], \
- else if ((ch >= 224) && (ch <= 247))
# X" \) s: p/ A6 I6 G1 O) J - isItPrintableChar = true;* y# Z4 I h: F5 D+ w k4 p2 O0 ?& c9 }; `
- return isItPrintableChar;
( @- l4 X6 R: q8 I% W2 A0 B- t, c - }% P4 Y4 [0 j# f& w, B
# b6 X) M e5 m- public boolean isMessageComplete()
# n8 ~$ S$ W k$ t* s r - {
6 [1 W$ P2 E" q4 g- p; n - if (!(this.isMessageComplete))
3 Y6 y! w& s! F; i! r - try/ r( A) O J: E8 |+ ?
- {- q3 n9 Q4 F8 f
- synchronized (SmsMessageSegmentContainer.class) Y; I- K7 q. w: P+ J& R% ^
- {
; p/ Z* v5 x7 D7 ~) N4 Y' V - List listSmsSegments = ActiveRecord.find(this.segments.sqlAll().orderBy("segmentNumber ASC"));7 l E+ f; M0 ` R5 Y
- if ((listSmsSegments != null) && (listSmsSegments.size() == this.totalSegments))- b) H3 V6 @6 }+ @' z
- {' s7 f9 |4 n# m
- this.isMessageComplete = true;
d( {. w! l/ A# z c/ i - logger.fine("At this point all SMS message segments have been recieved so we are going to put it all together");
9 v2 J' F, r8 D& p- Y; R! G- C6 U1 X - StringBuilder msgString = new StringBuilder(160 * this.totalSegments);
. ]8 @' T" Z" z! s* ~ K - if (msgString != null)& ?+ ]1 s& `" H$ I( w4 U5 p3 z
- {& t" q- R7 {, P2 h5 `& d' S
- if ("bbin".equals(this.bodyEncoding))& c4 z& [5 y# [# s+ b; w P
- {
3 b# w, Z5 Y# v3 p - ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
. r G3 O% ^. _0 q+ Z+ U - Iterator i$ = listSmsSegments.iterator();
% F4 m* ]( c8 T, z! ~9 u2 h, J
Q5 e# {' {" [# }- while (i$.hasNext())
/ }1 Z: o m8 S( X" `! M - {) K# T7 u* {; s, E
- SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();& S. B. J/ ~6 M& v
- if ((msg.getBody() != null) && (msg.getBody().length() != 0));
. m4 t4 {2 U7 o - byteOut.write(Base64.decode(msg.getBody()));
Z' j' E: ~, e* Z+ a6 r- b& p" c - }' u: I5 l! v9 L, ]6 [. f+ G U
- char[] testArray = byteOut.toString().toCharArray();% c. |- Z8 k% L! O, s/ j& a4 H
- boolean isAllCharsPrintable = true;
6 S% _% n- _. z. M8 H& B - int i = 0;$ T# ?2 m3 f, T
- for (i = 0; (i < testArray.length) && (isAllCharsPrintable); ++i) {
/ \5 W7 b7 v/ S5 \ - if (!(isPrintableChar(testArray[i])))+ J x5 } m7 m( Q
- isAllCharsPrintable = false;9 w1 f, Q/ \ ~5 M! d) d0 K
- }
( u8 U% H7 f7 `9 O* m1 N - if (isAllCharsPrintable)
6 {! }! m' [' w# Q! V( l& g! w# f - this.completeMsgString = byteOut.toString() + "";- N' _; [ a& J
- else8 r$ K; y9 f8 x5 `
- this.completeMsgString = Base64.encodeBytes(byteOut.toByteArray()) + "";7 ]8 g+ z6 D* X* O0 c8 [4 t
- System.out.println(this.completeMsgString);2 |9 r9 D" i5 j8 o2 r$ `0 J
- }
4 F; T3 t1 u6 t& x" b - else {
+ E3 M9 V w$ `% N& n - Iterator i$ = listSmsSegments.iterator();
5 v) W8 g& X5 B$ n0 u - G4 k! H. [) N: H4 R
- while (i$.hasNext())
6 K) N5 Q4 i) W, C9 V5 R( q5 m. e; B" \% E - {
2 F$ o* G6 E W4 a1 g - SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();
' A6 j0 X' T( S/ ]5 O$ @ - if ((msg.getBody() != null) && (msg.getBody().length() != 0));1 ?) w9 Y5 n+ j3 h) a
- msgString.append(TilUtils.decodeString(msg.getBodyEncoding(), msg.getBody()));- ?2 `& k$ Q; D8 H; K! ?
- }
& I1 t2 m) `. c9 A! |/ [6 K - this.completeMsgString = msgString.toString() + "";
4 Q" m: X; O; M% u6 M1 S( K - }
" H, O2 s5 |% W+ G6 @4 g - }
, H1 ?. u: v/ t* T b& Y9 o - else {
+ K) i" U- @0 T6 w: e - logger.severe("Error allocating memory to put Segmented SMS Message Together");8 V; Q/ a' [3 N. S
- }( S2 V0 g' }8 f+ R8 X+ d
- }$ h( V5 A& y6 K. H* i( |
- else if ((listSmsSegments != null) && (listSmsSegments.size() > this.totalSegments)) {' M+ E0 j8 f( K5 D, e* F$ H
- 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); }
- j4 ~4 @# ?7 v1 B - listSmsSegments = null;2 {5 _* `. p2 C" v2 y+ X1 N* Q
- }% K( ~" B' b% ?$ k
- }
6 X& z# K4 `' O* f; `/ ` - catch (ActiveRecordException e)4 I" B5 V0 g' |; e% R" g( Y1 W
- {2 k/ i* M! ?. u7 f4 n
- logger.warning("SMS Message couldn't be retrieved from the database at this time.");" A# L6 |$ B, h8 a% r3 F0 W0 i
- }
. j2 _, O1 @) ^7 B - catch (NullPointerException e)
$ w; x: k; o- f& u( Y - {
1 O& O& z) q- R0 S - logger.severe("Null Pointer Error putting Segmented SMS Message Together");
& n5 I" N% s* g+ \ - }- b/ z; X: V4 A- Q: q Q
- catch (IOException e). \4 b. Q( {% S( e$ `5 P' z' }
- {4 {7 b4 G) q0 h* M5 s- P5 @. c
- logger.severe("Error Decoding Binary Message");( b i p9 ?7 O0 j
- }8 ]% B9 {4 c+ j- y2 w
- return this.isMessageComplete;( M7 I; R; Z2 s! K( T5 t+ J
- }, R/ H0 J) P# y! A- O' D
- 9 M- H h9 ^' s4 D+ s0 Z
- public String getCompletedBody(); ]( B$ D8 S4 s, L0 X, F2 }
- {$ L2 Y' I# j. z; H6 W7 ]# k8 n! \
- if (!(isMessageComplete()))
4 E1 v& \7 A& o+ V - logger.warning("Message Not yet Complete so you should call this after testing with isMessageComplete");
; n* |- s2 y+ h6 k - return this.completeMsgString;( }* G2 ~) g- y7 J' c( `/ o
- }& [) I, h" {2 A4 d( o4 X
- % V6 q9 Q8 B, ?0 F' S. T
- public void addNewSegment(SmsMessageFromTil msgSegment)6 U* B9 v3 @ `
- {
" D$ Y* o0 S2 s& z& K - try, Z& _: i+ U+ W! I I" i
- {
' k! i0 t9 l9 M - if (logger.getLogger().isFinerLoggable)
$ d' A/ }! }$ ~; O - logger.finer("Attempting to Adding new SMS Message with MessageId:" + msgSegment.getMessageId() + " with SegmentNo:" + msgSegment.getSegmentNumber() + " of Total Segments:" + msgSegment.getTotalNumberOfSegments());
' k: ~0 l/ s, Z, \ a. m - synchronized (SmsMessageSegmentContainer.class)
- ~) x( K( r( J- H6 ^8 G8 v - {) e( {" U8 a: S2 @, [
- int numSegmentsAlreadyStoredWithThatSegNumber = ActiveRecord.count(this.segments.sqlAll().where("segmentNumber=?", new Object[] { $ F8 O- ~ L7 a" ~$ S2 b' q( i
- Integer.valueOf(msgSegment.getSegmentNumber()) }));* y. p2 w9 }, s( ]/ d0 I
- ( P# J/ {2 m) w
- if (numSegmentsAlreadyStoredWithThatSegNumber == 0)) ?+ P; W! b1 u2 r6 a( N; E8 s
- {7 J6 h# Q) e/ _. e4 |4 a
- logger.finer("New SMS Segmeted Accepted. Adding it to SMS Container");4 h" Z" B% e! a' ?( _
- logger.finer("saving segment to message holder");
; E0 W. J `% M, W" K - Batch batch = new Batch();. T/ @" ^8 O) }- G. ^& G
- this.segments.push(batch, msgSegment);9 O1 e5 ^* v, u0 O' c# f& {
- msgSegment.save(batch);( u8 v& S9 w, j2 c
- if (0L != msgSegment.getTimeStamp())6 a$ t! _/ p9 w
- setTimeStampLastReceivedSegment(msgSegment.getTimeStamp());
) f/ R3 I0 W% k0 x+ O3 ^ - save(batch);
5 z2 c$ s. ^% | j - batch.execute();
: o: ^/ S7 @& J2 m0 c. C, L - }
; N% h( J! ]7 e( F$ s - else {
- Y$ H8 H/ z- k' c9 K - logger.warning("Found duplicate SMS Message segment already saved");
& t" x% `1 r1 [ - }$ h+ K1 L: W+ d4 H+ W. i
- }
* ~ I' m) M4 `7 W% ^+ b8 a$ d' o - }# C8 C- \) W$ b P- v: x: w! v
- catch (ActiveRecordException e)8 _1 G( J* O- Z' n: W
- {- @4 ~! C7 H" u" G6 ~, e2 V
- logger.severe("Error saving SMS Message Segment. Data will probably be lost.");& o( y; p T* i; e
- }
, X1 T ]! X, p8 P! m - }4 r$ s, h' [' O# q3 k( }2 S" V9 A4 R
v3 ]8 y& _' g5 {2 r5 R- private String convertToJson(boolean doHidePrivateData)9 D6 i; Z% q: g* E
- {
/ ]9 Z7 d( U; g; r8 s; F - JSONObject smsJson = new JSONObject();
! B4 a& H: ?# ^/ k" A - SmsMessageFromTil msgSeg = null;/ j! I! b* Z' m; `1 q
- try
& o; i: m& N1 V9 K2 b6 d% M - {& [6 p% {. ^: M
- msgSeg = (SmsMessageFromTil)this.segments.findFirst();
% K; ]( y; @9 S4 D - if (msgSeg != null)
3 D. P' ?3 @' N( {$ G6 o - {: ^ e4 d I7 C0 @/ U
- if (doHidePrivateData)
& p9 V$ v0 N3 V" z1 Q6 _( o& B" V, \ - {
4 l$ A& D8 F/ a# Q - smsJson.put("body", "This has been hidden for logging");
2 v- E2 l: B4 L4 v$ X - smsJson.put("address", "555-333-4444");4 S4 Z1 ^. ^# f
- smsJson.put("callbackNumber", "555-333-4444");
5 d9 w' ~& Z+ d5 Y; K" }5 L - }
6 y5 |0 F' _% s6 h4 {! L o - else {
9 M5 |1 p% _; `: C - smsJson.put("body", getCompletedBody());
( H& J# q* ?6 ?) H9 a - smsJson.put("address", msgSeg.getWhoIsItFrom());$ m- n5 E0 J& k2 J4 U# ~7 Q9 M6 f1 s
- smsJson.put("callbackNumber", msgSeg.getCallbackNumber());
' o7 k( s* _- X' W1 N2 S0 i - }- f2 k" r/ t3 e& Y o
- smsJson.put("networkMsgId", msgSeg.getMessageId());
q+ K* c- }2 K5 R4 P - smsJson.put("deliveryReportOptions", msgSeg.getDeliveryReportOptions()); i& g9 ~* B* c3 p- E
- smsJson.put("timeStamp", msgSeg.getTimeStamp());0 Y* H" i. M6 K9 D" e2 v) @" \" F
- if (msgSeg.isThisMessageFromAGsmNetwork())) n4 L& ~7 z. v( P
- {. p9 K9 h6 _9 o: z; n% i" G
- smsJson.put("priority", msgSeg.getSmsClassInfo());
1 A! ]( J3 {; ?. G. K% k - if (msgSeg.isFromSim())
\5 f6 H0 I' H/ ] - smsJson.put("isFromSim", true);7 `6 W6 j5 C2 o
- }9 T' k6 n% M+ M g9 q$ D
- else {
9 N: l: [5 X: p- H( k9 D# L4 w# d3 F - smsJson.put("priority", msgSeg.getPriority());
/ v; E( W1 H% _) S5 U/ n$ O) O9 M9 ] - }% a R. G+ F0 O. u5 l. G
- smsJson.put("displayMode", msgSeg.getDisplayMode());
) @1 v3 C1 V1 r6 E - smsJson.put("privacyMode", msgSeg.getPrivacyMode()); break label257:
( A0 Z/ P9 j2 P$ [/ @ I* m - }
+ [0 ~1 c" I$ {, V0 z0 x- s - q" S X7 l& l, a$ w" w
- logger.severe("Did not find Message Segment to generate JSON string");1 G! G. \0 w7 u" l
- }9 |0 y" z5 ?+ z
- catch (ActiveRecordException e)- J; s! g! I6 L# Y
- {
2 |: y$ p$ Y6 L+ e# e2 { - logger.severe("Caught Exception", e);" G2 A# `( N. e4 o7 |2 N
- }! Z3 F( A. l* T, ^' c' c$ o0 e
- catch (JSONException e)
, p# o7 C& W% C2 a' {; p1 S - {
: }/ [0 x. b a2 j1 s% _$ V% @6 u+ c, j - logger.severe("Caught Exception", e);! Q5 B5 s5 E) D9 C( j* {3 C
- }
! F( a* Z! V* F( u6 A4 v' Q. o - label257: String jsonString = smsJson.toString();
& l$ ^/ I5 f# A, J/ q3 |! i - logger.finer("toJSONString SMS Message: " + jsonString);6 r D2 U9 p0 n' w( x$ b3 ?
- return jsonString;
# `. @+ d6 k R* u - }
- R, t3 C( c$ N! }+ q
6 ^# v" [6 y0 a# ^1 z- public String toJSONString()
& J3 \; i# [- H( _# o1 @ - {
* |- G; x7 ^! e7 l$ u! ] - return convertToJson(false);
5 A+ I' S& H" g$ V8 l% w" A# | - }
8 Y0 ]# Q+ l. x3 ~4 V) _
% ~' Y3 S, r u- public String toDebugJSONString()% c. J8 h, \! j: I# `4 K
- {
* S# @/ \! I# z% l9 ] - return convertToJson(true);
7 o) G8 ~( A" N0 f: i - }
' p, V1 e, `+ M6 D( V - }
复制代码 |
|