final JavaObjectValue value = (JavaObjectValue) var.getValue().itemAt(0);
if(!(value.getObject() instanceof RequestWrapper)) {
throw new XPathException(this, "Variable $request is not bound to a Request object.");
}
final RequestWrapper request = (RequestWrapper)value.getObject();
//if the content length is unknown or 0, return
if(request.getContentLength() == -1 || request.getContentLength() == 0) {
return Sequence.EMPTY_SEQUENCE;
}
InputStream isRequest = null;
Sequence result = Sequence.EMPTY_SEQUENCE;
try {
isRequest = request.getInputStream();
//was there any POST content?
/**
* There is a bug in HttpInput.available() in Jetty 7.2.2.v20101205
* This has been filed as Bug 333415 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=333415
* It is expected to be fixed in the Jetty 7.3.0 release
*/
//TODO reinstate call to .available() when Jetty 7.3.0 is released, use of .getContentLength() is not reliable because of http mechanics
//if(is != null && is.available() > 0) {
if(isRequest != null && request.getContentLength() > 0) {
// 1) determine if exists mime database considers this binary data
String contentType = request.getContentType();
if(contentType != null) {
//strip off any charset encoding info
if(contentType.indexOf(";") > -1) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
final MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
if(mimeType != null && !mimeType.isXMLType()) {
//binary data
result = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), isRequest);
}
}
if(result == Sequence.EMPTY_SEQUENCE) {
//2) not binary, try and parse as an XML documemnt, otherwise 3) return a string representation
//parsing will consume the stream so we must cache!
InputStream is = null;
FilterInputStreamCache cache = null;
try {
//we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
cache = FilterInputStreamCacheFactory.getCacheInstance(new FilterInputStreamCacheConfiguration(){
@Override
public String getCacheClass() {
return (String) context.getBroker().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
}
});
is = new CachingFilterInputStream(cache, isRequest);
//mark the start of the stream
is.mark(Integer.MAX_VALUE);
//2) try and parse as XML
result = parseAsXml(is);
if(result == Sequence.EMPTY_SEQUENCE) {
// 3) not a valid XML document, return a string representation of the document
String encoding = request.getCharacterEncoding();
if(encoding == null) {
encoding = "UTF-8";
}
try {