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 package info.magnolia.cms.security.auth.callback;
35
36 import info.magnolia.context.MgnlContext;
37
38 import java.io.IOException;
39 import java.net.URLEncoder;
40 import java.text.MessageFormat;
41 import java.util.Set;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.apache.commons.lang.StringUtils;
47
48
49
50
51
52
53
54
55
56
57 public class RedirectClientCallback extends AbstractHttpClientCallback {
58 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RedirectClientCallback.class);
59
60 private String location = "/.magnolia";
61
62 @Override
63 public void handle(HttpServletRequest request, HttpServletResponse response) {
64 String target;
65 if (location.startsWith("/")) {
66 target = request.getContextPath() + location;
67 } else {
68 target = location;
69 }
70 if (request.getRequestURI().equals(target)) {
71 log.debug("Unauthorized, can't redirect further, we're already at {}", target);
72 return;
73 }
74 log.debug("Unauthorized, will redirect to {}", target);
75
76 try {
77
78 String url = request.getRequestURL().toString();
79 if (MgnlContext.getParameters() != null && !MgnlContext.getParameters().isEmpty()) {
80 Set<String> keys = MgnlContext.getParameters().keySet();
81 String parameterString = "";
82 String[] values;
83 for (String key : keys) {
84
85
86 if (!key.equals("mgnlLogout")) {
87 values = MgnlContext.getParameterValues(key);
88 for (int i = 0; i < values.length; i++) {
89 parameterString += key + "=" + values[i] + "&";
90 }
91 }
92 }
93 if (StringUtils.isNotBlank(parameterString)) {
94
95 url += "?" + StringUtils.substringBeforeLast(parameterString, "&");
96 target += "?" + StringUtils.substringBeforeLast(parameterString, "&");
97 }
98 }
99 final String encodedUrl = URLEncoder.encode(url, "UTF-8");
100
101
102
103 final String formattedTarget = MessageFormat.format(target, encodedUrl);
104 response.sendRedirect(formattedTarget);
105 } catch (IOException e) {
106 throw new RuntimeException("Can't redirect to " + target + " : " + e.getMessage(), e);
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119 public void setLocation(String location) {
120 this.location = location;
121 }
122
123 protected String getLocation() {
124 return location;
125 }
126 }