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);
}
}
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;
}
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;
}
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()));
}
}
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;
}
Aggregations