1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.address.service;
35
36 import fr.paris.lutece.plugins.address.business.axis.AdresseService;
37 import fr.paris.lutece.plugins.address.business.axis.AdresseServiceLocator;
38 import fr.paris.lutece.plugins.address.business.axis.AdresseServicePortType;
39 import fr.paris.lutece.plugins.address.business.jaxb.Adresse;
40 import fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresses;
41 import fr.paris.lutece.plugins.address.util.LibraryAddressUtils;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44 import fr.paris.lutece.util.ReferenceList;
45
46 import org.apache.axis.client.Stub;
47
48 import java.io.StringReader;
49
50 import java.net.MalformedURLException;
51 import java.net.URL;
52
53 import java.rmi.RemoteException;
54
55 import java.util.HashSet;
56 import java.util.List;
57 import java.util.Set;
58
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpSession;
61
62 import javax.xml.bind.JAXBContext;
63 import javax.xml.bind.JAXBException;
64 import javax.xml.bind.Unmarshaller;
65 import javax.xml.rpc.ServiceException;
66 import javax.xml.transform.stream.StreamSource;
67
68 import org.apache.commons.lang3.StringUtils;
69
70
71
72
73 public class WebServiceAddressService implements IAddressService
74 {
75
76 private static final String JAXB_CONTEXT_WS_FICHE_ADDRESS = "fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse";
77 private static final String JAXB_CONTEXT_WS_SEARCH_ADDRESS = "fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse";
78 private static final String SESSION_LIST_ADDRESS_NAME = "LIBRARY" + "_WSLISTADDRESS";
79
80
81
82
83 private static final String PROPERTY_GEOLOCATION_RSID = "address.geolocation.rsid";
84
85
86
87
88 private static final int CONSTANTE_DEFAULT_GEOLOCATION_RSID = 27561;
89 private String _strUrlWS;
90 private String _strDefaultCity;
91 private String _strDateSearch;
92 private String _strUserName;
93 private String _strPassword;
94 private String _strTimeOut;
95
96
97
98
99
100
101 private int getGeolocationRSID( )
102 {
103 return AppPropertiesService.getPropertyInt( PROPERTY_GEOLOCATION_RSID, CONSTANTE_DEFAULT_GEOLOCATION_RSID );
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public Adresse getGeolocalisation( HttpServletRequest request, String addresse, String date, boolean bIsTest ) throws RemoteException
119 {
120 return getGeolocalisation( request, getGeolocationRSID( ), addresse, date, bIsTest );
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public Adresse getGeolocalisation( HttpServletRequest request, long id, String addresse, String date, boolean bIsTest ) throws RemoteException
136 {
137 String responseWebService = null;
138 AdresseService adresseService = new AdresseServiceLocator( );
139
140 try
141 {
142 URL urlWS = null;
143
144 Stub portType = null;
145
146 if ( ( getUrlWS( ) == null ) || getUrlWS( ).equals( "" ) )
147 {
148 portType = (Stub) adresseService.getAdresseServiceHttpPort( );
149 }
150 else
151 {
152 try
153 {
154 urlWS = new URL( getUrlWS( ) );
155 }
156 catch( MalformedURLException e )
157 {
158 AppLogService.error( e.getMessage( ), e );
159 }
160
161 portType = (Stub) adresseService.getAdresseServiceHttpPort( urlWS );
162 }
163
164 portType.setUsername( getUserName( ) );
165 portType.setPassword( getPassword( ) );
166
167 setTimeout( portType );
168
169 responseWebService = ( (AdresseServicePortType) portType ).geolocalization( getDefaultCity( ), addresse, String.valueOf( id ), date );
170 }
171 catch( ServiceException e )
172 {
173 AppLogService.error( e.getMessage( ), e );
174 }
175
176 Adresse adresseReturn = new Adresse( );
177
178 LibraryAddressUtils.fillAddressGeolocation( adresseReturn, responseWebService );
179
180 cleanListWSAdresses( request, SESSION_LIST_ADDRESS_NAME );
181
182 return adresseReturn;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public Adresse getAdresseInfo( HttpServletRequest request, long id, boolean bIsTest ) throws RemoteException
198 {
199 String responseWebService = null;
200 AdresseService adresseService = new AdresseServiceLocator( );
201
202 try
203 {
204 URL urlWS = null;
205
206 Stub portType = null;
207
208 if ( ( getUrlWS( ) == null ) || getUrlWS( ).equals( "" ) )
209 {
210 portType = (Stub) adresseService.getAdresseServiceHttpPort( );
211 }
212 else
213 {
214 try
215 {
216 urlWS = new URL( getUrlWS( ) );
217 }
218 catch( MalformedURLException e )
219 {
220 AppLogService.error( e.getMessage( ), e );
221 }
222
223 portType = (Stub) adresseService.getAdresseServiceHttpPort( urlWS );
224 }
225
226 portType.setUsername( getUserName( ) );
227 portType.setPassword( getPassword( ) );
228
229 setTimeout( portType );
230
231 responseWebService = ( (AdresseServicePortType) portType ).getAdresseInfo( getDefaultCity( ), id );
232 }
233 catch( ServiceException e )
234 {
235 AppLogService.error( e.getMessage( ), e );
236 }
237
238
239 fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse adresse = null;
240
241 JAXBContext jc;
242
243 try
244 {
245 jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_FICHE_ADDRESS );
246
247 Unmarshaller u = jc.createUnmarshaller( );
248 StringBuffer xmlStr = new StringBuffer( responseWebService );
249 adresse = (fr.paris.lutece.plugins.address.business.jaxb.wsFicheAdresse.Adresse) u
250 .unmarshal( new StreamSource( new StringReader( xmlStr.toString( ) ) ) );
251 }
252 catch( JAXBException e )
253 {
254 AppLogService.error( e.getMessage( ), e );
255 }
256
257 Adresse adresseReturn = new Adresse( );
258
259 adresseReturn.setIadresse( adresse.getIdentifiant( ) );
260 adresseReturn.setDunumero( adresse.getNumero( ) );
261 adresseReturn.setDubis( adresse.getSuffixe1( ) );
262 adresseReturn.setCodeCommune( adresse.getCodeInsee( ).toString( ) );
263 adresseReturn.setVille( adresse.getCommune( ) );
264
265
266
267
268
269 if ( !bIsTest )
270 {
271 List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAddress = getListWSAdresses( request ).getAdresse( );
272
273 for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAddress )
274 {
275 if ( String.valueOf( currentAdresse.getIdentifiant( ) ).equals( String.valueOf( id ) ) )
276 {
277 adresseReturn.setTypeVoie( currentAdresse.getTypeVoie( ) );
278 adresseReturn.setLibelleVoie( currentAdresse.getNomVoie( ) );
279
280
281 LibraryAddressUtils.fillAddressGeolocation( adresseReturn, currentAdresse.getGeometry( ) );
282
283 break;
284 }
285 }
286 }
287
288 cleanListWSAdresses( request, SESSION_LIST_ADDRESS_NAME );
289
290 return adresseReturn;
291 }
292
293
294
295
296
297
298
299
300
301
302
303 public ReferenceList searchAddress( HttpServletRequest request, String labeladresse ) throws RemoteException
304 {
305 String responseWebService = null;
306 AdresseService adresseService = new AdresseServiceLocator( );
307
308 try
309 {
310 URL urlWS = null;
311
312 Stub portType = null;
313
314 if ( ( getUrlWS( ) == null ) || getUrlWS( ).equals( "" ) )
315 {
316 portType = (Stub) adresseService.getAdresseServiceHttpPort( );
317 }
318 else
319 {
320 try
321 {
322 urlWS = new URL( getUrlWS( ) );
323 }
324 catch( MalformedURLException e )
325 {
326 AppLogService.error( e.getMessage( ), e );
327 }
328
329 portType = (Stub) adresseService.getAdresseServiceHttpPort( urlWS );
330 }
331
332 portType.setUsername( getUserName( ) );
333 portType.setPassword( getPassword( ) );
334
335 setTimeout( portType );
336
337 responseWebService = ( (AdresseServicePortType) portType ).searchAddress( getDefaultCity( ), labeladresse, null, getDateSearch( ) );
338
339
340 if ( responseWebService == null )
341 {
342 return null;
343 }
344 }
345 catch( ServiceException e )
346 {
347 AppLogService.error( e.getMessage( ), e );
348 }
349
350
351 Adresses adresses = null;
352
353 JAXBContext jc;
354
355 try
356 {
357 jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
358
359 Unmarshaller u = jc.createUnmarshaller( );
360 StringBuffer xmlStr = new StringBuffer( responseWebService );
361 adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString( ) ) ) );
362 }
363 catch( JAXBException e )
364 {
365 AppLogService.error( e.getMessage( ), e );
366 }
367
368 List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse( );
369
370 ReferenceList refList = null;
371
372
373 Set<AdresseElement> eltSet = null;
374
375
376 if ( ( listAdresses != null ) && !listAdresses.isEmpty( ) )
377 {
378 refList = new ReferenceList( );
379
380
381 eltSet = new HashSet<AdresseElement>( );
382
383 for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
384 {
385 String suffixe = "";
386
387 if ( currentAdresse.getSuffixe( ) != null )
388 {
389 suffixe = currentAdresse.getSuffixe( );
390 }
391
392 String strCurrentAdresse = "";
393
394 if ( currentAdresse.getTypeVoie( ) == null )
395 {
396 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getNomVoie( ) + " " + currentAdresse.getCommune( );
397 }
398 else
399 if ( LibraryAddressUtils.isTerminateByApostrophe( currentAdresse.getTypeVoie( ) ) )
400 {
401 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getTypeVoie( ) + currentAdresse.getNomVoie( )
402 + " " + currentAdresse.getCommune( );
403 }
404 else
405 {
406 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getTypeVoie( ) + " "
407 + currentAdresse.getNomVoie( ) + " " + currentAdresse.getCommune( );
408 }
409
410 String strIdAdresse = currentAdresse.getIdentifiant( ).toString( );
411
412
413 boolean isAdded = eltSet.add( new AdresseElement( strIdAdresse, strCurrentAdresse ) );
414
415 if ( isAdded )
416 {
417 refList.addItem( strIdAdresse, strCurrentAdresse );
418 }
419 }
420
421 setListWSAdresses( request, adresses );
422 }
423
424 return refList;
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440 public ReferenceList searchAddress( HttpServletRequest request, String labeladresse, String strArrondissement ) throws RemoteException
441 {
442 return searchAddress( request, labeladresse, Integer.toString( getGeolocationRSID( ) ), strArrondissement );
443 }
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459 public ReferenceList searchAddress( HttpServletRequest request, String labeladresse, String strSRID, String strArrondissement ) throws RemoteException
460 {
461 String responseWebService = null;
462 AdresseService adresseService = new AdresseServiceLocator( );
463
464 try
465 {
466 URL urlWS = null;
467
468 Stub portType = null;
469
470 if ( ( getUrlWS( ) == null ) || getUrlWS( ).equals( "" ) )
471 {
472 portType = (Stub) adresseService.getAdresseServiceHttpPort( );
473 }
474 else
475 {
476 try
477 {
478 urlWS = new URL( getUrlWS( ) );
479 }
480 catch( MalformedURLException e )
481 {
482 AppLogService.error( e.getMessage( ), e );
483 }
484
485 portType = (Stub) adresseService.getAdresseServiceHttpPort( urlWS );
486 }
487
488 portType.setUsername( getUserName( ) );
489 portType.setPassword( getPassword( ) );
490
491 setTimeout( portType );
492
493 responseWebService = ( (AdresseServicePortType) portType ).searchAddress( getDefaultCity( ), labeladresse, strSRID, getDateSearch( ) );
494
495
496 if ( responseWebService == null )
497 {
498 return null;
499 }
500 }
501 catch( ServiceException e )
502 {
503 AppLogService.error( e.getMessage( ), e );
504 }
505
506
507 Adresses adresses = null;
508
509 JAXBContext jc;
510
511 try
512 {
513 jc = JAXBContext.newInstance( JAXB_CONTEXT_WS_SEARCH_ADDRESS );
514
515 Unmarshaller u = jc.createUnmarshaller( );
516 StringBuffer xmlStr = new StringBuffer( responseWebService );
517 adresses = (Adresses) u.unmarshal( new StreamSource( new StringReader( xmlStr.toString( ) ) ) );
518 }
519 catch( JAXBException e )
520 {
521 AppLogService.error( e.getMessage( ), e );
522 }
523
524 List<fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse> listAdresses = adresses.getAdresse( );
525
526 ReferenceList refList = null;
527
528
529 Set<AdresseElement> eltSet = null;
530
531
532 if ( ( listAdresses != null ) && !listAdresses.isEmpty( ) )
533 {
534 refList = new ReferenceList( );
535
536
537 eltSet = new HashSet<AdresseElement>( );
538
539 for ( fr.paris.lutece.plugins.address.business.jaxb.wsSearchAdresse.Adresse currentAdresse : listAdresses )
540 {
541 String suffixe = "";
542
543 if ( currentAdresse.getSuffixe( ) != null )
544 {
545 suffixe = currentAdresse.getSuffixe( );
546 }
547
548 String strCurrentAdresse = "";
549
550 if ( currentAdresse.getTypeVoie( ) == null )
551 {
552 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getNomVoie( ) + " " + currentAdresse.getCommune( );
553 }
554 else
555 if ( LibraryAddressUtils.isTerminateByApostrophe( currentAdresse.getTypeVoie( ) ) )
556 {
557 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getTypeVoie( ) + currentAdresse.getNomVoie( )
558 + " " + currentAdresse.getCommune( );
559 }
560 else
561 {
562 strCurrentAdresse = currentAdresse.getNumero( ) + " " + suffixe + " " + currentAdresse.getTypeVoie( ) + " "
563 + currentAdresse.getNomVoie( ) + " " + currentAdresse.getCommune( );
564 }
565
566 String strIdAdresse = currentAdresse.getIdentifiant( ).toString( );
567
568 String arr = currentAdresse.getCommune( );
569 int index = arr.indexOf( "E" );
570 if ( index > 2 )
571 {
572 arr = arr.substring( index - 2, index );
573 index = arr.indexOf( "-" );
574
575 if ( index != -1 )
576 {
577 arr = arr.substring( 1, 2 );
578 }
579 }
580 else
581 {
582 arr = StringUtils.EMPTY;
583 }
584
585
586 boolean isAdded = eltSet.add( new AdresseElement( strIdAdresse, strCurrentAdresse ) );
587
588 if ( isAdded )
589 {
590 if ( arr.equals( strArrondissement ) )
591 {
592 refList.addItem( strIdAdresse, strCurrentAdresse );
593 }
594 }
595 }
596
597 setListWSAdresses( request, adresses );
598 }
599
600 return refList;
601 }
602
603
604
605
606
607 public String getDateSearch( )
608 {
609 return _strDateSearch;
610 }
611
612
613
614
615
616
617 public void setDateSearch( String strDateSearch )
618 {
619 _strDateSearch = strDateSearch;
620 }
621
622
623
624
625
626 public String getDefaultCity( )
627 {
628 return _strDefaultCity;
629 }
630
631
632
633
634
635
636 public void setDefaultCity( String strDefaultCity )
637 {
638 _strDefaultCity = strDefaultCity;
639 }
640
641
642
643
644
645 public String getUrlWS( )
646 {
647 return _strUrlWS;
648 }
649
650
651
652
653
654
655 public void setUrlWS( String strUrlWS )
656 {
657 _strUrlWS = strUrlWS;
658 }
659
660
661
662
663
664 public String getPassword( )
665 {
666 return _strPassword;
667 }
668
669
670
671
672
673
674 public void setPassword( String password )
675 {
676 _strPassword = password;
677 }
678
679
680
681
682
683 public String getUserName( )
684 {
685 return _strUserName;
686 }
687
688
689
690
691
692
693 public void setUserName( String userName )
694 {
695 _strUserName = userName;
696 }
697
698
699
700
701
702 public String getTimeOut( )
703 {
704 return _strTimeOut;
705 }
706
707
708
709
710
711
712 public void setTimeOut( String timeOut )
713 {
714 _strTimeOut = timeOut;
715 }
716
717
718
719
720
721
722 private void setTimeout( Stub portType )
723 {
724 try
725 {
726 portType.setTimeout( Integer.parseInt( getTimeOut( ) ) );
727 }
728 catch( NumberFormatException e )
729 {
730 AppLogService.error(
731 "WebServiceAddressService : timeOut is not set correctly for WebServiceAddressService. Please check address_context.xml. Will use no timeout" );
732 }
733 }
734
735
736
737
738
739
740
741 private Adresses getListWSAdresses( HttpServletRequest request )
742 {
743 HttpSession session = request.getSession( true );
744 String strSessionAttribute = SESSION_LIST_ADDRESS_NAME;
745 strSessionAttribute = strSessionAttribute.toUpperCase( );
746
747 return (Adresses) session.getAttribute( strSessionAttribute );
748 }
749
750
751
752
753
754
755
756
757 private void setListWSAdresses( HttpServletRequest request, Adresses adresses )
758 {
759 HttpSession session = request.getSession( true );
760 String strSessionAttribute = SESSION_LIST_ADDRESS_NAME;
761 strSessionAttribute = strSessionAttribute.toUpperCase( );
762 session.setAttribute( strSessionAttribute, adresses );
763 }
764
765
766
767
768
769
770
771
772 private void cleanListWSAdresses( HttpServletRequest request, String strAttributeName )
773 {
774 HttpSession session = request.getSession( true );
775 String strSessionAttribute = strAttributeName;
776 strSessionAttribute = strSessionAttribute.toUpperCase( );
777 session.removeAttribute( strSessionAttribute );
778 }
779
780
781
782
783 private class AdresseElement
784 {
785 private String _idAdresse;
786 private String _labelAdresse;
787
788
789
790
791
792
793
794
795
796 public AdresseElement( String strIdAdresse, String strLabelAdresse )
797 {
798 _idAdresse = strIdAdresse;
799 _labelAdresse = strLabelAdresse;
800 }
801
802
803
804
805
806
807 public String getLabelAdresse( )
808 {
809 return _labelAdresse;
810 }
811
812
813
814
815
816
817 public String getIdAdresse( )
818 {
819 return _idAdresse;
820 }
821
822
823
824
825
826
827 public int hashCode( )
828 {
829 return _labelAdresse.hashCode( );
830 }
831
832
833
834
835
836
837 public boolean equals( Object o )
838 {
839 AdresseElement adresseToCompare = (AdresseElement) o;
840
841 return this._labelAdresse.equals( adresseToCompare.getLabelAdresse( ) );
842 }
843 }
844 }