File tempFile = File.createTempFile("eXist", ".xml");
File tempFile = File.createTempFile("eXistRPCC", ".xml");
	public Hashtable getDocumentData(User user, String name, Hashtable parameters)	throws Exception{		Collection collection = null;		DocumentImpl doc = null;		DBBroker broker = null;		try {			broker = brokerPool.get(user);			int pos = name.lastIndexOf('/');			String collName = name.substring(0, pos);			String docName = name.substring(pos + 1);						collection = broker.openCollection(collName, Lock.READ_LOCK);			if (collection == null) {				LOG.debug("collection " + collName + " not found!");				throw new EXistException("Collection " + collName + " not found!");			}			if(!collection.getPermissions().validate(user, Permission.READ)) {			    collection.release();				throw new PermissionDeniedException("Insufficient privileges to read resource");			}			doc = collection.getDocumentWithLock(broker, docName);			collection.release();			if (doc == null) {				LOG.debug("document " + name + " not found!");				throw new EXistException("document not found");			}						if(!doc.getPermissions().validate(user, Permission.READ))			    throw new PermissionDeniedException("Insufficient privileges to read resource " + docName);			String encoding = (String)parameters.get(OutputKeys.ENCODING);			if(encoding == null)				encoding = "UTF-8";						Serializer serializer = broker.getSerializer();			serializer.setProperties(parameters);			Hashtable result = new Hashtable();			if(doc.getContentLength() > MAX_DOWNLOAD_CHUNK_SIZE) {				File tempFile = File.createTempFile("eXist", ".xml");				tempFile.deleteOnExit();				LOG.debug("Writing to temporary file: " + tempFile.getName());								OutputStream os = new FileOutputStream(tempFile);				Writer writer = new OutputStreamWriter(os, encoding);				serializer.serialize(doc, writer);				writer.close();								byte[] firstChunk = getChunk(tempFile, 0);				result.put("data", firstChunk);				result.put("handle", tempFile.getAbsolutePath());				result.put("offset", new Integer(firstChunk.length));			} else {				String xml = serializer.serialize(doc);				result.put("data", xml.getBytes(encoding));				result.put("offset", new Integer(0));			}			return result;		} finally {			if(collection != null)		        collection.releaseDocument(doc);			brokerPool.release(broker);		}	}