public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if(args[0].isEmpty() || args[1].isEmpty()) {
return (Sequence.EMPTY_SEQUENCE);
}
final BinaryValue binary = (BinaryValue) args[0].itemAt(0);
final String contentType = args[1].getStringValue();
String filename = null;
if((args.length > 2) && !args[2].isEmpty()) {
filename = args[2].getStringValue();
}
final ResponseModule myModule = (ResponseModule) context.getModule(ResponseModule.NAMESPACE_URI);
// response object is read from global variable $response
final Variable respVar = myModule.resolveVariable(ResponseModule.RESPONSE_VAR);
if((respVar == null) || (respVar.getValue() == null)) {
throw (new XPathException(this, "No response object found in the current XQuery context."));
}
if(respVar.getValue().getItemType() != Type.JAVA_OBJECT) {
throw (new XPathException(this, "Variable $response is not bound to an Java object."));
}
final JavaObjectValue respValue = (JavaObjectValue) respVar.getValue().itemAt(0);
if(!"org.exist.http.servlets.HttpResponseWrapper".equals(respValue.getObject().getClass().getName())) {
throw (new XPathException(this, signature.toString() + " can only be used within the EXistServlet or XQueryServlet"));
}
final ResponseWrapper response = (ResponseWrapper) respValue.getObject();
response.setHeader("Content-Type", contentType);
if(filename != null) {
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
}
try {
final OutputStream os = response.getOutputStream();
binary.streamBinaryTo(response.getOutputStream());
os.close();
//commit the response
response.flushBuffer();
} catch (final IOException e) {