1 | /** |
---|
2 | * Copyright (C) 2003-2007 Funambol |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the Honest Public License. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * Honest Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the Honest Public License |
---|
13 | * along with this program; if not, write to Funambol, |
---|
14 | * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA |
---|
15 | */ |
---|
16 | package br.com.prognus.psync.synclet; |
---|
17 | |
---|
18 | import java.util.*; |
---|
19 | |
---|
20 | import com.funambol.framework.core.*; |
---|
21 | import com.funambol.framework.core.Map; |
---|
22 | import com.funambol.framework.engine.pipeline.*; |
---|
23 | import com.funambol.framework.logging.FunambolLogger; |
---|
24 | import com.funambol.framework.logging.FunambolLoggerFactory; |
---|
25 | import com.funambol.framework.protocol.ProtocolUtil; |
---|
26 | |
---|
27 | /** |
---|
28 | * This class changes the input message client source URI with a correspondent |
---|
29 | * server source URI |
---|
30 | * |
---|
31 | * This is done in order to allow any client to sync with different source URI |
---|
32 | * using the same SyncSource. For example into phone is possible to write |
---|
33 | * ./contact or only contact to synchorized with the same contact SyncSource. |
---|
34 | * |
---|
35 | * @version $Id: ChangeSourceUriSynclet.java,v 1.5 2007-02-15 10:23:46 luigiafassina Exp $ |
---|
36 | */ |
---|
37 | public class ChangeSourceUriSynclet |
---|
38 | implements InputMessageProcessor, OutputMessageProcessor { |
---|
39 | |
---|
40 | // --------------------------------------------------------------- Constants |
---|
41 | private static final String SYNCLET_NAME = |
---|
42 | ChangeSourceUriSynclet.class.getName(); |
---|
43 | private static final String PROPERTY_SOURCENAME_CHANGED = |
---|
44 | "funambol.foundation.changesourcename.SOURCENAME_CHANGED"; |
---|
45 | |
---|
46 | // ------------------------------------------------------------ Private data |
---|
47 | private static final FunambolLogger log = |
---|
48 | FunambolLoggerFactory.getLogger("engine.pipeline"); |
---|
49 | |
---|
50 | private HashMap sourceNameMapping = null; |
---|
51 | |
---|
52 | // ---------------------------------------------------------- Public methods |
---|
53 | |
---|
54 | /** |
---|
55 | * Process input message and set MessageProcessingContext property. |
---|
56 | * |
---|
57 | * @param processingContext the message processing context |
---|
58 | * @param message the message to be processed |
---|
59 | * @throws Sync4jException |
---|
60 | */ |
---|
61 | public void preProcessMessage(MessageProcessingContext processingContext, |
---|
62 | SyncML message ) |
---|
63 | throws Sync4jException { |
---|
64 | |
---|
65 | if (log.isTraceEnabled()) { |
---|
66 | log.trace(SYNCLET_NAME + ".preProcessMessage(...)"); |
---|
67 | } |
---|
68 | |
---|
69 | HashMap sourceReplacedMap = |
---|
70 | (HashMap)processingContext.getSessionProperty( |
---|
71 | PROPERTY_SOURCENAME_CHANGED); |
---|
72 | |
---|
73 | if (sourceReplacedMap == null) { |
---|
74 | sourceReplacedMap = new HashMap(); |
---|
75 | processingContext.setSessionProperty(PROPERTY_SOURCENAME_CHANGED, |
---|
76 | sourceReplacedMap ); |
---|
77 | } |
---|
78 | |
---|
79 | // |
---|
80 | // Store and change the source uri into commands |
---|
81 | // |
---|
82 | manageInputAlert (message, sourceReplacedMap); |
---|
83 | manageInputStatus(message, sourceReplacedMap); |
---|
84 | manageInputSync (message, sourceReplacedMap); |
---|
85 | manageInputMap (message, sourceReplacedMap); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Process and manipulate the output message. |
---|
91 | * |
---|
92 | * @param processingContext the message processing context |
---|
93 | * @param message the message to be processed |
---|
94 | * @throws Sync4jException |
---|
95 | */ |
---|
96 | public void postProcessMessage(MessageProcessingContext processingContext, |
---|
97 | SyncML message) throws Sync4jException { |
---|
98 | if (log.isTraceEnabled()) { |
---|
99 | log.trace(SYNCLET_NAME + ".postProcessMessage(...)"); |
---|
100 | } |
---|
101 | |
---|
102 | HashMap sourceReplacedMap = |
---|
103 | (HashMap)processingContext.getSessionProperty( |
---|
104 | PROPERTY_SOURCENAME_CHANGED); |
---|
105 | if (!sourceReplacedMap.isEmpty()) { |
---|
106 | // |
---|
107 | // Replace original source uri into commands |
---|
108 | // |
---|
109 | manageOutputStatus (message, sourceReplacedMap); |
---|
110 | manageOutputResults(message, sourceReplacedMap); |
---|
111 | manageOutputAlert (message, sourceReplacedMap); |
---|
112 | manageOutputSync (message, sourceReplacedMap); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Set the hashmap with sources name's to change |
---|
118 | * |
---|
119 | * @param map |
---|
120 | */ |
---|
121 | public void setSourceNameMapping(HashMap map) { |
---|
122 | this.sourceNameMapping = map; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Get the hashmap with sources name's to change |
---|
127 | * |
---|
128 | * @return the map |
---|
129 | */ |
---|
130 | public HashMap getSourceNameMapping() { |
---|
131 | return this.sourceNameMapping; |
---|
132 | } |
---|
133 | // --------------------------------------------------------- Private Methods |
---|
134 | |
---|
135 | /** |
---|
136 | * Store and change Target URI into Alert commands |
---|
137 | * |
---|
138 | * @param message the client message |
---|
139 | * @param sourceReplacedMap the HashMap in which store uri to replace |
---|
140 | */ |
---|
141 | private void manageInputAlert(SyncML message, HashMap sourceReplacedMap) { |
---|
142 | if (log.isTraceEnabled()) { |
---|
143 | log.trace("Change TargetURI into input Alert commands"); |
---|
144 | } |
---|
145 | SyncBody syncBody = message.getSyncBody(); |
---|
146 | |
---|
147 | AbstractCommand[] allClientCommands = |
---|
148 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
149 | new AbstractCommand[0]); |
---|
150 | |
---|
151 | ArrayList alertList = |
---|
152 | ProtocolUtil.filterCommands(allClientCommands, Alert.class); |
---|
153 | |
---|
154 | Iterator itAlertList = alertList.iterator(); |
---|
155 | |
---|
156 | Alert alert = null; |
---|
157 | ArrayList items = null; |
---|
158 | Iterator itItem = null; |
---|
159 | Item item = null; |
---|
160 | Target target = null; |
---|
161 | String targetUri = null; |
---|
162 | |
---|
163 | while (itAlertList.hasNext()) { |
---|
164 | alert = (Alert)itAlertList.next(); |
---|
165 | items = alert.getItems(); |
---|
166 | itItem = items.iterator(); |
---|
167 | while (itItem.hasNext()) { |
---|
168 | |
---|
169 | item = (Item)itItem.next(); |
---|
170 | target = item.getTarget(); |
---|
171 | if (target != null) { |
---|
172 | targetUri = target.getLocURI(); |
---|
173 | if (log.isTraceEnabled()) { |
---|
174 | log.trace("original targetUri: " + targetUri); |
---|
175 | } |
---|
176 | |
---|
177 | if (sourceNameMapping.containsKey(targetUri)) { |
---|
178 | String targetNew = |
---|
179 | (String)sourceNameMapping.get(targetUri); |
---|
180 | |
---|
181 | if (log.isTraceEnabled()) { |
---|
182 | log.trace("new targetUri: " + targetNew); |
---|
183 | } |
---|
184 | target.setLocURI(targetNew); |
---|
185 | |
---|
186 | sourceReplacedMap.put(targetNew, targetUri); |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Store and change SourceRef into Status commmads |
---|
195 | * |
---|
196 | * @param message the client message |
---|
197 | * @param sourceReplacedMap the HashMap in which store uri to replace |
---|
198 | */ |
---|
199 | private void manageInputStatus(SyncML message, HashMap sourceReplacedMap) { |
---|
200 | if (log.isTraceEnabled()) { |
---|
201 | log.trace("Change SourceRef into input Status commands"); |
---|
202 | } |
---|
203 | SyncBody syncBody = message.getSyncBody(); |
---|
204 | |
---|
205 | AbstractCommand[] allClientCommands = |
---|
206 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
207 | new AbstractCommand[0]); |
---|
208 | |
---|
209 | ArrayList statusList = |
---|
210 | ProtocolUtil.filterCommands(allClientCommands, Status.class); |
---|
211 | |
---|
212 | Iterator itStatusList = statusList.iterator(); |
---|
213 | |
---|
214 | Status status = null; |
---|
215 | while (itStatusList.hasNext()) { |
---|
216 | status = (Status)itStatusList.next(); |
---|
217 | SourceRef[] srefs = |
---|
218 | (SourceRef[])status.getSourceRef().toArray(new SourceRef[0]); |
---|
219 | int s = srefs.length; |
---|
220 | for(int i=0;i<s;i++) { |
---|
221 | SourceRef sr = (SourceRef)srefs[i]; |
---|
222 | String sourceRef = sr.getValue(); |
---|
223 | if (log.isTraceEnabled()) { |
---|
224 | log.trace("original sourceRef: " + sourceRef); |
---|
225 | } |
---|
226 | |
---|
227 | if (sourceNameMapping.containsKey(sourceRef)) { |
---|
228 | String sourceRefNew = |
---|
229 | (String)sourceNameMapping.get(sr.getValue()); |
---|
230 | |
---|
231 | if (log.isTraceEnabled()) { |
---|
232 | log.trace("new sourceRef: " + sourceRefNew); |
---|
233 | } |
---|
234 | sr.setValue(sourceRefNew); |
---|
235 | |
---|
236 | sourceReplacedMap.put(sourceRefNew, sourceRef); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Store and change Target into Sync commands |
---|
244 | * |
---|
245 | * @param message the client message |
---|
246 | * @param sourceReplacedMap the HashMap in which store uri to replace |
---|
247 | */ |
---|
248 | private void manageInputSync(SyncML message, HashMap sourceReplacedMap) { |
---|
249 | if (log.isTraceEnabled()) { |
---|
250 | log.trace("Change TargetURI into input Sync commands"); |
---|
251 | } |
---|
252 | SyncBody syncBody = message.getSyncBody(); |
---|
253 | |
---|
254 | AbstractCommand[] allClientCommands = |
---|
255 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
256 | new AbstractCommand[0]); |
---|
257 | |
---|
258 | ArrayList syncList = |
---|
259 | ProtocolUtil.filterCommands(allClientCommands, Sync.class); |
---|
260 | |
---|
261 | Iterator itList = syncList.iterator(); |
---|
262 | |
---|
263 | Sync sync = null; |
---|
264 | String targetUri = null; |
---|
265 | |
---|
266 | while (itList.hasNext()) { |
---|
267 | sync = (Sync)itList.next(); |
---|
268 | targetUri = sync.getTarget().getLocURI(); |
---|
269 | if (log.isTraceEnabled()) { |
---|
270 | log.trace("original targetUri: " + targetUri); |
---|
271 | } |
---|
272 | |
---|
273 | if (sourceNameMapping.containsKey(targetUri)) { |
---|
274 | String targetNew = (String)sourceNameMapping.get(targetUri); |
---|
275 | |
---|
276 | if (log.isTraceEnabled()) { |
---|
277 | log.trace("new targetUri: " + targetNew); |
---|
278 | } |
---|
279 | sync.getTarget().setLocURI(targetNew); |
---|
280 | |
---|
281 | sourceReplacedMap.put(targetNew, targetUri); |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Store and change Target into Map commands |
---|
288 | * |
---|
289 | * @param message the client message |
---|
290 | * @param sourceReplacedMap the HashMap in which store uri to replace |
---|
291 | */ |
---|
292 | private void manageInputMap(SyncML message, HashMap sourceReplacedMap) { |
---|
293 | if (log.isTraceEnabled()) { |
---|
294 | log.trace("Change TargetURI into input Map commands"); |
---|
295 | } |
---|
296 | SyncBody syncBody = message.getSyncBody(); |
---|
297 | |
---|
298 | AbstractCommand[] allClientCommands = |
---|
299 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
300 | new AbstractCommand[0]); |
---|
301 | |
---|
302 | ArrayList mapList = |
---|
303 | ProtocolUtil.filterCommands(allClientCommands, Map.class); |
---|
304 | |
---|
305 | Iterator itList = mapList.iterator(); |
---|
306 | |
---|
307 | Map map = null; |
---|
308 | String targetUri = null; |
---|
309 | |
---|
310 | while (itList.hasNext()) { |
---|
311 | map = (Map)itList.next(); |
---|
312 | targetUri = map.getTarget().getLocURI(); |
---|
313 | if (log.isTraceEnabled()) { |
---|
314 | log.trace("original targetUri: " + targetUri); |
---|
315 | } |
---|
316 | |
---|
317 | if (sourceNameMapping.containsKey(targetUri)) { |
---|
318 | String targetNew = (String)sourceNameMapping.get(targetUri); |
---|
319 | |
---|
320 | if (log.isTraceEnabled()) { |
---|
321 | log.trace("new targetUri: " + targetNew); |
---|
322 | } |
---|
323 | map.getTarget().setLocURI(targetNew); |
---|
324 | |
---|
325 | sourceReplacedMap.put(targetNew, targetUri); |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Replace TargetRef into Status commands |
---|
332 | * |
---|
333 | * @param message the client message |
---|
334 | * @param sourceReplacedMap the HashMap with the uri to replace |
---|
335 | */ |
---|
336 | private void manageOutputStatus(SyncML message, HashMap sourceReplacedMap) { |
---|
337 | if (log.isTraceEnabled()) { |
---|
338 | log.trace("Replace TargetRef into output Status commands"); |
---|
339 | } |
---|
340 | SyncBody syncBody = message.getSyncBody(); |
---|
341 | |
---|
342 | AbstractCommand[] allServerCommands = |
---|
343 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
344 | new AbstractCommand[0]); |
---|
345 | |
---|
346 | ArrayList statusList = |
---|
347 | ProtocolUtil.filterCommands(allServerCommands, Status.class); |
---|
348 | |
---|
349 | Iterator itStatusList = statusList.iterator(); |
---|
350 | |
---|
351 | Status status = null; |
---|
352 | while (itStatusList.hasNext()) { |
---|
353 | status = (Status)itStatusList.next(); |
---|
354 | |
---|
355 | if (status.getTargetRef() == null) { |
---|
356 | continue; |
---|
357 | } |
---|
358 | TargetRef[] trefs = |
---|
359 | (TargetRef[])status.getTargetRef().toArray(new TargetRef[0]); |
---|
360 | int s = trefs.length; |
---|
361 | for(int i=0;i<s;i++) { |
---|
362 | TargetRef tr = (TargetRef)trefs[i]; |
---|
363 | if (sourceReplacedMap.containsKey(tr.getValue())) { |
---|
364 | tr.setValue((String)sourceReplacedMap.get(tr.getValue())); |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * Replace Source into Alert commands |
---|
372 | * |
---|
373 | * @param message the client message |
---|
374 | * @param sourceReplacedMap the HashMap with the uri to replace |
---|
375 | */ |
---|
376 | private void manageOutputAlert(SyncML message, HashMap sourceReplacedMap) { |
---|
377 | if (log.isTraceEnabled()) { |
---|
378 | log.trace("Replace Source into output Alert commands"); |
---|
379 | } |
---|
380 | SyncBody syncBody = message.getSyncBody(); |
---|
381 | |
---|
382 | AbstractCommand[] allServerCommands = |
---|
383 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
384 | new AbstractCommand[0]); |
---|
385 | |
---|
386 | ArrayList alertList = |
---|
387 | ProtocolUtil.filterCommands(allServerCommands, Alert.class); |
---|
388 | |
---|
389 | Iterator itAlertList = alertList.iterator(); |
---|
390 | |
---|
391 | Alert alert = null; |
---|
392 | ArrayList items = null; |
---|
393 | Iterator itItem = null; |
---|
394 | Item item = null; |
---|
395 | Source source = null; |
---|
396 | String sourceUri = null; |
---|
397 | |
---|
398 | while (itAlertList.hasNext()) { |
---|
399 | alert = (Alert)itAlertList.next(); |
---|
400 | items = alert.getItems(); |
---|
401 | itItem = items.iterator(); |
---|
402 | while (itItem.hasNext()) { |
---|
403 | |
---|
404 | item = (Item)itItem.next(); |
---|
405 | source = item.getSource(); |
---|
406 | if (source != null) { |
---|
407 | sourceUri = source.getLocURI(); |
---|
408 | |
---|
409 | if (sourceReplacedMap.containsKey(sourceUri)) { |
---|
410 | // |
---|
411 | // Here we have to create a new Source object because |
---|
412 | // the Alert can contain the same Source used in the |
---|
413 | // engine/sessionHandler to handle the Database. |
---|
414 | // If here we change the source object of the alert we |
---|
415 | // change also the source of the database because it's |
---|
416 | // the same object!!. |
---|
417 | // |
---|
418 | // |
---|
419 | Source newSource = new Source( |
---|
420 | (String)sourceReplacedMap.get(sourceUri), |
---|
421 | source.getLocName() |
---|
422 | ); |
---|
423 | item.setSource(newSource); |
---|
424 | } |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | /** |
---|
431 | * Replace SourceRef into Results command |
---|
432 | * |
---|
433 | * @param message the client message |
---|
434 | * @param sourceReplacedMap the HashMap with the uri to replace |
---|
435 | */ |
---|
436 | private void manageOutputResults(SyncML message, HashMap sourceReplacedMap) { |
---|
437 | if (log.isTraceEnabled()) { |
---|
438 | log.trace("Replace SourceRef into output Result commands"); |
---|
439 | } |
---|
440 | SyncBody syncBody = message.getSyncBody(); |
---|
441 | |
---|
442 | AbstractCommand[] allServerCommands = |
---|
443 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
444 | new AbstractCommand[0]); |
---|
445 | |
---|
446 | List list = |
---|
447 | ProtocolUtil.filterCommands(allServerCommands, Results.class); |
---|
448 | |
---|
449 | if (list.isEmpty()) { |
---|
450 | return; |
---|
451 | } |
---|
452 | |
---|
453 | Results results = (Results)list.get(0); |
---|
454 | |
---|
455 | Item[] items = (Item[])results.getItems().toArray(new Item[0]); |
---|
456 | |
---|
457 | if (items != null && items.length > 0) { |
---|
458 | if (items[0] instanceof DevInfItem) { |
---|
459 | DevInfItem item = (DevInfItem) items[0]; |
---|
460 | ArrayList dss = item.getDevInfData().getDevInf().getDataStores(); |
---|
461 | int s = dss.size(); |
---|
462 | for (int i = 0; i < s; i++) { |
---|
463 | DataStore ds = (DataStore) dss.get(i); |
---|
464 | String sourceRef = ds.getSourceRef().getValue(); |
---|
465 | if (sourceReplacedMap.containsKey(sourceRef)) { |
---|
466 | ds.getSourceRef().setValue((String) sourceReplacedMap. |
---|
467 | get(sourceRef)); |
---|
468 | } |
---|
469 | } |
---|
470 | } |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | |
---|
475 | /** |
---|
476 | * Replace Source into Sync commands |
---|
477 | * |
---|
478 | * @param message the client message |
---|
479 | * @param sourceReplacedMap the HashMap with the uri to replace |
---|
480 | */ |
---|
481 | private void manageOutputSync(SyncML message, HashMap sourceReplacedMap) { |
---|
482 | if (log.isTraceEnabled()) { |
---|
483 | log.trace("Replace Source into output Sync commands"); |
---|
484 | } |
---|
485 | SyncBody syncBody = message.getSyncBody(); |
---|
486 | |
---|
487 | AbstractCommand[] allServerCommands = |
---|
488 | (AbstractCommand[])syncBody.getCommands().toArray( |
---|
489 | new AbstractCommand[0]); |
---|
490 | |
---|
491 | ArrayList syncList = |
---|
492 | ProtocolUtil.filterCommands(allServerCommands, Sync.class); |
---|
493 | |
---|
494 | Iterator itSyncList = syncList.iterator(); |
---|
495 | |
---|
496 | Sync sync = null; |
---|
497 | Source source = null; |
---|
498 | String sourceUri = null; |
---|
499 | |
---|
500 | while (itSyncList.hasNext()) { |
---|
501 | sync = (Sync) itSyncList.next(); |
---|
502 | |
---|
503 | source = sync.getSource(); |
---|
504 | |
---|
505 | if (source != null) { |
---|
506 | sourceUri = source.getLocURI(); |
---|
507 | |
---|
508 | if (sourceReplacedMap.containsKey(sourceUri)) { |
---|
509 | // |
---|
510 | // Here we have to create a new Source object because |
---|
511 | // the Sync can contain the same Source used in the |
---|
512 | // engine/sessionHandler to handle the Database. |
---|
513 | // If here we change the source object of the sync commands we |
---|
514 | // change also the source of the database because it's |
---|
515 | // the same object!!. |
---|
516 | // |
---|
517 | // |
---|
518 | Source newSource = new Source( |
---|
519 | (String) sourceReplacedMap.get(sourceUri), |
---|
520 | source.getLocName() |
---|
521 | ); |
---|
522 | sync.setSource(newSource); |
---|
523 | } |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | } |
---|