|
原始代码是这样的
- package com.palm.messagingrouter;
$ e4 n* ]* L9 w8 O. _' y4 { - 9 ^, z$ Y7 x( g# {! E
- import com.palm.oasis.activerecord.ActiveRecord;( ^* j, ~2 r# V9 u* X/ o# P
- import com.palm.oasis.activerecord.ActiveRecordException;
6 V& X2 H5 k" l' I - import com.palm.oasis.activerecord.Association.Many;+ a& R* H, M' |) l2 ~
- import com.palm.oasis.activerecord.Batch;
& ?! y* U% @+ N - import com.palm.oasis.activerecord.SqlFind;
1 s! z0 |7 Y8 e1 \: _ - import com.palm.oasis.util.Base64;. R' W$ C1 Y% b
- import com.palm.oasis.util.ServiceLogger;
9 o# D! m' a6 ` - import com.palm.oasis.util.ZoneLogger;9 F) `/ z! _9 a3 F
- import java.io.ByteArrayOutputStream;
( f4 D3 u% f" y8 P" y - import java.io.IOException;8 x9 z N* ~# B" c
- import java.io.PrintStream;
}" V+ f2 z3 S3 X5 S - import java.util.Iterator;
* K- P3 o4 b0 \1 m; n& P# b8 C - import java.util.List;4 V; p) Z' R3 n) K
- import org.json.JSONException;
" P5 y/ E( {* e& m6 a8 N - import org.json.JSONObject;4 _+ h4 \" d7 U' W
! F( t& w4 |& U5 c* x- public class SmsMessageSegmentContainer extends MessageSegmentContainer
5 X! a9 j& D( N5 r* M - implements MessagingRouterJsonConstants, MessagingRouterTilConstants
^( M+ v6 S. s+ d7 D$ z - {
; ^& C( R: y3 i: K/ g - private static final long serialVersionUID = 1L;
& s" C) ~' _8 L6 |3 } - private String completeMsgString;
3 K5 S& ~. S2 J. R* G' Q; ]1 W - private String bodyEncoding;) V0 m' K* B* h e
- protected static ZoneLogger logger = MessagingRouterService.getLogger(SmsMessageSegmentContainer.class.getSimpleName());
/ B$ Y7 w7 a& g7 Q6 V3 F4 N - Association.Many segments;
, y1 ?) o# z- v* R
- a+ j N+ M0 n2 r- public SmsMessageSegmentContainer()
) r+ z2 f& f4 X" r" \6 {4 X - {* B2 z7 m+ w8 v G! C
- this.completeMsgString = null;% U$ M; t9 B4 O" q
- this.bodyEncoding = null;
# O, N+ r) M" u! {3 M2 ? - this.segments = hasMany(SmsMessageFromTil.class, 1);
: V. r4 Q& m) K; C5 |* \+ b - }$ k B1 {% m l4 }6 @' R) c9 A0 F
; k8 z: ?( d) e4 t" h# G- public SmsMessageSegmentContainer(long messageId, int totalSegments, String bodyEncoding)
3 ~: `7 N/ H9 ]9 H" [5 G - {' f( J% q! }( m/ B+ s; P8 V& L
- super(System.currentTimeMillis(), messageId, totalSegments);; q# J- E; b& S3 w/ Q: W! R' ]- P
- this.completeMsgString = null;6 {1 _2 @7 d( `$ x4 |
- this.bodyEncoding = null;( e3 A. Z& X' J" ?6 X
- this.segments = hasMany(SmsMessageFromTil.class, 1);& M, D" _$ H3 {; b
- this.bodyEncoding = bodyEncoding;6 n/ q8 o) ? x
- }
8 O: @+ t9 I5 j: U1 A; g - 8 ~* x/ x3 ?8 w5 q* H
- SmsMessageSegmentContainer(String messageId, int totalSegments, String bodyEncoding)
$ R7 c/ i1 ]. W- ^0 F* ? - {
3 G6 N( b" w5 Y7 Z* v6 C - super(System.currentTimeMillis(), Integer.parseInt(messageId), totalSegments);" E8 t8 A6 ^1 H F0 N. o
- this.completeMsgString = null;: p+ n+ U2 m- b% M1 K
- this.bodyEncoding = null;% |: y: D) f1 ?2 F# H% v0 E% u
- this.segments = hasMany(SmsMessageFromTil.class, 1);8 l5 ~, Z5 q* B5 z
- this.bodyEncoding = bodyEncoding;4 j2 P, M' N& }8 w% v( A, m
- }, z% g( G) ]2 I- T- }; G* _
% D4 ?! v8 q" t/ p! T% X- public String getBodyEncoding()
) O8 k1 K; p( \5 w% C& L6 g* B - {
8 W& {3 H" e+ {3 g4 y5 K. P: x - return this.bodyEncoding;
/ Y% T0 j/ {# X: } - }
1 E" O, ]4 {/ X% L7 w" V% C4 y' j - : I- N F4 l( z
- protected boolean isPrintableChar(char ch)4 ?7 [. l- c% k$ x3 n
- {
3 H& E/ o$ K4 y - boolean isItPrintableChar = false;
4 a5 m2 g' A3 y; }! ?2 a/ T - if ((ch >= ' ') && (ch <= '~')) {" r+ ]" y1 ~5 o# K- x2 H' f
- isItPrintableChar = true;
$ \5 Z8 R& D: g( v" [ - }2 y" ]: S+ E" s) z5 j& q9 L
- else if ((ch >= 128) && (ch <= 168)) {; G% c# e8 }: m
- isItPrintableChar = true; Y% J' k; [6 Q9 m3 \1 D
- }
" S- Y1 E( E' x0 d# g - else if ((ch >= 224) && (ch <= 247))3 ^$ a# U2 f; R' M( K
- isItPrintableChar = true;
" q. {4 x# o8 F! g - return isItPrintableChar;; ]+ f% T6 A. D4 a) n* A. i2 w
- }% {& q1 G- O1 p/ t. l* R/ ]: X _3 h
; l/ |: d* H; T9 H6 _0 L- O- public boolean isMessageComplete()6 g. D; T5 c" W" `* ~
- {6 [- E- E% g0 p
- if (!(this.isMessageComplete))
# @* m* w3 x) q' u0 m/ l - try' Z0 ^' t$ J4 H5 A8 [
- {& D. y0 Q( d0 F$ v! U L
- synchronized (SmsMessageSegmentContainer.class)9 n! m' X) b& m' B% h2 G2 D, q
- {
+ W6 q# o N8 } - List listSmsSegments = ActiveRecord.find(this.segments.sqlAll().orderBy("segmentNumber ASC"));& _4 |0 @, a4 @+ x; _6 y
- if ((listSmsSegments != null) && (listSmsSegments.size() == this.totalSegments))8 g9 I* T( F9 L
- {
3 l& N# i i8 J4 ^; p# A' k - this.isMessageComplete = true; W: _5 m4 d% [8 }% x: z
- logger.fine("At this point all SMS message segments have been recieved so we are going to put it all together");
/ U/ _4 S5 V" [) Y' \ - StringBuilder msgString = new StringBuilder(160 * this.totalSegments);& o* t! m4 {; p$ H3 I) G' I
- if (msgString != null)
5 c; t* Z- i' M( N' r+ t9 v - {
2 t+ f8 c) W1 n( g$ q1 `$ a - if ("bbin".equals(this.bodyEncoding)): ?& f6 M9 E5 h6 G
- {
4 O! T+ V7 E4 m" ?% z3 }. g! S - ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
4 L& g+ N7 y9 L& @% e: R - Iterator i$ = listSmsSegments.iterator();
9 W$ v4 k1 m) U6 m [$ a/ ?
+ o# S' F; k9 R d' h* x1 y- while (i$.hasNext())
; g1 }+ L% D2 w3 ^% q b% p - {. e; I) B7 }4 s2 l
- SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();- p q" ~% W7 [: P. ?, J
- if ((msg.getBody() != null) && (msg.getBody().length() != 0));0 ^- ^! N5 [8 v& `
- byteOut.write(Base64.decode(msg.getBody()));
: K* n( f, Z& z4 w: Z: [ - }
: H5 x! ^/ j4 N" S - char[] testArray = byteOut.toString().toCharArray();
* [( J* l; R ] - boolean isAllCharsPrintable = true;
7 e- R: `7 B+ r+ e5 u, l0 U3 c - int i = 0;
3 E: H m7 A$ p/ R% D. R - for (i = 0; (i < testArray.length) && (isAllCharsPrintable); ++i) {
! H* B E4 k% Y# g4 H - if (!(isPrintableChar(testArray[i])))
) {% ~) x: j! @8 V" @ - isAllCharsPrintable = false;
b) p1 q0 o, A. n7 z - }
9 ?7 W( Y" Y3 }2 ~( J - if (isAllCharsPrintable)/ I1 [! j# H: {& M4 Q$ ~. k/ x5 P
- this.completeMsgString = byteOut.toString() + "";, H/ Y/ ?& ?. \, k! G
- else
. n z2 B" a9 k5 |# n, r( ] s - this.completeMsgString = Base64.encodeBytes(byteOut.toByteArray()) + "";( b# u0 }8 g5 O# E
- System.out.println(this.completeMsgString);1 m1 v; C$ Z6 E" E/ a
- }
& ?' u" r* z. x9 O5 Q: d" | - else {
) \* ^/ G3 o# N6 r3 y& ~9 k - Iterator i$ = listSmsSegments.iterator();8 S) s) T( `% g7 I0 d/ Y
/ Z. Z7 [3 \ r# b. I- while (i$.hasNext())4 G3 W4 \6 G `, p4 z
- {
$ g. o# W2 ~' s% _ - SmsMessageFromTil msg = (SmsMessageFromTil)i$.next();
/ `- `' z( V- ?2 b5 F* j" j m) E - if ((msg.getBody() != null) && (msg.getBody().length() != 0));, ^& |+ p- I# Z; W2 k
- msgString.append(TilUtils.decodeString(msg.getBodyEncoding(), msg.getBody()));, } ]) C) r) ?+ _# a
- }. g/ q: g. s5 F% Y
- this.completeMsgString = msgString.toString() + "";
$ ?/ M, S: s. S. A0 \ - }; e' A" J( s8 r, g
- }% I1 N( X; {+ R( N+ V9 \' e# C
- else {/ ^6 b" M( a+ T, `
- logger.severe("Error allocating memory to put Segmented SMS Message Together");
/ c8 f: H( R! j6 M% [ - }# h( p K- M) F" {
- } v" J% y9 h W
- else if ((listSmsSegments != null) && (listSmsSegments.size() > this.totalSegments)) {
% M/ Z9 f, x. ^8 D, ` - 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); }" Y; M }! ~# i9 q, F2 k6 `4 A& Z
- listSmsSegments = null;
8 U" I+ q `8 ~, s! E; r! ` - }3 R9 Z: y$ a! t1 b: T6 h& b
- }
% e9 W+ |. {" }8 `. U# b3 r3 J" t& L - catch (ActiveRecordException e)4 |( i8 G- N |0 a% ^8 m% s" D$ r
- {# w" V0 l6 [0 d7 x( ^3 _5 w; _& q
- logger.warning("SMS Message couldn't be retrieved from the database at this time.");
4 F) ?* b1 T: s- H, O. c - }- l0 t$ f- ]' V1 \4 [' @
- catch (NullPointerException e). {! l" b( p! I- A9 r
- {
, u. z# _ [/ J: A. y: q# F* ]$ F - logger.severe("Null Pointer Error putting Segmented SMS Message Together");, \! z1 x8 a' d6 R8 o
- }
6 b0 f) h& z: t% i: g: s; s - catch (IOException e)5 v* v3 h1 F, u3 d
- {
9 J7 Y# c4 X2 ^ - logger.severe("Error Decoding Binary Message");
: r3 L0 j) ^( Q" C7 _, z. Q! S5 h - }( b2 V; e: k# _9 s. ~
- return this.isMessageComplete;6 Q7 h1 E, I8 \' L
- }
& _; u7 J) G$ D9 Y: U, u" w
: N5 o% B- ^; O! w6 Q- public String getCompletedBody()+ W2 x- v# \2 I( B: K7 x
- {
- f% j B$ e; B2 H( A - if (!(isMessageComplete()))$ K1 E( Q2 e/ B6 Z% c8 L) f
- logger.warning("Message Not yet Complete so you should call this after testing with isMessageComplete");& H7 V5 ~& `/ |
- return this.completeMsgString; J: a! l" J* z( J8 [
- }
+ C, _2 e. r0 z" {" ~& Y
Q( ` W: s: |7 x9 T- A" a1 Q y) V; y- public void addNewSegment(SmsMessageFromTil msgSegment)* b8 ~- J) o5 |2 e
- {
2 K# u4 D2 {7 k+ T& H) b - try
6 h x6 T; `' r6 `! k& f - {
4 j" {! J% D* L. W" L - if (logger.getLogger().isFinerLoggable)
: I# m0 ~) B3 L, `% n - logger.finer("Attempting to Adding new SMS Message with MessageId:" + msgSegment.getMessageId() + " with SegmentNo:" + msgSegment.getSegmentNumber() + " of Total Segments:" + msgSegment.getTotalNumberOfSegments());
$ B2 @' _ ?) r6 U u6 }- q2 } - synchronized (SmsMessageSegmentContainer.class)# v: k ^3 r; k6 x, z: a: I8 u5 B- t) `
- {( T% W5 ^0 i; L2 g4 k# C5 v1 \0 K
- int numSegmentsAlreadyStoredWithThatSegNumber = ActiveRecord.count(this.segments.sqlAll().where("segmentNumber=?", new Object[] { " p; ]# O5 C' v
- Integer.valueOf(msgSegment.getSegmentNumber()) }));
: J; A1 T. `- {; @( u' Z - 0 B8 o8 F$ m7 C
- if (numSegmentsAlreadyStoredWithThatSegNumber == 0)
- t, v1 O: L# m# V2 C - {
: c Z2 ?0 d! R* e: _" i% E - logger.finer("New SMS Segmeted Accepted. Adding it to SMS Container");+ @5 v7 I9 P: p
- logger.finer("saving segment to message holder");
5 E! Y& `/ L9 R7 T2 m - Batch batch = new Batch();( u( L+ x$ f x4 B8 a% F
- this.segments.push(batch, msgSegment);
2 W0 A. i, o* T5 }9 V# D, o, Q5 @ - msgSegment.save(batch);
; o7 K/ |- \7 m. }- _) l& j3 Q( Q$ A - if (0L != msgSegment.getTimeStamp())' P* L3 I. R' {8 R: C: o
- setTimeStampLastReceivedSegment(msgSegment.getTimeStamp());1 s' ]' x, C0 e/ Q
- save(batch);' |/ B, T1 ?" S) M
- batch.execute();9 j# O5 V8 ~5 C! T1 d) ^1 P
- }
, q% j! a% r8 G: Z* K. \ - else {
' C, `/ H& S. f9 G: v0 i - logger.warning("Found duplicate SMS Message segment already saved");. \$ p$ ?( a$ e3 M3 e* W/ d5 D
- }& V, e' x2 w7 \5 V- M: _" b
- }3 f& ^1 q9 `1 b b' }6 z. d! |6 e
- }
a# }0 e9 I& G4 X( G% R0 [ - catch (ActiveRecordException e)
/ W0 V7 b) p; ^) b$ H; G/ B* } - {
# e- q8 q+ L5 | - logger.severe("Error saving SMS Message Segment. Data will probably be lost.");9 B0 u" W7 R2 e+ F
- }
4 U6 m* O- c5 _ - }
& O8 I+ z# g d; h& k1 { - 9 e0 N. w L& g7 F& M$ \, Z
- private String convertToJson(boolean doHidePrivateData)
9 f, V- O& w( h1 z# s - {
) E$ T A' J s' S - JSONObject smsJson = new JSONObject();- p# @: w& ?* P. o
- SmsMessageFromTil msgSeg = null;1 q& J6 B7 U( m+ a
- try
( x, Z& x! Q- P$ p9 C3 S - {$ H# z. e, K) O0 M" V+ G
- msgSeg = (SmsMessageFromTil)this.segments.findFirst();8 a4 m, s5 d( l6 T" x
- if (msgSeg != null)
* z: f7 F O& r3 Z - {
, Z$ F$ c' o D; C- ]) \ - if (doHidePrivateData)0 E; }5 L3 Q; P2 p
- {% c" d! `' y7 z5 ]6 M, p8 p; P
- smsJson.put("body", "This has been hidden for logging");, G" W1 V; E5 _- ?
- smsJson.put("address", "555-333-4444");
5 o+ I$ w! h) c2 g0 V - smsJson.put("callbackNumber", "555-333-4444");
+ `- `. N" C) Z$ Z9 B" G% W - }& }/ k& M6 O- z/ ]) Q( q* P' C
- else {
$ y T+ }2 K: f; c; H; ? - smsJson.put("body", getCompletedBody());
+ R& \: ]8 r! h1 X - smsJson.put("address", msgSeg.getWhoIsItFrom());
1 S5 h9 R+ w6 ^+ G6 Z7 o, ` - smsJson.put("callbackNumber", msgSeg.getCallbackNumber());+ ^" j' I) S& g7 a: w- {
- }" u9 d6 L2 C, e9 h% q/ ?# F
- smsJson.put("networkMsgId", msgSeg.getMessageId());! P" y( M& K H; d: v
- smsJson.put("deliveryReportOptions", msgSeg.getDeliveryReportOptions());$ ^0 t' a4 T4 p, m i
- smsJson.put("timeStamp", msgSeg.getTimeStamp());. f5 O* W; U1 J7 ?; t' u% B
- if (msgSeg.isThisMessageFromAGsmNetwork())
0 C- k, J/ ~7 |2 M% t5 t' H9 r0 w. l - {
' }% K# f. B: u" B }) ] - smsJson.put("priority", msgSeg.getSmsClassInfo());1 B# t6 R7 X; q% i. L
- if (msgSeg.isFromSim())3 G9 @1 M$ q3 {: z8 q
- smsJson.put("isFromSim", true);8 v- [9 B) s# m
- }
9 f" r. F9 } L2 V - else {& r0 ^" Q9 e; o% P* `: }
- smsJson.put("priority", msgSeg.getPriority());, V; Q9 B0 o( J0 O) N; T- W
- }9 {% J! u& [& j2 A7 `; }: F
- smsJson.put("displayMode", msgSeg.getDisplayMode());
- U2 {3 J9 m, k" V! S - smsJson.put("privacyMode", msgSeg.getPrivacyMode()); break label257:, U* B8 q& n" ?6 N# D; E3 D3 K1 j
- }
- u+ O1 w6 L7 k6 K4 i - L' G" r" a/ T' h! E2 y
- logger.severe("Did not find Message Segment to generate JSON string");
, ?9 W u7 `6 ~5 {3 V# K- t - }* _4 H( ]* k* I9 p6 D5 v7 i! _
- catch (ActiveRecordException e)/ z) u3 [2 r) N) R
- {5 t- r7 u9 z. P; s( o+ O- V
- logger.severe("Caught Exception", e);8 \* f3 m6 g% A# S
- }
: r: |9 E m2 m3 G( j5 i7 A - catch (JSONException e)
; `, d- C0 r+ J - {
0 G v" X) @$ o! }7 z4 U - logger.severe("Caught Exception", e);1 w/ u& j) P5 ]% H. O, P
- }
2 P. E7 ^9 X' ?/ ^8 ]0 M' J3 g - label257: String jsonString = smsJson.toString();' L: M# b9 d) @& e1 ^7 z) k
- logger.finer("toJSONString SMS Message: " + jsonString);) G" o. P: r/ l! h Q% A' E
- return jsonString;
4 x0 X: i4 U3 W - }
9 T! c' c4 _2 t- A9 L
. ^5 F/ U8 j, R7 @3 Q6 n- public String toJSONString()
7 ^ e* o! l2 J( p - {
5 u0 w; Q; n( ~- }- }! J5 L6 g/ F - return convertToJson(false);! h+ ~# q* T7 b+ i ?/ _7 E
- }
9 \. ]3 u7 b% a, ]1 J
' v2 y0 k6 P& v J% u; l# j- public String toDebugJSONString()
1 j( [# Z7 F) K1 U5 W - {/ B; C) [' t% P6 Q% O) b" m
- return convertToJson(true);
+ ?% [" T7 W& r; w/ p - } a. ~. Y/ @2 @. X
- }
复制代码 |
|