1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.portletbridge.portlet;
17
18 import java.io.Serializable;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.regex.Pattern;
24
25 import javax.portlet.PortletPreferences;
26 import javax.portlet.RenderRequest;
27
28 import org.apache.commons.httpclient.Credentials;
29 import org.apache.commons.httpclient.HttpState;
30 import org.apache.commons.httpclient.NTCredentials;
31 import org.apache.commons.httpclient.UsernamePasswordCredentials;
32 import org.apache.commons.httpclient.auth.AuthScope;
33 import org.portletbridge.PortletBridgeException;
34 import org.portletbridge.ResourceException;
35
36 /***
37 * Default implementation of the per portlet memento. This stored information
38 * about the current state of the portlet that can be retrieved by the
39 * PortletBridgePortlet.
40 *
41 * @author JMcCrindle
42 */
43 public class DefaultPerPortletMemento implements PerPortletMemento, Serializable {
44
45 /***
46 * default serial version id
47 */
48 private static final long serialVersionUID = 7117499680906225653L;
49
50 private URI initUrl;
51
52 private SerializeableHttpState state = new SerializeableHttpState();
53
54 private String proxyHost;
55
56 private int proxyPort;
57
58 private Pattern scope = Pattern.compile(".*");
59
60 private Map bridgeContent = new HashMap();
61
62 private final BridgeAuthenticator bridgeAuthenticator;
63
64 /***
65 *
66 */
67 public DefaultPerPortletMemento(BridgeAuthenticator bridgeAuthenticator) {
68 this.bridgeAuthenticator = bridgeAuthenticator;
69 }
70
71
72
73
74
75
76 public SerializeableHttpState getHttpState() {
77 return state;
78 }
79
80
81
82
83
84
85 public String getProxyHost() {
86 return proxyHost;
87 }
88
89
90
91
92
93
94 public int getProxyPort() {
95 return proxyPort;
96 }
97
98
99
100
101
102
103 public void setPreferences(RenderRequest request)
104 throws ResourceException {
105 PortletPreferences preferences = request.getPreferences();
106 String initUrlPreference = preferences.getValue("initUrl", null);
107 if (initUrlPreference == null || initUrlPreference.trim().length() == 0) {
108 throw new ResourceException("error.initurl",
109 "preference not defined");
110 }
111 try {
112 this.initUrl = new URI(initUrlPreference);
113 } catch (URISyntaxException e) {
114 throw new ResourceException("error.initurl", e.getMessage(), e);
115 }
116
117 String configProxyAuthentication = preferences.getValue(
118 "proxyAuthentication", "none");
119
120 String configProxyAuthenticationUsername = preferences.getValue(
121 "proxyAuthenticationUsername", null);
122 String configProxyAuthenticationPassword = preferences.getValue(
123 "proxyAuthenticationPassword", null);
124 String configProxyAuthenticationHost = preferences.getValue(
125 "proxyAuthenticationHost", null);
126 String configProxyAuthenticationDomain = preferences.getValue(
127 "proxyAuthenticationDomain", null);
128
129 if (configProxyAuthentication != null
130 && configProxyAuthentication.trim().length() > 0) {
131 if ("ntlm".equalsIgnoreCase(configProxyAuthentication)) {
132 state.setProxyCredentials(AuthScope.ANY, new NTCredentials(
133 configProxyAuthenticationUsername,
134 configProxyAuthenticationPassword,
135 configProxyAuthenticationHost,
136 configProxyAuthenticationDomain));
137 } else if ("basic".equalsIgnoreCase(configProxyAuthentication)) {
138 state.setProxyCredentials(AuthScope.ANY,
139 new UsernamePasswordCredentials(
140 configProxyAuthenticationUsername,
141 configProxyAuthenticationPassword));
142 } else if ("none".equalsIgnoreCase(configProxyAuthentication)) {
143 state.clearProxyCredentials();
144 } else {
145 throw new PortletBridgeException(
146 "error.configProxyAuthentication");
147 }
148 } else {
149 throw new PortletBridgeException("error.configProxyEnabled");
150 }
151
152 if(bridgeAuthenticator != null) {
153 Credentials credentials = bridgeAuthenticator.getCredentials(request);
154 if(credentials != null) {
155 state.setCredentials(AuthScope.ANY, credentials);
156 } else {
157 state.clearCredentials();
158 }
159 }
160
161 proxyHost = preferences.getValue("proxyHost", System.getProperty("http.proxyHost"));
162 String proxyPortPreference = preferences.getValue("proxyPort", System.getProperty("http.proxyPort"));
163 if(proxyPortPreference != null) {
164 String trimmed = proxyPortPreference.trim();
165 if(trimmed.length() > 0) {
166 try {
167 proxyPort = Integer.parseInt(proxyPortPreference);
168 } catch(NumberFormatException e) {
169
170 proxyPort = 80;
171 }
172 } else {
173 proxyPort = 80;
174 }
175 } else {
176 proxyPort = 80;
177 }
178
179 String scopePreference = preferences.getValue("scope", null);
180 if(scopePreference != null) {
181 if(!scope.pattern().equals(scopePreference)) {
182 scope = Pattern.compile(scopePreference);
183 }
184 }
185 }
186
187
188
189
190
191
192 public Pattern getScope() {
193 return scope;
194 }
195
196
197
198
199
200
201 public URI getInitUrl() {
202 return initUrl;
203 }
204
205 public void enqueueContent(String bridgeRequestId, PortletBridgeContent content) {
206 synchronized(bridgeContent) {
207 bridgeContent.clear();
208 bridgeContent.put(bridgeRequestId, content);
209 }
210 }
211
212 public PortletBridgeContent dequeueContent(String bridgeRequestId) {
213 synchronized(bridgeContent) {
214 PortletBridgeContent portletBridgeContent = (PortletBridgeContent) bridgeContent.get(bridgeRequestId);
215 bridgeContent.clear();
216 return portletBridgeContent;
217 }
218 }
219
220 }