use of org.hisp.dhis.message.Message in project dhis2-core by dhis2.
the class MessageConversationController method getAttachment.
@GetMapping("/{mcUid}/{msgUid}/attachments/{fileUid}")
public void getAttachment(@PathVariable(value = "mcUid") String mcUid, @PathVariable(value = "msgUid") String msgUid, @PathVariable(value = "fileUid") String fileUid, @CurrentUser User currentUser, HttpServletResponse response) throws WebMessageException {
Message message = getMessage(mcUid, msgUid, currentUser);
FileResource fr = fileResourceService.getFileResource(fileUid);
if (message == null) {
throw new WebMessageException(notFound("No message found with id '" + msgUid + "' for message conversation with id '" + mcUid + "'"));
}
boolean attachmentExists = message.getAttachments().stream().filter(att -> att.getUid().equals(fileUid)).count() == 1;
if (fr == null || !attachmentExists) {
throw new WebMessageException(notFound("No messageattachment found with id '" + fileUid + "'"));
}
if (!fr.getDomain().equals(FileResourceDomain.MESSAGE_ATTACHMENT)) {
throw new WebMessageException(conflict("Invalid messageattachment."));
}
fileResourceUtils.configureFileResourceResponse(response, fr);
}
use of org.hisp.dhis.message.Message in project dhis2-core by dhis2.
the class MessageConversationController method getMessage.
/**
* /* Returns the specified message after making sure the user has access to
* it.
*
* @param mcUid the message conversation UID.
* @param msgUid the message UID.
* @param user the user.
* @return a {@link Message}.
* @throws WebMessageException
*/
private Message getMessage(String mcUid, String msgUid, User user) throws WebMessageException {
org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(mcUid);
if (conversation == null) {
throw new WebMessageException(notFound(String.format("No message conversation with uid '%s'", mcUid)));
}
if (!canReadMessageConversation(user, conversation)) {
throw new AccessDeniedException("Not authorized to access this conversation.");
}
List<Message> messages = conversation.getMessages().stream().filter(msg -> msg.getUid().equals(msgUid)).collect(Collectors.toList());
if (messages.size() < 1) {
throw new WebMessageException(notFound(String.format("No message with uid '%s' in messageConversation '%s", msgUid, mcUid)));
}
Message message = messages.get(0);
if (message.isInternal() && !configurationService.isUserInFeedbackRecipientUserGroup(user)) {
throw new WebMessageException(conflict("Not authorized to access this message"));
}
return message;
}
Aggregations