001: // HttpChallenge.java
002: // $Id: HttpChallenge.java,v 1.20 2007/02/21 12:58:13 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import org.w3c.util.ArrayDictionary;
009:
010: import java.lang.CloneNotSupportedException;
011:
012: public class HttpChallenge extends BasicValue {
013: String scheme = null;
014: ArrayDictionary params = null;
015: ArrayDictionary unqparams = null;
016:
017: /**
018: * get a copy of the challenge, used to add some output value
019: * without hurting the challenge
020: * @return the clone of this challenge
021: */
022: public HttpChallenge getClone() {
023: try {
024: return (HttpChallenge) this .clone();
025: } catch (CloneNotSupportedException ex) {
026: return null;
027: }
028: }
029:
030: /**
031: * parse.
032: * @exception HttpParserException if parsing failed.
033: */
034: protected void parse() throws HttpParserException {
035: ParseState ps = new ParseState(roff, rlen);
036: // Get the auth scheme
037: if (HttpParser.nextItem(raw, ps) < 0)
038: error("Invalid challenge: no scheme.");
039: this .scheme = ps.toString(raw);
040: // Get the list of params:
041: ParseState it = new ParseState();
042: it.separator = (byte) '=';
043: ps.separator = (byte) ',';
044: ps.prepare();
045: while (HttpParser.nextItem(raw, ps) >= 0) {
046: it.prepare(ps);
047: // Get the param name
048: if (HttpParser.nextItem(raw, it) < 0)
049: error("Invalid challenge: no param name.");
050: String key = it.toString(raw, true);
051: // Get the param value:
052: it.prepare();
053: if (HttpParser.nextItem(raw, it) < 0)
054: error("Invalid challenge: no param value.");
055: it.ioff = it.start;
056: int offset = HttpParser.skipSpaces(raw, it);
057: if ((offset < raw.length) && (raw[offset] == '\"')) {
058: HttpParser.unquote(raw, it);
059: if (params == null) {
060: params = new ArrayDictionary(5, 5);
061: }
062: params.put(key, it.toString(raw));
063: } else {
064: if (unqparams == null) {
065: unqparams = new ArrayDictionary(5, 5);
066: }
067: unqparams.put(key, it.toString(raw));
068: }
069: ps.prepare();
070: }
071: }
072:
073: protected void updateByteValue() {
074: HttpBuffer buf = new HttpBuffer();
075: buf.append(scheme);
076: buf.append(' ');
077: int len = 0;
078: if (params != null) {
079: len = params.size();
080: }
081: if (unqparams != null) {
082: len += unqparams.size();
083: }
084: if (params != null) {
085: int plen = params.size();
086: for (int i = 0; i < plen; i++) {
087: String key = (String) params.keyAt(i);
088: if (key == null)
089: continue;
090: buf.appendQuoted(key, (byte) '=', (String) params
091: .elementAt(i));
092: len--;
093: if (len > 0) {
094: buf.append((byte) ',');
095: buf.append((byte) ' ');
096: }
097: }
098: }
099: if (unqparams != null) {
100: int unqlen = unqparams.size();
101: for (int i = 0; i < unqlen; i++) {
102: String key = (String) unqparams.keyAt(i);
103: if (key == null)
104: continue;
105: buf.append(key, (byte) '=', (String) unqparams
106: .elementAt(i));
107: len--;
108: if (len > 0) {
109: buf.append((byte) ',');
110: buf.append((byte) ' ');
111: }
112: }
113: }
114: raw = buf.getByteCopy();
115: roff = 0;
116: rlen = raw.length;
117: }
118:
119: public Object getValue() {
120: validate();
121: return this ;
122: }
123:
124: /**
125: * Get the challenge scheme.
126: * @return A String encoding the challenge scheme identifier.
127: */
128:
129: public String getScheme() {
130: validate();
131: return scheme;
132: }
133:
134: /**
135: * Get an authentication parameter.
136: * @param name The name of the parameter.
137: * @return A String encoded value for this parameter, or <strong>null
138: * </strong>
139: */
140:
141: public String getAuthParameter(String name) {
142: validate();
143: String res = null;
144: if (params != null) {
145: res = (String) params.get(name);
146: }
147: if (res == null) {
148: if (unqparams != null) {
149: res = (String) unqparams.get(name);
150: }
151: }
152: return res;
153: }
154:
155: /**
156: * Set an auth parameter value.
157: * @param name The name of the parameter to set.
158: * @param value The new value for this parameter.
159: * @param quoted If true, the value will be quoted
160: */
161:
162: public void setAuthParameter(String name, String value,
163: boolean quoted) {
164: invalidateByteValue();
165: if (quoted) {
166: if (params == null) {
167: params = new ArrayDictionary(4, 4);
168: }
169: params.put(name, value);
170: } else {
171: if (unqparams == null) {
172: unqparams = new ArrayDictionary(4, 4);
173: }
174: unqparams.put(name, value);
175: }
176: }
177:
178: /**
179: * Set an auth parameter value.
180: * @param name The name of the parameter to set.
181: * @param value The new value for this parameter.
182: * The value will be quoted
183: */
184: public void setAuthParameter(String name, String value) {
185: setAuthParameter(name, value, true);
186: }
187:
188: HttpChallenge(boolean isValid) {
189: this .isValid = isValid;
190: }
191:
192: HttpChallenge(boolean isValid, String scheme) {
193: this .isValid = isValid;
194: this .scheme = scheme;
195: }
196:
197: public HttpChallenge() {
198: super();
199: }
200:
201: }
|