final int responseCode = ((URLSource) source).getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
// Special case: '404'
return Sequence.EMPTY_SEQUENCE;
} else if (responseCode != HttpURLConnection.HTTP_OK) {
throw new PermissionDeniedException("Server returned code " + responseCode);
}
}
//TODO : process pseudo-protocols URLs more efficiently.
org.exist.memtree.DocumentImpl memtreeDoc = null;
// we use eXist's in-memory DOM implementation
reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader();
//TODO : we should be able to cope with context.getBaseURI()
final InputSource src = new InputSource(istream);
final SAXAdapter adapter = new SAXAdapter();
reader.setContentHandler(adapter);
reader.parse(src);
final Document doc = adapter.getDocument();
memtreeDoc = (org.exist.memtree.DocumentImpl)doc;
memtreeDoc.setContext(context);
memtreeDoc.setDocumentURI(path);
document = memtreeDoc;
} catch(final ConnectException e) {
// prevent long stacktraces
throw new XPathException(e.getMessage()+ " ("+path+")");
} catch(final MalformedURLException e) {
throw new XPathException(e.getMessage(), e);
} catch(final SAXException e) {
throw new XPathException("An error occurred while parsing " + path + ": " + e.getMessage(), e);
}
catch(final IOException e) {
// Special case: FileNotFoundException
if(e instanceof FileNotFoundException)
{
return Sequence.EMPTY_SEQUENCE;
}
else
{
throw new XPathException("An error occurred while parsing " + path + ": " + e.getMessage(), e);
}
} finally {
if (reader != null)
{context.getBroker().getBrokerPool().getParserPool().returnXMLReader(reader);}
}
}
else
{
/* Database documents */
// check if the loaded documents should remain locked
boolean lockOnLoad = context.lockDocumentsOnLoad();
final int lockType = lockOnLoad ? Lock.WRITE_LOCK : Lock.READ_LOCK;
DocumentImpl doc = null;
try
{
XmldbURI pathUri = XmldbURI.xmldbUriFor(path, false);
final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
// relative collection Path: add the current base URI
pathUri = baseURI.resolveCollectionPath(pathUri);
}
// relative collection Path: add the current module call URI
try {
pathUri = XmldbURI.xmldbUriFor(context.getModuleLoadPath()).resolveCollectionPath(pathUri);
} catch (final Exception e) {
//workaround: ignore Windows issue
}
// try to open the document and acquire a lock
doc = context.getBroker().getXMLResource(pathUri, lockType);
if(doc != null)
{
if(!doc.getPermissions().validate(context.getSubject(), Permission.READ))
{
doc.getUpdateLock().release(lockType);
throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
}
if(doc.getResourceType() == DocumentImpl.BINARY_FILE)
{
throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");