Search in sources :

Example 1 with LocalCollection

use of org.exist.xmldb.LocalCollection in project exist by eXist-db.

the class TestCase method getResource.

public Resource getResource(Object r) throws XMLDBException {
    LocalCollection collection = null;
    Subject user = null;
    LocalXMLResource res = null;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    if (r instanceof NodeProxy) {
        NodeProxy p = (NodeProxy) r;
        res = new LocalXMLResource(user, pool, collection, p);
    } else if (r instanceof Node) {
        res = new LocalXMLResource(user, pool, collection, XmldbURI.EMPTY_URI);
        res.setContentAsDOM((Node) r);
    } else if (r instanceof AtomicValue) {
        res = new LocalXMLResource(user, pool, collection, XmldbURI.EMPTY_URI);
        res.setContent(r);
    } else if (r instanceof LocalXMLResource)
        res = (LocalXMLResource) r;
    else
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "unknown object " + r.getClass());
    try {
        Field field = res.getClass().getDeclaredField("outputProperties");
        field.setAccessible(true);
        field.set(res, new Properties(defaultProperties));
    } catch (Exception e) {
    }
    return res;
}
Also used : Field(java.lang.reflect.Field) LocalXMLResource(org.exist.xmldb.LocalXMLResource) LocalCollection(org.exist.xmldb.LocalCollection) Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) AtomicValue(org.exist.xquery.value.AtomicValue) Properties(java.util.Properties) NodeProxy(org.exist.dom.persistent.NodeProxy) Subject(org.exist.security.Subject) BrokerPool(org.exist.storage.BrokerPool) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 2 with LocalCollection

use of org.exist.xmldb.LocalCollection in project exist by eXist-db.

the class BrokerPoolTest method noPrivilegeEscalationThroughBrokerRelease_xmldb.

@Test
public void noPrivilegeEscalationThroughBrokerRelease_xmldb() throws EXistException, XMLDBException {
    // take a broker with the guest user
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Subject guestUser = pool.getSecurityManager().getGuestSubject();
    try (final DBBroker broker1 = pool.get(Optional.of(guestUser))) {
        assertEquals("Expected `guest` user, but was: " + broker1.getCurrentSubject().getName(), guestUser.getId(), broker1.getCurrentSubject().getId());
        // perform an XML:DB operation as the SYSTEM user
        final Subject sysUser = pool.getSecurityManager().getSystemSubject();
        new LocalCollection(sysUser, pool, XmldbURI.ROOT_COLLECTION_URI);
        // ensure that after releasing the broker, the user has been returned to the guest user
        assertEquals("Expected `guest` user, but was: " + broker1.getCurrentSubject().getName(), guestUser.getId(), broker1.getCurrentSubject().getId());
    }
}
Also used : LocalCollection(org.exist.xmldb.LocalCollection) Subject(org.exist.security.Subject) Test(org.junit.Test)

Example 3 with LocalCollection

use of org.exist.xmldb.LocalCollection in project exist by eXist-db.

the class AbstractExtractFunction method processCompressedEntry.

