final JavaObjectValue respValue = (JavaObjectValue)
respVar.getValue().itemAt(0);
if (!"org.exist.http.servlets.HttpResponseWrapper".equals(respValue.getObject().getClass().getName()))
{throw new XPathException(this, signatures[1].toString() +
" can only be used within the EXistServlet or XQueryServlet");}
final ResponseWrapper response = (ResponseWrapper) respValue.getObject();
//setup the response correctly
final String mediaType = handler.getTransformer().getOutputProperty("media-type");
final String encoding = handler.getTransformer().getOutputProperty("encoding");
if(mediaType != null)
{
if(encoding == null)
{
response.setContentType(mediaType);
}
else
{
response.setContentType(mediaType + "; charset=" + encoding);
}
}
//do the transformation
try {
final OutputStream os = new BufferedOutputStream(response.getOutputStream());
final StreamResult result = new StreamResult(os);
handler.setResult(result);
final Serializer serializer = context.getBroker().getSerializer();
serializer.reset();
Receiver receiver = new ReceiverToSAX(handler);
try {
serializer.setProperties(serializationProps);
if (expandXIncludes) {
XIncludeFilter xinclude = new XIncludeFilter(serializer, receiver);
String xipath = serializationProps.getProperty(EXistOutputKeys.XINCLUDE_PATH);
if (xipath != null) {
final File f = new File(xipath);
if (!f.isAbsolute())
{xipath = new File(context.getModuleLoadPath(), xipath).getAbsolutePath();}
} else
{xipath = context.getModuleLoadPath();}
xinclude.setModuleLoadPath(xipath);
receiver = xinclude;
}
serializer.setReceiver(receiver);
serializer.toSAX(inputNode, 1, inputNode.getItemCount(), false, false);
} catch (final Exception e) {
throw new XPathException(this, "Exception while transforming node: " + e.getMessage(), e);
}
errorListener.checkForErrors();
os.close();
//commit the response
response.flushBuffer();
} catch (final IOException e) {
throw new XPathException(this, "IO exception while transforming node: " + e.getMessage(), e);
}
return Sequence.EMPTY_SEQUENCE;
}