From 18d5fb6fb10a0c806fe4eb4f0ef6d36973c820f5 Mon Sep 17 00:00:00 2001 From: Tanechka Date: Sat, 29 Apr 2017 10:08:39 -0700 Subject: [PATCH 1/2] send with attachments --- parent/pom.xml | 2 +- services/common-ws/pom.xml | 6 +++ .../masterjava/service/mail/MailService.java | 13 +++-- .../masterjava/service/mail/MailWSClient.java | 24 +++++++-- .../service/mail/MailWSClientMain.java | 10 +++- .../masterjava/service/mail/MailSender.java | 9 ++-- .../service/mail/MailServiceExecutor.java | 9 +++- .../service/mail/MailServiceImpl.java | 18 +++++-- .../service/mail/MailServiceClient.java | 17 ++++-- .../masterjava/webapp/SendServlet.java | 20 ++++++- .../main/webapp/WEB-INF/templates/users.html | 54 ++++++++++++++----- 11 files changed, 144 insertions(+), 38 deletions(-) diff --git a/parent/pom.xml b/parent/pom.xml index 46400fa..4e8a004 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -19,7 +19,7 @@ 1.7.25 - /apps/masterjava/config/ + c:/apps/masterjava/config/ false diff --git a/services/common-ws/pom.xml b/services/common-ws/pom.xml index 95ba18f..0b52b37 100644 --- a/services/common-ws/pom.xml +++ b/services/common-ws/pom.xml @@ -73,5 +73,11 @@ 3.1.0 provided + + + org.jvnet.mimepull + mimepull + 1.9.4 + \ No newline at end of file diff --git a/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailService.java b/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailService.java index f7e9cb3..d756924 100644 --- a/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailService.java +++ b/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailService.java @@ -2,11 +2,15 @@ import ru.javaops.web.WebStateException; +import javax.activation.DataHandler; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; +import javax.xml.bind.annotation.XmlMimeType; +import javax.xml.ws.soap.MTOM; import java.util.Set; +@MTOM @WebService(targetNamespace = "http://mail.javaops.ru/") //@SOAPBinding( // style = SOAPBinding.Style.DOCUMENT, @@ -19,12 +23,15 @@ String sendToGroup( @WebParam(name = "to") Set to, @WebParam(name = "cc") Set cc, @WebParam(name = "subject") String subject, - @WebParam(name = "body") String body) throws WebStateException; + @WebParam(name = "body") String body, + @XmlMimeType("application/octet-stream") DataHandler data, + @WebParam(name = "attacmentName") String attacmentName) throws WebStateException; @WebMethod GroupResult sendBulk( @WebParam(name = "to") Set to, @WebParam(name = "subject") String subject, - @WebParam(name = "body") String body) throws WebStateException; - + @WebParam(name = "body") String body, + @XmlMimeType("application/octet-stream") DataHandler data, + @WebParam(name = "attacmentName") String attacmentName) throws WebStateException; } \ No newline at end of file diff --git a/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java b/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java index e418d2d..f1fdbfe 100644 --- a/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java +++ b/services/mail-api/src/main/java/ru/javaops/masterjava/service/mail/MailWSClient.java @@ -8,7 +8,10 @@ import ru.javaops.web.WebStateException; import ru.javaops.web.WsClient; +import javax.activation.DataHandler; import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.soap.SOAPBinding; import java.util.Set; @Slf4j @@ -24,11 +27,20 @@ public class MailWSClient { } - public static String sendToGroup(final Set to, final Set cc, final String subject, final String body) throws WebStateException { + public static String sendToGroup(final Set to, + final Set cc, + final String subject, + final String body, + final DataHandler attachment, + final String attacmentName) throws WebStateException { log.info("Send mail to '" + to + "' cc '" + cc + "' subject '" + subject + (log.isDebugEnabled() ? "\nbody=" + body : "")); String status; try { - status = WS_CLIENT.getPort().sendToGroup(to, cc, subject, body); + MailService mailService = WS_CLIENT.getPort(); + BindingProvider bp = (BindingProvider) mailService; + SOAPBinding binding = (SOAPBinding) bp.getBinding(); + binding.setMTOMEnabled(true); + status = WS_CLIENT.getPort().sendToGroup(to, cc, subject, body, attachment, attacmentName); log.info("Sent with status: " + status); } catch (Exception e) { log.error("sendToGroup failed", e); @@ -37,11 +49,15 @@ public static String sendToGroup(final Set to, final Set c return status; } - public static GroupResult sendBulk(final Set to, final String subject, final String body) throws WebStateException { + public static GroupResult sendBulk(final Set to, + final String subject, + final String body, + final DataHandler attachment, + final String attacmentName) throws WebStateException { log.info("Send mail to '" + to + "' subject '" + subject + (log.isDebugEnabled() ? "\nbody=" + body : "")); GroupResult result; try { - result = WS_CLIENT.getPort().sendBulk(to, subject, body); + result = WS_CLIENT.getPort().sendBulk(to, subject, body, attachment, attacmentName); } catch (WebStateException e) { log.error("sendBulk failed", e); throw WsClient.getWebStateException(e); diff --git a/services/mail-api/src/test/java/ru/javaops/masterjava/service/mail/MailWSClientMain.java b/services/mail-api/src/test/java/ru/javaops/masterjava/service/mail/MailWSClientMain.java index 3e440fa..1c62ef3 100644 --- a/services/mail-api/src/test/java/ru/javaops/masterjava/service/mail/MailWSClientMain.java +++ b/services/mail-api/src/test/java/ru/javaops/masterjava/service/mail/MailWSClientMain.java @@ -3,15 +3,23 @@ import com.google.common.collect.ImmutableSet; import lombok.extern.slf4j.Slf4j; +import javax.activation.DataHandler; +import javax.activation.FileDataSource; +import java.io.File; + @Slf4j public class MailWSClientMain { public static void main(String[] args) { + + File file = new File("C:\\Users\\val\\Documents\\task.txt");//File.createTempFile("somefilename-", null, null); + DataHandler attachment = new DataHandler(new FileDataSource(file)); + ImmutableSet addressees = ImmutableSet.of( new Addressee("gkislin@javaops.ru"), new Addressee("Мастер Java "), new Addressee("Bad Email ")); try { - String state = MailWSClient.sendToGroup(addressees, ImmutableSet.of(), "Subject", "Body"); + String state = MailWSClient.sendToGroup(addressees, ImmutableSet.of(), "Subject", "Body", attachment, file.getName()); System.out.println(state); } catch (Throwable e) { log.error(e.toString(), e); diff --git a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailSender.java b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailSender.java index ce130dd..56d82c9 100644 --- a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailSender.java +++ b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailSender.java @@ -11,6 +11,7 @@ import ru.javaops.masterjava.service.mail.persist.MailCaseDao; import ru.javaops.web.WebStateException; +import javax.activation.DataHandler; import java.util.Set; /** @@ -21,18 +22,20 @@ public class MailSender { private static final MailCaseDao MAIL_CASE_DAO = DBIProvider.getDao(MailCaseDao.class); - static MailResult sendTo(Addressee to, String subject, String body) throws WebStateException { - val state = sendToGroup(ImmutableSet.of(to), ImmutableSet.of(), subject, body); + static MailResult sendTo(Addressee to, String subject, String body, DataHandler attachment, String attacmentName) throws WebStateException { + val state = sendToGroup(ImmutableSet.of(to), ImmutableSet.of(), subject, body, attachment, attacmentName); return new MailResult(to.getEmail(), state); } - static String sendToGroup(Set to, Set cc, String subject, String body) throws WebStateException { + static String sendToGroup(Set to, Set cc, String subject, String body, DataHandler attachment, String attacmentName) throws WebStateException { log.info("Send mail to \'" + to + "\' cc \'" + cc + "\' subject \'" + subject + (log.isDebugEnabled() ? "\nbody=" + body : "")); + String state = MailResult.OK; try { val email = MailConfig.createHtmlEmail(); email.setSubject(subject); email.setHtmlMsg(body); + if (attachment != null) email.attach(attachment.getDataSource(), attacmentName, "attachment"); for (Addressee addressee : to) { email.addTo(addressee.getEmail(), addressee.getName()); } diff --git a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceExecutor.java b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceExecutor.java index db64ec6..9906e5f 100644 --- a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceExecutor.java +++ b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceExecutor.java @@ -3,6 +3,7 @@ import lombok.extern.slf4j.Slf4j; import one.util.streamex.StreamEx; +import javax.activation.DataHandler; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -17,11 +18,15 @@ public class MailServiceExecutor { private static final ExecutorService mailExecutor = Executors.newFixedThreadPool(8); - public static GroupResult sendBulk(final Set addressees, final String subject, final String body) { + public static GroupResult sendBulk(final Set addressees, + final String subject, + final String body, + final DataHandler attachment, + final String attacmentName) { final CompletionService completionService = new ExecutorCompletionService<>(mailExecutor); List> futures = StreamEx.of(addressees) - .map(addressee -> completionService.submit(() -> MailSender.sendTo(addressee, subject, body))) + .map(addressee -> completionService.submit(() -> MailSender.sendTo(addressee, subject, body, attachment, attacmentName))) .toList(); return new Callable() { diff --git a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceImpl.java b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceImpl.java index 136fdbe..96d0d10 100644 --- a/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceImpl.java +++ b/services/mail-service/src/main/java/ru/javaops/masterjava/service/mail/MailServiceImpl.java @@ -2,6 +2,7 @@ import ru.javaops.web.WebStateException; +import javax.activation.DataHandler; import javax.jws.WebService; import java.util.Set; @@ -11,12 +12,21 @@ public class MailServiceImpl implements MailService { @Override - public String sendToGroup(Set to, Set cc, String subject, String body) throws WebStateException { - return MailSender.sendToGroup(to, cc, subject, body); + public String sendToGroup(Set to, + Set cc, + String subject, + String body, + DataHandler attachment, + String attacmentName) throws WebStateException { + return MailSender.sendToGroup(to, cc, subject, body, attachment, attacmentName); } @Override - public GroupResult sendBulk(Set to, String subject, String body) throws WebStateException { - return MailServiceExecutor.sendBulk(to, subject, body); + public GroupResult sendBulk(Set to, + String subject, + String body, + DataHandler attachment, + String attacmentName) throws WebStateException { + return MailServiceExecutor.sendBulk(to, subject, body, attachment, attacmentName); } } \ No newline at end of file diff --git a/services/mail-service/src/test/java/ru/javaops/masterjava/service/mail/MailServiceClient.java b/services/mail-service/src/test/java/ru/javaops/masterjava/service/mail/MailServiceClient.java index 53d90c6..1f1547e 100644 --- a/services/mail-service/src/test/java/ru/javaops/masterjava/service/mail/MailServiceClient.java +++ b/services/mail-service/src/test/java/ru/javaops/masterjava/service/mail/MailServiceClient.java @@ -3,8 +3,11 @@ import com.google.common.collect.ImmutableSet; import ru.javaops.web.WebStateException; +import javax.activation.DataHandler; +import javax.activation.FileDataSource; import javax.xml.namespace.QName; import javax.xml.ws.Service; +import java.io.File; import java.net.MalformedURLException; import java.net.URL; @@ -17,16 +20,20 @@ public static void main(String[] args) throws MalformedURLException { MailService mailService = service.getPort(MailService.class); + File file = new File("C:\\Users\\val\\Documents\\scooter.pdf");//File.createTempFile("somefilename-", null, null); + DataHandler attachment = new DataHandler(new FileDataSource(file)); + ImmutableSet addressees = ImmutableSet.of( - new Addressee("gkislin@javaops.ru"), - new Addressee("Мастер Java "), - new Addressee("Bad Email ")); + new Addressee("")//, + //new Addressee("Мастер Java "), + //new Addressee("Bad Email ") + ); try { - String status = mailService.sendToGroup(addressees, ImmutableSet.of(), "Bulk email subject", "Bulk email body"); + String status = mailService.sendToGroup(addressees, ImmutableSet.of(), "Bulk email subject", "Bulk email body", attachment, file.getName()); System.out.println(status); - GroupResult groupResult = mailService.sendBulk(addressees, "Individual mail subject", "Individual mail body"); + GroupResult groupResult = mailService.sendBulk(addressees, "Individual mail subject", "Individual mail body", attachment, file.getName()); System.out.println(groupResult); } catch (WebStateException e) { System.out.println(e); diff --git a/web/webapp/src/main/java/ru/javaops/masterjava/webapp/SendServlet.java b/web/webapp/src/main/java/ru/javaops/masterjava/webapp/SendServlet.java index 27effc5..61a0e47 100644 --- a/web/webapp/src/main/java/ru/javaops/masterjava/webapp/SendServlet.java +++ b/web/webapp/src/main/java/ru/javaops/masterjava/webapp/SendServlet.java @@ -1,29 +1,47 @@ package ru.javaops.masterjava.webapp; +import com.sun.xml.ws.util.ByteArrayDataSource; import lombok.extern.slf4j.Slf4j; import ru.javaops.masterjava.service.mail.MailWSClient; import ru.javaops.web.WebStateException; +import javax.activation.DataHandler; import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; import java.io.IOException; +@MultipartConfig(maxFileSize = 2 * 1024 * 1024 * 1024L) @WebServlet("/send") @Slf4j public class SendServlet extends HttpServlet { + @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); + String users = req.getParameter("users"); String subject = req.getParameter("subject"); String body = req.getParameter("body"); + Part part = req.getPart("attachment"); + String attachmentName = ""; + DataHandler attachment = null; + byte[] fileData = new byte[(int) part.getSize()]; + + if (part.getSize() != 0) { + part.getInputStream().read(fileData); + attachmentName = part.getSubmittedFileName(); + attachment = new DataHandler(new ByteArrayDataSource(fileData, null)); + } + String groupResult; try { - groupResult = MailWSClient.sendBulk(MailWSClient.split(users), subject, body).toString(); + groupResult = MailWSClient.sendBulk(MailWSClient.split(users), subject, body, attachment, attachmentName).toString(); } catch (WebStateException e) { groupResult = e.toString(); } diff --git a/web/webapp/src/main/webapp/WEB-INF/templates/users.html b/web/webapp/src/main/webapp/WEB-INF/templates/users.html index 9b2f3b7..f4b782b 100644 --- a/web/webapp/src/main/webapp/WEB-INF/templates/users.html +++ b/web/webapp/src/main/webapp/WEB-INF/templates/users.html @@ -2,18 +2,16 @@ Users + + + - - - - -
- +
@@ -36,18 +34,21 @@
#

+

-
-

-

- +

+
+

+ +
- \ No newline at end of file From 4adef11a2066d3d19c506916b08a57e51b738a5a Mon Sep 17 00:00:00 2001 From: Tanechka Date: Sat, 29 Apr 2017 16:50:20 -0700 Subject: [PATCH 2/2] wsdl --- config_templates/wsdl/mailService.wsdl | 141 +++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 config_templates/wsdl/mailService.wsdl diff --git a/config_templates/wsdl/mailService.wsdl b/config_templates/wsdl/mailService.wsdl new file mode 100644 index 0000000..1a120d8 --- /dev/null +++ b/config_templates/wsdl/mailService.wsdl @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file