/**
 * Processes a compressed entry from an archive
 *
 * @param name The name of the entry
 * @param isDirectory true if the entry is a directory, false otherwise
 * @param is an InputStream for reading the uncompressed data of the entry
 * @param filterParam is an additional param for entry filtering function
 * @param storeParam is an additional param for entry storing function
 *
 * @return the result of processing the compressed entry.
 *
 * @throws XPathException if a query error occurs
 * @throws XMLDBException if a database error occurs
 * @throws IOException if an I/O error occurs
 */
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
    String dataType = isDirectory ? "folder" : "resource";
    // call the entry-filter function
    Sequence[] filterParams = new Sequence[3];
    filterParams[0] = new StringValue(name);
    filterParams[1] = new StringValue(dataType);
    filterParams[2] = filterParam;
    Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
    if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        Sequence entryDataFunctionResult;
        Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
        if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
            Sequence[] dataParams = new Sequence[3];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
            String path = entryDataFunctionResult.itemAt(0).getStringValue();
            Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
            if (isDirectory) {
                XMLDBAbstractCollectionManipulator.createCollection(root, path);
            } else {
                Resource resource;
                Path file = Paths.get(path).normalize();
                name = FileUtils.fileName(file);
                path = file.getParent().toAbsolutePath().toString();
                Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
                MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                // copy the input data
                final byte[] entryData;
                try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                    baos.write(is);
                    entryData = baos.toByteArray();
                }
                try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                    NodeValue content = ModuleUtils.streamToXML(context, bis);
                    resource = target.createResource(name, "XMLResource");
                    ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
                    handler.startDocument();
                    content.toSAX(context.getBroker(), handler, null);
                    handler.endDocument();
                } catch (SAXException e) {
                    resource = target.createResource(name, "BinaryResource");
                    resource.setContent(entryData);
                }
                if (resource != null) {
                    if (mime != null) {
                        ((EXistResource) resource).setMimeType(mime.getName());
                    }
                    target.storeResource(resource);
                }
            }
        } else {
            // copy the input data
            final byte[] entryData;
            try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                baos.write(is);
                entryData = baos.toByteArray();
            }
            // try and parse as xml, fall back to binary
            try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                uncompressedData = ModuleUtils.streamToXML(context, bis);
            } catch (SAXException saxe) {
                if (entryData.length > 0) {
                    try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                        uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
                    }
                }
            }
            // call the entry-data function
            Sequence[] dataParams = new Sequence[4];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = uncompressedData;
            dataParams[3] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
        }
        return entryDataFunctionResult;
    }
}
Also used : Path(java.nio.file.Path) LocalCollection(org.exist.xmldb.LocalCollection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) MimeType(org.exist.util.MimeType) ContentHandler(org.xml.sax.ContentHandler) XMLResource(org.xmldb.api.modules.XMLResource) SAXException(org.xml.sax.SAXException) EXistResource(org.exist.xmldb.EXistResource) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 4 with LocalCollection

use of org.exist.xmldb.LocalCollection in project exist by eXist-db.

the class QueryDialog method setupComponents.

