001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.io.dav.http;
013:
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.text.ParseException;
017: import java.util.Collection;
018: import java.util.Iterator;
019: import java.util.LinkedList;
020: import java.util.Map;
021: import java.util.TreeMap;
022:
023: /**
024: * @version 1.1.1
025: * @author TMate Software Ltd.
026: */
027: public class HTTPHeader {
028:
029: public static final String CONNECTION_HEADER = "Connection";
030: public static final String PROXY_CONNECTION_HEADER = "Proxy-Connection";
031: public static final String TRANSFER_ENCODING_HEADER = "Transfer-Encoding";
032: public static final String CONTENT_LENGTH_HEADER = "Content-Length";
033: public static final String CONTENT_ENCODING_HEADER = "Content-Encoding";
034: public static final String CONTENT_TYPE_HEADER = "Content-Type";
035: public static final String AUTHENTICATE_HEADER = "WWW-Authenticate";
036: public static final String PROXY_AUTHENTICATE_HEADER = "Proxy-Authenticate";
037: public static final String LOCATION_HEADER = "Location";
038: public static final String LOCK_OWNER_HEADER = "X-SVN-Lock-Owner";
039: public static final String CREATION_DATE_HEADER = "X-SVN-Creation-Date";
040: public static final String SVN_VERSION_NAME_HEADER = "X-SVN-Version-Name";
041: public static final String SVN_OPTIONS_HEADER = "X-SVN-Options";
042: public static final String TEXT_MD5 = "X-SVN-Result-Fulltext-MD5";
043: public static final String BASE_MD5 = "X-SVN-Base-Fulltext-MD5";
044: public static final String LOCK_TOKEN_HEADER = "Lock-Token";
045: public static final String IF_HEADER = "If";
046: public static final String DEPTH_HEADER = "Depth";
047: public static final String LABEL_HEADER = "Label";
048: public static final String DESTINATION_HEADER = "Destination";
049:
050: private Map myHeaders;
051:
052: public HTTPHeader() {
053: }
054:
055: public String toString() {
056: StringBuffer representation = new StringBuffer();
057: if (myHeaders == null) {
058: return representation.toString();
059: }
060:
061: for (Iterator headers = myHeaders.keySet().iterator(); headers
062: .hasNext();) {
063: String headerName = (String) headers.next();
064: Collection headerValues = (Collection) myHeaders
065: .get(headerName);
066: for (Iterator values = headerValues.iterator(); values
067: .hasNext();) {
068: String value = (String) values.next();
069: representation.append(headerName);
070: representation.append(": ");
071: representation.append(value);
072: representation.append(HTTPRequest.CRLF);
073: }
074: }
075: return representation.toString();
076: }
077:
078: public void addHeaderValue(String name, String value) {
079: Map headers = getHeaders();
080: Collection values = (Collection) headers.get(name);
081: if (values == null) {
082: values = new LinkedList();
083: headers.put(name, values);
084: }
085: values.add(value);
086: }
087:
088: public Collection getHeaderValues(String name) {
089: if (myHeaders == null) {
090: return null;
091: }
092: return (Collection) myHeaders.get(name);
093: }
094:
095: public String getFirstHeaderValue(String name) {
096: if (myHeaders == null) {
097: return null;
098: }
099:
100: LinkedList values = (LinkedList) myHeaders.get(name);
101: if (values != null) {
102: return (String) values.getFirst();
103: }
104: return null;
105: }
106:
107: public boolean hasHeader(String name) {
108: if (myHeaders != null) {
109: return myHeaders.containsKey(name);
110: }
111: return false;
112: }
113:
114: public void removeHeader(String name) {
115: if (myHeaders != null) {
116: myHeaders.remove(name);
117: }
118: }
119:
120: public void setHeaderValue(String name, String value) {
121: Map headers = getHeaders();
122: Collection values = (Collection) headers.get(name);
123: if (values == null) {
124: values = new LinkedList();
125: headers.put(name, values);
126: }
127: values.clear();
128: values.add(value);
129: }
130:
131: private Map getHeaders() {
132: if (myHeaders == null) {
133: myHeaders = new TreeMap();
134: }
135: return myHeaders;
136: }
137:
138: public static HTTPHeader parseHeader(InputStream is, String charset)
139: throws IOException, ParseException {
140: HTTPHeader headers = new HTTPHeader();
141: String name = null;
142: StringBuffer value = null;
143: for (;;) {
144: String line = HTTPParser.readLine(is, charset);
145: if ((line == null) || (line.trim().length() < 1)) {
146: break;
147: }
148: if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) {
149: if (value != null) {
150: value.append(' ');
151: value.append(line.trim());
152: }
153: } else {
154: if (name != null) {
155: headers.addHeaderValue(name, value != null ? value
156: .toString() : "");
157: }
158:
159: int colon = line.indexOf(":");
160: if (colon < 0) {
161: throw new ParseException("Unable to parse header: "
162: + line, 0);
163: }
164: name = line.substring(0, colon).trim();
165: value = new StringBuffer(line.substring(colon + 1)
166: .trim());
167: }
168:
169: }
170:
171: if (name != null) {
172: headers.addHeaderValue(name, value != null ? value
173: .toString() : "");
174: }
175: return headers;
176: }
177: }
|