| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util.http; |
| 16 |
|
| 17 |
import java.io.InputStream; |
| 18 |
import java.io.IOException; |
| 19 |
|
| 20 |
import org.apache.commons.logging.Log; |
| 21 |
import org.apache.commons.logging.LogFactory; |
| 22 |
|
| 23 |
public class HttpResponse { |
| 24 |
|
| 25 |
private static Log logger = |
| 26 |
LogFactory.getLog(HttpResponse.class.getName()); |
| 27 |
|
| 28 |
protected InputStream input; |
| 29 |
protected long charsRead = 0; |
| 30 |
|
| 31 |
public String httpType = null; |
| 32 |
public String httpMsg = null; |
| 33 |
public int httpCode = -1; |
| 34 |
public long contentLength = -1; |
| 35 |
public String connection = null; |
| 36 |
public String contentType = null; |
| 37 |
public String server = null; |
| 38 |
public boolean chunked = false; |
| 39 |
public String location = null; |
| 40 |
|
| 41 |
public HttpResponse(InputStream in) throws IOException { |
| 42 |
input = in; |
| 43 |
parse(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Read a line of text from the given Stream and return it |
| 48 |
* as a String. Assumes lines end in CRLF. |
| 49 |
*/ |
| 50 |
protected String readLine(InputStream in) throws IOException { |
| 51 |
StringBuffer buf = new StringBuffer(); |
| 52 |
int c, length = 0; |
| 53 |
|
| 54 |
while(true) { |
| 55 |
c = in.read(); |
| 56 |
|
| 57 |
if (c == -1 || c == '\n' || length > 512) { |
| 58 |
charsRead++; |
| 59 |
break; |
| 60 |
} else if (c == '\r') { |
| 61 |
in.read(); |
| 62 |
charsRead+=2; |
| 63 |
break; |
| 64 |
} else { |
| 65 |
buf.append((char)c); |
| 66 |
length++; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
charsRead += length; |
| 71 |
return buf.toString(); |
| 72 |
} |
| 73 |
|
| 74 |
public static String getRest(String line) { |
| 75 |
int pos = line.indexOf(":"); |
| 76 |
if (pos == -1) { |
| 77 |
return null; |
| 78 |
} else |
| 79 |
return line.substring(pos+1).trim(); |
| 80 |
} |
| 81 |
|
| 82 |
public void parseHttp(String line) { |
| 83 |
|
| 84 |
int p1 = line.indexOf(" "); |
| 85 |
if (p1 == -1) { |
| 86 |
return; |
| 87 |
} |
| 88 |
httpType = line.substring(0,p1); |
| 89 |
|
| 90 |
int p2 = line.indexOf(" ",p1+1); |
| 91 |
|
| 92 |
String tmp; |
| 93 |
|
| 94 |
if (p2 == -1) { |
| 95 |
tmp = line.substring(p2); |
| 96 |
} else { |
| 97 |
tmp = line.substring(p1,p2); |
| 98 |
httpMsg = line.substring(p2).trim(); |
| 99 |
} |
| 100 |
httpCode = Integer.parseInt(tmp.trim()); |
| 101 |
} |
| 102 |
|
| 103 |
private void parse() throws IOException { |
| 104 |
String line, tmp; |
| 105 |
|
| 106 |
line = readLine(input); |
| 107 |
if (logger.isTraceEnabled()) { |
| 108 |
logger.trace(line); |
| 109 |
} |
| 110 |
parseHttp(line); |
| 111 |
|
| 112 |
while ( (line=readLine(input)).length() != 0 ) { |
| 113 |
if (logger.isTraceEnabled()) { |
| 114 |
logger.trace(line); |
| 115 |
} |
| 116 |
tmp = getRest(line); |
| 117 |
|
| 118 |
if (line.startsWith(HTTPProtocol.CONNECTION)) { |
| 119 |
connection = tmp; |
| 120 |
} else if (line.startsWith(HTTPProtocol.SERVER)) { |
| 121 |
server = tmp; |
| 122 |
} else if (line.startsWith(HTTPProtocol.CONTENT_TYPE)) { |
| 123 |
contentType = tmp; |
| 124 |
} else if (line.startsWith(HTTPProtocol.CONTENT_LENGTH)) { |
| 125 |
contentLength = Long.parseLong(tmp.trim()); |
| 126 |
} else if (line.startsWith(HTTPProtocol.CHUNKED)) { |
| 127 |
chunked = true; |
| 128 |
} else if (line.startsWith(HTTPProtocol.LOCATION)) { |
| 129 |
location = tmp; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** Generates a string representation of the http header |
| 135 |
* |
| 136 |
* @return <code>String</code> a string representation of the http header |
| 137 |
*/ |
| 138 |
public String toString() { |
| 139 |
StringBuffer buf = new StringBuffer(); |
| 140 |
|
| 141 |
buf.append("Http : " + httpType + "\n"); |
| 142 |
buf.append("Message : " + httpMsg + "\n"); |
| 143 |
buf.append("Code : " + httpCode + "\n"); |
| 144 |
|
| 145 |
if (server != null) { |
| 146 |
buf.append("Server : " + server + "\n"); |
| 147 |
} |
| 148 |
|
| 149 |
buf.append("Length : " + contentLength + "\n"); |
| 150 |
buf.append("Chunked : " + chunked + "\n"); |
| 151 |
buf.append("Type : " + contentType + "\n"); |
| 152 |
|
| 153 |
if (connection != null) { |
| 154 |
buf.append("Connection : " + connection + "\n"); |
| 155 |
} |
| 156 |
|
| 157 |
if (location != null) { |
| 158 |
buf.append("Location : " + location + "\n"); |
| 159 |
} |
| 160 |
|
| 161 |
return buf.toString(); |
| 162 |
} |
| 163 |
|
| 164 |
} |