View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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  
28  import javax.mail.Authenticator;
29  import javax.mail.PasswordAuthentication;
30  import javax.mail.Session;
31  import javax.naming.Context;
32  import javax.naming.Name;
33  import javax.naming.RefAddr;
34  import javax.naming.Reference;
35  import javax.naming.StringRefAddr;
36  import javax.naming.spi.ObjectFactory;
37  
38  import org.eclipse.jetty.util.security.Password;
39  
40  /**
41   * MailSessionReference
42   *
43   * This is a subclass of javax.mail.Reference and an ObjectFactory for javax.mail.Session objects.
44   *
45   * The subclassing of Reference allows all of the setup for a javax.mail.Session
46   * to be captured without necessitating first instantiating a Session object. The
47   * reference is bound into JNDI and it is only when the reference is looked up that
48   * this object factory will create an instance of javax.mail.Session using the
49   * information captured in the Reference.
50   *
51   */
52  public class MailSessionReference extends Reference implements ObjectFactory
53  {
54  
55  
56      public static class PasswordAuthenticator extends Authenticator
57      {
58          PasswordAuthentication passwordAuthentication;
59          private String user;
60          private String password;
61  
62          public PasswordAuthenticator()
63          {
64  
65          }
66  
67          public PasswordAuthenticator(String user, String password)
68          {
69              passwordAuthentication = new PasswordAuthentication (user, (password.startsWith(Password.__OBFUSCATE)?Password.deobfuscate(password):password));
70          }
71  
72          public PasswordAuthentication getPasswordAuthentication()
73          {
74              return passwordAuthentication;
75          }
76  
77          public void setUser (String user)
78          {
79              this.user = user;
80          }
81          public String getUser ()
82          {
83              return this.user;
84          }
85  
86          public String getPassword ()
87          {
88              return this.password;
89          }
90  
91          public void setPassword(String password)
92          {
93              this.password = password;
94          }
95      };
96  
97      public MailSessionReference()
98      {
99         super ("javax.mail.Session", MailSessionReference.class.getName(), null);
100     }
101 
102 
103     /**
104      * Create a javax.mail.Session instance based on the information passed in the Reference
105      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
106      * @param ref the Reference
107      * @param arg1 not used
108      * @param arg2 not used
109      * @param arg3 not used
110      * @return the object found
111      * @throws Exception if unable to get object instance
112      */
113     public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception
114     {
115         if (ref == null)
116         return null;
117 
118         Reference reference = (Reference)ref;
119 
120 
121         Properties props = new Properties();
122         String user = null;
123         String password = null;
124 
125         Enumeration refs = reference.getAll();
126         while (refs.hasMoreElements())
127         {
128             RefAddr refAddr = (RefAddr)refs.nextElement();
129             String name = refAddr.getType();
130             String value =  (String)refAddr.getContent();
131             if (name.equalsIgnoreCase("user"))
132                 user = value;
133             else if (name.equalsIgnoreCase("pwd"))
134                 password = value;
135             else
136                 props.put(name, value);
137         }
138 
139         if (password == null)
140             return Session.getInstance(props);
141         else
142             return Session.getInstance(props, new PasswordAuthenticator(user, password));
143     }
144 
145 
146     public void setUser (String user)
147     {
148        StringRefAddr addr =  (StringRefAddr)get("user");
149        if (addr != null)
150        {
151          throw new RuntimeException ("user already set on SessionReference, can't be changed");
152        }
153        add(new StringRefAddr("user", user));
154     }
155 
156     public void setPassword (String password)
157     {
158         StringRefAddr addr = (StringRefAddr)get("pwd");
159         if (addr != null)
160             throw new RuntimeException ("password already set on SessionReference, can't be changed");
161         add(new StringRefAddr ("pwd", password));
162     }
163 
164     public void setProperties (Properties properties)
165     {
166         Iterator entries = properties.entrySet().iterator();
167         while (entries.hasNext())
168         {
169             Map.Entry e = (Map.Entry)entries.next();
170             StringRefAddr sref = (StringRefAddr)get((String)e.getKey());
171             if (sref != null)
172                 throw new RuntimeException ("property "+e.getKey()+" already set on Session reference, can't be changed");
173             add(new StringRefAddr((String)e.getKey(), (String)e.getValue()));
174         }
175     }
176 
177 
178 
179 }