private void setupComponents(boolean loadedFromDb) {
    getContentPane().setLayout(new BorderLayout());
    final JToolBar toolbar = new JToolBar();
    URL url = getClass().getResource("icons/Open24.gif");
    JButton button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.opentooltip"));
    button.addActionListener(e -> open());
    toolbar.add(button);
    if (loadedFromDb) {
        url = getClass().getResource("icons/SaveAs23.gif");
        button = new JButton(new ImageIcon(url));
        button.setToolTipText("Save to database");
        button.addActionListener(e -> saveToDb(query.getText()));
        toolbar.add(button);
    }
    url = getClass().getResource("icons/SaveAs24.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.saveastooltip"));
    button.addActionListener(e -> save(query.getText(), "query"));
    toolbar.add(button);
    url = getClass().getResource("icons/SaveAs25.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.saveresultstooltip"));
    button.addActionListener(e -> save(resultDisplay.getText(), "result"));
    toolbar.add(button);
    toolbar.addSeparator();
    url = getClass().getResource("icons/Copy24.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.copytooltip"));
    button.addActionListener(e -> query.copy());
    toolbar.add(button);
    url = getClass().getResource("icons/Cut24.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.cuttooltip"));
    button.addActionListener(e -> query.cut());
    toolbar.add(button);
    url = getClass().getResource("icons/Paste24.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.pastetooltip"));
    button.addActionListener(e -> query.paste());
    toolbar.add(button);
    toolbar.addSeparator();
    // TODO: change icon
    url = getClass().getResource("icons/Find24.gif");
    button = new JButton(new ImageIcon(url));
    button.setToolTipText(Messages.getString("QueryDialog.compiletooltip"));
    button.addActionListener(e -> compileQuery());
    toolbar.add(button);
    toolbar.addSeparator();
    url = getClass().getResource("icons/Find24.gif");
    submitButton = new JButton(Messages.getString("QueryDialog.submitbutton"), new ImageIcon(url));
    submitButton.setToolTipText(Messages.getString("QueryDialog.submittooltip"));
    toolbar.add(submitButton);
    submitButton.addActionListener(e -> {
        submitButton.setEnabled(false);
        if (collection instanceof LocalCollection) {
            killButton.setEnabled(true);
        }
        queryRunnable = doQuery();
    });
    toolbar.addSeparator();
    url = getClass().getResource("icons/Delete24.gif");
    killButton = new JButton(Messages.getString("QueryDialog.killbutton"), new ImageIcon(url));
    killButton.setToolTipText(Messages.getString("QueryDialog.killtooltip"));
    toolbar.add(killButton);
    killButton.setEnabled(false);
    killButton.addActionListener(e -> {
        if (queryRunnable != null) {
            queryRunnable.killQuery();
            killButton.setEnabled(false);
            queryRunnable = null;
        }
    });
    final JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    split.setResizeWeight(0.6);
    final JComponent qbox = createQueryBox();
    split.setTopComponent(qbox);
    final JPanel vbox = new JPanel();
    vbox.setLayout(new BorderLayout());
    final JLabel label = new JLabel(Messages.getString("QueryDialog.resultslabel"));
    vbox.add(label, BorderLayout.NORTH);
    resultTabs = new JTabbedPane();
    resultDisplay = new RSyntaxTextArea(14, 80);
    resultDisplay.setEditable(false);
    // remove undo
    resultDisplay.getPopupMenu().remove(0);
    // remove redo
    resultDisplay.getPopupMenu().remove(0);
    // remove undo/redo splitter
    resultDisplay.getPopupMenu().remove(0);
    resultDisplay.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
    resultDisplay.setCodeFoldingEnabled(true);
    resultDisplayScrollPane = new RTextScrollPane(resultDisplay);
    resultTabs.add(Messages.getString("QueryDialog.XMLtab"), resultDisplayScrollPane);
    exprDisplay = new RSyntaxTextArea(14, 80);
    exprDisplay.setEditable(false);
    // remove undo
    exprDisplay.getPopupMenu().remove(0);
    // remove redo
    exprDisplay.getPopupMenu().remove(0);
    // remove undo/redo splitter
    exprDisplay.getPopupMenu().remove(0);
    exprDisplayScrollPane = new RTextScrollPane(exprDisplay);
    resultTabs.add(Messages.getString("QueryDialog.tracetab"), exprDisplayScrollPane);
    vbox.add(resultTabs, BorderLayout.CENTER);
    final Box statusbar = Box.createHorizontalBox();
    statusbar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    statusMessage = new JTextField(20);
    statusMessage.setEditable(false);
    statusMessage.setFocusable(true);
    statusbar.add(statusMessage);
    queryPositionDisplay = new JTextField(5);
    queryPositionDisplay.setEditable(false);
    queryPositionDisplay.setFocusable(true);
    statusbar.add(queryPositionDisplay);
    query.addCaretListener(e -> {
        final RSyntaxTextArea txt = (RSyntaxTextArea) e.getSource();
        queryPositionDisplay.setText("Line: " + (txt.getCaretLineNumber() + 1) + " Column:" + (txt.getCaretOffsetFromLineStart() + 1));
    });
    progress = new JProgressBar();
    progress.setPreferredSize(new Dimension(200, statusbar.getHeight()));
    progress.setVisible(false);
    statusbar.add(progress);
    vbox.add(statusbar, BorderLayout.SOUTH);
    split.setBottomComponent(vbox);
    split.setDividerLocation(0.6);
    getContentPane().add(toolbar, BorderLayout.NORTH);
    getContentPane().add(split, BorderLayout.CENTER);
}
Also used : ImageIcon(javax.swing.ImageIcon) JPanel(javax.swing.JPanel) LocalCollection(org.exist.xmldb.LocalCollection) JTabbedPane(javax.swing.JTabbedPane) JButton(javax.swing.JButton) JComponent(javax.swing.JComponent) JProgressBar(javax.swing.JProgressBar) JLabel(javax.swing.JLabel) JComboBox(javax.swing.JComboBox) Box(javax.swing.Box) JToolBar(javax.swing.JToolBar) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) URL(java.net.URL) BorderLayout(java.awt.BorderLayout) RSyntaxTextArea(org.fife.ui.rsyntaxtextarea.RSyntaxTextArea) JSplitPane(javax.swing.JSplitPane) RTextScrollPane(org.fife.ui.rtextarea.RTextScrollPane)

Aggregations

LocalCollection (org.exist.xmldb.LocalCollection)4 Subject (org.exist.security.Subject)2 SAXException (org.xml.sax.SAXException)2 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Properties (java.util.Properties)1 Box (javax.swing.Box)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1 JComboBox (javax.swing.JComboBox)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JProgressBar (javax.swing.JProgressBar)1 JSplitPane (javax.swing.JSplitPane)1