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.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
54
55
56
57
58 public class ArchiveClientWsService extends AbstractArchiveClientService
59 {
60 private RequestAuthenticator _requestAuthenticatorForWS;
61
62
63
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
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
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
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
108 Map<String, String> params = new HashMap<String, String>( );
109 params.put( ArchiveClientConstants.PARAM_ARCHIVE_ITEM_KEY, Integer.toString( archiveItemKey ) );
110
111
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
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
137 Map<String, String> params = new HashMap<String, String>( );
138 params.put( ArchiveClientConstants.PARAM_ARCHIVE_ITEM_KEY, Integer.toString( archiveItemKey ) );
139
140
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
157
158
159
160
161
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
185
186
187 public void setRequestAuthenticatorForWS( RequestAuthenticator requestAuthenticatorForWS )
188 {
189 this._requestAuthenticatorForWS = requestAuthenticatorForWS;
190 }
191 }