View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.jndi.factories;
20  
21  
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Properties;
27  import javax.mail.Authenticator;
28  import javax.mail.PasswordAuthentication;
29  import javax.mail.Session;
30  import javax.naming.Context;
31  import javax.naming.Name;
32  import javax.naming.RefAddr;
33  import javax.naming.Reference;
34  import javax.naming.StringRefAddr;
35  import javax.naming.spi.ObjectFactory;
36  
37  import org.eclipse.jetty.util.security.Password;
38  
39  /**
40   * MailSessionReference
41   *
42   * This is a subclass of javax.mail.Reference and an ObjectFactory for javax.mail.Session objects.
43   *
44   * The subclassing of Reference allows all of the setup for a javax.mail.Session
45   * to be captured without necessitating first instantiating a Session object. The
46   * reference is bound into JNDI and it is only when the reference is looked up that
47   * this object factory will create an instance of javax.mail.Session using the
48   * information captured in the Reference.
49   *
50   */
51  public class MailSessionReference extends Reference implements ObjectFactory
52  {
53  
54  
55      public static class PasswordAuthenticator extends Authenticator
56      {
57          PasswordAuthentication passwordAuthentication;
58          private String user;
59          private String password;
60  
61          public PasswordAuthenticator()
62          {
63  
64          }
65  
66          public PasswordAuthenticator(String user, String password)
67          {
68              passwordAuthentication = new PasswordAuthentication (user, (password.startsWith(Password.__OBFUSCATE)?Password.deobfuscate(password):password));
69          }
70  
71          public PasswordAuthentication getPasswordAuthentication()
72          {
73              return passwordAuthentication;
74          }
75  
76          public void setUser (String user)
77          {
78              this.user = user;
79          }
80          public String getUser ()
81          {
82              return this.user;
83          }
84  
85          public String getPassword ()
86          {
87              return this.password;
88          }
89  
90          public void setPassword(String password)
91          {
92              this.password = password;
93          }
94  
95  
96      };
97  
98  
99  
100 
101 
102     /**
103      *
104      */
105     public MailSessionReference()
106     {
107        super ("javax.mail.Session", MailSessionReference.class.getName(), null);
108     }
109 
110 
111     /**
112      * Create a javax.mail.Session instance based on the information passed in the Reference
113      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
114      * @param ref the Reference
115      * @param arg1 not used
116      * @param arg2 not used
117      * @param arg3 not used
118      * @return the object found
119      * @throws Exception
120      */
121     public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception
122     {
123         if (ref == null)
124         return null;
125 
126         Reference reference = (Reference)ref;
127 
128 
129         Properties props = new Properties();
130         String user = null;
131         String password = null;
132 
133         Enumeration refs = reference.getAll();
134         while (refs.hasMoreElements())
135         {
136             RefAddr refAddr = (RefAddr)refs.nextElement();
137             String name = refAddr.getType();
138             String value =  (String)refAddr.getContent();
139             if (name.equalsIgnoreCase("user"))
140                 user = value;
141             else if (name.equalsIgnoreCase("pwd"))
142                 password = value;
143             else
144                 props.put(name, value);
145         }
146 
147         if (password == null)
148             return Session.getInstance(props);
149         else
150             return Session.getInstance(props, new PasswordAuthenticator(user, password));
151     }
152 
153 
154     public void setUser (String user)
155     {
156        StringRefAddr addr =  (StringRefAddr)get("user");
157        if (addr != null)
158        {
159          throw new RuntimeException ("user already set on SessionReference, can't be changed");
160        }
161        add(new StringRefAddr("user", user));
162     }
163 
164     public void setPassword (String password)
165     {
166         StringRefAddr addr = (StringRefAddr)get("pwd");
167         if (addr != null)
168             throw new RuntimeException ("password already set on SessionReference, can't be changed");
169         add(new StringRefAddr ("pwd", password));
170     }
171 
172     public void setProperties (Properties properties)
173     {
174         Iterator entries = properties.entrySet().iterator();
175         while (entries.hasNext())
176         {
177             Map.Entry e = (Map.Entry)entries.next();
178             StringRefAddr sref = (StringRefAddr)get((String)e.getKey());
179             if (sref != null)
180                 throw new RuntimeException ("property "+e.getKey()+" already set on Session reference, can't be changed");
181             add(new StringRefAddr((String)e.getKey(), (String)e.getValue()));
182         }
183     }
184 
185 
186 
187 }