*
* @throws IOException
*/
private InputStream readFromSocket(Socket sock, MessageContext msgContext,InputStream inp, Hashtable headers )
throws IOException {
Message outMsg = null;
byte b;
int len = 0;
int colonIndex = -1;
boolean headersOnly= false;
if(null != headers){
headersOnly= true;
}else{
headers= new Hashtable();
}
String name, value;
String statusMessage = "";
int returnCode = 0;
if(null == inp) inp = new BufferedInputStream(sock.getInputStream());
// Should help performance. Temporary fix only till its all stream oriented.
// Need to add logic for getting the version # and the return code
// but that's for tomorrow!
/* Logic to read HTTP response headers */
boolean readTooMuch = false;
b = 0;
for (ByteArrayOutputStream buf = new ByteArrayOutputStream(4097); ;) {
if (!readTooMuch) {
b = (byte) inp.read();
}
if (b == -1) {
break;
}
readTooMuch = false;
if ((b != '\r') && (b != '\n')) {
if ((b == ':') && (colonIndex == -1)) {
colonIndex = len;
}
len++;
buf.write(b);
} else if (b == '\r') {
continue;
} else { // b== '\n'
if (len == 0) {
break;
}
b = (byte) inp.read();
readTooMuch = true;
// A space or tab at the begining of a line means the header continues.
if ((b == ' ') || (b == '\t')) {
continue;
}
buf.close();
byte[] hdata = buf.toByteArray();
buf.reset();
if (colonIndex != -1) {
name =
new String(hdata, 0, colonIndex,
HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);
value =
new String(hdata, colonIndex + 1, len - 1 - colonIndex,
HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);
colonIndex = -1;
} else {
name =
new String(hdata, 0, len,
HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);
value = "";
}
if (log.isDebugEnabled()) {
log.debug(name + value);
}
if (msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE)
== null) {
// Reader status code
int start = name.indexOf(' ') + 1;
String tmp = name.substring(start).trim();
int end = tmp.indexOf(' ');
if (end != -1) {
tmp = tmp.substring(0, end);
}
returnCode = Integer.parseInt(tmp);
msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE,
new Integer(returnCode));
statusMessage = name.substring(start + end + 1);
msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_MESSAGE,
statusMessage);
} else {
headers.put(name.toLowerCase(), value);
}
len = 0;
}
}
if(headersOnly){
return inp;
}
/* All HTTP headers have been read. */
String contentType =
(String) headers
.get(HTTPConstants.HEADER_CONTENT_TYPE.toLowerCase());
contentType = (null == contentType)
? null
: contentType.trim();
if ((returnCode > 199) && (returnCode < 300)) {
// SOAP return is OK - so fall through
} else if ((contentType != null) && !contentType.startsWith("text/html")
&& ((returnCode > 499) && (returnCode < 600))) {
// SOAP Fault should be in here - so fall through
} else {
// Unknown return code - so wrap up the content into a
// SOAP Fault.
ByteArrayOutputStream buf = new ByteArrayOutputStream(4097);
while (-1 != (b = (byte) inp.read())) {
buf.write(b);
}
AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" + statusMessage, null, null);
fault.setFaultDetailString(Messages.getMessage("return01",
"" + returnCode, buf.toString()));
throw fault;
}
if (b != -1) { // more data than just headers.
String contentLocation =
(String) headers
.get(HTTPConstants.HEADER_CONTENT_LOCATION.toLowerCase());
contentLocation = (null == contentLocation)
? null
: contentLocation.trim();
String contentLength =
(String) headers
.get(HTTPConstants.HEADER_CONTENT_LENGTH.toLowerCase());
contentLength = (null == contentLength)
? null
: contentLength.trim();
String transferEncoding =
(String) headers
.get(HTTPConstants.HEADER_TRANSFER_ENCODING.toLowerCase());
if (null != transferEncoding
&& transferEncoding.trim()
.equals(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED)) {
inp = new ChunkedInputStream(inp);
}
outMsg = new Message( new SocketInputStream(inp, sock), false, contentType,
contentLocation);
outMsg.setMessageType(Message.RESPONSE);
msgContext.setResponseMessage(outMsg);
if (log.isDebugEnabled()) {
if (null == contentLength) {
log.debug("\n"
+ Messages.getMessage("no00", "Content-Length"));
}
log.debug("\n" + Messages.getMessage("xmlRecd00"));
log.debug("-----------------------------------------------");
log.debug((String) outMsg.getSOAPPartAsString());
}
}
// if we are maintaining session state,
// handle cookies (if any)