}
} finally { context.popDocumentContext(); }
	public Sequence eval(Sequence[] args, Sequence contextSequence)throws XPathException {				RequestModule myModule = (RequestModule) context.getModule(RequestModule.NAMESPACE_URI);		// request object is read from global variable $request		Variable var = myModule.resolveVariable(RequestModule.REQUEST_VAR);		if(var == null)			throw new XPathException("No request object found in the current XQuery context.");		if(var.getValue().getItemType() != Type.JAVA_OBJECT)			throw new XPathException("Variable $request is not bound to an Java object.");		JavaObjectValue value = (JavaObjectValue) var.getValue().itemAt(0);				if(value.getObject() instanceof RequestWrapper)		{			RequestWrapper request = (RequestWrapper)value.getObject();							//if the content length is unknown, return			if(request.getContentLength() == -1)			{				return Sequence.EMPTY_SEQUENCE;			}						//first, get the content of the request			byte[] bufRequestData = null;			try			{				InputStream is = request.getInputStream();				ByteArrayOutputStream bos = new ByteArrayOutputStream(request.getContentLength());				byte[] buf = new byte[256];				int l = 0;				while ((l = is.read(buf)) > -1)				{					bos.write(buf, 0, l);				}				bufRequestData = bos.toByteArray();			}			catch(IOException ioe)			{				throw new XPathException("An IO exception ocurred: " + ioe.getMessage(), ioe);			}						//if the POST content is not null, it may be an xml document, so we first try to return the xml document			if(bufRequestData != null)			{								try				{	//TODO: code taken from DocUtils, could add a parse(byte[]) method to DocUtils to share the code? 					Sequence document = Sequence.EMPTY_SEQUENCE;										//try and construct xml document from input stream, we use eXist's in-memory DOM implementation					DocumentImpl memtreeDoc = null;					SAXParserFactory factory = SAXParserFactory.newInstance();					factory.setNamespaceAware(true);						//TODO : we should be able to cope with context.getBaseURI()									InputSource src = new InputSource(new ByteArrayInputStream(bufRequestData));					SAXParser parser = factory.newSAXParser();					XMLReader reader = parser.getXMLReader();					SAXAdapter adapter = new SAXAdapter();					reader.setContentHandler(adapter);					reader.parse(src);										Document doc = adapter.getDocument();					memtreeDoc = (org.exist.memtree.DocumentImpl)doc;					memtreeDoc.setContext(context);					document = memtreeDoc;										if(document != Sequence.EMPTY_SEQUENCE)					{						//return the xml document						return document;					}				}				catch (ParserConfigurationException e)				{									//do nothing, we will default to trying to return a string below				}				catch (SAXException e)				{					//do nothing, we will default to trying to return a string below				}				catch (IOException e)				{					//do nothing, we will default to trying to return a string below				}									}						//else, return a string representation of the xml document			String encoding = request.getCharacterEncoding();			if(encoding == null)			{				encoding = "UTF-8";			}						try			{				String s = new String(bufRequestData, encoding);				return new StringValue(s);						}			catch (IOException e)			{				throw new XPathException("An IO exception ocurred: " + e.getMessage(), e);			}		}		else		{			throw new XPathException("Variable $request is not bound to a Request object.");		}	}