This project has retired. For details please refer to its Attic page.
UDDI_090_Smtp_ExternalTest xref
View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v3.tck;
17  
18  import java.io.BufferedInputStream;
19  import java.io.InputStream;
20  import java.util.Enumeration;
21  import java.util.Properties;
22  import javax.mail.Flags;
23  import javax.mail.Folder;
24  import javax.mail.Message;
25  import javax.mail.Session;
26  import javax.mail.Store;
27  import javax.mail.internet.MimeMessage;
28  import org.apache.commons.codec.net.QuotedPrintableCodec;
29  import org.apache.commons.configuration.ConfigurationException;
30  import org.apache.commons.mail.util.MimeMessageParser;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  
34  /**
35   *
36   * @author Alex O'Ree
37   */
38  public class UDDI_090_Smtp_ExternalTest extends UDDI_090_SubscriptionListenerIntegrationBase {
39  
40          public UDDI_090_Smtp_ExternalTest(){
41                  super();
42          }
43          @AfterClass
44          public static void stop() throws ConfigurationException {
45                  if (!TckPublisher.isEnabled()) return;
46                  stopManager();
47  
48          }
49  
50          @BeforeClass
51          public static void start() throws Exception {
52                  if (!TckPublisher.isEnabled()) return;
53                  startManager();
54                  email = TckPublisher.getProperties().getProperty("mail.pop3.to");
55          }
56  
57          static String  email = TckPublisher.getProperties().getProperty("mail.pop3.to");
58  
59          @Override
60          public boolean verifyDelivery(String findMe) {
61                  logger.info("Waiting " + TckPublisher.getSubscriptionTimeout() + " seconds for delivery, searching for " + findMe);
62                  boolean received = false;
63                  for (int i = 0; i < TckPublisher.getSubscriptionTimeout(); i++) {
64                          try {
65                                  Thread.sleep(1000);
66                          } catch (InterruptedException ex) {
67                          }
68                          System.out.print(".");
69                          if (fetchMail(findMe) > 0) {
70                                  logger.info("Received Email Notification");
71                                  received = true;
72                                  break;
73                          }
74                  }
75                  return received;
76          }
77  
78          @Override
79          public void reset() {
80  
81          }
82  
83          @Override
84          public String getXMLLocationOfServiceForDelivery() {
85                  return TckSubscriptionListener.LISTENER_SMTP_SERVICE_EXTERNAL_XML;
86          }
87  
88          @Override
89          public String getTransport() {
90                  return "SMTP";
91          }
92  
93          @Override
94          public int getPort() {
95                  return 0;
96          }
97  
98          @Override
99          public String getHostame() {
100                 return TckPublisher.getProperties().getProperty("mail.pop3.to");
101         }
102 
103         /**
104          * gets all current messages from the mail server and returns the number
105          * of messages containing the string, messages containing the string are
106          * deleted from the mail server String is the body of each message
107          *
108          * @return number of matching and deleted messages
109          * @param contains a string to search for
110          */
111         private static int fetchMail(String contains) {
112 
113                 //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
114 
115                 /* Set the mail properties */
116                 Properties properties = TckPublisher.getProperties();
117                 // Set manual Properties
118 
119                 int found = 0;
120                 Session session = Session.getDefaultInstance(properties, null);
121                 Store store = null;
122                 try {
123                         store = session.getStore("pop3");
124 
125                         store.connect(properties.getProperty("mail.pop3.host"), Integer.parseInt(properties.getProperty("mail.pop3.port", "110")), properties.getProperty("mail.pop3.username"), properties.getProperty("mail.pop3.password"));
126                         /* Mention the folder name which you want to read. */
127                         // inbox = store.getDefaultFolder();
128                         // inbox = inbox.getFolder("INBOX");
129                         Folder inbox = store.getFolder("INBOX");
130 
131                         /* Open the inbox using store. */
132                         inbox.open(Folder.READ_WRITE);
133 
134                         Message messages[] = inbox.getMessages();
135 
136                         for (int i = 0; i < messages.length; i++) {
137 
138                                 MimeMessageParser p = new MimeMessageParser(new MimeMessage(session, messages[i].getInputStream()));
139                                 Enumeration allHeaders = p.getMimeMessage().getAllHeaders();
140                                 while (allHeaders.hasMoreElements()) {
141                                         Object j = allHeaders.nextElement();
142                                         if (j instanceof javax.mail.Header) {
143                                                 javax.mail.Header msg = (javax.mail.Header) j;
144                                                 logger.info("XML as message header is " + msg.getValue());
145                                                 if (msg.getValue().contains(contains)) {
146                                                         //found it
147                                                         messages[i].setFlag(Flags.Flag.DELETED, true);
148                                                         found++;
149                                                 }
150                                         }
151                                 }
152                                 for (int k = 0; k < p.getAttachmentList().size(); k++) {
153                                         InputStream is = p.getAttachmentList().get((k)).getInputStream();
154                                         QuotedPrintableCodec qp = new QuotedPrintableCodec();
155                                         // If "is" is not already buffered, wrap a BufferedInputStream
156                                         // around it.
157                                         if (!(is instanceof BufferedInputStream)) {
158                                                 is = new BufferedInputStream(is);
159                                         }
160                                         int c;
161                                         StringBuilder sb = new StringBuilder();
162                                         logger.info("Message : ");
163                                         while ((c = is.read()) != -1) {
164                                                 sb.append(c);
165                                                 System.out.write(c);
166                                         }
167                                         is.close();
168                                         String decoded = qp.decode(sb.toString());
169                                         logger.info("decode message is " + decoded);
170                                         if (decoded.contains(contains)) {
171                                                 //found it
172                                                 messages[i].setFlag(Flags.Flag.DELETED, true);
173                                                 found++;
174                                         }
175                                 }
176 
177                         }
178                         inbox.close(true);
179 
180                 } catch (Exception ex) {
181                         ex.printStackTrace();
182                 } finally {
183                         if (store != null) {
184                                 try {
185                                         store.close();
186                                 } catch (Exception ex) {
187                                 }
188                         }
189                 }
190                 return found;
191         }
192 
193         @Override
194         public String getSubscription1XML() {
195                  return TckSubscriptionListener.SUBSCRIPTION_SMTP_XML;
196         }
197 
198         @Override
199         public String getSubscription2XML() {
200                  return TckSubscriptionListener.SUBSCRIPTION2_SMTP_XML;
201         }
202 
203         @Override
204         public String getSubscription3XML() {
205                 return  TckSubscriptionListener.SUBSCRIPTION3_SMTP_XML;
206         }
207 
208         @Override
209         public String getSubscriptionKey1() {
210                 return TckSubscriptionListener.SUBSCRIPTION_SMTP_KEY;
211         }
212 
213         @Override
214         public String getSubscriptionKey2() {
215                 return TckSubscriptionListener.SUBSCRIPTION_SMTP_KEY;
216         }
217 
218         @Override
219         public String getSubscriptionKey3() {
220                 return TckSubscriptionListener.SUBSCRIPTION_SMTP_KEY;
221         }
222 
223         @Override
224         public boolean IsEnabled() {
225                 return TckPublisher.isSMTPEnabled();
226         }
227         
228 
229 
230 }