1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
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
113
114
115
116
117
118
119
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 }