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
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
42
43
44
45
46
47
48
49
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
105
106
107
108
109
110
111
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 }