daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.jimkast.xsl.ext;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.net.URLDecoder;
  12. import java.net.URLStreamHandler;
  13. import java.nio.charset.StandardCharsets;
  14. import java.util.Collections;
  15. import java.util.List;
  16. import java.util.Properties;
  17. import javax.activation.DataHandler;
  18. import javax.activation.URLDataSource;
  19. import javax.mail.BodyPart;
  20. import javax.mail.Message;
  21. import javax.mail.MessagingException;
  22. import javax.mail.Multipart;
  23. import javax.mail.Session;
  24. import javax.mail.Transport;
  25. import javax.mail.internet.InternetAddress;
  26. import javax.mail.internet.MimeBodyPart;
  27. import javax.mail.internet.MimeMessage;
  28. import javax.mail.internet.MimeMultipart;
  29. import javax.mail.util.ByteArrayDataSource;
  30. import org.cactoos.Proc;
  31. import org.cactoos.func.IoCheckedProc;
  32. import org.eclipse.jetty.util.MultiMap;
  33. import org.eclipse.jetty.util.UrlEncoded;
  34.  
  35. public final class EmailExtendedURL extends URLStreamHandler {
  36.     private final Session session;
  37.  
  38.     public EmailExtendedURL() {
  39.         Properties props = new Properties();
  40.         props.setProperty("mail.smtp.host", "localhost");
  41.         props.setProperty("mail.smtp.port", "25");
  42.         this.session = Session.getDefaultInstance(props);
  43.     }
  44.  
  45.     public EmailExtendedURL(Session session) {
  46.         this.session = session;
  47.     }
  48.  
  49.     @Override
  50.     protected URLConnection openConnection(URL u) {
  51.         return new Conn(session, u);
  52.     }
  53.  
  54.  
  55.     public static final class Conn extends URLConnection {
  56.         private OutputStream out = null;
  57.         private Session session;
  58.  
  59.         public Conn(Session session, URL url) {
  60.             super(url);
  61.             this.session = session;
  62.         }
  63.  
  64.         @Override
  65.         public void connect() {
  66.             if (out == null) {
  67.                 out = EmailOutputStream.fromUrl(session, getURL());
  68.             }
  69.         }
  70.  
  71.         @Override
  72.         public OutputStream getOutputStream() {
  73.             return out;
  74.         }
  75.     }
  76.  
  77.    
  78.     public static final class EmailOutputStream extends OutputStream {
  79.         private static final List<String> EMPTY = Collections.emptyList();
  80.         private static final List<String> SINGLE_EMPTY = Collections.singletonList("");
  81.         private static final List<String> CONTENT_TYPE = Collections.singletonList("text/plain");
  82.    
  83.         private final Session session;
  84.         private final MultiMap<String> params;
  85.         private final IoCheckedProc<Message> wire;
  86.         private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  87.         private boolean flushed = false;
  88.    
  89.         public EmailOutputStream(Session session, MultiMap<String> params) {
  90.             this(session, params, Transport::send);
  91.         }
  92.    
  93.         public EmailOutputStream(Session session, MultiMap<String> params, Proc<Message> wire) {
  94.             this(session, params, new IoCheckedProc<>(wire));
  95.         }
  96.    
  97.         public EmailOutputStream(Session session, MultiMap<String> params, IoCheckedProc<Message> wire) {
  98.             this.session = session;
  99.             this.params = params;
  100.             this.wire = wire;
  101.         }
  102.    
  103.         @Override
  104.         public void write(int b) {
  105.             baos.write(b);
  106.         }
  107.    
  108.         @Override
  109.         public void write(byte[] b) throws IOException {
  110.             baos.write(b);
  111.         }
  112.    
  113.         @Override
  114.         public void write(byte[] b, int off, int len) {
  115.             baos.write(b, off, len);
  116.         }
  117.    
  118.         @Override
  119.         public void flush() throws IOException {
  120.             if (flushed) {
  121.                 return;
  122.             }
  123.             flushed = true;
  124.             try {
  125.                 MimeMessage msg = new MimeMessage(session);
  126.                 msg.setSubject(params.getOrDefault("subject", SINGLE_EMPTY).get(0));
  127.                 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(params.getOrDefault("to", SINGLE_EMPTY).get(0)));
  128.                 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(params.getOrDefault("cc", SINGLE_EMPTY).get(0)));
  129.                 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(params.getOrDefault("bcc", SINGLE_EMPTY).get(0)));
  130.                 msg.setReplyTo(InternetAddress.parse(params.getOrDefault("reply-to", SINGLE_EMPTY).get(0)));
  131.                 String from = params.getOrDefault("from", SINGLE_EMPTY).get(0);
  132.                 if (!from.isEmpty()) {
  133.                     msg.setFrom(new InternetAddress(from));
  134.                 }
  135.                 Multipart mp = new MimeMultipart();
  136.                 BodyPart main = new MimeBodyPart();
  137.                 main.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(baos.toByteArray()), params.getOrDefault("content-type", CONTENT_TYPE).get(0))));
  138.     //            main.setContent(baos.toByteArray(), params.getOrDefault("content-type", CONTENT_TYPE).get(0));
  139.                 mp.addBodyPart(main);
  140.                 for (String href : params.getOrDefault("attachment", EMPTY)) {
  141.                     BodyPart part = new MimeBodyPart();
  142.                     URL url = new URL(href);
  143.                     part.setDataHandler(new DataHandler(new URLDataSource(url)));
  144.                     part.setFileName(URLDecoder.decode(url.getPath().substring(url.getPath().lastIndexOf('/') + 1), "UTF-8"));
  145.                     mp.addBodyPart(part);
  146.                 }
  147.                 msg.setContent(mp);
  148.                 wire.exec(msg);
  149.             } catch (MessagingException e) {
  150.                 throw new IOException(e);
  151.             }
  152.         }
  153.    
  154.         @Override
  155.         public void close() throws IOException {
  156.             this.flush();
  157.         }
  158.    
  159.    
  160.         public static OutputStream fromUrl(Session session, URL url) {
  161.             MultiMap<String> params = new MultiMap<>();
  162.             String q = url.getQuery();
  163.             if (q != null && !q.isEmpty()) {
  164.                 UrlEncoded.decodeTo(q, params, StandardCharsets.UTF_8);
  165.             }
  166.             try {
  167.                 params.add("to", URLDecoder.decode(url.getPath(), "UTF-8"));
  168.                 return new EmailOutputStream(session, params);
  169.             } catch (UnsupportedEncodingException e) {
  170.                 throw new RuntimeException(e);
  171.             }
  172.         }
  173.    
  174.      
  175.         public static OutputStream fromUrl(Session session, String url) throws MalformedURLException {
  176.             return fromUrl(session, new URL(url));
  177.         }
  178.     }
  179. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top