Search in sources :

Example 6 with OutboundMessageResponse

use of org.hisp.dhis.outboundmessage.OutboundMessageResponse in project dhis2-core by dhis2.

the class SmsMessageSender method handleResponse.

private OutboundMessageResponse handleResponse(OutboundMessageResponse status) {
    Set<GatewayResponse> okCodes = Sets.newHashSet(GatewayResponse.RESULT_CODE_0, GatewayResponse.RESULT_CODE_200, GatewayResponse.RESULT_CODE_202);
    GatewayResponse gatewayResponse = (GatewayResponse) status.getResponseObject();
    if (okCodes.contains(gatewayResponse)) {
        log.info("SMS sent");
        return new OutboundMessageResponse(gatewayResponse.getResponseMessage(), gatewayResponse, true);
    } else {
        log.error("SMS failed, failure cause: " + gatewayResponse.getResponseMessage());
        return new OutboundMessageResponse(gatewayResponse.getResponseMessage(), gatewayResponse, false);
    }
}
Also used : GatewayResponse(org.hisp.dhis.sms.outbound.GatewayResponse) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse)

Example 7 with OutboundMessageResponse

use of org.hisp.dhis.outboundmessage.OutboundMessageResponse in project dhis2-core by dhis2.

the class SmsMessageSender method generateSummary.

private OutboundMessageResponseSummary generateSummary(List<OutboundMessageResponse> statuses, OutboundMessageBatch batch, SmsGateway smsGateway) {
    Set<GatewayResponse> okCodes = Sets.newHashSet(GatewayResponse.RESULT_CODE_0, GatewayResponse.RESULT_CODE_200, GatewayResponse.RESULT_CODE_202);
    OutboundMessageResponseSummary summary = new OutboundMessageResponseSummary();
    int total, sent = 0;
    boolean ok = true;
    String errorMessage = StringUtils.EMPTY;
    total = batch.getMessages().size();
    for (OutboundMessageResponse status : statuses) {
        if (okCodes.contains(status.getResponseObject())) {
            sent = (smsGateway instanceof BulkSmsGateway) ? total : sent + 1;
        } else {
            ok = false;
            errorMessage = status.getDescription();
        }
    }
    summary.setTotal(total);
    summary.setChannel(DeliveryChannel.SMS);
    summary.setSent(sent);
    summary.setFailed(total - sent);
    if (!ok) {
        summary.setBatchStatus(OutboundMessageBatchStatus.FAILED);
        summary.setErrorMessage(errorMessage);
        log.error(errorMessage);
    } else {
        summary.setBatchStatus(OutboundMessageBatchStatus.COMPLETED);
        summary.setResponseMessage("SENT");
        log.info("SMS batch processed successfully");
    }
    return summary;
}
Also used : GatewayResponse(org.hisp.dhis.sms.outbound.GatewayResponse) OutboundMessageResponseSummary(org.hisp.dhis.outboundmessage.OutboundMessageResponseSummary) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse)

Example 8 with OutboundMessageResponse

use of org.hisp.dhis.outboundmessage.OutboundMessageResponse in project dhis2-core by dhis2.

the class BulkSmsGateway method getResponse.

private OutboundMessageResponse getResponse(ResponseEntity<String> responseEntity) {
    OutboundMessageResponse status = new OutboundMessageResponse();
    if (responseEntity == null) {
        status.setResponseObject(GatewayResponse.FAILED);
        status.setOk(false);
        return status;
    }
    String response = responseEntity.getBody();
    GatewayResponse gatewayResponse = BULKSMS_GATEWAY_RESPONSE_MAP.get(StringUtils.split(response, "|")[0]);
    gatewayResponse.setBatchId(StringUtils.split(response, "|")[2]);
    status.setResponseObject(gatewayResponse);
    status.setDescription(gatewayResponse.getResponseMessage());
    return status;
}
Also used : GatewayResponse(org.hisp.dhis.sms.outbound.GatewayResponse) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse)

Example 9 with OutboundMessageResponse

use of org.hisp.dhis.outboundmessage.OutboundMessageResponse in project dhis2-core by dhis2.

the class SmsController method sendSMSMessage.

@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/outbound", method = RequestMethod.POST, consumes = "application/json")
public void sendSMSMessage(HttpServletResponse response, HttpServletRequest request) throws WebMessageException, IOException {
    OutboundSms sms = renderService.fromJson(request.getInputStream(), OutboundSms.class);
    OutboundMessageResponse status = smsSender.sendMessage(null, sms.getMessage(), sms.getRecipients());
    if (status.isOk()) {
        webMessageService.send(WebMessageUtils.ok("SMS sent"), response, request);
    } else {
        throw new WebMessageException(WebMessageUtils.error(status.getDescription()));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) OutboundSms(org.hisp.dhis.sms.outbound.OutboundSms) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with OutboundMessageResponse

use of org.hisp.dhis.outboundmessage.OutboundMessageResponse in project dhis2-core by dhis2.

the class EmailMessageSender method sendMessage.

@Override
public OutboundMessageResponse sendMessage(String subject, String text, Set<String> recipients) {
    EmailConfiguration emailConfig = getEmailConfiguration();
    String errorMessage = "No recipient found";
    OutboundMessageResponse status = new OutboundMessageResponse();
    if (emailConfig.getHostName() == null) {
        status.setOk(false);
        status.setResponseObject(EmailResponse.NOT_CONFIGURED);
        return status;
    }
    try {
        HtmlEmail email = getHtmlEmail(emailConfig.getHostName(), emailConfig.getPort(), emailConfig.getUsername(), emailConfig.getPassword(), emailConfig.isTls(), emailConfig.getFrom());
        email.setSubject(customizeTitle(DEFAULT_SUBJECT_PREFIX) + subject);
        email.setTextMsg(text);
        boolean hasRecipients = false;
        for (String recipient : recipients) {
            if (isEmailValid(recipient)) {
                email.addBcc(recipient);
                hasRecipients = true;
                log.info("Sending email to : " + recipient + " to host: " + emailConfig.getHostName() + ":" + emailConfig.getPort());
            } else {
                log.error(recipient + " is not a valid email");
                errorMessage = "No valid email address found";
            }
        }
        if (hasRecipients) {
            email.send();
            log.info("Email sent using host: " + emailConfig.getHostName() + ":" + emailConfig.getPort() + " with TLS: " + emailConfig.isTls());
            return new OutboundMessageResponse("Email sent", EmailResponse.SENT, true);
        } else {
            status = new OutboundMessageResponse(errorMessage, EmailResponse.ABORTED, false);
        }
    } catch (EmailException ex) {
        log.warn("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    } catch (RuntimeException ex) {
        log.warn("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    }
    return status;
}
Also used : EmailConfiguration(org.hisp.dhis.email.EmailConfiguration) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse)

Aggregations

OutboundMessageResponse (org.hisp.dhis.outboundmessage.OutboundMessageResponse)11 GatewayResponse (org.hisp.dhis.sms.outbound.GatewayResponse)4 IOException (java.io.IOException)2 EmailException (org.apache.commons.mail.EmailException)2 HtmlEmail (org.apache.commons.mail.HtmlEmail)2 EmailConfiguration (org.hisp.dhis.email.EmailConfiguration)2 OutboundMessageResponseSummary (org.hisp.dhis.outboundmessage.OutboundMessageResponseSummary)2 User (org.hisp.dhis.user.User)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 OutboundSms (org.hisp.dhis.sms.outbound.OutboundSms)1 UserGroup (org.hisp.dhis.user.UserGroup)1 Async (org.springframework.scheduling.annotation.Async)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1