01: package com.meterware.httpunit;
02:
03: /********************************************************************************************************************
04: * $Id: AuthorizationRequiredException.java,v 1.6 2002/11/08 01:59:37 russgold Exp $
05: *
06: * Copyright (c) 2000-2001, Russell Gold
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
09: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
10: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
11: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
14: * of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20: * DEALINGS IN THE SOFTWARE.
21: *
22: *******************************************************************************************************************/
23: import java.util.Properties;
24: import java.io.*;
25:
26: /**
27: * This exception is thrown when an unauthorized request is made for a page that requires authentication.
28: **/
29: public class AuthorizationRequiredException extends RuntimeException {
30:
31: AuthorizationRequiredException(String wwwAuthenticateHeader) {
32: final int index = wwwAuthenticateHeader.indexOf(' ');
33: if (index < 0) { // non-conforming header
34: _scheme = "Basic";
35: _params = wwwAuthenticateHeader;
36: } else {
37: _scheme = wwwAuthenticateHeader.substring(0, index);
38: _params = wwwAuthenticateHeader.substring(index + 1);
39: }
40: loadProperties();
41: }
42:
43: private void loadProperties() {
44: try {
45: _properties = new Properties();
46: _properties.load(new ByteArrayInputStream(_params
47: .getBytes()));
48: } catch (IOException e) {
49: }
50: }
51:
52: protected AuthorizationRequiredException(String scheme,
53: String params) {
54: _scheme = scheme;
55: _params = params;
56: loadProperties();
57: }
58:
59: public String getMessage() {
60: return _scheme + " authentication required: " + _params;
61: }
62:
63: /**
64: * Returns the name of the <a href="http://www.freesoft.org/CIE/RFC/Orig/rfc2617.txt">authentication scheme</a>.
65: **/
66: public String getAuthenticationScheme() {
67: return _scheme;
68: }
69:
70: /**
71: * Returns the named authentication parameter. For Basic authentication, the only parameter is "realm".
72: **/
73: public String getAuthenticationParameter(String parameterName) {
74: return _properties.getProperty(parameterName);
75: }
76:
77: //------------------------------------- private members ------------------------------------------
78:
79: private String _params;
80: private String _scheme;
81: private Properties _properties;
82: }
|