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.blobstore.modules.rest.rs; |
35 | |
|
36 | |
import java.io.InputStream; |
37 | |
import java.util.HashMap; |
38 | |
import java.util.Map; |
39 | |
|
40 | |
import javax.servlet.http.HttpServletRequest; |
41 | |
import javax.ws.rs.Consumes; |
42 | |
import javax.ws.rs.FormParam; |
43 | |
import javax.ws.rs.GET; |
44 | |
import javax.ws.rs.POST; |
45 | |
import javax.ws.rs.Path; |
46 | |
import javax.ws.rs.PathParam; |
47 | |
import javax.ws.rs.Produces; |
48 | |
import javax.ws.rs.core.Context; |
49 | |
import javax.ws.rs.core.MediaType; |
50 | |
|
51 | |
import org.apache.commons.io.IOUtils; |
52 | |
import org.apache.commons.lang.StringUtils; |
53 | |
import org.springframework.beans.factory.BeanDefinitionStoreException; |
54 | |
import org.springframework.beans.factory.CannotLoadBeanClassException; |
55 | |
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
56 | |
|
57 | |
import com.sun.jersey.core.header.FormDataContentDisposition; |
58 | |
|
59 | |
import fr.paris.lutece.plugins.blobstore.modules.rest.util.constants.BlobStoreRestConstants; |
60 | |
import fr.paris.lutece.plugins.blobstore.service.BlobStoreFileItem; |
61 | |
import fr.paris.lutece.plugins.blobstore.service.BlobStorePlugin; |
62 | |
import fr.paris.lutece.plugins.blobstore.service.IBlobStoreService; |
63 | |
import fr.paris.lutece.plugins.blobstore.service.NoSuchBlobException; |
64 | |
import fr.paris.lutece.plugins.rest.service.RestConstants; |
65 | |
import fr.paris.lutece.portal.service.spring.SpringContextService; |
66 | |
import fr.paris.lutece.portal.service.template.AppTemplateService; |
67 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
68 | |
import fr.paris.lutece.portal.service.util.AppPathService; |
69 | |
import fr.paris.lutece.util.html.HtmlTemplate; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
@Path( RestConstants.BASE_PATH + BlobStorePlugin.PLUGIN_NAME ) |
78 | 0 | public class BlobStoreRest |
79 | |
{ |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
@GET |
86 | |
@Path( BlobStoreRestConstants.PATH_WADL ) |
87 | |
@Produces( MediaType.APPLICATION_XML ) |
88 | |
public String getWADL( @Context HttpServletRequest request ) |
89 | |
{ |
90 | 0 | StringBuilder sbBase = new StringBuilder( AppPathService.getBaseUrl( request ) ); |
91 | |
|
92 | 0 | if ( sbBase.toString( ).endsWith( BlobStoreRestConstants.SLASH ) ) |
93 | |
{ |
94 | 0 | sbBase.deleteCharAt( sbBase.length( ) - 1 ); |
95 | |
} |
96 | |
|
97 | 0 | sbBase.append( RestConstants.BASE_PATH + BlobStorePlugin.PLUGIN_NAME ); |
98 | |
|
99 | 0 | Map<String, Object> model = new HashMap<String, Object>( ); |
100 | 0 | model.put( BlobStoreRestConstants.MARK_BASE_URL, sbBase.toString( ) ); |
101 | |
|
102 | 0 | HtmlTemplate t = AppTemplateService.getTemplate( BlobStoreRestConstants.TEMPLATE_WADL, request.getLocale( ), |
103 | |
model ); |
104 | |
|
105 | 0 | return t.getHtml( ); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
@GET |
115 | |
@Path( BlobStoreRestConstants.PATH_FILE_URL ) |
116 | |
@Produces( MediaType.TEXT_PLAIN ) |
117 | |
public String getFileUrl( @PathParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore, |
118 | |
@PathParam( BlobStoreRestConstants.PARAMETER_BLOB_KEY ) String strBlobKey ) |
119 | |
{ |
120 | 0 | String strDownloadUrl = StringUtils.EMPTY; |
121 | |
|
122 | 0 | if ( StringUtils.isNotBlank( strBlobStore ) && StringUtils.isNotBlank( strBlobKey ) ) |
123 | |
{ |
124 | |
IBlobStoreService blobStoreService; |
125 | |
|
126 | |
try |
127 | |
{ |
128 | 0 | blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, |
129 | |
strBlobStore ); |
130 | 0 | strDownloadUrl = blobStoreService.getFileUrl( strBlobKey ); |
131 | |
} |
132 | 0 | catch ( BeanDefinitionStoreException e ) |
133 | |
{ |
134 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
135 | |
} |
136 | 0 | catch ( NoSuchBeanDefinitionException e ) |
137 | |
{ |
138 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
139 | |
} |
140 | 0 | catch ( CannotLoadBeanClassException e ) |
141 | |
{ |
142 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
143 | 0 | } |
144 | |
} |
145 | |
else |
146 | |
{ |
147 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS ); |
148 | |
} |
149 | |
|
150 | 0 | return strDownloadUrl; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
@POST |
160 | |
@Path( BlobStoreRestConstants.PATH_DELETE_BLOBSTORE ) |
161 | |
@Produces( MediaType.TEXT_HTML ) |
162 | |
@Consumes( MediaType.APPLICATION_FORM_URLENCODED ) |
163 | |
public String doDeleteBlobStore( @FormParam( BlobStoreRestConstants.PARAMETER_BLOB_KEY ) String strBlobKey, |
164 | |
@FormParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore ) |
165 | |
{ |
166 | 0 | String strResponse = StringUtils.EMPTY; |
167 | |
|
168 | 0 | if ( StringUtils.isNotBlank( strBlobKey ) && StringUtils.isNotBlank( strBlobStore ) ) |
169 | |
{ |
170 | 0 | strResponse = strBlobKey; |
171 | |
|
172 | |
IBlobStoreService blobStoreService; |
173 | |
|
174 | |
try |
175 | |
{ |
176 | 0 | blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, |
177 | |
strBlobStore ); |
178 | |
|
179 | 0 | BlobStoreFileItem fileItem = new BlobStoreFileItem( strBlobKey, blobStoreService ); |
180 | 0 | fileItem.delete( ); |
181 | |
} |
182 | 0 | catch ( BeanDefinitionStoreException e ) |
183 | |
{ |
184 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
185 | |
} |
186 | 0 | catch ( NoSuchBeanDefinitionException e ) |
187 | |
{ |
188 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
189 | |
} |
190 | 0 | catch ( CannotLoadBeanClassException e ) |
191 | |
{ |
192 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
193 | |
} |
194 | 0 | catch ( NoSuchBlobException e ) |
195 | |
{ |
196 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
197 | 0 | } |
198 | |
} |
199 | |
else |
200 | |
{ |
201 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS ); |
202 | |
} |
203 | |
|
204 | 0 | return strResponse; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
@POST |
215 | |
@Path( BlobStoreRestConstants.PATH_CREATE_BLOBSTORE ) |
216 | |
@Consumes( MediaType.MULTIPART_FORM_DATA ) |
217 | |
public String doCreateBlobStore( @FormParam( BlobStoreRestConstants.PARAMETER_BLOBSTORE ) String strBlobStore, |
218 | |
@FormParam( BlobStoreRestConstants.PARAMETER_BLOB ) InputStream blob, |
219 | |
@FormParam( BlobStoreRestConstants.PARAMETER_BLOB ) FormDataContentDisposition blobDetail ) |
220 | |
{ |
221 | 0 | String strBlobKey = StringUtils.EMPTY; |
222 | |
|
223 | 0 | if ( StringUtils.isNotBlank( strBlobStore ) && ( blob != null ) ) |
224 | |
{ |
225 | |
IBlobStoreService blobStoreService; |
226 | |
|
227 | |
try |
228 | |
{ |
229 | 0 | blobStoreService = (IBlobStoreService) SpringContextService.getPluginBean( BlobStorePlugin.PLUGIN_NAME, |
230 | |
strBlobStore ); |
231 | 0 | strBlobKey = blobStoreService.storeInputStream( blob ); |
232 | |
|
233 | 0 | String strJSON = BlobStoreFileItem.buildFileMetadata( blobDetail.getFileName( ), blobDetail.getSize( ), |
234 | 0 | strBlobKey, blobDetail.getType( ) ); |
235 | |
|
236 | 0 | if ( AppLogService.isDebugEnabled( ) ) |
237 | |
{ |
238 | 0 | AppLogService.debug( "Storing " + blobDetail.getName( ) + " with : " + strJSON ); |
239 | |
} |
240 | |
|
241 | 0 | strBlobKey = blobStoreService.store( strJSON.getBytes( ) ); |
242 | |
} |
243 | 0 | catch ( BeanDefinitionStoreException e ) |
244 | |
{ |
245 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
246 | |
} |
247 | 0 | catch ( NoSuchBeanDefinitionException e ) |
248 | |
{ |
249 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
250 | |
} |
251 | 0 | catch ( CannotLoadBeanClassException e ) |
252 | |
{ |
253 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_NO_SUCH_BLOBSTORE ); |
254 | |
} |
255 | |
finally |
256 | |
{ |
257 | 0 | IOUtils.closeQuietly( blob ); |
258 | 0 | } |
259 | |
} |
260 | |
else |
261 | |
{ |
262 | 0 | AppLogService.error( BlobStoreRestConstants.MESSAGE_MANDATORY_FIELDS ); |
263 | |
} |
264 | |
|
265 | 0 | return strBlobKey; |
266 | |
} |
267 | |
} |