View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * WebServiceCaller
55   *
56   */
57  public class WebServiceCaller implements IWebServiceCaller
58  {
59      /**
60       * {@inheritDoc}
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       * Trace the web service call
98       * 
99       * @param strUrl
100      *            The WS URI
101      * @param mapParameters
102      *            The parameters
103      * @param authenticator
104      *            The Request Authenticator
105      * @param listElements
106      *            The list of elements to use to build the signature
107      * @return The trace
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 }