use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.
the class AnyUriResolver method resolveEntity.
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier xri) throws XNIException, IOException {
if (xri.getExpandedSystemId() == null && xri.getLiteralSystemId() == null && xri.getNamespace() == null && xri.getPublicId() == null) {
// quick fail
return null;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Resolving XMLResourceIdentifier: {}", getXriDetails(xri));
}
String resourcePath = null;
String baseSystemId = null;
if (firstTime) {
// First time use constructor supplied path
resourcePath = docPath;
baseSystemId = parentURI;
xri.setExpandedSystemId(docPath);
firstTime = false;
} else {
resourcePath = xri.getExpandedSystemId();
}
xri.setBaseSystemId(docPath);
LOG.debug("resourcePath='{}'", resourcePath);
// prevent NPE
if (resourcePath == null) {
return null;
}
InputStream is = null;
if (resourcePath.startsWith("xmldb:")) {
final XmldbURL xmldbURL = new XmldbURL(resourcePath);
if (xmldbURL.isEmbedded()) {
is = new EmbeddedInputStream(xmldbURL);
} else {
is = new XmlrpcInputStream(threadGroup, xmldbURL);
}
} else {
is = new URL(resourcePath).openStream();
}
final XMLInputSource xis = new XMLInputSource(xri.getPublicId(), resourcePath, baseSystemId, is, "UTF-8");
if (LOG.isDebugEnabled()) {
LOG.debug("XMLInputSource: {}", getXisDetails(xis));
}
return xis;
}
use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.
the class ExistResolver method resolveInputSource.
/* ============== */
/* Helper methods */
/* ============== */
private InputSource resolveInputSource(BrokerPool bPool, String path) throws IOException {
LOG.debug("Resolving {}", path);
final InputSource inputsource = new InputSource();
if (path != null) {
if (path.startsWith(LOCALURI) || path.startsWith(SHORTLOCALURI)) {
final XmldbURL url = new XmldbURL(path);
final EmbeddedInputStream eis = new EmbeddedInputStream(bPool, url);
inputsource.setByteStream(eis);
inputsource.setSystemId(path);
} else {
final InputStream is = new URL(path).openStream();
inputsource.setByteStream(is);
inputsource.setSystemId(path);
}
}
return inputsource;
}
use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.
the class ExistResolver method resolveStreamSource.
private StreamSource resolveStreamSource(BrokerPool bPool, String path) throws TransformerException {
LOG.debug("Resolving {}", path);
final StreamSource streamsource = new StreamSource();
try {
if (path != null) {
if (path.startsWith(LOCALURI) || path.startsWith(SHORTLOCALURI)) {
final XmldbURL url = new XmldbURL(path);
final EmbeddedInputStream eis = new EmbeddedInputStream(bPool, url);
streamsource.setInputStream(eis);
streamsource.setSystemId(path);
} else {
final InputStream is = new URL(path).openStream();
streamsource.setInputStream(is);
streamsource.setSystemId(path);
}
}
} catch (final IOException ex) {
throw new TransformerException(ex);
}
return streamsource;
}
use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.
the class XmlrpcDownload method stream.
/**
* Write document referred by the URL to the output stream.
*
* @param xmldbURL Document location in database.
* @param os Stream to which the document is written.
* @throws IOException An IO error occurred.
*/
public void stream(final XmldbURL xmldbURL, final OutputStream os) throws IOException {
LOG.debug("Begin document download");
try {
final XmlRpcClient client = new XmlRpcClient();
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setEncoding("UTF-8");
config.setEnabledForExtensions(true);
config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
// Setup client client
if (xmldbURL.hasUserInfo()) {
config.setBasicUserName(xmldbURL.getUsername());
config.setBasicPassword(xmldbURL.getPassword());
}
client.setConfig(config);
// Setup xml serializer
final Map<String, String> options = new HashMap<>();
options.put("indent", "no");
options.put("encoding", "UTF-8");
// Setup client parameters
final List<Object> params = new ArrayList<>();
params.add(xmldbURL.getCollectionPath());
params.add(options);
// Shoot first method write data
Map ht = (Map) client.execute("getDocumentData", params);
int offset = (int) ht.get("offset");
byte[] data = (byte[]) ht.get("data");
final String handle = (String) ht.get("handle");
os.write(data);
// When there is more data to download
while (offset != 0) {
// Clean and re-setup client parameters
params.clear();
params.add(handle);
params.add(offset);
// Get and write next chunk
ht = (Map) client.execute("getNextChunk", params);
data = (byte[]) ht.get("data");
offset = (int) ht.get("offset");
os.write(data);
}
} catch (final XmlRpcException ex) {
LOG.error(ex);
throw new IOException(ex.getMessage(), ex);
} catch (final IOException ex) {
LOG.error(ex);
throw ex;
} finally {
LOG.debug("Finished document download");
}
}
use of org.exist.protocolhandler.xmldb.XmldbURL in project exist by eXist-db.
the class XmlrpcUpload method stream.
/**
* Write data from a (input)stream to the specified XMLRPC url and leave
* the input stream open.
*
* @param xmldbURL URL pointing to location on eXist-db server.
* @param is Document stream
* @throws IOException When something is wrong.
*/
public void stream(XmldbURL xmldbURL, InputStream is) throws IOException {
LOG.debug("Begin document upload");
try {
// Setup xmlrpc client
final XmlRpcClient client = new XmlRpcClient();
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setEncoding("UTF-8");
config.setEnabledForExtensions(true);
config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
if (xmldbURL.hasUserInfo()) {
config.setBasicUserName(xmldbURL.getUsername());
config.setBasicPassword(xmldbURL.getPassword());
}
client.setConfig(config);
String contentType = MimeType.BINARY_TYPE.getName();
final MimeType mime = MimeTable.getInstance().getContentTypeFor(xmldbURL.getDocumentName());
if (mime != null) {
contentType = mime.getName();
}
// Initialize xmlrpc parameters
final List<Object> params = new ArrayList<>(5);
String handle = null;
// Copy data from inputstream to database
final byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
params.clear();
if (handle != null) {
params.add(handle);
}
params.add(buf);
params.add(len);
handle = (String) client.execute("upload", params);
}
// All data transported, now parse data on server
params.clear();
params.add(handle);
params.add(xmldbURL.getCollectionPath());
params.add(Boolean.TRUE);
params.add(contentType);
final Boolean result = (Boolean) client.execute("parseLocal", params);
// Check XMLRPC result
if (result) {
LOG.debug("Document stored.");
} else {
LOG.debug("Could not store document.");
throw new IOException("Could not store document.");
}
} catch (final IOException ex) {
LOG.debug(ex);
throw ex;
} catch (final Exception ex) {
LOG.debug(ex);
throw new IOException(ex.getMessage(), ex);
} finally {
LOG.debug("Finished document upload");
}
}
Aggregations