use of org.nhindirect.schema.edge.ws.ErrorType in project nhin-d by DirectProject.
the class MessageServiceImplService method checkAuth.
/**
* Ensures that the request has been
* @throws Exception
*/
private void checkAuth(SendResponseType response) {
// Check for authorization before we do anything
// This SHOULD be done at the security interceptor, but you
// never know.
Authentication inAuth = SecurityContextHolder.getContext().getAuthentication();
if (!inAuth.isAuthenticated()) {
ErrorType et = new ErrorType();
et.setCode(ErrorCodeType.NOT_AUTH);
et.setMessage("User: " + inAuth.getName() + " is not authorized to use this service");
response.setError(et);
log.warn("Unauthorized attempt to use the Message Service!");
}
username = inAuth.getName();
password = (String) inAuth.getCredentials();
}
use of org.nhindirect.schema.edge.ws.ErrorType in project nhin-d by DirectProject.
the class MessageServiceImplService method sendMessage.
@Override
public /**
* Converts an incoming WS request into an email message and sends it to the configured
* email server
*/
SendResponseType sendMessage(EmailType body) {
if (log.isDebugEnabled())
log.debug("Enter");
SendResponseType response = new SendResponseType();
checkAuth(response);
if (response.getError() == null) {
log.info("Auth success");
Multipart mailBody;
MimeBodyPart mainBody;
MimeBodyPart mimeAttach;
String fromaddress = body.getHead().getFrom().getAddress();
log.info("Got FROM address");
try {
InternetAddress addressFrom;
addressFrom = new InternetAddress(fromaddress.toString());
if (log.isDebugEnabled())
log.debug("Sender: " + addressFrom);
InternetAddress[] addressTo = new InternetAddress[1];
int i = 0;
for (AddressType recipient : body.getHead().getTo()) {
addressTo[i] = new InternetAddress(recipient.getAddress());
if (log.isDebugEnabled())
log.debug("Recipient: " + addressTo[i]);
i++;
}
Session session = Session.getInstance(smtpProps, new SMTPAuthenticator());
// Build message object
MimeMessage mimeMsg = new MimeMessage(session);
mimeMsg.setFrom(addressFrom);
mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
if (body.getHead().getSubject() != null) {
mimeMsg.setSubject(body.getHead().getSubject());
} else {
mimeMsg.setSubject("Direct message");
}
mailBody = new MimeMultipart();
mainBody = new MimeBodyPart();
if (body.getBody().getText() != null) {
mainBody.setText(body.getBody().getText());
} else {
mainBody.setText("");
}
mailBody.addBodyPart(mainBody);
copyAttachments(body, mailBody);
mimeMsg.setContent(mailBody);
DirectMimeMessage dMsg = new DirectMimeMessage(mimeMsg, getSenderHost());
dMsg.updateMessageID();
Transport transport;
if (getUseTLSforSMTP().equals("SOCKET")) {
transport = session.getTransport("smtps");
} else {
transport = session.getTransport("smtp");
}
transport.connect();
try {
transport.sendMessage(dMsg, addressTo);
// Transport.send(dMsg);
response.setMessageID(dMsg.getMessageID());
transport.close();
} finally {
transport.close();
}
} catch (AddressException e) {
ErrorType et = new ErrorType();
et.setCode(ErrorCodeType.ADDRESSING);
et.setMessage(e.getMessage());
response.setError(et);
log.error(e);
} catch (MessagingException e) {
ErrorType et = new ErrorType();
et.setCode(ErrorCodeType.MESSAGING);
et.setMessage(e.getMessage());
response.setError(et);
log.error(e);
} catch (Exception e) {
ErrorType et = new ErrorType();
et.setCode(ErrorCodeType.SYSTEM);
et.setMessage(e.getMessage());
response.setError(et);
log.error(e);
e.printStackTrace();
}
}
return response;
}
Aggregations