1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.jndi.factories;
15
16
17 import java.util.Enumeration;
18 import java.util.Hashtable;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.Properties;
22
23 import javax.mail.Authenticator;
24 import javax.mail.PasswordAuthentication;
25 import javax.mail.Session;
26 import javax.naming.Context;
27 import javax.naming.Name;
28 import javax.naming.RefAddr;
29 import javax.naming.Reference;
30 import javax.naming.StringRefAddr;
31 import javax.naming.spi.ObjectFactory;
32
33 import org.eclipse.jetty.http.security.Password;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class MailSessionReference extends Reference implements ObjectFactory
48 {
49
50
51 public static class PasswordAuthenticator extends Authenticator
52 {
53 PasswordAuthentication passwordAuthentication;
54 private String user;
55 private String password;
56
57 public PasswordAuthenticator()
58 {
59
60 }
61
62 public PasswordAuthenticator(String user, String password)
63 {
64 passwordAuthentication = new PasswordAuthentication (user, (password.startsWith(Password.__OBFUSCATE)?Password.deobfuscate(password):password));
65 }
66
67 public PasswordAuthentication getPasswordAuthentication()
68 {
69 return passwordAuthentication;
70 }
71
72 public void setUser (String user)
73 {
74 this.user = user;
75 }
76 public String getUser ()
77 {
78 return this.user;
79 }
80
81 public String getPassword ()
82 {
83 return this.password;
84 }
85
86 public void setPassword(String password)
87 {
88 this.password = password;
89 }
90
91
92 };
93
94
95
96
97
98
99
100
101 public MailSessionReference()
102 {
103 super ("javax.mail.Session", MailSessionReference.class.getName(), null);
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117 public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception
118 {
119 if (ref == null)
120 return null;
121
122 Reference reference = (Reference)ref;
123
124
125 Properties props = new Properties();
126 String user = null;
127 String password = null;
128
129 Enumeration refs = reference.getAll();
130 while (refs.hasMoreElements())
131 {
132 RefAddr refAddr = (RefAddr)refs.nextElement();
133 String name = refAddr.getType();
134 String value = (String)refAddr.getContent();
135 if (name.equalsIgnoreCase("user"))
136 user = value;
137 else if (name.equalsIgnoreCase("pwd"))
138 password = value;
139 else
140 props.put(name, value);
141 }
142
143 if (password == null)
144 return Session.getInstance(props);
145 else
146 return Session.getInstance(props, new PasswordAuthenticator(user, password));
147 }
148
149
150 public void setUser (String user)
151 {
152 StringRefAddr addr = (StringRefAddr)get("user");
153 if (addr != null)
154 {
155 throw new RuntimeException ("user already set on SessionReference, can't be changed");
156 }
157 add(new StringRefAddr("user", user));
158 }
159
160 public void setPassword (String password)
161 {
162 StringRefAddr addr = (StringRefAddr)get("pwd");
163 if (addr != null)
164 throw new RuntimeException ("password already set on SessionReference, can't be changed");
165 add(new StringRefAddr ("pwd", password));
166 }
167
168 public void setProperties (Properties properties)
169 {
170 Iterator entries = properties.entrySet().iterator();
171 while (entries.hasNext())
172 {
173 Map.Entry e = (Map.Entry)entries.next();
174 StringRefAddr sref = (StringRefAddr)get((String)e.getKey());
175 if (sref != null)
176 throw new RuntimeException ("property "+e.getKey()+" already set on Session reference, can't be changed");
177 add(new StringRefAddr((String)e.getKey(), (String)e.getValue()));
178 }
179 }
180
181
182
183 }