Custom SocketFactory and ClassNotFoundException
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Marco_Barbi
Posted On:   Wednesday, April 15, 2009 05:30 AM

Hi, I implemented a custom SocketFactory to work with an SMTP server requiring a TLS connection. Everything works fine if I run my code either a simple stand-alone Java application (JDK 1.6.0_13) or a simple test Web application with JSPs (Tomcat 6.0.14 + JDK 1.6.0_13). However, if I put my code in a production environment (Tomcat 6.0.14 + JDK 1.6.0_13) already developed, I got the following Exception: javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.net.SocketException: java.lang.ClassNotFoundException: org.seamless_eu.smpWS.util.DummySSLSocketFactory at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1230)    More>>

Hi,



I implemented a custom SocketFactory to work with an SMTP server requiring a TLS connection. Everything works fine if I run my code either a simple stand-alone Java application (JDK 1.6.0_13) or a simple test Web application with JSPs (Tomcat 6.0.14 + JDK 1.6.0_13).



However, if I put my code in a production environment (Tomcat 6.0.14 + JDK 1.6.0_13) already developed, I got the following Exception:


			
javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
java.net.SocketException: java.lang.ClassNotFoundException: org.seamless_eu.smpWS.util.DummySSLSocketFactory
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1230)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:378)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at org.seamless_eu.smpWS.util.SimpleEmailWrapper$1.run(SimpleEmailWrapper.java:121)
at org.seamless_eu.smpWS.util.SimpleEmailWrapper.send(SimpleEmailWrapper.java:131)
at org.seamless_eu.smpWS.RegisterNewUserCmd.execute(RegisterNewUserCmd.java:113)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at it.satanet.app.execution.ExecutionChain.execute(ExecutionChain.java:49)
at org.seamless_eu.smpWS.SMPCoreServiceImpl.registerAsNewCOMM(SMPCoreServiceImpl.java:371)
at org.seamless_eu.user.gui.bb.RegistrationBean$1.execute(RegistrationBean.java:70)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at it.satanet.app.execution.ExecutionChain.execute(ExecutionChain.java:49)
at org.seamless_eu.user.gui.bb.RegistrationBean.doSave(RegistrationBean.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:131)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:387)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.SocketException: java.lang.ClassNotFoundException: org.seamless_eu.smpWS.util.DummySSLSocketFactory
at javax.net.ssl.DefaultSSLSocketFactory.throwException(SSLSocketFactory.java:179)
at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:199)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:249)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1226)
... 49 more
Caused by: java.lang.ClassNotFoundException: org.seamless_eu.smpWS.util.DummySSLSocketFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at javax.net.ssl.SSLSocketFactory.getDefault(SSLSocketFactory.java:85)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:247)
... 50 more


Here the code of the client class:


			
package org.seamless_eu.smpWS.util;

import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.seamless_eu.commonInterfaces.util.CustomLogger;

public class SimpleEmailWrapper {

private String subject;
private String hostName;
private String body;
private String to;
private String from;
private String smtpAuthUsername;
private String smtpAuthPassword;
private boolean smtpAuthRequired;

public SimpleEmailWrapper() {
}

public String getSubject() {
return subject;
}

public boolean isSmtpAuthRequired() {
return smtpAuthRequired;
}

public void setSmtpAuthRequired(boolean smtpAuthRequired) {
this.smtpAuthRequired = smtpAuthRequired;
}

public String getSmtpAuthUsername() {
return smtpAuthUsername;
}

public void setSmtpAuthUsername(String smtpAuthUsername) {
this.smtpAuthUsername = smtpAuthUsername;
}

public String getSmtpAuthPassword() {
return smtpAuthPassword;
}

public void setSmtpAuthPassword(String stmpAuthPassword) {
this.smtpAuthPassword = stmpAuthPassword;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getHostName() {
return hostName;
}

public void setHostName(String hostName) {
this.hostName = hostName;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
};

public void send() throws Exception {
Thread t = new Thread() {

@Override
public void run() {
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", hostName);
properties.put("mail.smtp.auth", String.valueOf(smtpAuthRequired));
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.protocols", "SSLv3 TLSv1");
Security.setProperty("ssl.SocketFactory.provider", "org.seamless_eu.smpWS.util.DummySSLSocketFactory");
Security.setProperty("ssl.ServerSocketFactory.provider", "org.seamless_eu.smpWS.util.DummySSLSocketFactory");
Session session = Session.getInstance(properties);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport tr = session.getTransport("smtp");
tr.connect(hostName, smtpAuthUsername, smtpAuthPassword);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
CustomLogger.info("Simple wrapper ok", getClass());
} catch (Throwable e) {
CustomLogger.error("Error in creating simple wrapper", e, getClass());
}
}
};
t.run();
}
}


Of course, all the required classes are bundled together in the same jar file.

Any help?

Thanks.

Marco    <<Less
About | Sitemap | Contact