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.service.processor;
35  
36  import fr.paris.lutece.plugins.crmclient.business.ICRMItem;
37  import fr.paris.lutece.plugins.crmclient.service.authenticator.AuthenticatorService;
38  import fr.paris.lutece.plugins.crmclient.util.CRMException;
39  import fr.paris.lutece.plugins.crmclient.util.http.HttpMethodEnum;
40  import fr.paris.lutece.plugins.crmclient.util.http.IWebServiceCaller;
41  import fr.paris.lutece.util.url.UrlItem;
42  
43  import org.springframework.beans.factory.InitializingBean;
44  
45  import org.springframework.util.Assert;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Map.Entry;
50  
51  import javax.inject.Inject;
52  import javax.inject.Named;
53  
54  /**
55   *
56   * CRMClientWSProcessor
57   *
58   */
59  public class CRMClientWSProcessor implements ICRMClientProcessor, InitializingBean
60  {
61      private List<String> _listSignatureElements;
62      @Inject
63      @Named( "crmclient.webServiceCaller" )
64      private IWebServiceCaller _webServiceCaller;
65      @Inject
66      @Named( "crmclient.requestAuthenticatorService" )
67      private AuthenticatorService _authenticatorService;
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String doProcess( ICRMItem crmItem ) throws CRMException
74      {
75          // List elements to include to the signature
76          List<String> listElements = buildListElements( crmItem );
77  
78          return _webServiceCaller.callWebService( crmItem.getUrlForWS( ), crmItem.getParameters( ),
79                  _authenticatorService.getRequestAuthenticatorForWs( crmItem.getCRMWebAppCode( ) ), listElements, HttpMethodEnum.POST );
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public String getProcess( ICRMItem crmItem ) throws CRMException
87      {
88          // List elements to include to the signature
89          List<String> listElements = buildListElements( crmItem );
90          UrlItem url = new UrlItem( crmItem.getUrlForWS( ) );
91  
92          for ( Entry<String, String> parameter : crmItem.getParameters( ).entrySet( ) )
93          {
94              url.addParameter( parameter.getKey( ), parameter.getValue( ) );
95          }
96  
97          return _webServiceCaller.callWebService( url.getUrl( ), crmItem.getParameters( ),
98                  _authenticatorService.getRequestAuthenticatorForWs( crmItem.getCRMWebAppCode( ) ), listElements, HttpMethodEnum.GET );
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void afterPropertiesSet( ) throws Exception
106     {
107         Assert.notNull( _webServiceCaller, "The WebService Caller must be set." );
108         Assert.notNull( _listSignatureElements, "The list of signature elements must be set." );
109     }
110 
111     /**
112      * Set the signature elements
113      * 
114      * @param listSignatureElements
115      *            the signature elements
116      */
117     public void setSignatureElements( List<String> listSignatureElements )
118     {
119         _listSignatureElements = listSignatureElements;
120     }
121 
122     /**
123      * Set the web service caller
124      * 
125      * @param webServiceCaller
126      *            the web service caller
127      */
128     public void setWebServiceCaller( IWebServiceCaller webServiceCaller )
129     {
130         _webServiceCaller = webServiceCaller;
131     }
132 
133     /**
134      * Build the list of elements to put on the request authenticator
135      * 
136      * @param crmItem
137      *            the crm item
138      * @return the list of elements
139      */
140     private List<String> buildListElements( ICRMItem crmItem )
141     {
142         List<String> listElements = new ArrayList<String>( );
143 
144         for ( String signatureElement : _listSignatureElements )
145         {
146             if ( crmItem.getParameters( ).containsKey( signatureElement ) )
147             {
148                 listElements.add( crmItem.getParameters( ).get( signatureElement ) );
149             }
150         }
151 
152         return listElements;
153     }
154 }