User: acoliver2
Date: 03/09/11 16:55:22
Added: src/java/org/jboss/mail/pop3 MutablePOP3Request.java
POP3Protocol.java POP3ProtocolInstance.java
POP3ProtocolMBean.java POP3Request.java
POP3Response.java package.html
Log:
Eric Daugherty's changes:
Added basic POP3 classes and implemented QUIT command. You can now
connect to the POP3 server and quit! :) I sent you the updated service
file yesterday, but I may have sent the wrong one as well, so I'm
reattaching it.
I refactored methods from SMTPProtocolInstance into Abstract Protocol
and reused them in POP3ProtocolInstance. I refactored most of the
SMTPResponse into AbstractResponse and reused it in POP3Response.
POP3Request just extends SMTPRequest right now. That obviously needs to
change going forward, but we need to determine how much of what appears
common should be moved into a super interface/abstract class.
Thanks Eric -- Open Source works for me.
Revision Changes Path
1.1 jboss-mail/src/java/org/jboss/mail/pop3/MutablePOP3Request.java
Index: MutablePOP3Request.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.net.Socket;
import org.jboss.mail.Protocol;
import org.jboss.mail.Request;
/**
* This represents a mutable version of POP3Request and Request.
*
* @author Eric Daugherty
*/
public class MutablePOP3Request implements POP3Request, Request {
private POP3ProtocolInstance protocol;
private List arguments;
private InputStream stream;
private String strCommand;
private Socket socket;
/**
* Constructs a new MutableSMTPRequest for the passed in protocol and input stream
* @param instance POP3Protocol instance
* @param stream instance
*/
public MutablePOP3Request(
POP3ProtocolInstance instance,
InputStream stream) {
this.stream = stream;
this.protocol = instance;
this.arguments = new ArrayList(2);
}
/**
* set the command to be processed
* @param strCommand SMTP command such as HELO or EHLO
*/
public void setCommand(String strCommand) {
this.strCommand = strCommand;
}
/**
* get the name of the command to be processed. This will be used to match a handler.
* (i.e. HELO, EHLO, QUIT)
*/
public String getCommand() {
return this.strCommand;
}
/**
* For SMTP this will generally just be one String value which will be parsed into multiple arguments.
* @param argument modifying the command (for instance HELO mymailserver, mymailserver would be the
* argument)
*/
public void addArgument(Object argument) {
arguments.add(argument);
}
/**
* @return iterator for all arguments (SMTP will be none or one)
*/
public Iterator arguments() {
return arguments.iterator();
}
/**
* @return the assoicated protocol
*/
public Protocol getProtocol() {
return protocol;
}
/**
* set the socket
* @param socket which originates this request (should be used to get information such as address)
*/
public void setSocket(Socket socket) {
this.socket = socket;
}
/**
* @return address of remote host
*/
public String getRemoteAddr() {
return socket.getInetAddress().getHostName()
+ " ("
+ socket.getInetAddress().getHostAddress()
+ ")";
}
/**
* @return input stream of the request.
*/
public InputStream getInputStream() {
return stream;
}}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/POP3Protocol.java
Index: POP3Protocol.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import java.util.*;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.jboss.mail.ServiceProxy;
import org.jboss.mail.MailListener;
import org.jboss.mail.smtp.SMTPConstants;
import org.jboss.mail.pop3.handlers.POP3Handlers;
/**
* POP3Protocol implements the POP3 Protocol for Server. POP3 is the
* protocol used to retrieve mails by org.jboss.org.jboss.mail clients.
*
* @author Eric Daugherty
*/
public class POP3Protocol implements POP3ProtocolMBean
{
Map handlers;
private static final String SENDER = "SENDER";
Map properties;
/**
* constructs an SMTPProtocol with no configuration
*/
public POP3Protocol() {
handlers = POP3Handlers.instance();
}
public String getName() {
return "POP3";
}
// documented in super class
/* (non-Javadoc)
* @see org.jboss.mail.Protocol#getProperties()
*/
//TODO: return properties as XML
public Element getProperties() {
return null;
}
/* (non-Javadoc)
* @see org.jboss.mail.smtp.SMTPProtocolMBean#setProperties(org.w3c.dom.Element)
*/
public void setProperties(Element properties) {
//TODO: Copied from SMTPProtocol. Should be refactord into common code!
NodeList nodes = properties.getElementsByTagName("property");
Map pproperties = new HashMap(nodes.getLength());
for (int k = 0; k < nodes.getLength(); k++) {
String pname = null;
Object pval = null;
Node node = nodes.item(k);
NodeList subnodes = node.getChildNodes();
for (int x = 0; x < subnodes.getLength(); x++) {
Node subnode = subnodes.item(x);
if (subnode.getNodeName().equals("name")) {
pname = subnode.getFirstChild().getNodeValue();
} else if (subnode.getNodeName().equals("value")) {
pval = subnode.getFirstChild().getNodeValue();
} else if (subnode.getNodeName().equals("values")) {
NodeList values = subnode.getChildNodes();
pval = new ArrayList(values.getLength());
List plist = (List) pval;
for (int y = 0; y < values.getLength(); y++) {
Node value = values.item(y);
if (value.getNodeName() != null
&& value.getNodeName().equals("value")) {
plist.add(value.getFirstChild().getNodeValue());
}
}
} /* else {
throw new RuntimeException("property and password pairs are all that are needed/permitted "+subnode.getNodeName());
}*/
}
if (pname == null || pval == null) {
throw new RuntimeException("users need passwords and passwords need users...");
}
setProperty(pproperties, pname, pval);
}
this.properties = pproperties;
}
/**
* helper function to set properties into the map, typing them and all...
* @param pproperties the map which we're building
* @param pname the name of the property
* @param pval the value to set
*/
private void setProperty(Map pproperties, String pname, Object pval) {
//TODO: Copied from SMTPProtocol. Update for POP3 Properties
if (pname.equals(SMTPConstants.SERVER_NAME)) {
String sval = (String) pval;
pproperties.put(pname, sval);
}
/*
else if (pname.equals(SMTPConstants.SERVER_NAME)) {
String sval = (String) pval;
pproperties.put(pname, sval);
} else if (pname.equals(SMTPConstants.AUTH_REQUIRED)) {
String sval = (String) pval;
pproperties.put(pname, sval);
} else if (pname.equals(SMTPConstants.AUTH_METHODS)) {
String sval = (String) pval;
pproperties.put(pname, sval);
} else if (pname.equals(SMTPConstants.USER_REPOSITORY)) {
try {
ServiceProxy proxy =
(ServiceProxy) JBMailJMXUtil.getService((String) pval);
pproperties.put(pname, proxy);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} else if (pname.equals(SMTPConstants.VERIFY_IDENTITY)) {
String sval = (String) pval;
pproperties.put(pname, sval);
} else if (pname.equals(SMTPConstants.MAX_MSG_SIZE)) {
String sval = (String) pval;
pproperties.put(pname, new Long(sval));
} else if (pname.equals(SMTPConstants.MAIL_LISTENERS)) {
List list = (List) pval;
List plist = new ArrayList(list.size());
Iterator i = list.iterator();
while (i.hasNext()) {
try {
MailListener listener =
(MailListener) JBMailJMXUtil.getService(
(String) i.next());
plist.add(listener);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
pproperties.put(pname, plist);
}
*/
}
/* (non-Javadoc)
* @see org.jboss.mail.ServiceProxyFactory#produceServiceProxy()
*/
public ServiceProxy produceServiceProxy() {
// TODO Auto-generated method stub
return new POP3ProtocolInstance(handlers, properties);
}
}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/POP3ProtocolInstance.java
Index: POP3ProtocolInstance.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import java.util.Map;
import java.util.List;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
import java.net.Socket;
import org.jboss.mail.*;
import org.jboss.mail.smtp.SMTPProtocolInstance;
import org.jboss.mail.pop3.handlers.POP3Handler;
import org.w3c.dom.Element;
/**
* Service Proxy created by the MBean which is used by the Server (actually ServerThread) to implement
* the protocol.
*
* @author Eric Daugherty
*/
public class POP3ProtocolInstance
extends AbstractProtocol
implements Protocol, ServiceProxy {
/**
* returned from SMTPHandlers.instance();
*/
private Map handlers;
/**
* create an POP3ProtocolInstance with the proper handlers and the already
* constructed properties map
* @param handlers from POP3Handlers.instance();
* @param properties from POP3Protocol.setProperties()
*/
public POP3ProtocolInstance(Map handlers, Map properties) {
super(properties);
this.handlers = handlers;
}
/*
* (non-Javadoc)
* @see org.jboss.mail.Protocol#greet(java.io.OutputStream)
*/
public void greet(OutputStream stream) throws IOException {
PrintWriter writer = new PrintWriter(stream);
writer.println(
"+OK "
+ getProperty(SMTPProtocolInstance.SERVER_NAME) //TODO: Refactor to common code.
+ " POP3 Server (JBMAIL POP3 Server version 0.1) "
//+ getProperty(STATUS)
+ " "
+ getProperty("long internet date"));
writer.flush();
}
/*
* (non-Javadoc)
* @see org.jboss.mail.Protocol#parseRequest(java.io.InputStream, java.net.Socket)
*/
public Request parseRequest(InputStream stream, Socket socket)
throws IOException {
MutablePOP3Request request = new MutablePOP3Request( this, stream );
String commandString = readCommand(stream);
System.out.println("Got commandString: " + commandString);
String[] command = parseCommand(commandString);
request.setCommand(command[0].trim());
request.addArgument(command[1]);
request.setSocket(socket);
return request;
}
/*
* (non-Javadoc)
* @see org.jboss.mail.Protocol#handleRequest(java.io.OutputStream, org.jboss.mail.Request)
*/
public Response handleRequest(OutputStream stream, Request request) {
System.out.println(
"HandleRequest called with request = "
+ (request == null ? "true" : "false"));
System.out.println(
"HandleRequest called with command = "
+ (request == null
? "null"
: ((POP3Request) request).getCommand()));
POP3Handler handler =
(POP3Handler) handlers.get(((POP3Request) request).getCommand());
POP3Response response = null;
if (handler != null) {
try {
response =
handler.handleRequest(stream, (POP3Request) request, this);
} catch (IOException e) {
handleIOError(stream);
}
} else {
try {
response =
new POP3Response(request, stream, this);
PrintWriter writer = response.getWriter();
writer.println(
"-ERR "
+ getProperty("servername")
+ " Syntax error, command unrecognized: "
+ ((POP3Request) request).getCommand());
writer.flush();
} catch (IOException ioe) {
response = (POP3Response) handleIOError(stream);
}
}
return response;
}
/**
* gets a response string for a request
* @param request which is being processed
* @return response string
* @throws IOException in the event the response cannot be read due to some communication exception
*/
public String readResponse(Request request) throws IOException {
return readCommand( ((POP3Request) request).getInputStream());
}
/**
* in the event an IO Error occurs (generally fatal to the request) this is called
*/
public Response handleIOError(OutputStream stream) {
System.out.println("Handle IO Error");
//TODO: Log it
return null;
}
/* (non-Javadoc)
* @see org.jboss.mail.Protocol#getName()
*/
public String getName() {
return "POP3";
}
/* (non-Javadoc)
* @see org.jboss.mail.Protocol#setProperties(java.util.List)
*/
public void setProperties(List properties) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.jboss.mail.Protocol#getProperties()
*/
public Element getProperties() {
// TODO Auto-generated method stub
return null;
}
}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/POP3ProtocolMBean.java
Index: POP3ProtocolMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import org.jboss.mail.ServiceProxyFactory;
import org.w3c.dom.Element;
/**
* POP3Protocol is used by clients to retrieve mail.
*
* @author Eric Daugherty
*/
public interface POP3ProtocolMBean extends ServiceProxyFactory
{
/**
* used by the XMBean stuff to set up our properties (see sample)
* @param properties Element from JBoss
*/
void setProperties(Element properties);
/**
* return the proeprties as a DOM object
* @return Element containing our properties...
*/
Element getProperties();
}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/POP3Request.java
Index: POP3Request.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import org.jboss.mail.smtp.SMTPRequest;
/**
* The specific POP3Request type extends the basic request with various POP3-specific request properties
*
* @author Eric Daugherty
*/
public interface POP3Request extends SMTPRequest {
//TODO: So far, this is identical to SMTPRequest, so I'm extending it. This should be changed!
//TODO: Need to determine how to structure Request inheritance.
}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/POP3Response.java
Index: POP3Response.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mail.pop3;
import java.io.OutputStream;
import org.jboss.mail.AbstractResponse;
import org.jboss.mail.Response;
import org.jboss.mail.Protocol;
import org.jboss.mail.Request;
public class POP3Response extends AbstractResponse implements Response {
/**
* POP3Response constructor which creates an instance which is not terminal to the conversation
*
* @param request which generated this response
* @param out stream which the response will be serialized to
* @param protocol instance which is to be used
*/
public POP3Response(
Request request,
OutputStream out,
Protocol protocol) {
this(request, out, protocol, false);
}
/**
* POP3Response constructor which creates an instance which may or may not be terminal
*
* @param request which generated this response
* @param out stream which the response will be serialized to
* @param protocol instance which is to be used
* @param finish whether or not this response terminates the conversation loop
*/
public POP3Response(
Request request,
OutputStream out,
Protocol protocol,
boolean finish) {
this.request = request;
this.out = out;
this.protocol = protocol;
this.finish = finish;
}
}
1.1 jboss-mail/src/java/org/jboss/mail/pop3/package.html
Index: package.html
===================================================================
<body>
<p>Provides classes implementing POP3 functionality.</p>
</body>
|