Subversion Repositories linshare

[/] [branches/] [0.7/] [linshare/] [src/] [main/] [java/] [org/] [linagora/] [linShare/] [view/] [tapestry/] [rest/] [impl/] [ShareRestServiceImpl.java] - Blame information for rev 167

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 sbahloul
/*
2
 *    This file is part of Linshare.
3
 *
4
 *   Linshare is free software: you can redistribute it and/or modify
5
 *   it under the terms of the GNU Affero General Public License as
6
 *   published by the Free Software Foundation, either version 3 of
7
 *   the License, or (at your option) any later version.
8
 *
9
 *   Linshare is distributed in the hope that it will be useful,
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *   GNU Affero General Public License for more details.
13
 *
14
 *   You should have received a copy of the GNU Affero General Public
15
 *   License along with Foobar.  If not, see
16
 *                                    <http://www.gnu.org/licenses/>.
17
 *
18
 *   (c) 2008 Groupe Linagora - http://linagora.org
19
 *
20
*/
21
package org.linagora.linShare.view.tapestry.rest.impl;
22
 
23
import java.io.IOException;
24
import java.util.ArrayList;
25
import java.util.List;
26
 
27
import org.apache.commons.httpclient.HttpStatus;
28
import org.apache.tapestry5.Asset;
29
import org.apache.tapestry5.ioc.Messages;
30
import org.apache.tapestry5.ioc.services.ThreadLocale;
31
import org.apache.tapestry5.services.ApplicationStateManager;
32
import org.apache.tapestry5.services.Request;
33
import org.apache.tapestry5.services.Response;
34
import org.apache.tapestry5.services.ValidationMessagesSource;
35
import org.linagora.linShare.core.Facade.DocumentFacade;
36
import org.linagora.linShare.core.Facade.ShareFacade;
37
import org.linagora.linShare.core.Facade.UserFacade;
38
import org.linagora.linShare.core.domain.objects.SuccessesAndFailsItems;
39
import org.linagora.linShare.core.domain.vo.DocumentVo;
40
import org.linagora.linShare.core.domain.vo.ShareDocumentVo;
41
import org.linagora.linShare.core.domain.vo.UserVo;
42
import org.linagora.linShare.core.exception.BusinessException;
43
import org.linagora.linShare.view.tapestry.rest.ShareRestService;
44
import org.linagora.linShare.view.tapestry.services.Templating;
45
import org.linagora.linShare.view.tapestry.services.impl.PropertiesSymbolProvider;
46
import org.linagora.restmarshaller.Marshaller;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49
 
50
import us.antera.t5restfulws.RestfulWebMethod;
51
 
52
 
