View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.archiveclient.service.archive;
35  
36  import fr.paris.lutece.plugins.archiveclient.service.util.ArchiveClientConstants;
37  import fr.paris.lutece.plugins.archiveclient.service.util.ArchiveClientException;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.service.util.AppPropertiesService;
40  import fr.paris.lutece.util.httpaccess.HttpAccess;
41  import fr.paris.lutece.util.httpaccess.HttpAccessException;
42  import fr.paris.lutece.util.signrequest.RequestAuthenticator;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import java.util.ArrayList;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  
52  /**
53   * ArchiveClientWsService
54   *
55   * @author merlinfe
56   *
57   */
58  public class ArchiveClientWsService extends AbstractArchiveClientService
59  {
60      private RequestAuthenticator _requestAuthenticatorForWS;
61  
62      /**
63       * {@inheritDoc}
64       */
65      public int generateArchive( String strFolderToArchive, String strArchiveDestination, String strArchiveName,
66          String strArchiveType ) throws ArchiveClientException
67      {
68          int nIdgenarateArchive = -1;
69  
70          String strUrl = AppPropertiesService.getProperty( ArchiveClientConstants.PROPERTY_WEBAPP_ARCHIVE_REST_URL ) +
71              ArchiveClientConstants.URL_REST_GENERATE_ARCHIVE;
72  
73          // List parameters to post
74          Map<String, String> params = new HashMap<String, String>(  );
75          params.put( ArchiveClientConstants.PARAM_FOLDER_TO_ARCHIVE, strFolderToArchive );
76          params.put( ArchiveClientConstants.PARAM_ARCHIVE_DESTINATION, strArchiveDestination );
77          params.put( ArchiveClientConstants.PARAM_ARCHIVE_NAME, strArchiveName );
78          params.put( ArchiveClientConstants.PARAM_ARCHIVE_TYPE, strArchiveType );
79  
80          // List elements to include to the signature
81          List<String> listElements = new ArrayList<String>(  );
82  
83          try
84          {
85              String strResponse = callArchiveWs( strUrl, params, listElements );
86              nIdgenarateArchive = Integer.parseInt( strResponse );
87          }
88          catch ( Exception e )
89          {
90              AppLogService.error( e );
91              throw new ArchiveClientException( e );
92          }
93  
94          return nIdgenarateArchive;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public String informationArchive( int archiveItemKey )
101         throws ArchiveClientException
102     {
103         String strResponse = null;
104         String strUrl = AppPropertiesService.getProperty( ArchiveClientConstants.PROPERTY_WEBAPP_ARCHIVE_REST_URL ) +
105             ArchiveClientConstants.URL_REST_INFORMATION_ARCHIVE;
106 
107         // List parameters to post
108         Map<String, String> params = new HashMap<String, String>(  );
109         params.put( ArchiveClientConstants.PARAM_ARCHIVE_ITEM_KEY, Integer.toString( archiveItemKey ) );
110 
111         // List elements to include to the signature
112         List<String> listElements = new ArrayList<String>(  );
113         listElements.add( Integer.toString( archiveItemKey ) );
114 
115         try
116         {
117             strResponse = callArchiveWs( strUrl, params, listElements );
118         }
119         catch ( Exception e )
120         {
121             AppLogService.error( e );
122             throw new ArchiveClientException( e );
123         }
124 
125         return strResponse;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     public void removeArchive( int archiveItemKey ) throws ArchiveClientException
132     {
133         String strUrl = AppPropertiesService.getProperty( ArchiveClientConstants.PROPERTY_WEBAPP_ARCHIVE_REST_URL ) +
134             ArchiveClientConstants.URL_REST_REMOVE_ARCHIVE;
135 
136         // List parameters to post
137         Map<String, String> params = new HashMap<String, String>(  );
138         params.put( ArchiveClientConstants.PARAM_ARCHIVE_ITEM_KEY, Integer.toString( archiveItemKey ) );
139 
140         // List elements to include to the signature
141         List<String> listElements = new ArrayList<String>(  );
142         listElements.add( Integer.toString( archiveItemKey ) );
143 
144         try
145         {
146             callArchiveWs( strUrl, params, listElements );
147         }
148         catch ( Exception e )
149         {
150             AppLogService.error( e );
151             throw new ArchiveClientException( e );
152         }
153     }
154 
155     /**
156      * This method calls Rest WS archive
157      * @param strUrl the url
158      * @param params the params to pass in the post
159      * @param listElements the list of elements to include in the signature
160      * @return the response as a string
161      * @throws HttpAccessException the exception if there is a problem
162      */
163     private String callArchiveWs( String strUrl, Map<String, String> params, List<String> listElements )
164         throws HttpAccessException
165     {
166         String strResponse = StringUtils.EMPTY;
167 
168         try
169         {
170             HttpAccess httpAccess = new HttpAccess(  );
171             strResponse = httpAccess.doPost( strUrl, params, _requestAuthenticatorForWS, listElements );
172         }
173         catch ( HttpAccessException e )
174         {
175             String strError = "ArchiveWebServices - Error connecting to '" + strUrl + "' : ";
176             AppLogService.error( strError + e.getMessage(  ), e );
177             throw new HttpAccessException( strError, e );
178         }
179 
180         return strResponse;
181     }
182 
183     /**
184      * Setter method for requestAuthenticatorForWS
185      * @param requestAuthenticatorForWS the request authenticator
186      */
187     public void setRequestAuthenticatorForWS( RequestAuthenticator requestAuthenticatorForWS )
188     {
189         this._requestAuthenticatorForWS = requestAuthenticatorForWS;
190     }
191 }