/
ForumMessageServiceImpl.java
405 lines (370 loc) · 14.7 KB
/
ForumMessageServiceImpl.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jdon.jivejdon.api.impl.message;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.jdon.annotation.Service;
import com.jdon.annotation.intercept.Poolable;
import com.jdon.annotation.intercept.SessionContextAcceptable;
import com.jdon.container.visitor.data.SessionContext;
import com.jdon.controller.events.EventModel;
import com.jdon.jivejdon.api.ForumMessageService;
import com.jdon.jivejdon.api.property.UploadService;
import com.jdon.jivejdon.api.util.SessionContextUtil;
import com.jdon.jivejdon.auth.NoPermissionException;
import com.jdon.jivejdon.auth.ResourceAuthorization;
import com.jdon.jivejdon.domain.command.PostRepliesMessageCommand;
import com.jdon.jivejdon.domain.command.PostTopicMessageCommand;
import com.jdon.jivejdon.domain.command.ReviseForumMessageCommand;
import com.jdon.jivejdon.domain.model.Forum;
import com.jdon.jivejdon.domain.model.ForumMessage;
import com.jdon.jivejdon.domain.model.ForumThread;
import com.jdon.jivejdon.domain.model.account.Account;
import com.jdon.jivejdon.domain.model.attachment.AttachmentsVO;
import com.jdon.jivejdon.domain.model.attachment.UploadFile;
import com.jdon.jivejdon.domain.model.message.output.RenderingFilterManager;
import com.jdon.jivejdon.domain.model.property.MessagePropertysVO;
import com.jdon.jivejdon.domain.model.property.Property;
import com.jdon.jivejdon.domain.service.MessageDomainService;
import com.jdon.jivejdon.infrastructure.dto.AnemicMessageDTO;
import com.jdon.jivejdon.infrastructure.repository.ForumFactory;
import com.jdon.jivejdon.spi.component.filter.InFilterManager;
import com.jdon.jivejdon.util.Constants;
import com.jdon.util.UtilValidate;
/**
* ForumMessageServiceImpl is the shell of ForumMessage core implementions.
*
* invoking order: ForumMessageServiceImpl --> MessageContentFilter --->
* ForumMessageServiceImp
*
* @author <a href="mailto:banq@163.com">banq</a>
*
*/
@Poolable
@Service("forumMessageService")
public class ForumMessageServiceImpl implements ForumMessageService {
private final static Logger logger = LogManager.getLogger(ForumMessageServiceImpl.class);
protected final InFilterManager inFilterManager;
protected final SessionContextUtil sessionContextUtil;
protected final ResourceAuthorization resourceAuthorization;
protected final MessageDomainService messageKernel;
protected final UploadService uploadService;
protected final ForumFactory forumBuilder;
private final RenderingFilterManager renderingFilterManager;
protected SessionContext sessionContext;
public ForumMessageServiceImpl(SessionContextUtil sessionContextUtil, ResourceAuthorization messageServiceAuth,
InFilterManager inFilterManager, MessageDomainService messageKernel, UploadService uploadService,
ForumFactory forumBuilder, RenderingFilterManager renderingFilterManager) {
this.sessionContextUtil = sessionContextUtil;
this.resourceAuthorization = messageServiceAuth;
this.inFilterManager = inFilterManager;
this.renderingFilterManager = renderingFilterManager;
this.messageKernel = messageKernel;
this.uploadService = uploadService;
this.forumBuilder = forumBuilder;
}
/**
* must be often checked, every request, if use login.
*
* @param forumMessage
*/
public boolean checkIsAuthenticated(ForumMessage forumMessage) {
boolean allowEdit = false;
if (forumMessage == null)
return allowEdit;
if (!forumMessage.isCreated())
return allowEdit;// forumMessage is null or only has mesageId
Account account = sessionContextUtil.getLoginAccount(sessionContext);
allowEdit = resourceAuthorization.isAuthenticated(forumMessage, account);
return allowEdit;
}
/**
* create Topic Message
*/
public Long createTopicMessage(EventModel em) throws Exception {
AnemicMessageDTO forumMessagePostDTO = (AnemicMessageDTO) em.getModelIF();
if (!prepareCreate(forumMessagePostDTO))
return null;
Forum forum = forumBuilder.getForum(forumMessagePostDTO.getForum().getForumId());
if (forum == null)
return null;
Long mIDInt = forumBuilder.getNextId(Constants.MESSAGE);
try {
List<UploadFile> uploads = uploadService.loadAllUploadFilesOfMessage(mIDInt, sessionContext);
AttachmentsVO attachmentsVO = new AttachmentsVO(mIDInt, uploads);
Account operator = sessionContextUtil.getLoginAccount(sessionContext);
Collection<Property> properties = new ArrayList<>();
properties.add(new Property(MessagePropertysVO.PROPERTY_IP, operator.getPostIP()));
MessagePropertysVO messagePropertysVO = new MessagePropertysVO();
messagePropertysVO.replacePropertys(properties);
if (!UtilValidate.isEmpty(forumMessagePostDTO.getMessageVO().getBody())
|| !UtilValidate.isEmpty(forumMessagePostDTO.getMessageVO().getSubject())) {
PostTopicMessageCommand postTopicMessageCommand = new PostTopicMessageCommand(mIDInt, forum, operator,
inFilterManager.applyFilters(forumMessagePostDTO.getMessageVO()), attachmentsVO,
messagePropertysVO, forumMessagePostDTO.getTagTitle(),forumMessagePostDTO.getToken());
messageKernel.post(forum.getForumId(), forum, postTopicMessageCommand);
}
} catch (Exception e) {
logger.error(e);
em.setErrors(Constants.ERRORS);
} finally {
uploadService.clearSession(sessionContext);
}
// for MessageListNavAction
forumMessagePostDTO.setMessageId(mIDInt);
return mIDInt;
}
/**
* set the login account into the domain model
*/
public Long createReplyMessage(EventModel em) throws Exception {
AnemicMessageDTO forumMessageReplyPostDTO = (AnemicMessageDTO) em.getModelIF();
if (UtilValidate.isEmpty(forumMessageReplyPostDTO.getMessageVO().getBody()))
return null;
if ((forumMessageReplyPostDTO.getParentMessage() == null
|| forumMessageReplyPostDTO.getParentMessage().getMessageId() == null)) {
return null;
}
ForumMessage parentMessage = getMessage(forumMessageReplyPostDTO.getParentMessage().getMessageId());
if (parentMessage == null) {
logger.error("not this parent Message: " + forumMessageReplyPostDTO.getParentMessage().getMessageId());
return null;
}
if (!prepareCreate(forumMessageReplyPostDTO))
return null;
Long mIDInt = this.forumBuilder.getNextId(Constants.MESSAGE);
try {
List<UploadFile> uploads = uploadService.loadAllUploadFilesOfMessage(mIDInt, sessionContext);
AttachmentsVO attachmentsVO = new AttachmentsVO(mIDInt, uploads);
forumMessageReplyPostDTO.setAttachment(attachmentsVO);
Account operator = sessionContextUtil.getLoginAccount(sessionContext);
forumMessageReplyPostDTO.setOperator(operator);
forumMessageReplyPostDTO.setAccount(operator);
Collection<Property> properties = new ArrayList<>();
properties.add(new Property(MessagePropertysVO.PROPERTY_IP, operator.getPostIP()));
MessagePropertysVO messagePropertysVO = new MessagePropertysVO();
messagePropertysVO.replacePropertys(properties);
PostRepliesMessageCommand postRepliesMessageCommand = new PostRepliesMessageCommand(parentMessage, mIDInt,
operator, inFilterManager.applyFilters(forumMessageReplyPostDTO.getMessageVO()), attachmentsVO,
messagePropertysVO, forumMessageReplyPostDTO.getTagTitle());
messageKernel.addreply(parentMessage.getForumThread().getThreadId(), parentMessage,
postRepliesMessageCommand);
} catch (Exception e) {
logger.error(e);
em.setErrors(Constants.ERRORS);
} finally {
uploadService.clearSession(sessionContext);
}
// for MessageListNav2Action
forumMessageReplyPostDTO.setMessageId(mIDInt);
return mIDInt;
}
private boolean prepareCreate(AnemicMessageDTO forumMessage) throws Exception {
// the poster
Account account = sessionContextUtil.getLoginAccount(sessionContext);
if (account == null)
return false;
forumMessage.setAccount(account);
return true;
}
private boolean isAuthenticated(ForumMessage forumMessage) {
try {
Account account = sessionContextUtil.getLoginAccount(sessionContext);
resourceAuthorization.verifyAuthenticated(forumMessage, account);
return true;
} catch (NoPermissionException e) {
logger.error("No Permission to operate it mesageId=" + forumMessage.getMessageId());
}
return false;
}
/**
* 1. auth check: amdin and the owner can modify this nessage. 2. if the message
* has childern, only admin can update it. before business logic, we must get a
* true message from persistence layer, now the ForumMessage packed in
* EventModel object is not full, it is a DTO from prensentation layer.
*
*
*/
public void updateMessage(EventModel em) throws Exception {
AnemicMessageDTO newForumMessageInputparamter = (AnemicMessageDTO) em.getModelIF();
ForumMessage oldforumMessage = messageKernel.getMessage(newForumMessageInputparamter.getMessageId());
if (oldforumMessage == null)
return;
if (!isAuthenticated(oldforumMessage)) {
em.setErrors(Constants.NOPERMISSIONS);
return;
}
try {
Account operator = sessionContextUtil.getLoginAccount(sessionContext);
List<UploadFile> uploads = uploadService.loadAllUploadFilesOfMessage(oldforumMessage.getMessageId(),
this.sessionContext);
AttachmentsVO attachmentsVO = new AttachmentsVO(newForumMessageInputparamter.getMessageId(), uploads);
Collection<Property> properties = new ArrayList<>();
properties.add(new Property(MessagePropertysVO.PROPERTY_IP, operator.getPostIP()));
MessagePropertysVO messagePropertysVO = new MessagePropertysVO();
messagePropertysVO.replacePropertys(properties);
ReviseForumMessageCommand reviseForumMessageCommand = new ReviseForumMessageCommand(oldforumMessage,
inFilterManager.applyFilters(newForumMessageInputparamter.getMessageVO()), attachmentsVO,
messagePropertysVO);
messageKernel.revise(oldforumMessage.getForumThread().getThreadId(), oldforumMessage,
reviseForumMessageCommand);
} catch (Exception e) {
logger.error(e);
em.setErrors(Constants.ERRORS);
} finally {
uploadService.clearSession(sessionContext);
}
}
public void updateThreadName(Long threadId, String name) throws Exception {
Optional<ForumThread> forumThreadOptional = messageKernel.getThread(threadId);
if (!forumThreadOptional.isPresent())
return;
if (!isAuthenticated(forumThreadOptional.get().getRootMessage())) {
return;
}
forumThreadOptional.get().updateName(name);
}
/**
* delete a message the auth is same as to the updateMessage
*
*
*/
public void deleteMessage(EventModel em) throws Exception {
AnemicMessageDTO anemicMessageDTO = (AnemicMessageDTO) em.getModelIF();
ForumMessage forumMessage = messageKernel.getMessage(anemicMessageDTO.getMessageId());
if (forumMessage == null)
return;
if (!isAuthenticated(forumMessage)) {
em.setErrors(Constants.NOPERMISSIONS);
return;
}
// em.setModelIF(forumMessage);
try {
messageKernel.deleteMessage(anemicMessageDTO);
} catch (Exception e) {
em.setErrors(Constants.ERRORS);
logger.error(e);
}
}
public void deleteUserMessages(String username) throws Exception {
try {
Account account = sessionContextUtil.getLoginAccount(sessionContext);
if (resourceAuthorization.isAdmin(account))
messageKernel.deleteUserMessages(username);
} catch (Exception e) {
logger.error(e);
throw new Exception(e);
}
}
/**
* only admin or moderator can mask a message
*/
public void maskMessage(EventModel em) throws Exception {
logger.debug("enter maskMessage");
Account account = sessionContextUtil.getLoginAccount(sessionContext);
if (!resourceAuthorization.isAdmin(account))
return;
ForumMessage forumMessageParamter = (ForumMessage) em.getModelIF();
// masked value is from messageForm and from
// /message/messageMaskAction.shtml?method=maskMessage&masked=false
boolean masked;
try {
masked = forumMessageParamter.isMasked();
ForumMessage forumMessage = messageKernel.getMessage(forumMessageParamter.getMessageId());
if (forumMessage == null) {
logger.error("the message don't existed!");
return;
}
forumMessage.updateMasked(masked);
} catch (Exception e) {
logger.error(e);
}
}
/**
* get the full forum in forumMessage, and return it.
*/
public AnemicMessageDTO initMessage(EventModel em) {
return messageKernel.initMessage(em);
}
public AnemicMessageDTO initReplyMessage(EventModel em) {
return messageKernel.initReplyMessage(em);
}
/*
* return message with body filter to client; return a full ForumMessage need
* solve the relations with Forum ForumThread
*/
public ForumMessage getMessage(Long messageId) {
if (messageId == null)
return null;
return messageKernel.getMessage(messageId);
}
/**
* find a Message for modify
*/
public AnemicMessageDTO findMessage(Long messageId) {
if (messageId == null)
return null;
logger.debug("enter ForumMessageServiceImpl's findMessage");
ForumMessage message = messageKernel.getMessage(messageId);
if (message == null)
return null;
try {
ForumMessage newMessage = (ForumMessage) message.clone();
newMessage.reloadMessageVOOrignal();
AnemicMessageDTO anemicMessageDTO = new AnemicMessageDTO();
anemicMessageDTO.setMessageId(newMessage.getMessageId());
anemicMessageDTO.setForum(newMessage.getForum());
anemicMessageDTO.setAccount(newMessage.getAccount());
anemicMessageDTO.setMessageVO(newMessage.getMessageVO());
anemicMessageDTO.setForumThread(newMessage.getForumThread());
// after update must enable it see updateMessage;
return anemicMessageDTO;
} catch (Exception e) {
logger.error(e);
}
return null;
}
/**
* return a full ForumThread one ForumThread has one rootMessage need solve the
* realtion with Forum rootForumMessage latestPost
*
* @param threadId
* @return
*/
public ForumThread getThread(Long threadId) {
return messageKernel.getThread(threadId).orElse(null);
}
public RenderingFilterManager getFilterManager() {
return this.renderingFilterManager;
}
/**
* @return Returns the sessionContext.
*/
public SessionContext getSessionContext() {
return sessionContext;
}
/**
* @param sessionContext The sessionContext to set.
*/
@SessionContextAcceptable
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
}