53
public class ShareRestServiceImpl implements ShareRestService {
54
 
55
        private final ApplicationStateManager applicationStateManager;
56
 
57
        private final ShareFacade shareFacade;
58
        private final UserFacade userFacade;
59
        private final DocumentFacade documentFacade;
60
 
61
        private final Marshaller xstreamMarshaller;
62
 
63
    private final Asset sharedTemplate;
64
    private final Asset sharedTemplateTxt;
65
    private final Asset passwordSharedTemplate;
66
    private final Asset passwordSharedTemplateTxt;
67
 
68
        private final PropertiesSymbolProvider propertiesSymbolProvider;
69
 
70
        private final Templating templating;
71
        private final ThreadLocale threadLocale;
72
        private final ValidationMessagesSource validationMessagesSource;
73
 
74
        private static final Logger logger = LoggerFactory.getLogger(ShareRestServiceImpl.class);
75
 
76
        public ShareRestServiceImpl(final ApplicationStateManager applicationStateManager,
77
                                final ShareFacade shareFacade,
78
                                final UserFacade userFacade,
79
                                final DocumentFacade documentFacade,
80
                                final Asset sharedTemplate,
81
                                final Asset sharedTemplateTxt,
82
                                final Asset passwordSharedTemplate,
83
                                final Asset passwordSharedTemplateTxt,
84
                                final PropertiesSymbolProvider propertiesSymbolProvider,
85
                                final Templating templating,  final ValidationMessagesSource validationMessagesSource,
86
                                final ThreadLocale threadLocale,
87
                                final Marshaller xstreamMarshaller) {
88
                super();
89
                this.applicationStateManager = applicationStateManager;
90
                this.shareFacade = shareFacade;
91
                this.userFacade = userFacade;
92
                this.documentFacade = documentFacade;
93
                this.sharedTemplate = sharedTemplate;
94
                this.sharedTemplateTxt = sharedTemplateTxt;
95
                this.passwordSharedTemplate = passwordSharedTemplate;
96
                this.passwordSharedTemplateTxt = passwordSharedTemplateTxt;
97
                this.propertiesSymbolProvider = propertiesSymbolProvider;
98
                this.templating = templating;
99
                this.threadLocale = threadLocale;
100
                this.validationMessagesSource = validationMessagesSource;
101
                this.xstreamMarshaller = xstreamMarshaller;
102
        }
103
 
104
 
105
        /* (non-Javadoc)
106
         * @see org.linagora.linShare.view.tapestry.rest.impl.ShareRestService#sharedocument(org.apache.tapestry5.services.Request, org.apache.tapestry5.services.Response, java.lang.String, java.lang.String)
107
         */
108
        @RestfulWebMethod
109
        public void sharedocument(Request request, Response response, String targetMail, String uuid) throws IOException {
110
                UserVo actor = applicationStateManager.getIfExists(UserVo.class);
111
 
112
                if (actor== null) {
113
                        response.sendError(HttpStatus.SC_UNAUTHORIZED, "You are not authorized to use this service");
114
                        return;
115
                }
116
 
117
                if ((actor.isGuest() && !actor.isUpload())) {
118
                        response.sendError(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
119
                        return;
120
                }
121
 
122
 
123
                // fetch the document
124
                DocumentVo docVo = documentFacade.getDocument(actor.getLogin(), uuid);
125
 
126
                if (docVo == null) {
127
                        response.sendError(HttpStatus.SC_NOT_FOUND, "Document not found");
128
                        return;
129
                }
130
 
131
                List<DocumentVo> listDoc = new ArrayList<DocumentVo>();
132
                listDoc.add(docVo);
133
 
134
                List<String> listRecipient = new ArrayList<String>();
135
                listRecipient.add(targetMail);
136
 
137
                SuccessesAndFailsItems<ShareDocumentVo> successes;
138
 
139
                // Prepare mail content :
140 167 sduprey
                String linShareUrlInternal=propertiesSymbolProvider.valueForSymbol("linshare.info.url.internal");
141
                String linShareUrlBase=propertiesSymbolProvider.valueForSymbol("linshare.info.url.base");
142 1 sbahloul
 
143
                String sharedTemplateContent = templating.readFullyTemplateContent(sharedTemplate.getResource().openStream());
144
                String sharedTemplateContentTxt = templating.readFullyTemplateContent(sharedTemplateTxt.getResource().openStream());
145
                String passwordSharedTemplateContent = templating.readFullyTemplateContent(passwordSharedTemplate.getResource().openStream());
146
                String passwordSharedTemplateContentTxt = templating.readFullyTemplateContent(passwordSharedTemplateTxt.getResource().openStream());
147
 
148
                // Get the messages service to localize the mail sent
149
                Messages messages = validationMessagesSource.getValidationMessages(threadLocale.getLocale());
150
 
151
                String subject = messages.get("mail.user.all.share.subject");
152
 
153
                // Get if it has to be a secured share
154
                String securedShareValue = request.getParameter("securedShare");
155
                boolean secureSharing = securedShareValue == null ? false : securedShareValue.equals("1");
156
 
157
                try {
158
                        successes = shareFacade.createSharingWithMailUsingRecipientsEmail(
159 167 sduprey
                    actor, listDoc, listRecipient, "", subject, linShareUrlInternal, linShareUrlBase,
160 1 sbahloul
                    secureSharing, sharedTemplateContent, sharedTemplateContentTxt, passwordSharedTemplateContent, passwordSharedTemplateContentTxt);
161
                } catch (BusinessException e) {
162
                        logger.error("could not share the document " + docVo.getIdentifier() + " to user " + targetMail + " by user " + actor.getMail() + " reason : " + e.getMessage());
163
                        response.setHeader("BusinessError", e.getErrorCode().getCode()+"");
164
                        response.sendError(HttpStatus.SC_METHOD_FAILURE, "Could not share the document");
165
                        return;
166
                }
167
 
168
                if ( (successes.getSuccessesItem()==null) || ((successes.getFailsItem()!=null) && (successes.getFailsItem().size()>0))) {
169
                        logger.error("could not share the document " + docVo.getIdentifier() + " to user " + targetMail + " by user " + actor.getMail());
170
                        response.sendError(HttpStatus.SC_METHOD_FAILURE, "Could not share the document");
171
                        return;
172
                }
173
 
174
                logger.debug("Shared the document " + docVo.getIdentifier() + " to user " + targetMail + " by user " + actor.getMail());
175
                response.setStatus(HttpStatus.SC_OK);
176
 
177
        }
178
 
179
        @RestfulWebMethod
180
        public void multiplesharedocuments(Request request, Response response)
181
                        throws IOException {
182
                UserVo actor = applicationStateManager.getIfExists(UserVo.class);
183
 
184
                if (actor== null) {
185
                        response.sendError(HttpStatus.SC_UNAUTHORIZED, "You are not authorized to use this service");
186
                        return;
187
                }
188
 
189
                if ((actor.isGuest() && !actor.isUpload())) {
190
                        response.sendError(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
191
                        return;
192
                }
193
 
194
                if (!"POST".equals(request.getMethod())) {
195
                        response.sendError(HttpStatus.SC_METHOD_NOT_ALLOWED, "Method not allowed");
196
                        response.setHeader("Allow", "POST");
197
                        return;
198
                }
199
 
200
                if (!request.getParameterNames().contains("targetMail")) {
201
                        response.sendError(HttpStatus.SC_BAD_REQUEST, "Missing parameter targetMail");
202
                        return;
203
                }
204
 
205
                if (!request.getParameterNames().contains("file")) {
206
                        response.sendError(HttpStatus.SC_BAD_REQUEST, "Missing parameter file");
207
                        return;
208
                }
209
                // fetch the target
210
                String targetMail = request.getParameter("targetMail");
211
                if ((targetMail == null) || (targetMail.equals(""))) {
212
                        response.sendError(HttpStatus.SC_BAD_REQUEST, "Target is empty");
213
                        return;
214
                }
215
                List<String> listRecipient = new ArrayList<String>();
216
                listRecipient.add(targetMail);
217
 
218
                // Get if it has to be a secured share
219
                String securedShareValue = request.getParameter("securedShare");
220
                boolean secureSharing = securedShareValue == null ? false : securedShareValue.equals("1");
221
 
222
                String fileUUID =request.getParameter("file") ;
223
                List<DocumentVo> listDoc = new ArrayList<DocumentVo>();
224
 
225
                // fetch the document
226
                DocumentVo docVo = documentFacade.getDocument(actor.getLogin(), fileUUID);
227
                if (docVo==null) {
228
                        response.sendError(HttpStatus.SC_NOT_FOUND, "Document " + fileUUID + " not found");
229
                        return;
230
                }
231
 
232
                listDoc.add(docVo);
233
 
234
                // fetching all the files
235
                int i=1;
236
                while (request.getParameterNames().contains("file"+i)) {
237
                        fileUUID =request.getParameter("file"+i) ;
238
                        // fetch the document
239
                        docVo = documentFacade.getDocument(actor.getLogin(), fileUUID);
240
                        if (docVo==null) {
241
                                response.sendError(HttpStatus.SC_NOT_FOUND, "Document " + fileUUID + " not found");
242
                                return;
243
                        }
244
                        listDoc.add(docVo);
245
                        i++;
246
                }
247
 
248
 
249
                // getting the optionnal message
250
                String message = "";
251
                if (request.getParameterNames().contains("message")) {
252
                        message = request.getParameter("message");
253
                }
254
 
255
                // getting the templating data
256 167 sduprey
                String linShareUrlInternal=propertiesSymbolProvider.valueForSymbol("linshare.info.url.internal");
257
                String linShareUrlBase=propertiesSymbolProvider.valueForSymbol("linshare.info.url.base");
258 1 sbahloul
 
259
 
260
                String sharedTemplateContent = null;
261
                String sharedTemplateContentTxt = null;
262
                String passwordSharedTemplateContent = null;
263
                String passwordSharedTemplateContentTxt = null;
264
 
265
                sharedTemplateContent = templating.readFullyTemplateContent(sharedTemplate.getResource().openStream());
266
                sharedTemplateContentTxt = templating.readFullyTemplateContent(sharedTemplateTxt.getResource().openStream());
267
                passwordSharedTemplateContent = templating.readFullyTemplateContent(passwordSharedTemplate.getResource().openStream());
268
                passwordSharedTemplateContentTxt = templating.readFullyTemplateContent(passwordSharedTemplateTxt.getResource().openStream());
269
 
270
                /**
271
                 * retrieve the subject of the mail.
272
                 */
273
                // Get the messages service to localize the mail sent
274
                Messages messages = validationMessagesSource.getValidationMessages(threadLocale.getLocale());
275
 
276
                String subject=messages.get("mail.user.all.share.subject");
277
 
278
                SuccessesAndFailsItems<ShareDocumentVo> successes;
279
 
280
                try {
281
                        successes = shareFacade.createSharingWithMailUsingRecipientsEmail(
282 167 sduprey
                                        actor, listDoc, listRecipient, message, subject, linShareUrlInternal, linShareUrlBase, secureSharing,
283 1 sbahloul
                                        sharedTemplateContent, sharedTemplateContentTxt,passwordSharedTemplateContent,passwordSharedTemplateContentTxt);
284
                } catch (BusinessException e) {
285
                        logger.error("could not share the document " + docVo.getIdentifier() + " to user " + targetMail + " by user " + actor.getMail() + " reason : " + e.getMessage());
286
                        response.setHeader("BusinessError", e.getErrorCode().getCode()+"");
287
                        response.sendError(HttpStatus.SC_METHOD_FAILURE, "Could not share the document");
288
                        return;
289
 
290
                }
291
                if ( (successes.getSuccessesItem()==null) || ((successes.getFailsItem()!=null) && (successes.getFailsItem().size()>0))) {
292
                        logger.error("could not share the documents");
293
                        response.sendError(HttpStatus.SC_METHOD_FAILURE, "Could not share the documents");
294
                        return;
295
                }
296
 
297
                response.setStatus(HttpStatus.SC_OK);
298
 
299
        }
300
 
301
 
302
 
303
}