Search in sources :

Example 1 with EXistInputSource

use of org.exist.util.EXistInputSource in project exist by eXist-db.

the class FileSystemBackupDescriptor method getBlobInputSource.

@Override
public EXistInputSource getBlobInputSource(final String blobId) {
    final Path blobFile = root.resolve("blob").resolve(blobId);
    EXistInputSource is = null;
    if ((!Files.isDirectory(blobFile)) && Files.isReadable(blobFile)) {
        is = new FileInputSource(blobFile);
    }
    return is;
}
Also used : Path(java.nio.file.Path) FileInputSource(org.exist.util.FileInputSource) EXistInputSource(org.exist.util.EXistInputSource)

Example 2 with EXistInputSource

use of org.exist.util.EXistInputSource in project exist by eXist-db.

the class Restore method restore.

public void restore(final DBBroker broker, @Nullable final Txn transaction, final String newAdminPass, final Path f, final RestoreListener listener, final boolean overwriteApps) throws EXistException, IOException, SAXException, PermissionDeniedException {
    // set the admin password
    if (newAdminPass != null) {
        setAdminCredentials(broker, newAdminPass);
    }
    // get the backup descriptors, can be more than one if it was an incremental backup
    final Deque<BackupDescriptor> descriptors = getBackupDescriptors(f);
    final Set<String> appsToSkip = overwriteApps ? Collections.emptySet() : AppRestoreUtils.checkApps(broker, descriptors);
    // count all files
    long totalNrOfFiles = 0;
    for (BackupDescriptor backupDescriptor : descriptors) {
        totalNrOfFiles += backupDescriptor.getNumberOfFiles();
    }
    // continue restore
    final XMLReaderPool parserPool = broker.getBrokerPool().getParserPool();
    XMLReader reader = null;
    try {
        reader = parserPool.borrowXMLReader();
        listener.started(totalNrOfFiles);
        while (!descriptors.isEmpty()) {
            final BackupDescriptor descriptor = descriptors.pop();
            if (appsToSkip.contains(descriptor.getSymbolicPath())) {
                listener.skipResources("Skipping app path " + descriptor.getSymbolicPath() + ". Newer version " + "is already installed.", descriptor.getNumberOfFiles());
            } else {
                final EXistInputSource is = descriptor.getInputSource();
                is.setEncoding(UTF_8.displayName());
                final RestoreHandler handler = new RestoreHandler(broker, transaction, descriptor, listener, appsToSkip);
                reader.setContentHandler(handler);
                reader.parse(is);
            }
        }
    } finally {
        listener.finished();
        if (reader != null) {
            parserPool.returnXMLReader(reader);
        }
    }
}
Also used : EXistInputSource(org.exist.util.EXistInputSource) RestoreHandler(org.exist.backup.restore.RestoreHandler) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader)

Example 3 with EXistInputSource

use of org.exist.util.EXistInputSource in project exist by eXist-db.

the class RemoteCollection method uploadAndStore.

