added error handling.
@@ -284,26 +284,27 @@ | ||
284 | 284 | |
285 | 285 | HttpClient httpclient = getHttpClient(); |
286 | 286 | |
287 | - String cookieMapName = SSOProxyConstraints.STORED_COOKIE_LIST | |
287 | + String cookieListName = SSOProxyConstraints.STORED_COOKIE_LIST | |
288 | 288 | + hostConfig.getName(); |
289 | 289 | |
290 | 290 | HttpSession httpSession = request.getSession(); |
291 | - setCookiesToServer(httpSession, cookieMapName, httpclient); | |
291 | + setCookiesToServer(httpSession, cookieListName, httpclient); | |
292 | 292 | |
293 | - HttpMethod httpMethod = authConfig.buildLoginHttpMethod(request); | |
293 | + HttpMethod httpMethod = null; | |
294 | + try { | |
295 | + httpMethod = authConfig.buildLoginHttpMethod(request); | |
294 | 296 | |
295 | - // build request | |
296 | - requestBuilderChain.reset(); | |
297 | - requestBuilderChain.build(RequestBuilder.LOGIN_PROCESS, request, | |
298 | - hostConfig, httpMethod); | |
297 | + // build request | |
298 | + requestBuilderChain.reset(); | |
299 | + requestBuilderChain.build(RequestBuilder.LOGIN_PROCESS, request, | |
300 | + hostConfig, httpMethod); | |
299 | 301 | |
300 | - try { | |
301 | 302 | // execute method |
302 | 303 | int result = httpclient.executeMethod(httpMethod); |
303 | 304 | |
304 | 305 | // update cookies |
305 | 306 | setCookiesToProxy(httpSession, httpclient.getState().getCookies(), |
306 | - cookieMapName); | |
307 | + cookieListName); | |
307 | 308 | |
308 | 309 | Map<String, Object> resultMap = buildResult(httpMethod.getURI() |
309 | 310 | .toString(), httpMethod, result); |
@@ -313,10 +314,13 @@ | ||
313 | 314 | } catch (Exception e) { |
314 | 315 | // } catch (HttpException e) { |
315 | 316 | // } catch (IOException e) { |
316 | - // TODO message | |
317 | - throw new AccessException("TODO.msg", e); | |
317 | + // error | |
318 | + throw new AccessException("000016", new Object[] { hostConfig | |
319 | + .getName() }, e); | |
318 | 320 | } finally { |
319 | - httpMethod.releaseConnection(); | |
321 | + if (httpMethod != null) { | |
322 | + httpMethod.releaseConnection(); | |
323 | + } | |
320 | 324 | } |
321 | 325 | } |
322 | 326 |
@@ -332,14 +336,15 @@ | ||
332 | 336 | HttpSession httpSession = request.getSession(); |
333 | 337 | setCookiesToServer(httpSession, cookieMapName, httpclient); |
334 | 338 | |
335 | - HttpMethod httpMethod = authConfig.buildAuthHttpMethod(request); | |
339 | + HttpMethod httpMethod = null; | |
340 | + try { | |
341 | + httpMethod = authConfig.buildAuthHttpMethod(request); | |
336 | 342 | |
337 | - // build request | |
338 | - requestBuilderChain.reset(); | |
339 | - requestBuilderChain.build(RequestBuilder.AUTH_PROCESS, request, | |
340 | - hostConfig, httpMethod); | |
343 | + // build request | |
344 | + requestBuilderChain.reset(); | |
345 | + requestBuilderChain.build(RequestBuilder.AUTH_PROCESS, request, | |
346 | + hostConfig, httpMethod); | |
341 | 347 | |
342 | - try { | |
343 | 348 | // execute method |
344 | 349 | int result = httpclient.executeMethod(httpMethod); |
345 | 350 |
@@ -354,10 +359,13 @@ | ||
354 | 359 | } catch (Exception e) { |
355 | 360 | // } catch (HttpException e) { |
356 | 361 | // } catch (IOException e) { |
357 | - // TODO message | |
358 | - throw new AccessException("TODO.msg", e); | |
362 | + // error | |
363 | + throw new AccessException("000017", new Object[] { hostConfig | |
364 | + .getName() }, e); | |
359 | 365 | } finally { |
360 | - httpMethod.releaseConnection(); | |
366 | + if (httpMethod != null) { | |
367 | + httpMethod.releaseConnection(); | |
368 | + } | |
361 | 369 | } |
362 | 370 | } |
363 | 371 |
@@ -399,8 +407,9 @@ | ||
399 | 407 | |
400 | 408 | Forwarder forwarder = (Forwarder) container.getComponent(forwarderName); |
401 | 409 | if (forwarder == null) { |
402 | - //TODO | |
403 | - throw new AccessException("TODO.msg"); | |
410 | + // error | |
411 | + throw new AccessException("000018", new Object[] { forwarderName, | |
412 | + hostConfigName }); | |
404 | 413 | } |
405 | 414 | |
406 | 415 | Map<String, Object> props = new HashMap<String, Object>(); |
@@ -420,8 +429,9 @@ | ||
420 | 429 | } catch (Exception e) { |
421 | 430 | // } catch (SSOProxyException e) { |
422 | 431 | // } catch (IOException e) { |
423 | - //TODO | |
424 | - throw new AccessException("TODO.msg", e); | |
432 | + // error | |
433 | + throw new AccessException("000019", new Object[] { forwarderName, | |
434 | + hostConfigName }); | |
425 | 435 | } |
426 | 436 | |
427 | 437 | } |
@@ -443,7 +453,9 @@ | ||
443 | 453 | .buildProxyUrl(redirectLocation)); |
444 | 454 | } catch (IOException e) { |
445 | 455 | // redirect failed |
446 | - throw new AccessException("TODO.msg", e); | |
456 | + // error | |
457 | + throw new AccessException("000020", | |
458 | + new Object[] { redirectLocation }, e); | |
447 | 459 | } |
448 | 460 | } else { |
449 | 461 | throw new AccessException("TODO.msg"); |
@@ -3,6 +3,7 @@ | ||
3 | 3 | import java.io.IOException; |
4 | 4 | import java.io.InputStream; |
5 | 5 | import java.io.UnsupportedEncodingException; |
6 | +import java.util.Locale; | |
6 | 7 | import java.util.Map; |
7 | 8 | |
8 | 9 | import javax.servlet.ServletException; |
@@ -11,11 +12,11 @@ | ||
11 | 12 | import javax.servlet.http.HttpServletResponse; |
12 | 13 | |
13 | 14 | import jp.sf.ssoproxy.SSOProxyConstraints; |
14 | -import jp.sf.ssoproxy.access.AccessException; | |
15 | 15 | import jp.sf.ssoproxy.access.AccessManager; |
16 | 16 | import jp.sf.ssoproxy.config.AuthConfig; |
17 | 17 | import jp.sf.ssoproxy.config.HostConfig; |
18 | 18 | import jp.sf.ssoproxy.config.ProxyConfig; |
19 | +import jp.sf.ssoproxy.util.ErrorHandlingUtil; | |
19 | 20 | |
20 | 21 | import org.seasar.framework.container.S2Container; |
21 | 22 | import org.seasar.framework.container.factory.SingletonS2ContainerFactory; |
@@ -22,22 +23,67 @@ | ||
22 | 23 | |
23 | 24 | public class ProxyServlet extends HttpServlet { |
24 | 25 | |
26 | + private static final long serialVersionUID = 778883232389401884L; | |
27 | + | |
25 | 28 | private static final String PROXY_CONFIG_COMPONENT = "proxyConfig"; |
26 | 29 | |
30 | + private String errorPage; | |
31 | + | |
32 | + private Locale systemLocale; | |
33 | + | |
27 | 34 | @Override |
35 | + public void init() throws ServletException { | |
36 | + // set an error page | |
37 | + errorPage = getServletConfig().getInitParameter( | |
38 | + SSOProxyConstraints.ERROR_JSP_KEY); | |
39 | + if (errorPage == null) { | |
40 | + errorPage = SSOProxyConstraints.DEFAULT_ERROR_JSP; | |
41 | + } | |
42 | + // set a system locale | |
43 | + String value = getServletConfig().getInitParameter( | |
44 | + SSOProxyConstraints.SYSTEM_LOCALE_KEY); | |
45 | + if (value != null) { | |
46 | + try { | |
47 | + String[] values = value.split("_"); | |
48 | + if (values.length == 3) { | |
49 | + systemLocale = new Locale(values[0], values[1], values[2]); | |
50 | + } else if (values.length == 2) { | |
51 | + systemLocale = new Locale(values[0], values[1]); | |
52 | + } else if (values.length == 1) { | |
53 | + systemLocale = new Locale(values[0]); | |
54 | + } else { | |
55 | + systemLocale = Locale.ENGLISH; | |
56 | + } | |
57 | + } catch (RuntimeException e) { | |
58 | + systemLocale = Locale.ENGLISH; | |
59 | + } | |
60 | + } else { | |
61 | + systemLocale = Locale.ENGLISH; | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + @Override | |
28 | 66 | protected void doGet(HttpServletRequest request, |
29 | 67 | HttpServletResponse response) throws ServletException, IOException { |
30 | - //TODO check a user name from session | |
68 | + // needs to check a user name from session by filter | |
31 | 69 | |
32 | 70 | String pathInfo = request.getPathInfo(); |
33 | 71 | if (pathInfo == null) { |
34 | - // TODO | |
72 | + // error | |
73 | + String erroCode = "000002"; | |
74 | + ErrorHandlingUtil.printLog(erroCode, null, systemLocale); | |
75 | + ErrorHandlingUtil.forwardErrorPage(request, response, erroCode, | |
76 | + null, errorPage); | |
35 | 77 | return; |
36 | 78 | } |
37 | 79 | |
38 | 80 | int actualPathPos = pathInfo.indexOf("/", 1); |
39 | 81 | if (actualPathPos < 0) { |
40 | - //TODO | |
82 | + // error | |
83 | + String erroCode = "000003"; | |
84 | + ErrorHandlingUtil.printLog(erroCode, null, systemLocale); | |
85 | + ErrorHandlingUtil.forwardErrorPage(request, response, erroCode, | |
86 | + null, errorPage); | |
41 | 87 | return; |
42 | 88 | } |
43 | 89 |
@@ -50,7 +96,12 @@ | ||
50 | 96 | String hostConfigName = pathInfo.substring(1, actualPathPos); |
51 | 97 | HostConfig hostConfig = proxyConfig.getHostConfig(hostConfigName); |
52 | 98 | if (hostConfig == null) { |
53 | - //TODO | |
99 | + // error | |
100 | + String erroCode = "000004"; | |
101 | + ErrorHandlingUtil.printLog(erroCode, | |
102 | + new Object[] { hostConfigName }, systemLocale); | |
103 | + ErrorHandlingUtil.forwardErrorPage(request, response, erroCode, | |
104 | + new Object[] { hostConfigName }, errorPage); | |
54 | 105 | return; |
55 | 106 | } |
56 | 107 |
@@ -58,7 +109,14 @@ | ||
58 | 109 | AccessManager accessManager = (AccessManager) container |
59 | 110 | .getComponent(hostConfig.getAccessManagerName()); |
60 | 111 | if (accessManager == null) { |
61 | - //TODO | |
112 | + // error | |
113 | + String erroCode = "000005"; | |
114 | + ErrorHandlingUtil.printLog(erroCode, new Object[] { | |
115 | + hostConfig.getAccessManagerName(), hostConfigName }, | |
116 | + systemLocale); | |
117 | + ErrorHandlingUtil.forwardErrorPage(request, response, erroCode, | |
118 | + new Object[] { hostConfig.getAccessManagerName(), | |
119 | + hostConfigName }, errorPage); | |
62 | 120 | return; |
63 | 121 | } |
64 | 122 |
@@ -66,18 +124,19 @@ | ||
66 | 124 | // set request encoding |
67 | 125 | request.setCharacterEncoding(hostConfig.getEncoding()); |
68 | 126 | } catch (UnsupportedEncodingException e) { |
69 | - //TODO | |
70 | - e.printStackTrace(); | |
127 | + // error | |
128 | + ErrorHandlingUtil.printLog(e, systemLocale); | |
129 | + ErrorHandlingUtil.forwardErrorPage(request, response, e, errorPage); | |
71 | 130 | return; |
72 | 131 | } |
73 | 132 | |
74 | 133 | Map<String, Object> resultMap = null; |
75 | - String url = hostConfig.buildUrl(pathInfo.substring(actualPathPos)); | |
76 | - // TODO the url has request parameters or not when checking it in a following method... | |
77 | - AuthConfig authConfig = hostConfig.getAuthConfig(request.getMethod(), | |
78 | - url, request.getParameterMap()); | |
134 | + try { | |
135 | + String url = hostConfig.buildUrl(pathInfo.substring(actualPathPos)); | |
136 | + // TODO the url has request parameters or not when checking it in a following method... | |
137 | + AuthConfig authConfig = hostConfig.getAuthConfig(request | |
138 | + .getMethod(), url, request.getParameterMap()); | |
79 | 139 | |
80 | - try { | |
81 | 140 | // check a login page |
82 | 141 | if (authConfig != null) { |
83 | 142 | // Check a login page |
@@ -122,13 +181,10 @@ | ||
122 | 181 | .get(SSOProxyConstraints.MIME_TYPE_PARAM))); |
123 | 182 | // flush |
124 | 183 | response.flushBuffer(); |
125 | - } catch (AccessException e) { | |
126 | - //TODO | |
127 | - e.printStackTrace(); | |
128 | - return; | |
129 | 184 | } catch (Exception e) { |
130 | - //TODO | |
131 | - e.printStackTrace(); | |
185 | + // error | |
186 | + ErrorHandlingUtil.printLog(e, systemLocale); | |
187 | + ErrorHandlingUtil.forwardErrorPage(request, response, e, errorPage); | |
132 | 188 | return; |
133 | 189 | } finally { |
134 | 190 | accessManager.release(resultMap); |
@@ -137,9 +193,9 @@ | ||
137 | 193 | } |
138 | 194 | |
139 | 195 | @Override |
140 | - protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
141 | - throws ServletException, IOException { | |
142 | - doGet(req, resp); | |
196 | + protected void doPost(HttpServletRequest request, | |
197 | + HttpServletResponse response) throws ServletException, IOException { | |
198 | + doGet(request, response); | |
143 | 199 | } |
144 | 200 | |
145 | 201 | private boolean isRedirectStatusCode(int statusCode) { |
@@ -4,6 +4,7 @@ | ||
4 | 4 | import java.util.Map; |
5 | 5 | |
6 | 6 | import jp.sf.ssoproxy.config.AuthConfig; |
7 | +import jp.sf.ssoproxy.config.ConfigException; | |
7 | 8 | import jp.sf.ssoproxy.config.HostConfig; |
8 | 9 | |
9 | 10 | public class HostConfigImpl implements HostConfig { |
@@ -43,7 +44,7 @@ | ||
43 | 44 | } |
44 | 45 | |
45 | 46 | public AuthConfig getAuthConfig(String method, String url, |
46 | - Map<String, String[]> params) { | |
47 | + Map<String, String[]> params) throws ConfigException { | |
47 | 48 | if (authConfigs != null) { |
48 | 49 | for (int i = 0; i < authConfigs.length; i++) { |
49 | 50 | if (authConfigs[i].checkLoginPageUrl(method, url, params)) { |
@@ -1,10 +1,8 @@ | ||
1 | 1 | package jp.sf.ssoproxy.config.impl; |
2 | 2 | |
3 | 3 | import java.io.BufferedReader; |
4 | -import java.io.IOException; | |
5 | 4 | import java.io.InputStream; |
6 | 5 | import java.io.InputStreamReader; |
7 | -import java.io.UnsupportedEncodingException; | |
8 | 6 | import java.util.HashMap; |
9 | 7 | import java.util.List; |
10 | 8 | import java.util.Map; |
@@ -12,8 +10,8 @@ | ||
12 | 10 | import javax.servlet.http.HttpServletRequest; |
13 | 11 | |
14 | 12 | import jp.sf.ssoproxy.SSOProxyConstraints; |
15 | -import jp.sf.ssoproxy.access.AccessException; | |
16 | 13 | import jp.sf.ssoproxy.config.AuthConfig; |
14 | +import jp.sf.ssoproxy.config.ConfigException; | |
17 | 15 | import jp.sf.ssoproxy.util.UrlBuilderUtil; |
18 | 16 | |
19 | 17 | import org.apache.commons.httpclient.HttpMethod; |
@@ -48,7 +46,7 @@ | ||
48 | 46 | * @see jp.sf.ssoproxy.config.impl.AuthConfig#checkLoginPageUrl(java.lang.String) |
49 | 47 | */ |
50 | 48 | public boolean checkLoginPageUrl(String method, String url, |
51 | - Map<String, String[]> params) { | |
49 | + Map<String, String[]> params) throws ConfigException { | |
52 | 50 | //TODO method and params |
53 | 51 | if (url != null && url.equals(loginPageUrl)) { |
54 | 52 | return true; |
@@ -56,7 +54,8 @@ | ||
56 | 54 | return false; |
57 | 55 | } |
58 | 56 | |
59 | - public boolean checkLoginPage(InputStream inputStream) { | |
57 | + public boolean checkLoginPage(InputStream inputStream) | |
58 | + throws ConfigException { | |
60 | 59 | try { |
61 | 60 | BufferedReader reader = new BufferedReader(new InputStreamReader( |
62 | 61 | inputStream, loginPageEncoding)); |
@@ -67,12 +66,11 @@ | ||
67 | 66 | } |
68 | 67 | line = reader.readLine(); |
69 | 68 | } |
70 | - } catch (UnsupportedEncodingException e) { | |
71 | - // TODO Auto-generated catch block | |
72 | - e.printStackTrace(); | |
73 | - } catch (IOException e) { | |
74 | - // TODO Auto-generated catch block | |
75 | - e.printStackTrace(); | |
69 | + // } catch (UnsupportedEncodingException e) { | |
70 | + // } catch (IOException e) { | |
71 | + } catch (Exception e) { | |
72 | + // error | |
73 | + throw new ConfigException("000013", e); | |
76 | 74 | } |
77 | 75 | return false; |
78 | 76 | } |
@@ -80,7 +78,8 @@ | ||
80 | 78 | /* (non-Javadoc) |
81 | 79 | * @see jp.sf.ssoproxy.config.impl.AuthConfig#buildLoginHttpMethod() |
82 | 80 | */ |
83 | - public HttpMethod buildLoginHttpMethod(HttpServletRequest request) throws AccessException{ | |
81 | + public HttpMethod buildLoginHttpMethod(HttpServletRequest request) | |
82 | + throws ConfigException { | |
84 | 83 | if (SSOProxyConstraints.POST_METHOD.equals(loginPageMethod)) { |
85 | 84 | return UrlBuilderUtil.buildPostMethod(loginPageUrl, |
86 | 85 | getParameterMap(request, SSOProxyConstraints.GET_METHOD, |
@@ -92,14 +91,15 @@ | ||
92 | 91 | getParameterMap(request, SSOProxyConstraints.GET_METHOD, |
93 | 92 | loginPageDataList), loginPageEncoding); |
94 | 93 | } |
95 | - //TODO msg | |
96 | - throw new IllegalArgumentException(); | |
94 | + // error | |
95 | + throw new ConfigException("000014", new Object[] { loginPageUrl }); | |
97 | 96 | } |
98 | 97 | |
99 | 98 | /* (non-Javadoc) |
100 | 99 | * @see jp.sf.ssoproxy.config.impl.AuthConfig#buildAuthHttpMethod() |
101 | 100 | */ |
102 | - public HttpMethod buildAuthHttpMethod(HttpServletRequest request) throws AccessException{ | |
101 | + public HttpMethod buildAuthHttpMethod(HttpServletRequest request) | |
102 | + throws ConfigException { | |
103 | 103 | if (SSOProxyConstraints.POST_METHOD.equals(authPageMethod)) { |
104 | 104 | return UrlBuilderUtil.buildPostMethod(authPageUrl, getParameterMap( |
105 | 105 | request, SSOProxyConstraints.GET_METHOD, authPageDataList), |
@@ -110,12 +110,13 @@ | ||
110 | 110 | request, SSOProxyConstraints.GET_METHOD, authPageDataList), |
111 | 111 | authPageEncoding); |
112 | 112 | } |
113 | - //TODO msg | |
114 | - throw new IllegalArgumentException(); | |
113 | + // error | |
114 | + throw new ConfigException("000015", new Object[] { authPageUrl }); | |
115 | 115 | } |
116 | 116 | |
117 | 117 | protected Map<String, String[]> getParameterMap(HttpServletRequest request, |
118 | - String method, List<Map<String, String>> dataList) throws AccessException{ | |
118 | + String method, List<Map<String, String>> dataList) | |
119 | + throws ConfigException { | |
119 | 120 | Map<String, String[]> params = new HashMap<String, String[]>(); |
120 | 121 | if (dataList != null) { |
121 | 122 | for (Map<String, String> entry : dataList) { |
@@ -131,7 +132,7 @@ | ||
131 | 132 | } |
132 | 133 | |
133 | 134 | protected abstract String getDataValue(HttpServletRequest request, |
134 | - String value) throws AccessException; | |
135 | + String value) throws ConfigException; | |
135 | 136 | |
136 | 137 | public String getAuthPageMethod() { |
137 | 138 | return authPageMethod; |
@@ -5,7 +5,7 @@ | ||
5 | 5 | import javax.servlet.http.HttpServletRequest; |
6 | 6 | |
7 | 7 | import jp.sf.ssoproxy.SSOProxyConstraints; |
8 | -import jp.sf.ssoproxy.access.AccessException; | |
8 | +import jp.sf.ssoproxy.config.ConfigException; | |
9 | 9 | import jp.sf.ssoproxy.util.JetspeedUtil; |
10 | 10 | |
11 | 11 | import org.apache.jetspeed.security.SecurityException; |
@@ -23,14 +23,14 @@ | ||
23 | 23 | |
24 | 24 | @Override |
25 | 25 | protected String getDataValue(HttpServletRequest request, String value) |
26 | - throws AccessException { | |
26 | + throws ConfigException { | |
27 | 27 | if (usernameKey.equals(value)) { |
28 | 28 | // from user attribute |
29 | 29 | String remoteUser = (String) request.getSession().getAttribute( |
30 | 30 | SSOProxyConstraints.CURRENT_REMOTE_USER); |
31 | 31 | if (remoteUser == null) { |
32 | - //TODO | |
33 | - throw new IllegalStateException(); | |
32 | + // error | |
33 | + throw new ConfigException("000009"); | |
34 | 34 | } |
35 | 35 | |
36 | 36 | try { |
@@ -39,8 +39,8 @@ | ||
39 | 39 | Preferences prefs = user.getUserAttributes(); |
40 | 40 | return prefs.get(usernameAttributeKey, ""); |
41 | 41 | } catch (SecurityException e) { |
42 | - // TODO | |
43 | - throw new AccessException("TODO.msg"); | |
42 | + // error | |
43 | + throw new ConfigException("000010", new Object[] { remoteUser }); | |
44 | 44 | } |
45 | 45 | } else if (passwordKey.equals(value)) { |
46 | 46 | // from user attribute |
@@ -47,8 +47,8 @@ | ||
47 | 47 | String remoteUser = (String) request.getSession().getAttribute( |
48 | 48 | SSOProxyConstraints.CURRENT_REMOTE_USER); |
49 | 49 | if (remoteUser == null) { |
50 | - //TODO | |
51 | - throw new IllegalStateException(); | |
50 | + // error | |
51 | + throw new ConfigException("000011"); | |
52 | 52 | } |
53 | 53 | |
54 | 54 | try { |
@@ -57,8 +57,8 @@ | ||
57 | 57 | Preferences prefs = user.getUserAttributes(); |
58 | 58 | return prefs.get(passwordAttributeKey, ""); |
59 | 59 | } catch (SecurityException e) { |
60 | - // TODO | |
61 | - throw new AccessException("TODO.msg"); | |
60 | + // error | |
61 | + throw new ConfigException("000012", new Object[] { remoteUser }); | |
62 | 62 | } |
63 | 63 | } |
64 | 64 | return value; |
@@ -14,5 +14,5 @@ | ||
14 | 14 | public abstract String getForwarderName(String mimeType); |
15 | 15 | |
16 | 16 | public abstract AuthConfig getAuthConfig(String method, String url, |
17 | - Map<String, String[]> params); | |
17 | + Map<String, String[]> params) throws ConfigException; | |
18 | 18 | } |
\ No newline at end of file |
@@ -0,0 +1,41 @@ | ||
1 | +package jp.sf.ssoproxy.config; | |
2 | + | |
3 | +import jp.sf.ssoproxy.SSOProxyException; | |
4 | + | |
5 | +public class ConfigException extends SSOProxyException { | |
6 | + | |
7 | + private static final long serialVersionUID = 7749580473857490597L; | |
8 | + | |
9 | + public ConfigException(String messageId) { | |
10 | + super(messageId); | |
11 | + } | |
12 | + | |
13 | + public ConfigException(String messageId, Object[] args) { | |
14 | + super(messageId, args); | |
15 | + } | |
16 | + | |
17 | + public ConfigException(String messageId, String message, Throwable cause) { | |
18 | + super(messageId, message, cause); | |
19 | + } | |
20 | + | |
21 | + public ConfigException(String messageId, Object[] args, String message, | |
22 | + Throwable cause) { | |
23 | + super(messageId, args, message, cause); | |
24 | + } | |
25 | + | |
26 | + public ConfigException(String messageId, String message) { | |
27 | + super(messageId, message); | |
28 | + } | |
29 | + | |
30 | + public ConfigException(String messageId, Object[] args, String message) { | |
31 | + super(messageId, args, message); | |
32 | + } | |
33 | + | |
34 | + public ConfigException(String messageId, Throwable cause) { | |
35 | + super(messageId, cause); | |
36 | + } | |
37 | + | |
38 | + public ConfigException(String messageId, Object[] args, Throwable cause) { | |
39 | + super(messageId, args, cause); | |
40 | + } | |
41 | +} |
@@ -5,21 +5,20 @@ | ||
5 | 5 | |
6 | 6 | import javax.servlet.http.HttpServletRequest; |
7 | 7 | |
8 | -import jp.sf.ssoproxy.access.AccessException; | |
9 | - | |
10 | 8 | import org.apache.commons.httpclient.HttpMethod; |
11 | 9 | |
12 | 10 | public interface AuthConfig { |
13 | 11 | |
14 | 12 | public abstract boolean checkLoginPageUrl(String method, String url, |
15 | - Map<String, String[]> params); | |
13 | + Map<String, String[]> params) throws ConfigException; | |
16 | 14 | |
17 | - public abstract boolean checkLoginPage(InputStream inputStream); | |
15 | + public abstract boolean checkLoginPage(InputStream inputStream) | |
16 | + throws ConfigException; | |
18 | 17 | |
19 | 18 | public abstract HttpMethod buildLoginHttpMethod(HttpServletRequest request) |
20 | - throws AccessException; | |
19 | + throws ConfigException; | |
21 | 20 | |
22 | 21 | public abstract HttpMethod buildAuthHttpMethod(HttpServletRequest request) |
23 | - throws AccessException; | |
22 | + throws ConfigException; | |
24 | 23 | |
25 | 24 | } |
\ No newline at end of file |
@@ -1,6 +1,7 @@ | ||
1 | 1 | package jp.sf.ssoproxy.filter; |
2 | 2 | |
3 | 3 | import java.io.IOException; |
4 | +import java.util.Locale; | |
4 | 5 | |
5 | 6 | import javax.servlet.Filter; |
6 | 7 | import javax.servlet.FilterChain; |
@@ -9,13 +10,43 @@ | ||
9 | 10 | import javax.servlet.ServletRequest; |
10 | 11 | import javax.servlet.ServletResponse; |
11 | 12 | import javax.servlet.http.HttpServletRequest; |
13 | +import javax.servlet.http.HttpServletResponse; | |
12 | 14 | |
13 | 15 | import jp.sf.ssoproxy.SSOProxyConstraints; |
16 | +import jp.sf.ssoproxy.util.ErrorHandlingUtil; | |
14 | 17 | |
15 | 18 | public class SessionAuthFilter implements Filter { |
19 | + private String errorPage; | |
16 | 20 | |
21 | + private Locale systemLocale; | |
22 | + | |
17 | 23 | public void init(FilterConfig config) throws ServletException { |
18 | - | |
24 | + // set an error page | |
25 | + errorPage = config.getInitParameter(SSOProxyConstraints.ERROR_JSP_KEY); | |
26 | + if (errorPage == null) { | |
27 | + errorPage = SSOProxyConstraints.DEFAULT_ERROR_JSP; | |
28 | + } | |
29 | + // set a system locale | |
30 | + String value = config | |
31 | + .getInitParameter(SSOProxyConstraints.SYSTEM_LOCALE_KEY); | |
32 | + if (value != null) { | |
33 | + try { | |
34 | + String[] values = value.split("_"); | |
35 | + if (values.length == 3) { | |
36 | + systemLocale = new Locale(values[0], values[1], values[2]); | |
37 | + } else if (values.length == 2) { | |
38 | + systemLocale = new Locale(values[0], values[1]); | |
39 | + } else if (values.length == 1) { | |
40 | + systemLocale = new Locale(values[0]); | |
41 | + } else { | |
42 | + systemLocale = Locale.ENGLISH; | |
43 | + } | |
44 | + } catch (RuntimeException e) { | |
45 | + systemLocale = Locale.ENGLISH; | |
46 | + } | |
47 | + } else { | |
48 | + systemLocale = Locale.ENGLISH; | |
49 | + } | |
19 | 50 | } |
20 | 51 | |
21 | 52 | public void destroy() { |
@@ -25,7 +56,8 @@ | ||
25 | 56 | public void doFilter(ServletRequest request, ServletResponse response, |
26 | 57 | FilterChain chain) throws IOException, ServletException { |
27 | 58 | |
28 | - if (request instanceof HttpServletRequest) { | |
59 | + if (request instanceof HttpServletRequest | |
60 | + && response instanceof HttpServletResponse) { | |
29 | 61 | HttpServletRequest httpRequest = (HttpServletRequest) request; |
30 | 62 | String remoteUser = (String) httpRequest.getSession().getAttribute( |
31 | 63 | SSOProxyConstraints.CURRENT_REMOTE_USER); |
@@ -32,11 +64,17 @@ | ||
32 | 64 | if (remoteUser != null) { |
33 | 65 | chain.doFilter(request, response); |
34 | 66 | } else { |
35 | - //TODO redirect? | |
67 | + // error | |
68 | + String erroCode = "000008"; | |
69 | + ErrorHandlingUtil.printLog(erroCode, null, systemLocale); | |
70 | + ErrorHandlingUtil.forwardErrorPage( | |
71 | + (HttpServletRequest) request, | |
72 | + (HttpServletResponse) response, erroCode, null, | |
73 | + errorPage); | |
36 | 74 | } |
37 | 75 | } else { |
38 | 76 | //TODO redirect? |
39 | - | |
77 | + response.getWriter().write("Not supported."); | |
40 | 78 | } |
41 | 79 | } |
42 | 80 |
@@ -1,13 +1,8 @@ | ||
1 | 1 | package jp.sf.ssoproxy.forwarder.impl; |
2 | 2 | |
3 | -import java.io.BufferedReader; | |
4 | -import java.io.IOException; | |
5 | 3 | import java.io.InputStream; |
6 | -import java.io.InputStreamReader; | |
7 | 4 | import java.io.OutputStream; |
8 | 5 | import java.io.OutputStreamWriter; |
9 | -import java.io.Reader; | |
10 | -import java.io.UnsupportedEncodingException; | |
11 | 6 | import java.util.Map; |
12 | 7 | |
13 | 8 | import jp.sf.ssoproxy.SSOProxyConstraints; |
@@ -16,18 +11,22 @@ | ||
16 | 11 | import jp.sf.ssoproxy.forwarder.ForwarderException; |
17 | 12 | import jp.sf.ssoproxy.handler.html.HtmlHandler; |
18 | 13 | |
14 | +import org.apache.commons.logging.Log; | |
15 | +import org.apache.commons.logging.LogFactory; | |
19 | 16 | import org.cyberneko.html.parsers.SAXParser; |
20 | 17 | import org.seasar.framework.container.S2Container; |
21 | -import org.seasar.framework.log.Logger; | |
22 | 18 | import org.xml.sax.InputSource; |
23 | 19 | |
24 | 20 | public class HtmlForwarder implements Forwarder { |
21 | + /** | |
22 | + * Logger for this class | |
23 | + */ | |
24 | + private static final Log log = LogFactory.getLog(HtmlForwarder.class); | |
25 | + | |
25 | 26 | private static final String DEFAULT_HTML_HANDLER_NAME = "defaultHtmlHandler"; |
26 | 27 | |
27 | 28 | private static final String DEFAULT_INPUT_ENCODING = "UTF-8"; |
28 | 29 | |
29 | - private static final Logger logger = Logger.getLogger(HtmlForwarder.class); | |
30 | - | |
31 | 30 | private S2Container container; |
32 | 31 | |
33 | 32 | private String htmlHandlerName; |
@@ -51,29 +50,9 @@ | ||
51 | 50 | props.get(SSOProxyConstraints.URL_PARAM)); |
52 | 51 | htmlHandler.getProperties().put(SSOProxyConstraints.PROXY_CONFIG_PARAM, |
53 | 52 | props.get(SSOProxyConstraints.PROXY_CONFIG_PARAM)); |
54 | - // htmlHandler.getProperties().put(SSOProxyConstraints.REQUEST_PARAM, | |
55 | - // (String) props.get(SSOProxyConstraints.REQUEST_PARAM)); | |
56 | - // htmlHandler.getProperties().put(SSOProxyConstraints.RESPONSE_PARAM, | |
57 | - // (String) props.get(SSOProxyConstraints.RESPONSE_PARAM)); | |
58 | 53 | |
59 | 54 | InputSource inputSource = new InputSource(is); |
60 | 55 | inputSource.setEncoding(inputEncoding); |
61 | - // for debug | |
62 | - // InputSource inputSource; | |
63 | - // try { | |
64 | - // BufferedReader reader = new BufferedReader(new InputStreamReader( | |
65 | - // is, inputEncoding)); | |
66 | - // System.out.println("TEST: " + reader.readLine()); | |
67 | - // System.out.println("TEST: " + reader.readLine()); | |
68 | - // System.out.println("TEST: " + reader.readLine()); | |
69 | - // inputSource = new InputSource(reader); | |
70 | - // } catch (UnsupportedEncodingException e1) { | |
71 | - // inputSource = new InputSource(is); | |
72 | - // inputSource.setEncoding(inputEncoding); | |
73 | - // } catch (IOException e1) { | |
74 | - // inputSource = new InputSource(is); | |
75 | - // inputSource.setEncoding(inputEncoding); | |
76 | - // } | |
77 | 56 | |
78 | 57 | try { |
79 | 58 | SAXParser parser = new SAXParser(); |
@@ -88,8 +67,8 @@ | ||
88 | 67 | } |
89 | 68 | OutputStreamWriter osw = new OutputStreamWriter(os, encoding); |
90 | 69 | osw.write(htmlHandler.toString()); |
91 | - if (logger.isDebugEnabled()) { | |
92 | - logger.debug("content=" + htmlHandler.toString()); | |
70 | + if (log.isDebugEnabled()) { | |
71 | + log.debug("content=" + htmlHandler.toString()); | |
93 | 72 | } |
94 | 73 | osw.flush(); |
95 | 74 | } catch (Exception e) { |
@@ -98,8 +77,9 @@ | ||
98 | 77 | // } catch (UnsupportedEncodingException e) { |
99 | 78 | // } catch (SAXException e) { |
100 | 79 | // } catch (IOException e) { |
101 | - //TODO msg | |
102 | - throw new ForwarderException("TODO.msg", e); | |
80 | + // error | |
81 | + throw new ForwarderException("000006", new Object[] { props | |
82 | + .get(SSOProxyConstraints.URL_PARAM) }, e); | |
103 | 83 | } |
104 | 84 | } |
105 | 85 |
@@ -5,6 +5,7 @@ | ||
5 | 5 | import java.io.OutputStream; |
6 | 6 | import java.util.Map; |
7 | 7 | |
8 | +import jp.sf.ssoproxy.SSOProxyConstraints; | |
8 | 9 | import jp.sf.ssoproxy.SSOProxyException; |
9 | 10 | import jp.sf.ssoproxy.forwarder.Forwarder; |
10 | 11 | import jp.sf.ssoproxy.forwarder.ForwarderException; |
@@ -28,8 +29,9 @@ | ||
28 | 29 | bytes = null; |
29 | 30 | } |
30 | 31 | } catch (IOException e) { |
31 | - //TODO | |
32 | - throw new ForwarderException("TODO.msg"); | |
32 | + // error | |
33 | + throw new ForwarderException("000007", new Object[] { props | |
34 | + .get(SSOProxyConstraints.URL_PARAM) }, e); | |
33 | 35 | } |
34 | 36 | } |
35 | 37 |
@@ -0,0 +1,30 @@ | ||
1 | +package jp.sf.ssoproxy.util; | |
2 | + | |
3 | +import java.text.MessageFormat; | |
4 | +import java.util.Locale; | |
5 | +import java.util.ResourceBundle; | |
6 | + | |
7 | +public class MessageUtil { | |
8 | + private static final String RESOURCE_MESSAGES = "jp.sf.ssoproxy.resource.Messages"; | |
9 | + | |
10 | + public static ResourceBundle getResourceBundle(Locale locale) { | |
11 | + // TODO cache | |
12 | + return ResourceBundle.getBundle(RESOURCE_MESSAGES, locale); | |
13 | + } | |
14 | + | |
15 | + public static String getMessage(String key, Locale locale) { | |
16 | + return getMessage(key, null, locale); | |
17 | + } | |
18 | + | |
19 | + public static String getMessage(String key, Object[] args, Locale locale) { | |
20 | + try { | |
21 | + String msg = getResourceBundle(locale).getString(key); | |
22 | + if (args != null) { | |
23 | + msg = MessageFormat.format(msg, args); | |
24 | + } | |
25 | + return msg; | |
26 | + } catch (Exception e) { | |
27 | + return key; | |
28 | + } | |
29 | + } | |
30 | +} |
@@ -0,0 +1,80 @@ | ||
1 | +package jp.sf.ssoproxy.util; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.util.Locale; | |
5 | + | |
6 | +import javax.servlet.RequestDispatcher; | |
7 | +import javax.servlet.ServletException; | |
8 | +import javax.servlet.http.HttpServletRequest; | |
9 | +import javax.servlet.http.HttpServletResponse; | |
10 | + | |
11 | +import org.apache.commons.logging.Log; | |
12 | +import org.apache.commons.logging.LogFactory; | |
13 | + | |
14 | +import jp.sf.ssoproxy.SSOProxyConstraints; | |
15 | +import jp.sf.ssoproxy.SSOProxyException; | |
16 | +import jp.sf.ssoproxy.servlet.ProxyServlet; | |
17 | + | |
18 | +public class ErrorHandlingUtil { | |
19 | + | |
20 | + /** | |
21 | + * Logger for this class | |
22 | + */ | |
23 | + private static final Log log = LogFactory.getLog(ProxyServlet.class); | |
24 | + | |
25 | + public static void forwardErrorPage(HttpServletRequest request, | |
26 | + HttpServletResponse response, Throwable t, String errorPage) | |
27 | + throws ServletException, IOException { | |
28 | + String errorCode = null; | |
29 | + if (t instanceof SSOProxyException) { | |
30 | + SSOProxyException spe = (SSOProxyException) t; | |
31 | + errorCode = spe.getMessageId(); | |
32 | + forwardErrorPage(request, response, errorCode, spe.getArgs(), | |
33 | + errorPage); | |
34 | + } else { | |
35 | + forwardErrorPage(request, response, "000001", null, errorPage); | |
36 | + } | |
37 | + } | |
38 | + | |
39 | + public static void forwardErrorPage(HttpServletRequest request, | |
40 | + HttpServletResponse response, String errorCode, Object[] args, | |
41 | + String errorPage) throws ServletException, IOException { | |
42 | + String errorMessage = MessageUtil.getMessage(errorCode, args, request | |
43 | + .getLocale()); | |
44 | + | |
45 | + // TODO response status code | |
46 | + response.setStatus(500); | |
47 | + | |
48 | + request.setAttribute(SSOProxyConstraints.ERROR_CODE, errorCode); | |
49 | + request.setAttribute(SSOProxyConstraints.ERROR_MESSAGE, errorMessage); | |
50 | + | |
51 | + RequestDispatcher rd = request.getRequestDispatcher(errorPage); | |
52 | + rd.forward(request, response); | |
53 | + } | |
54 | + | |
55 | + public static void printLog(Throwable t, Locale systemLocale) { | |
56 | + String errorCode = null; | |
57 | + String errorMessage = null; | |
58 | + if (t instanceof SSOProxyException) { | |
59 | + SSOProxyException spe = (SSOProxyException) t; | |
60 | + errorCode = spe.getMessageId(); | |
61 | + errorMessage = MessageUtil.getMessage(errorCode, spe.getArgs(), | |
62 | + systemLocale); | |
63 | + } else { | |
64 | + errorCode = "000001"; | |
65 | + errorMessage = MessageUtil.getMessage(errorCode, systemLocale); | |
66 | + } | |
67 | + | |
68 | + // TODO logging level | |
69 | + log.error(errorMessage, t); | |
70 | + } | |
71 | + | |
72 | + public static void printLog(String errorCode, Object[] args, | |
73 | + Locale systemLocale) { | |
74 | + String errorMessage = MessageUtil.getMessage(errorCode, args, | |
75 | + systemLocale); | |
76 | + | |
77 | + // TODO logging level | |
78 | + log.error(errorMessage); | |
79 | + } | |
80 | +} |
@@ -47,4 +47,14 @@ | ||
47 | 47 | |
48 | 48 | public static final String CURRENT_REMOTE_USER = "CurrentRemoteUser"; |
49 | 49 | |
50 | + public static final String ERROR_CODE = "ErrorCode"; | |
51 | + | |
52 | + public static final String ERROR_MESSAGE = "ErrorMessage"; | |
53 | + | |
54 | + public static final String SYSTEM_LOCALE_KEY = "sytemLocale"; | |
55 | + | |
56 | + public static final String ERROR_JSP_KEY = "errorPage"; | |
57 | + | |
58 | + public static final String DEFAULT_ERROR_JSP = "/error.jsp"; | |
59 | + | |
50 | 60 | } |
@@ -115,6 +115,16 @@ | ||
115 | 115 | <scope>provided</scope> |
116 | 116 | </dependency> |
117 | 117 | <dependency> |
118 | + <groupId>jstl</groupId> | |
119 | + <artifactId>jstl</artifactId> | |
120 | + <version>1.1.2</version> | |
121 | + </dependency> | |
122 | + <dependency> | |
123 | + <groupId>taglibs</groupId> | |
124 | + <artifactId>standard</artifactId> | |
125 | + <version>1.1.2</version> | |
126 | + </dependency> | |
127 | + <dependency> | |
118 | 128 | <groupId>org.seasar.container</groupId> |
119 | 129 | <artifactId>s2-framework</artifactId> |
120 | 130 | <version>2.4.25</version> |
@@ -146,6 +156,11 @@ | ||
146 | 156 | <version>2.3</version> |
147 | 157 | </dependency> |
148 | 158 | <dependency> |
159 | + <groupId>commons-logging</groupId> | |
160 | + <artifactId>commons-logging</artifactId> | |
161 | + <version>1.1.1</version> | |
162 | + </dependency> | |
163 | + <dependency> | |
149 | 164 | <groupId>nekohtml</groupId> |
150 | 165 | <artifactId>nekohtml</artifactId> |
151 | 166 | <version>0.9.5</version> |