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.crmclient.util.http;
35
36 import fr.paris.lutece.plugins.crmclient.util.CRMException;
37 import fr.paris.lutece.portal.service.util.AppLogService;
38 import fr.paris.lutece.util.httpaccess.HttpAccess;
39 import fr.paris.lutece.util.httpaccess.HttpAccessException;
40 import fr.paris.lutece.util.signrequest.HeaderHashAuthenticator;
41 import fr.paris.lutece.util.signrequest.NoSecurityAuthenticator;
42 import fr.paris.lutece.util.signrequest.RequestAuthenticator;
43 import fr.paris.lutece.util.signrequest.RequestHashAuthenticator;
44
45 import org.apache.commons.lang.StringUtils;
46
47 import java.util.Date;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Map.Entry;
51
52
53
54
55
56
57 public class WebServiceCaller implements IWebServiceCaller
58 {
59
60
61
62 @Override
63 public String callWebService( String strUrl, Map<String, String> mapParameters, RequestAuthenticator authenticator, List<String> listElements,
64 HttpMethodEnum httpMethod ) throws CRMException
65 {
66 String strResponse = StringUtils.EMPTY;
67
68 if ( AppLogService.isDebugEnabled( ) )
69 {
70 AppLogService.debug( trace( strUrl, mapParameters, authenticator, listElements ) );
71 }
72
73 try
74 {
75 HttpAccess httpAccess = new HttpAccess( );
76
77 if ( httpMethod == HttpMethodEnum.POST )
78 {
79 strResponse = httpAccess.doPost( strUrl, mapParameters, authenticator, listElements );
80 }
81 else
82 {
83 strResponse = httpAccess.doGet( strUrl, authenticator, listElements );
84 }
85 }
86 catch( HttpAccessException e )
87 {
88 String strError = "Error connecting to '" + strUrl + "' : ";
89 AppLogService.error( strError + e.getMessage( ), e );
90 throw new CRMException( strError, e );
91 }
92
93 return strResponse;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 protected String trace( String strUrl, Map<String, String> mapParameters, RequestAuthenticator authenticator, List<String> listElements )
110 {
111 StringBuilder sbTrace = new StringBuilder( );
112 sbTrace.append( "\n ---------------------- CRM Client WebService Call -------------------" );
113 sbTrace.append( "\nWebService URL : " ).append( strUrl );
114 sbTrace.append( "\nParameters : " );
115
116 for ( Entry<String, String> parameter : mapParameters.entrySet( ) )
117 {
118 sbTrace.append( "\n " ).append( parameter.getKey( ) ).append( ":" ).append( parameter.getValue( ) );
119 }
120
121 sbTrace.append( "\nSecurity : " );
122 sbTrace.append( "\nValues used for the request signature : " );
123
124 for ( String strValue : listElements )
125 {
126 sbTrace.append( "\n " ).append( strValue );
127 }
128
129 if ( authenticator instanceof RequestHashAuthenticator )
130 {
131 RequestHashAuthenticator auth = (RequestHashAuthenticator) authenticator;
132 String strTimestamp = "" + new Date( ).getTime( );
133 String strSignature = auth.buildSignature( listElements, strTimestamp );
134 sbTrace.append( "\n Request Authenticator : RequestHashAuthenticator" );
135 sbTrace.append( "\n Timestamp sample : " ).append( strTimestamp );
136 sbTrace.append( "\n Signature for this timestamp : " ).append( strSignature );
137 }
138 else
139 if ( authenticator instanceof HeaderHashAuthenticator )
140 {
141 HeaderHashAuthenticator auth = (HeaderHashAuthenticator) authenticator;
142 String strTimestamp = Long.toString( new Date( ).getTime( ) );
143 String strSignature = auth.buildSignature( listElements, strTimestamp );
144 sbTrace.append( "\n Request Authenticator : HeaderHashAuthenticator" );
145 sbTrace.append( "\n Timestamp sample : " ).append( strTimestamp );
146 sbTrace.append( "\n Signature for this timestamp : " ).append( strSignature );
147 }
148 else
149 if ( authenticator instanceof NoSecurityAuthenticator )
150 {
151 sbTrace.append( "\n No request authentification" );
152 }
153 else
154 {
155 sbTrace.append( "\n Unknown Request authenticator" );
156 }
157
158 sbTrace.append( "\n --------------------------------------------------------------------" );
159
160 return sbTrace.toString( );
161 }
162 }