Search in sources :

Example 1 with ErrorType

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();
}
Also used : ErrorType(org.nhindirect.schema.edge.ws.ErrorType) PasswordAuthentication(javax.mail.PasswordAuthentication) Authentication(org.springframework.security.core.Authentication)

Example 2 with ErrorType

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;
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) SendResponseType(org.nhindirect.schema.edge.ws.SendResponseType) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) ErrorType(org.nhindirect.schema.edge.ws.ErrorType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) MimeBodyPart(javax.mail.internet.MimeBodyPart) AddressType(org.nhindirect.schema.edge.ws.AddressType) Transport(javax.mail.Transport) Session(javax.mail.Session)

Aggregations

ErrorType (org.nhindirect.schema.edge.ws.ErrorType)2 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 Multipart (javax.mail.Multipart)1 PasswordAuthentication (javax.mail.PasswordAuthentication)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 AddressType (org.nhindirect.schema.edge.ws.AddressType)1 SendResponseType (org.nhindirect.schema.edge.ws.SendResponseType)1 InvalidPropertyException (org.springframework.beans.InvalidPropertyException)1 Authentication (org.springframework.security.core.Authentication)1