-
Notifications
You must be signed in to change notification settings - Fork 5
/
RedirectResourceV101.java
259 lines (225 loc) · 9.46 KB
/
RedirectResourceV101.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2007 Wyona
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.wyona.org/licenses/APACHE-LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wyona.yanel.impl.resources.redirect;
import org.w3c.dom.Document;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.ResourceConfiguration;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.security.core.api.Identity;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationUtil;
import org.apache.log4j.Logger;
/**
* Controller to do redirects
*/
public class RedirectResourceV101 extends Resource implements ViewableV2, CreatableV2 {
private static Logger log = Logger.getLogger(RedirectResourceV101.class);
public static String IDENTITY_MAP_KEY = "identity-map";
// Only a temporary variable needed during creation (roundtrip)
private String defaultHrefSetByCreator;
private static String REDIRECT_URL = "redirectURL";
/**
*
*/
public ViewDescriptor[] getViewDescriptors() {
ViewDescriptor[] vd = new ViewDescriptor[1];
vd[0] = new ViewDescriptor("default");
vd[0].setMimeType(null);
return vd;
}
/**
*
*/
public View getView(String viewId) throws Exception {
return getView(viewId, null);
}
/**
* Generates view
*/
public View getView(String viewId, String revisionName) throws Exception {
View view = new View();
view.setResponse(false); // this resource writes the response itself
HttpServletResponse response = getResponse();
String defaultHref = getResourceConfigProperty("href");
if (defaultHref == null) throw new Exception("No default redirect has been set!");
// Default
response.setStatus(307);
response.setHeader("Location", defaultHref);
ResourceConfiguration rc = getConfiguration();
Document customConfigDoc = rc.getCustomConfiguration();
if (customConfigDoc != null) {
Configuration config = ConfigurationUtil.toConfiguration(customConfigDoc.getDocumentElement());
// Localization
Configuration[] languageRedirectConfigs = config.getChildren("language");
// NOTE: Within realm.xml one can overwrite the language handler, for example using the content language instead, which is overwriting getRequestedLanguage(), but this doesn't make sense in the case of the redirect resource (also see http://lists.wyona.org/pipermail/yanel-development/2008-April/002150.html)
String localizationLanguage = new org.wyona.yanel.impl.DefaultLanguageHandler().getLocalizationLanguage(this);
//String localizationLanguage = getRequestedLanguage();
log.debug("Localization: " + localizationLanguage);
for (int i = 0; i < languageRedirectConfigs.length; i++) {
try {
if (languageRedirectConfigs[i].getAttribute("code").equals(localizationLanguage)) {
response.setStatus(307);
String href = languageRedirectConfigs[i].getAttribute("href");
response.setHeader("Location", href);
String device = languageRedirectConfigs[i].getAttribute("device", null);
if (device != null) {
if (device.equals("web.xml:mobile-devices")) {
//log.debug("Client language '" + localizationLanguage + "' matched and device '" + device + "' is supported. Let's check whether client is a mobile device ...");
if (isMobileDevice()) {
//log.debug("Client is mobile device, hence redirect to: " + href);
return view;
} else {
//log.debug("Client is not a mobile device.");
continue;
}
} else {
log.warn("Device configuration '" + device + "' not supported yet.");
continue;
}
} else {
//log.debug("No device specified (Language: " + localizationLanguage + ").");
}
return view;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
// Username
String currentUser = null;
Identity identity = getIdentity(getRequest());
if (identity != null) {
currentUser = identity.getUsername();
}
if (currentUser != null) {
Configuration[] userRedirectConfigs = config.getChildren("user");
for (int i = 0; i < userRedirectConfigs.length; i++) {
try {
if (userRedirectConfigs[i].getAttribute("name") == currentUser || (currentUser).equals(userRedirectConfigs[i].getAttribute("name"))) {
response.setStatus(307);
response.setHeader("Location", userRedirectConfigs[i].getAttribute("href"));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
}
}
return view;
}
/**
*
*/
public String getMimeType(String viewid) {
return null;
}
/**
* Always return true, because if a resource config is set, then it also exists. TODO: One might want to consider a more sophisticated exists() ...
*/
public boolean exists() throws Exception {
return true;
}
/**
*
*/
public long getSize() throws Exception {
return -1;
}
/**
* Gets the identity from the session associated with the given request.
* @param request
* @return identity or null if there is no identity in the session for the current
* realm or if there is no session at all
*/
private Identity getIdentity(HttpServletRequest request) throws Exception {
return getEnvironment().getIdentity();
}
// All methods below are re CreatableV2 and CreatableV1 interface implementations
/**
*
*/
public void create(HttpServletRequest request) {
log.warn("Do nothing! Only resource configuration is needed.");
}
/**
*
*/
public String getPropertyType(String propertyName) {
return CreatableV2.TYPE_STRING;
}
/**
*
*/
public String getCreateName(String suggestedName) {
return suggestedName;
}
/**
* @see org.wyona.yanel.core.api.attributes.CreatableV2#createRTIProperties(HttpServletRequest)
*/
public HashMap createRTIProperties(HttpServletRequest request) {
HashMap map = new HashMap();
map.put("href", request.getParameter("rp." + REDIRECT_URL));
return map;
}
/**
* @see org.wyona.yanel.core.api.attributes.CreatableV1#getProperty(String)
*/
public Object getProperty(String name) {
log.debug("name: " + name);
return defaultHrefSetByCreator;
}
/**
* @see org.wyona.yanel.core.api.attributes.CreatableV1#setProperty(String, Object)
*/
public void setProperty(String name, Object value) {
log.debug("name: " + name);
defaultHrefSetByCreator = (String) value;
}
/**
* @see org.wyona.yanel.core.api.attributes.CreatableV1#getPropertyNames()
*/
public String[] getPropertyNames() {
String[] pn = new String[1];
pn[0] = REDIRECT_URL;
return pn;
}
/**
* Check whether user agent is a mobile device
*/
private boolean isMobileDevice() {
javax.servlet.http.HttpSession session = getEnvironment().getRequest().getSession(true);
if (session != null) {
String mobileDevice = (String) session.getAttribute("yanel.mobile");
//TODO: String mobileDevice = (String) getEnvironment().getRequest().getSession(true).getAttribute(org.wyona.yanel.servlet.YanelServlet.MOBILE_KEY);
if (mobileDevice != null && !mobileDevice.equals("false")) {
return true;
} else {
return false;
}
} else {
log.warn("No HTTP session available (maybe because Yanel is used via the command line or some custom junit tests do not provide session handling)!");
return false;
}
}
}