Search in sources :

Example 1 with Message

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);
}
Also used : UserMessage(org.hisp.dhis.message.UserMessage) Message(org.hisp.dhis.message.Message) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with Message

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;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) UserMessage(org.hisp.dhis.message.UserMessage) WebMessageUtils.created(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created) Pagination(org.hisp.dhis.query.Pagination) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) MessageService(org.hisp.dhis.message.MessageService) CurrentUser(org.hisp.dhis.user.CurrentUser) MessageConversationStatus(org.hisp.dhis.message.MessageConversationStatus) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) Message(org.hisp.dhis.message.Message) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) MessageConversationPriority(org.hisp.dhis.message.MessageConversationPriority) MessageType(org.hisp.dhis.message.MessageType) UserGroup(org.hisp.dhis.user.UserGroup) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Junction(org.hisp.dhis.query.Junction) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Defaults(org.hisp.dhis.fieldfilter.Defaults) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) FileResourceUtils(org.hisp.dhis.webapi.utils.FileResourceUtils) UserGroupService(org.hisp.dhis.user.UserGroupService) WebMessageUtils.conflict(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMessageUtils.notFound(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) QueryParserException(org.hisp.dhis.query.QueryParserException) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) Pager(org.hisp.dhis.common.Pager) FileResource(org.hisp.dhis.fileresource.FileResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) MessageConversationSchemaDescriptor(org.hisp.dhis.schema.descriptors.MessageConversationSchemaDescriptor) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) HttpStatus(org.springframework.http.HttpStatus) ConfigurationService(org.hisp.dhis.configuration.ConfigurationService) FileResourceDomain(org.hisp.dhis.fileresource.FileResourceDomain) Collections(java.util.Collections) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UserMessage(org.hisp.dhis.message.UserMessage) Message(org.hisp.dhis.message.Message) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException)

Aggregations

WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)2 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 FileResource (org.hisp.dhis.fileresource.FileResource)2 Message (org.hisp.dhis.message.Message)2 UserMessage (org.hisp.dhis.message.UserMessage)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Pager (org.hisp.dhis.common.Pager)1