String requestMethod = exchange.getRequestMethod();
if (requestMethod.equals("POST")) {
/*
* Read the posted content
*/
Headers headers = exchange.getRequestHeaders();
int responseCode = HTTP_RESPONSE_OK;
String contentType = headers.getFirst("Content-Type");
if (contentType != null && contentType.compareTo(HTTP_CONTENT_ZIP) == 0) {
zipped = true;
}
boolean outOfSequence = false;
int bufferSize = 50000; // Default buffer size if no Content-Length header is present.
String contentLength = headers.getFirst("Content-Length");
if (contentLength != null) {
bufferSize = Integer.parseInt(contentLength);
} else if (hop != null && hop.isLengthRequired()) {
responseCode = HTTP_RESPONSE_LENGTH_REQUIRED;
}
InputStream input = exchange.getRequestBody();
byte[] messageBytes = new byte[bufferSize];
int messageLength = input.read(messageBytes);
input.close();
String md5Reported = headers.getFirst("Content-MD5");
if (md5Reported != null) {
String md5Computed = computeMd5(messageBytes, 0, messageLength);
if (md5Reported.compareTo(md5Computed) != 0) {
responseCode = HTTP_RESPONSE_MD5_MISMATCH;
Logger.getLogger(HttpService.class.getName()).log(Level.FINE,
"MD5 reported as {0}, computed as {1}, length expected {2}, found {3}",
new Object[]{md5Reported, md5Computed, bufferSize, messageLength});
}
} else if (hop != null && hop.isMd5Required()) {
responseCode = HTTP_RESPONSE_MD5_REQUIRED;
}
if (responseCode == HTTP_RESPONSE_OK) {
boolean completeMessage = true;
m.setSendingIpAddress(sendingIpAddress);
m.setSegmentCount(1);
m.setLongestSegmentLength(messageLength);
if (id > 0) {
completeMessage = false;
PartialMessage pm = null;
if (segment == 1) {
pm = new PartialMessage();
pm.id = id;
pm.segment = segment;
byte[] a = Arrays.copyOf(messageBytes, messageLength);
pm.messageSegments.add(a);
pm.length += messageLength;
partialMessages.put(sendingIpAddressAndPort, pm);
} else {
pm = partialMessages.get(sendingIpAddressAndPort);
if (pm != null) {
if (pm.id == id && ++pm.segment == segment) {
byte[] a = Arrays.copyOf(messageBytes, messageLength);
pm.messageSegments.add(a);
pm.length += messageLength;
if (end) {
messageLength = pm.length;
messageBytes = new byte[messageLength];
int offset = 0;
int longest = 0;
for (byte[] seg : pm.messageSegments) {
System.arraycopy(seg, 0, messageBytes, offset, seg.length);
offset += seg.length;
if (seg.length > longest) {
longest = seg.length;
}
}
m.setSegmentCount(pm.messageSegments.size());
m.setLongestSegmentLength(longest);
completeMessage = true;
partialMessages.remove(sendingIpAddressAndPort);
}
} else {
if (pm.id != id) {
Logger.getLogger(HttpService.class.getName()).log(Level.FINE,
"Message id mismatch from {0}. Expected id {1}, found {2}, expected sequence {3}, found {4}",
new Object[]{sendingIpAddressAndPort, pm.id, id, pm.segment, segment});
} else {
Logger.getLogger(HttpService.class.getName()).log(Level.FINE,
"Message segment out of sequence from {0}, message id {1}, expected sequence {2}, found {3}",
new Object[]{sendingIpAddressAndPort, id, pm.segment, segment});
}
outOfSequence = true;
partialMessages.remove(sendingIpAddressAndPort);
}
} else {
Logger.getLogger(HttpService.class.getName()).log(Level.FINE,
"Received segment from {0}, id {1}, segment {2} but no partial message previously stored.",
new Object[]{sendingIpAddressAndPort, id, segment});
}
}
}
if (completeMessage) {
m.setSendingIpAddress(sendingIpAddress);
if (zipped) {
m.setCompressedXml(messageBytes);
m.setCompressedXmlLength(messageLength);
} else {
String xml = new String(messageBytes, 0, messageLength);
m.setXml(xml);
}
/*
* Process the message.
*/
mediator.processReceivedMessage(m);
}
}
if (!outOfSequence) {
/*
* Acknoweldge to the sender that we received the message.
* (Don't acknowledge an out-of-sequence message).
*/
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(responseCode, 0);
OutputStream responseBody = exchange.getResponseBody();
responseBody.close();
}
exchange.close();