|
原始代码是这样的
- package com.palm.messagingrouter;% {$ G4 R7 I0 E" E4 w: _
- : j8 v9 ^ k! h. M
- import com.palm.oasis.activerecord.ActiveRecord;
3 |8 i2 }+ t2 E+ P - import com.palm.oasis.activerecord.ActiveRecordException;! g9 ^$ w6 A" o6 T+ e8 n
- import com.palm.oasis.activerecord.Association.Many;+ L( ], y% t; W2 @0 F6 c8 \
- import com.palm.oasis.activerecord.Batch;
0 t* s, j( q$ M* Q - import com.palm.oasis.activerecord.SqlFind;0 c6 T: q! r' u/ ]* K9 t* A
- import com.palm.oasis.util.Base64;3 {# s2 |9 k- R+ R1 @* m
- import com.palm.oasis.util.ServiceLogger;
2 Y% R% ?/ O( Q" `1 l I - import com.palm.oasis.util.ZoneLogger;+ B$ }) C- z& r. |
- import java.io.ByteArrayOutputStream;
& u/ R2 f3 g# l ? - import java.io.IOException;& I/ R$ u4 K& U! L5 K" O, V0 |
- import java.io.PrintStream;
9 r( { y. d5 r1 \: E3 N - import java.util.Iterator;
! Y9 e( L: k7 x8 b - import java.util.List;
+ s8 s0 b, E+ S* J+ G8 W) I - import org.json.JSONException;
9 p/ H. u7 S9 {4 q - import org.json.JSONObject;, K8 {- }5 j6 B8 s
- ( @2 k" h# H8 D1 n
- public class SmsMessageSegmentContainer extends MessageSegmentContainer9 c1 L) X1 X! ~8 `/ K
- implements MessagingRouterJsonConstants, MessagingRouterTilConstants7 `& x/ V4 l; G4 L: t
- {
6 t/ {* E3 @" k# a, ] - private static final long serialVersionUID = 1L;1 i2 M1 e0 J& ^7 u0 ]
- private String completeMsgString;' i2 b) f; F$ e0 K d( R `1 d
- private String bodyEncoding;* i/ n/ p) x4 k# |- t8 d
- protected static ZoneLogger logger = MessagingRouterService.getLogger(SmsMessageSegmentContainer.class.getSimpleName());, e3 A# l1 H, }) w" c
- Association.Many segments;. Y1 m( I' ~5 H& R( K
# U/ Z8 n- k7 ?& w( {( ^( k8 x- public SmsMessageSegmentContainer()# D( u }+ w/ U
- {
4 L ], f- f% b- X9 n6 \' O - this.completeMsgString = null;
4 B- @* ~# m- q - this.bodyEncoding = null;
2 K; ^- l8 B* B( V/ f - this.segments = hasMany(SmsMessageFromTil.class, 1); T$ ^, C* o4 l' y% Q- ^* n
- }
/ o" `& b: w* }1 l \
7 R1 g( z7 \: t) g! ]/ O2 I& R- public SmsMessageSegmentContainer(long messageId, int totalSegments, String bodyEncoding)
, G$ B5 n1 ]* j# b) @" x - {" ?+ J- O% H. z1 O* o3 \
- super(System.currentTimeMillis(), messageId, totalSegments);
3 X& c2 J2 S+ f4 Q" f - this.completeMsgString = null;
; Y6 y' S% m, _+ s7 g; W8 ^ - this.bodyEncoding = null;& Y. s5 J6 S/ V" ~+ M
- this.segments = hasMany(SmsMessageFromTil.class, 1);1 b' n, \" `, g: j6 s1 ?
- this.bodyEncoding = bodyEncoding;" `5 w2 M0 O( p q% {! ~3 H! S
- }2 r/ c7 A; J1 p3 C- }" ~ ? `
% o+ B7 G* ], i' h- SmsMessageSegmentContainer(String messageId, int totalSegments, String bodyEncoding)
+ r$ i) D( V& @! j3 r) r - {
" O; T9 n& ~+ ~7 k/ N% @ - super(System.currentTimeMillis(), Integer.parseInt(messageId), totalSegments);
, {+ \, H+ f" F6 A+ U% p - this.completeMsgString = null;1 n3 S) x$ V, D* b
- this.bodyEncoding = null;
# K3 W3 Y8 a! m - this.segments = hasMany(SmsMessageFromTil.class, 1);8 g; [- ^ J2 ^1 [6 R4 n& e4 O" a
- this.bodyEncoding = bodyEncoding;
/ B" B! w+ U& D2 f& G - }
) X b9 g$ ^4 Y" S1 O N
" f J; P3 _5 g [- B- public String getBodyEncoding()2 Z/ ^- J4 [5 _2 g! d+ y1 n
- {% ^ u2 b5 P& |; s) l1 `- j
- return this.bodyEncoding; |- C" h/ `& F4 n+ T8 j. V
- }8 c& ]! H6 K( r6 y, J5 q3 |
- + x) r% R4 i- V) j2 F
- protected boolean isPrintableChar(char ch)
( u0 R; i, y* u& p6 q) |% j" H( b - {
' K# n: ^9 x# N. T - boolean isItPrintableChar = false;8 d& f/ X" Q; U5 U0 w8 C. _4 b
- if ((ch >= ' ') && (ch <= '~')) {, j$ R9 p. h. `- j" }( w/ O
- isItPrintableChar = true;
( t! t( ]& a9 s- |/ H" P1 j6 l" m - }
1 m# ^2 `) S" J. d1 M7 K: q - else if ((ch >= 128) && (ch <= 168)) {3 U( t; p% j4 S# t
- isItPrintableChar = true;, {# ^5 E7 h5 X$ d
- }
- e, Z3 I7 M* t/ t" d4 F/ m C - else if ((ch >= 224) && (ch <= 247))
0 h0 W/ S5 b( X& m7 ^ - isItPrintableChar = true;$ d; L- x; r! ]: u' Q2 `, X5 J
- return isItPrintableChar;; A3 x( S1 I, x9 h
- }- H1 U4 |1 C! V. Z" V
9 l4 Z7 ]4 b8 h- public boolean isMessageComplete()
1 p7 \" y$ Y/ e- ?5 d' e$ D8 Z: u - {
, U# ~4 }" B, d: m! x - if (!(this.isMessageComplete))
' g* ?, {) U' v6 ]0 h+ _- | - try
! E l" u* }! s6 n0 g - {
( q3 y0 o- D& p; H - synchronized (SmsMessageSegmentContainer.class)
+ s3 v/ v) h( }2 I) y - {3 l' V' i- u5 E& L2 ^$ P9 y* ~7 ]
- List listSmsSegments = ActiveRecord.find(this.segments.sqlAll().orderBy("segmentNumber ASC"));9 [# A- E, t4 g. l
- if ((listSmsSegments != null) && (listSmsSegments.size() == this.totalSegments))
! b m% a& M% ? - {* e6 f h: N( E2 D6 ?
- this.isMessageComplete = true;
5 d3 l2 N* I F( y; j1 m+ Y% W - logger.fine("At this point all SMS message segments have been recieved so we are going to put it all together");% T+ M- w' [8 |2 V0 ?
- StringBuilder msgString = new StringBuilder(160 * this.totalSegments);$ P) f7 W6 K4 l3 K
- if (msgString != null)9 q% M2 _" U. X+ d: A ?2 ?" c
- {
Y) x! o% L' W- k" A+ I* D - if ("bbin".equals(this.bodyEncoding))
6 A- b- p7 q. D" D - {
5 ]) f+ Q5 Z+ F - ByteArrayOutputStream byteOut = new ByteArrayOutputStream();6 F: C6 o* F: l0 O* a' `3 v" q9 d
- Iterator i$ = listSmsSegments.iterator();
$ a7 F1 C6 r! s8 Z/ ^9 i6 p
/ i1 u1 C U# b- while (i$.hasNext())
4 o* Y6 W" N. O& o# B& v" n7 T% k. p - {
+ G" k; R; G& \) G" q2 @ - SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();
]0 K7 b7 B" l: {. x9 l5 W) \( H - if ((msg.getBody() != null) && (msg.getBody().length() != 0));! Y. h) }! s8 n0 d
- byteOut.write(Base64.decode(msg.getBody()));& ~9 U- c" @7 l3 K
- }( Y1 S9 {' }6 H C$ s/ {
- char[] testArray = byteOut.toString().toCharArray();, J1 I6 {- t) d% K
- boolean isAllCharsPrintable = true;
4 K2 K* B- u: n" L' s6 T - int i = 0;
& X( Z+ R" `: _( O; K* [ - for (i = 0; (i < testArray.length) && (isAllCharsPrintable); ++i) {% v1 e* O0 g- A
- if (!(isPrintableChar(testArray[i])))
1 W' F7 u; d; E( h3 O - isAllCharsPrintable = false;
6 g& V$ X' S6 h, |, W - }
" O1 E4 B& o2 D# f5 F - if (isAllCharsPrintable)
: H: z' e& ?% H4 y3 E - this.completeMsgString = byteOut.toString() + "";
) j3 i Y2 D" b. e5 r* w - else
- e9 {0 v7 y) k x - this.completeMsgString = Base64.encodeBytes(byteOut.toByteArray()) + "";' Z' p" y: [* D+ i. x5 d F
- System.out.println(this.completeMsgString);
* Q. P3 U3 m4 I0 u - }
/ C; a' C6 h) S+ A& e& _ - else {( A; ~! P Z f* T
- Iterator i$ = listSmsSegments.iterator();
8 S/ r0 s# ]: b+ { - 2 j& W4 a: @; l
- while (i$.hasNext()) J" X* c8 f4 v& U) ]1 D
- {
& D; @9 S* F' F) e. L - SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();
0 u0 e% ?: y1 U. R- ?2 {( x2 W - if ((msg.getBody() != null) && (msg.getBody().length() != 0));
6 J6 W, y; L* ]+ i9 s1 P- A9 y - msgString.append(TilUtils.decodeString(msg.getBodyEncoding(), msg.getBody()));
7 p5 Y! }3 L1 a; h' g# k - }4 Y* b1 `; J, w
- this.completeMsgString = msgString.toString() + "";
# [# R& k7 H- h8 l0 @& B7 c - }
; {9 L- X# z. R) U - }0 ]. N9 z# P1 k" B. v
- else {* s9 [% n2 s, U) `6 q: T Y
- logger.severe("Error allocating memory to put Segmented SMS Message Together");
3 k/ j% P, z6 b# H* _0 ? r+ N - }
5 o/ v4 |* b# b8 @ r( o0 j6 g4 K - }3 {: N* o; J: M( a H9 j+ l
- else if ((listSmsSegments != null) && (listSmsSegments.size() > this.totalSegments)) {5 Y, H# S! O2 y
- 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); }
2 s( r0 ~+ X6 F! T ^% |' L - listSmsSegments = null;3 ]+ @" b; q( j4 L$ t8 X
- }$ s( `" S7 O% N4 a* O' B z% ~
- }) ?* s, A1 I2 M4 ^6 F
- catch (ActiveRecordException e)
! {: C0 D8 i8 Y+ M% s& S; J - {
( |) u; l2 v1 v% Q& |2 p* t2 D - logger.warning("SMS Message couldn't be retrieved from the database at this time.");
, ^/ n& W+ o) T" l1 l# I8 [5 r - }
8 M" X% U$ y$ ^/ c& J8 D; ~ - catch (NullPointerException e)
; ?- `( Q( u d: p8 o# N& d* p - {
& I" ]0 G& V4 a! {( E - logger.severe("Null Pointer Error putting Segmented SMS Message Together");
1 n7 Y! V' T7 n6 ?( b - }9 C6 F7 j: N9 w
- catch (IOException e)2 K' S/ P( s' Z$ c; X
- {
+ _, _3 G& ^' Z7 Y/ m2 {" g) e - logger.severe("Error Decoding Binary Message");4 v0 @# E2 b# N; f# @# S
- }3 ^, q1 P8 B) h: K: N
- return this.isMessageComplete;
+ ]% d* x+ C9 q' r - }! V0 d0 b- R" o- V% ]4 r
- 0 J: p$ ^, I# \$ }8 J
- public String getCompletedBody()
: I: |6 T/ A- G# y) ] - {
$ b4 P+ o* H, R+ _# N - if (!(isMessageComplete()))
+ X$ y- T' J" `8 `7 |' e$ ?. y* Z - logger.warning("Message Not yet Complete so you should call this after testing with isMessageComplete");7 a+ M8 r% P: e9 Q! D
- return this.completeMsgString;
5 ^' v) G2 ^7 [3 m9 B4 Q - }7 }2 S+ o8 G Z. H/ O' W
- / J7 Y) m6 g: w: W& Q; v E
- public void addNewSegment(SmsMessageFromTil msgSegment)
) G8 M9 Z5 C& c$ \' o - {
5 o& ^$ q1 @* X - try
! |: o: N: Z: f& K* W- l7 v - {" r" \3 h' R* r* O
- if (logger.getLogger().isFinerLoggable)" ^! n- o# b" \6 j# W, b: X$ w
- logger.finer("Attempting to Adding new SMS Message with MessageId:" + msgSegment.getMessageId() + " with SegmentNo:" + msgSegment.getSegmentNumber() + " of Total Segments:" + msgSegment.getTotalNumberOfSegments());# l6 T1 U, D3 T( n) J, B
- synchronized (SmsMessageSegmentContainer.class)
( u3 r4 F! c; R5 p' ]2 N - {
4 A2 ] Z9 D2 @ - int numSegmentsAlreadyStoredWithThatSegNumber = ActiveRecord.count(this.segments.sqlAll().where("segmentNumber=?", new Object[] { 4 j' ~4 ~7 U4 A1 ?
- Integer.valueOf(msgSegment.getSegmentNumber()) }));4 T8 E6 C& \& P3 x$ o
- . G2 n6 }! N G0 H: x3 j/ m7 Z% ~
- if (numSegmentsAlreadyStoredWithThatSegNumber == 0)2 R+ E! N- q1 g; [
- {
' x2 C6 s" s! Q- N! P4 s9 a - logger.finer("New SMS Segmeted Accepted. Adding it to SMS Container");, X5 T: P' Q* v9 O
- logger.finer("saving segment to message holder");% y9 u) d. b# s& m4 Y9 @6 H, @
- Batch batch = new Batch();" c" L" a) k. T! [4 p- ~/ w: A' _* D
- this.segments.push(batch, msgSegment);5 \# w2 h/ y& q' H% ]4 G6 j
- msgSegment.save(batch);
& `# X8 X( M0 }4 J @ - if (0L != msgSegment.getTimeStamp())0 T( C ^0 L7 `7 \5 N
- setTimeStampLastReceivedSegment(msgSegment.getTimeStamp());2 u1 j$ R( _5 @0 B7 q/ A9 P
- save(batch);
& K0 r; }+ r' p" v/ x( k, V) ?! | - batch.execute();( U" ~' k2 N( M: c/ k
- }7 C6 @5 z! F4 ?5 Q# C
- else {& @/ h8 e" j; d& A
- logger.warning("Found duplicate SMS Message segment already saved");; x2 Q% P& H) I9 \$ l, {
- }& z9 Q! Q c$ N2 g5 Y( _- y
- }
. x, q- E3 k# p - }+ e6 N2 g, E3 M2 D! [
- catch (ActiveRecordException e)& M- q) z' k, |: x5 Q$ i
- {& [4 }) B) n* k( L5 P8 H$ d0 l
- logger.severe("Error saving SMS Message Segment. Data will probably be lost.");# W1 a" u+ D! m2 g6 M1 ~' W2 n
- }+ I4 j, t0 L/ V" M
- }
+ s0 C+ a8 C7 C, e9 l @3 p; w* |$ n0 k
/ n" c; \% ]5 Z$ |$ P5 @7 U& d- private String convertToJson(boolean doHidePrivateData)& R2 ?. m- w* V+ c
- {# C2 a, m* E g; `# B* y
- JSONObject smsJson = new JSONObject();) u) P( g6 C+ b" G; ^5 `
- SmsMessageFromTil msgSeg = null;
6 ~: `+ B; u, b- F* ]( C - try" v: r) D7 @) p. |0 O$ k- L+ J
- {: s- g# l& t: m& d
- msgSeg = (SmsMessageFromTil)this.segments.findFirst();; r9 G- C( p L( O ^; U
- if (msgSeg != null). M& `' k- H8 ]- Y, x |% r2 r1 W
- {
% }4 B! M% g! |* d' \) R( V5 _ - if (doHidePrivateData)( U5 N. J: a9 \4 i
- {5 O- [% U5 C+ m/ Q% T0 V- P: @
- smsJson.put("body", "This has been hidden for logging");3 l d8 o! S: X; z/ l5 C; I
- smsJson.put("address", "555-333-4444");
, C! o( ]# l- {; J+ j - smsJson.put("callbackNumber", "555-333-4444");8 h4 V9 U9 ?/ V) x
- }
8 B% L/ i }9 v$ i - else {
V* c9 }9 n8 t% t6 | - smsJson.put("body", getCompletedBody());5 a9 D9 C, S4 a7 v- }, A
- smsJson.put("address", msgSeg.getWhoIsItFrom());3 z% A) h. U3 o5 ?+ v
- smsJson.put("callbackNumber", msgSeg.getCallbackNumber());: J6 G1 j- d5 a+ i: V+ R8 e
- }
+ G2 \, U$ U& f) V - smsJson.put("networkMsgId", msgSeg.getMessageId());
1 L7 G. b& r$ G/ ?& I0 B - smsJson.put("deliveryReportOptions", msgSeg.getDeliveryReportOptions());
$ n I( N& r# F+ F+ A" d& y - smsJson.put("timeStamp", msgSeg.getTimeStamp());2 z2 Y7 L% u1 \1 i6 a1 h
- if (msgSeg.isThisMessageFromAGsmNetwork())
1 E3 V' I3 T: q# Y' q - {
) {5 B( R: z' |+ z3 z1 K - smsJson.put("priority", msgSeg.getSmsClassInfo());
2 i4 V: M1 P! y: \& [$ C - if (msgSeg.isFromSim())
- ~& n6 T5 r# c' o - smsJson.put("isFromSim", true);3 h K( n/ ~/ I
- }
2 q. K/ L. T" O: R/ B; O9 J- m% g - else {
: J- {. K+ r& v8 d4 l" [ - smsJson.put("priority", msgSeg.getPriority());5 \% W' b0 k4 ^: s q
- }* O3 M. A5 B5 e6 f( D7 u% v
- smsJson.put("displayMode", msgSeg.getDisplayMode());
( R$ w* |5 ?. F9 O9 Z: X - smsJson.put("privacyMode", msgSeg.getPrivacyMode()); break label257:+ n+ [" v, F7 a1 S
- }7 r, @: N# _/ ?; W! O& A& @' W
# N4 f, l- ?! n% n1 m- logger.severe("Did not find Message Segment to generate JSON string");: u$ n: h$ u& N; |! q0 V: W# d7 N
- }4 |$ J; a! @( [% o0 G
- catch (ActiveRecordException e)
. W. [) w7 a# T - {0 s+ u4 z; v* R0 f
- logger.severe("Caught Exception", e);
* \ c& e# F: ?( U) [ - }
2 K" r) Z0 U2 f; G* e A7 D: w - catch (JSONException e)5 _4 ?: d: e; U
- {% K2 n4 P# g+ ^+ K+ x
- logger.severe("Caught Exception", e);! `+ c1 N5 ]8 ^ x
- }
, U, i9 T7 l1 c. u - label257: String jsonString = smsJson.toString();* w3 W" Z2 O1 }7 n( D" ]% y
- logger.finer("toJSONString SMS Message: " + jsonString);
) l! i, e1 f9 F) C - return jsonString;
) [+ ^ t( e5 h; m+ [: d2 X% Q - }8 r& P- V6 u2 `
! X7 [" k6 |! R: c- public String toJSONString()
) H t1 @- x% C; p - {
! r, [( {1 \0 b0 n1 Z - return convertToJson(false);0 R, E' B* i6 `, k
- }
9 {, L1 u5 J. u4 r' D - + C0 ~6 j# p5 W/ y3 l+ k. D. Y
- public String toDebugJSONString()
( J \* U% D' q* W4 a. H% Z - {% M/ E. ]; J" q; K f- k
- return convertToJson(true);
7 W1 N) k, n! q9 k( P9 t0 p - }
4 t! P' G# v' l3 W - }
复制代码 |
|