View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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      };
98  
99  
100 
101 
102 
103     /**
104      *
105      */
106     public MailSessionReference()
107     {
108        super ("javax.mail.Session", MailSessionReference.class.getName(), null);
109     }
110 
111 
112     /**
113      * Create a javax.mail.Session instance based on the information passed in the Reference
114      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
115      * @param ref the Reference
116      * @param arg1 not used
117      * @param arg2 not used
118      * @param arg3 not used
119      * @return the object found
120      * @throws Exception
121      */
122     public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception
123     {
124         if (ref == null)
125         return null;
126 
127         Reference reference = (Reference)ref;
128 
129 
130         Properties props = new Properties();
131         String user = null;
132         String password = null;
133 
134         Enumeration refs = reference.getAll();
135         while (refs.hasMoreElements())
136         {
137             RefAddr refAddr = (RefAddr)refs.nextElement();
138             String name = refAddr.getType();
139             String value =  (String)refAddr.getContent();
140             if (name.equalsIgnoreCase("user"))
141                 user = value;
142             else if (name.equalsIgnoreCase("pwd"))
143                 password = value;
144             else
145                 props.put(name, value);
146         }
147 
148         if (password == null)
149             return Session.getInstance(props);
150         else
151             return Session.getInstance(props, new PasswordAuthenticator(user, password));
152     }
153 
154 
155     public void setUser (String user)
156     {
157        StringRefAddr addr =  (StringRefAddr)get("user");
158        if (addr != null)
159        {
160          throw new RuntimeException ("user already set on SessionReference, can't be changed");
161        }
162        add(new StringRefAddr("user", user));
163     }
164 
165     public void setPassword (String password)
166     {
167         StringRefAddr addr = (StringRefAddr)get("pwd");
168         if (addr != null)
169             throw new RuntimeException ("password already set on SessionReference, can't be changed");
170         add(new StringRefAddr ("pwd", password));
171     }
172 
173     public void setProperties (Properties properties)
174     {
175         Iterator entries = properties.entrySet().iterator();
176         while (entries.hasNext())
177         {
178             Map.Entry e = (Map.Entry)entries.next();
179             StringRefAddr sref = (StringRefAddr)get((String)e.getKey());
180             if (sref != null)
181                 throw new RuntimeException ("property "+e.getKey()+" already set on Session reference, can't be changed");
182             add(new StringRefAddr((String)e.getKey(), (String)e.getValue()));
183         }
184     }
185 
186 
187 
188 }