private void uploadAndStore(final Resource res) throws XMLDBException {
    InputStream is = null;
    String descString = "<unknown>";
    try {
        if (res instanceof RemoteBinaryResource) {
            is = ((RemoteBinaryResource) res).getStreamContent();
            descString = ((RemoteBinaryResource) res).getStreamSymbolicPath();
        } else {
            final Object content = res.getContent();
            if (content instanceof File) {
                final File file = (File) content;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                } catch (final FileNotFoundException e) {
                    throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "could not read resource from file " + file.getAbsolutePath(), e);
                }
            } else if (content instanceof InputSource) {
                is = ((InputSource) content).getByteStream();
                if (content instanceof EXistInputSource) {
                    descString = ((EXistInputSource) content).getSymbolicPath();
                }
            } else if (content instanceof String) {
                // TODO(AR) we really should not allow String to be used here, as we loose the encoding info and default to UTF-8!
                is = new UnsynchronizedByteArrayInputStream(((String) content).getBytes(UTF_8));
            } else {
                LOG.error("Unable to get content from {}", content);
            }
        }
        final byte[] chunk;
        if (res instanceof ExtendedResource) {
            if (res instanceof AbstractRemoteResource) {
                final long contentLen = ((AbstractRemoteResource) res).getContentLength();
                if (contentLen != -1) {
                    // content length is known
                    chunk = new byte[(int) Math.min(contentLen, MAX_UPLOAD_CHUNK)];
                } else {
                    chunk = new byte[MAX_UPLOAD_CHUNK];
                }
            } else {
                final long streamLen = ((ExtendedResource) res).getStreamLength();
                if (streamLen != -1) {
                    // stream length is known
                    chunk = new byte[(int) Math.min(streamLen, MAX_UPLOAD_CHUNK)];
                } else {
                    chunk = new byte[MAX_UPLOAD_CHUNK];
                }
            }
        } else {
            chunk = new byte[MAX_UPLOAD_CHUNK];
        }
        try {
            String fileName = null;
            if (chunk.length > 0) {
                int len;
                while ((len = is.read(chunk)) > -1) {
                    final List<Object> params = new ArrayList<>();
                    if (fileName != null) {
                        params.add(fileName);
                    }
                    /*
                    Only compress the chunk if it is larger than 256 bytes,
                    otherwise the compression framing overhead results in a larger chunk
                    */
                    if (len < 256) {
                        params.add(chunk);
                        params.add(len);
                        fileName = (String) execute("upload", params);
                    } else {
                        final byte[] compressed = Compressor.compress(chunk, len);
                        params.add(compressed);
                        params.add(len);
                        fileName = (String) execute("uploadCompressed", params);
                    }
                }
            }
            if (fileName == null) {
                // Zero length stream? Let's get a fileName!
                final List<Object> params = new ArrayList<>();
                params.add(new byte[0]);
                params.add(0);
                fileName = (String) execute("upload", params);
            }
            final List<Object> params = new ArrayList<>();
            final List<Object> paramsEx = new ArrayList<>();
            params.add(fileName);
            paramsEx.add(fileName);
            try {
                final String resURI = getPathURI().append(XmldbURI.xmldbUriFor(res.getId())).toString();
                params.add(resURI);
                paramsEx.add(resURI);
            } catch (final URISyntaxException e) {
                throw new XMLDBException(ErrorCodes.INVALID_URI, e);
            }
            params.add(Boolean.TRUE);
            paramsEx.add(Boolean.TRUE);
            if (res instanceof EXistResource) {
                final EXistResource rxres = (EXistResource) res;
                params.add(rxres.getMimeType());
                paramsEx.add(rxres.getMimeType());
                // This one is only for the new style!!!!
                paramsEx.add((BinaryResource.RESOURCE_TYPE.equals(res.getResourceType())) ? Boolean.FALSE : Boolean.TRUE);
                if (rxres.getCreationTime() != null) {
                    params.add(rxres.getCreationTime());
                    paramsEx.add(rxres.getCreationTime());
                    params.add(rxres.getLastModificationTime());
                    paramsEx.add(rxres.getLastModificationTime());
                }
            }
            try {
                execute("parseLocalExt", paramsEx);
            } catch (final XMLDBException e) {
                // Identifying old versions
                final String excMsg = e.getCause().getMessage();
                if (excMsg.contains("No such handler") || excMsg.contains("No method matching")) {
                    execute("parseLocal", params);
                } else {
                    throw e;
                }
            }
        } catch (final IOException e) {
            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "failed to read resource from " + descString, e);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (final IOException ioe) {
                LOG.warn(ioe.getMessage(), ioe);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) EXistInputSource(org.exist.util.EXistInputSource) BufferedInputStream(java.io.BufferedInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) EXistInputSource(org.exist.util.EXistInputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) File(java.io.File)

Example 4 with EXistInputSource

use of org.exist.util.EXistInputSource in project exist by eXist-db.

the class FileSystemBackupDescriptor method getInputSource.

@Override
public EXistInputSource getInputSource(final String describedItem) {
    final Path child = descriptor.getParent().resolve(describedItem);
    EXistInputSource is = null;
    if ((!Files.isDirectory(child)) && Files.isReadable(child)) {
        is = new FileInputSource(child);
    }
    return is;
}
Also used : Path(java.nio.file.Path) FileInputSource(org.exist.util.FileInputSource) EXistInputSource(org.exist.util.EXistInputSource)

Example 5 with EXistInputSource

use of org.exist.util.EXistInputSource in project exist by eXist-db.

the class SystemImport method restore.

public void restore(final String username, final Object credentials, @Nullable final String newCredentials, final Path f, final RestoreListener listener) throws IOException, SAXException, AuthenticationException, ConfigurationException, PermissionDeniedException, TransactionException {
    // login
    try (final DBBroker broker = db.authenticate(username, credentials);
        final Txn transaction = broker.continueOrBeginTransaction()) {
        // set the new password
        if (newCredentials != null) {
            setAdminCredentials(broker, newCredentials);
        }
        // get the backup descriptors, can be more than one if it was an incremental backup
        final Deque<BackupDescriptor> descriptors = getBackupDescriptors(f);
        final XMLReaderPool parserPool = broker.getBrokerPool().getParserPool();
        XMLReader reader = null;
        try {
            reader = parserPool.borrowXMLReader();
            listener.started(0);
            while (!descriptors.isEmpty()) {
                final BackupDescriptor descriptor = descriptors.pop();
                final EXistInputSource is = descriptor.getInputSource();
                is.setEncoding("UTF-8");
                final SystemImportHandler handler = new SystemImportHandler(broker, transaction, descriptor, listener);
                reader.setContentHandler(handler);
                reader.parse(is);
            }
        } finally {
            listener.finished();
            if (reader != null) {
                parserPool.returnXMLReader(reader);
            }
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) SystemImportHandler(org.exist.backup.restore.SystemImportHandler) EXistInputSource(org.exist.util.EXistInputSource) Txn(org.exist.storage.txn.Txn) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader)

Aggregations

EXistInputSource (org.exist.util.EXistInputSource)7 Path (java.nio.file.Path)4 InputSource (org.xml.sax.InputSource)3 File (java.io.File)2 FileInputSource (org.exist.util.FileInputSource)2 XMLReaderPool (org.exist.util.XMLReaderPool)2 XMLReader (org.xml.sax.XMLReader)2 java.io (java.io)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 RestoreHandler (org.exist.backup.restore.RestoreHandler)1 SystemImportHandler (org.exist.backup.restore.SystemImportHandler)1 DBBroker (org.exist.storage.DBBroker)1 Txn (org.exist.storage.txn.Txn)1