Package org.exist.dom

Examples of org.exist.dom.DocumentImpl


        final XmldbURI newName = XmldbURI.create("dest");

        final Collection srcCollection = EasyMock.createStrictMock(Collection.class);
        final Permission srcPermissions = EasyMock.createStrictMock(Permission.class);

        final DocumentImpl srcSubDocument = EasyMock.createStrictMock(DocumentImpl.class);
        final Permission srcSubDocumentPermissions = EasyMock.createStrictMock(Permission.class);

        final Collection destCollection = EasyMock.createStrictMock(Collection.class);
        final Permission destPermissions = EasyMock.createStrictMock(Permission.class);

        final Collection newDestCollection = EasyMock.createStrictMock(Collection.class);
        final Permission newDestPermissions = EasyMock.createStrictMock(Permission.class);

        final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class)
                .addMockedMethod("getCollection")
                .addMockedMethod("getSubject")
                .createStrictMock();

        final Subject subject = EasyMock.createStrictMock(Subject.class);

        //grant EXECUTE and READ permissions on the src
        expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);

        //grant EXECUTE and WRITE permission on the dest
        expect(broker.getCollection(dest)).andReturn(destCollection);
        final Capture<XmldbURI> newDestURICapture = new Capture<XmldbURI>();
        expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
        expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
        expect(newDestCollection.getPermissionsNoLock()).andReturn(newDestPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(newDestPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);

        //one sub-document with READ permission
        expect(srcCollection.iterator(broker)).andReturn(new ArrayIterator<DocumentImpl>(srcSubDocument));
        expect(srcSubDocument.getPermissions()).andReturn(srcSubDocumentPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(srcSubDocumentPermissions.validate(subject, Permission.READ)).andReturn(true);
        expect(newDestCollection.isEmpty(broker)).andReturn(true); //no documents in the dest collection

        //no sub-collections
View Full Code Here


        final XmldbURI newName = XmldbURI.create("dest");

        final Collection srcCollection = EasyMock.createStrictMock(Collection.class);
        final Permission srcPermissions = EasyMock.createStrictMock(Permission.class);

        final DocumentImpl srcSubDocument = EasyMock.createStrictMock(DocumentImpl.class);
        final Permission srcSubDocumentPermissions = EasyMock.createStrictMock(Permission.class);

        final Collection destCollection = EasyMock.createStrictMock(Collection.class);
        final Permission destPermissions = EasyMock.createStrictMock(Permission.class);

        final Collection newDestCollection = EasyMock.createStrictMock(Collection.class);
        final Permission newDestPermissions = EasyMock.createStrictMock(Permission.class);

        final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class)
                .addMockedMethod("getCollection")
                .addMockedMethod("getSubject")
                .createStrictMock();

        final Subject subject = EasyMock.createStrictMock(Subject.class);

        //grant EXECUTE and READ permissions on the src
        expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);

        //grant EXECUTE and WRITE permission on the dest
        expect(broker.getCollection(dest)).andReturn(destCollection);
        final Capture<XmldbURI> newDestURICapture = new Capture<XmldbURI>();
        expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
        expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
        expect(newDestCollection.getPermissionsNoLock()).andReturn(newDestPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(newDestPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);

        //one sub-document with READ permission
        expect(srcCollection.iterator(broker)).andReturn(new ArrayIterator<DocumentImpl>(srcSubDocument));
        expect(srcSubDocument.getPermissions()).andReturn(srcSubDocumentPermissions);
        expect(broker.getSubject()).andReturn(subject);
        expect(srcSubDocumentPermissions.validate(subject, Permission.READ)).andReturn(false);

        //expectations for exception that should be thrown
        expect(srcCollection.getURI()).andReturn(src);
        expect(srcSubDocument.getURI()).andReturn(src.append(newName).append("someSubDocument.xml"));
        expect(broker.getSubject()).andReturn(subject);
        expect(subject.getName()).andReturn("Fake user");

        //no sub-collections
        expect(srcCollection.collectionIterator(broker)).andReturn(new EmptyIterator<XmldbURI>());
View Full Code Here

            final int len = children.getLength();
            if (LOG.isDebugEnabled())
                {LOG.debug("found " + len + " nodes to insert");}
            for (int i = 0; i < ql.length; i++) {
                final StoredNode node = ql[i];
                final DocumentImpl doc = (DocumentImpl)node.getOwnerDocument();
                doc.getMetadata().setIndexListener(listener);
                if (!doc.getPermissions().validate(broker.getSubject(), Permission.WRITE)) {
                        throw new PermissionDeniedException("permission to update document denied");
                }
                parent = (NodeImpl) node.getParentNode();
                switch (mode) {
                    case INSERT_BEFORE:
                        parent.insertBefore(transaction, children, node);
                        break;
                    case INSERT_AFTER:
                        parent.insertAfter(transaction, children, node);
                        break;
                }
                doc.getMetadata().clearIndexListener();
                doc.getMetadata().setLastModified(System.currentTimeMillis());
                modifiedDocuments.add(doc);
                broker.storeXMLResource(transaction, doc);
                notifier.notifyUpdate(doc, UpdateListener.UPDATE);
            }
            checkFragmentation(transaction, modifiedDocuments);
View Full Code Here

            final IndexListener listener = new IndexListener(ql);
            final NotificationService notifier = broker.getBrokerPool().getNotificationService();
            final String newName = children.item(0).getNodeValue();
            for (int i = 0; i < ql.length; i++) {
                final StoredNode node = ql[i];
                final DocumentImpl doc = (DocumentImpl)node.getOwnerDocument();
                if (!doc.getPermissions().validate(broker.getSubject(), Permission.WRITE)) {
                        throw new PermissionDeniedException("User '" + broker.getSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
                }
                doc.getMetadata().setIndexListener(listener);
                parent = (NodeImpl) node.getParentNode();
                switch (node.getNodeType()) {
                    case Node.ELEMENT_NODE:
                        final ElementImpl newElem = new ElementImpl((ElementImpl) node);
                        newElem.setNodeName(new QName(newName, "", null));
                        parent.updateChild(transaction, node, newElem);
                        modificationCount++;
                        break;
                    case Node.ATTRIBUTE_NODE:
                        final AttrImpl newAttr = new AttrImpl((AttrImpl) node);
                        newAttr.setNodeName(new QName(newName, "", null));
                        parent.updateChild(transaction, node, newAttr);
                        modificationCount++;
                        break;
                    default:
                        throw new EXistException("unsupported node-type");
                }

                doc.getMetadata().clearIndexListener();
                doc.getMetadata().setLastModified(System.currentTimeMillis());
                modifiedDocuments.add(doc);
                broker.storeXMLResource(transaction, doc);
                notifier.notifyUpdate(doc, UpdateListener.UPDATE);
            }
            checkFragmentation(transaction, modifiedDocuments);
View Full Code Here

        }

    }

    private Sequence extractMetadataFromLocalResource(XmldbURI docUri) throws XPathException {
        DocumentImpl doc = null;
        try {
            doc = context.getBroker().getXMLResource(docUri, Lock.READ_LOCK);
            if (doc instanceof BinaryDocument) {
                //resolve real filesystem path of binary file
                File binaryFile = ((NativeBroker) context.getBroker()).getCollectionBinaryFileFsPath(docUri);
                if (!binaryFile.exists()) {
                    throw new XPathException("Binary Document at " + docUri.toString() + " does not exist.");
                }
                return exifToolExtract(binaryFile);
            } else {
                throw new XPathException("The binay document at " + docUri.toString() + " cannot be found.");
            }
        } catch (PermissionDeniedException pde) {
            throw new XPathException("Could not access binary document: " + pde.getMessage(), pde);
        } finally {
            if (doc != null) {
                doc.getUpdateLock().release(Lock.READ_LOCK);
            }
        }
    }
View Full Code Here

                return;
            vCollection = getVersionsCollection(broker, transaction, documentPath.removeLastSegment());

            String existingURI = document.getFileURI().toString();
            XmldbURI baseURI = XmldbURI.create(existingURI + BASE_SUFFIX);
            DocumentImpl baseRev = vCollection.getDocument(broker, baseURI);

            String vFileName;

            if (baseRev == null) {
                vFileName = baseURI.toString();
                removeLast = false;
                // copy existing document to base revision here!
                broker.copyResource(transaction, document, vCollection, baseURI);
            } else if (remove) {
                vFileName = existingURI + DELETED_SUFFIX;
                removeLast = false;
            } else {
                vFileName = existingURI + TEMP_SUFFIX;
                removeLast = true;
            }

            // setReferenced(true) will tell the broker that the document
            // data is referenced from another document and should not be
            // deleted when the orignal document is removed.
            document.getMetadata().setReferenced(true);


            if(document instanceof BinaryDocument) {
                XmldbURI binUri = XmldbURI.createInternal(vFileName);
                broker.copyResource(transaction, document, vCollection, binUri);
                vDoc = vCollection.getDocument(broker, binUri);
            } else {
                vDoc = new DocumentImpl(broker.getBrokerPool(), vCollection, XmldbURI.createInternal(vFileName));
                vDoc.copyOf(document, true);
                vDoc.copyChildren(document);
            }
           
            if (!remove) {
View Full Code Here

        }
    }

    private String installAndDeployFromDb(String path, String repoURI) throws XPathException {
        XmldbURI docPath = XmldbURI.createInternal(path);
        DocumentImpl doc = null;
        try {
            doc = context.getBroker().getXMLResource(docPath, Lock.READ_LOCK);
            if (doc.getResourceType() != DocumentImpl.BINARY_FILE)
                throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not a valid .xar", new StringValue(path));

            File file = ((NativeBroker)context.getBroker()).getCollectionBinaryFileFsPath(doc.getURI());
            RepoPackageLoader loader = null;
            if (repoURI != null)
                loader = new RepoPackageLoader(repoURI);
            Deployment deployment = new Deployment(context.getBroker());
            return deployment.installAndDeploy(file, loader);
        } catch (PackageException e) {
            throw new XPathException(this, EXPathErrorCode.EXPDY007, e.getMessage());
        } catch (IOException e) {
            throw new XPathException(this, EXPathErrorCode.EXPDY007, e.getMessage());
        } catch (PermissionDeniedException e) {
            throw new XPathException(this, EXPathErrorCode.EXPDY007, e.getMessage());
        } finally {
            if (doc != null)
                doc.getUpdateLock().release(Lock.READ_LOCK);
        }
    }
View Full Code Here

    }

    private BinaryDocument _getDocument(String path) throws XPathException {
      try {
      XmldbURI uri = XmldbURI.createInternal(path);
      DocumentImpl doc = context.getBroker().getXMLResource(uri, Lock.READ_LOCK);
      if (doc.getResourceType() != DocumentImpl.BINARY_FILE)
        throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not a valid .xar", new StringValue(path));
      return (BinaryDocument) doc;
    } catch (PermissionDeniedException e) {
      throw new XPathException(this, EXPathErrorCode.EXPDY003, e.getMessage(), new StringValue(path), e);
    }
View Full Code Here

  public Sequence eval(Sequence[] args, Sequence contextSequence)
      throws XPathException {
    XmldbURI uri = XmldbURI.createInternal(args[0].getStringValue());
    String field = args[1].getStringValue();
   
    DocumentImpl doc = null;
    try {
      doc = context.getBroker().getXMLResource(uri, Lock.READ_LOCK);
      if (doc == null) {
                return Sequence.EMPTY_SEQUENCE;
            }
      // Get the lucene worker
            LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
            String content = index.getFieldContent(doc.getDocId(), field);
            return content == null ? Sequence.EMPTY_SEQUENCE : new org.exist.xquery.value.StringValue(content);
    } catch (PermissionDeniedException e) {
      throw new XPathException(this, LuceneModule.EXXQDYFT0001, "Permission denied to read document " + args[0].getStringValue());
    } catch (IOException e) {
      throw new XPathException(this, LuceneModule.EXXQDYFT0002, "IO error while reading document " + args[0].getStringValue());
    } finally {
      if (doc != null)
        doc.getUpdateLock().release(Lock.READ_LOCK);
    }
  }
View Full Code Here

                    }
                } catch (PermissionDeniedException pde) {
                    throw new XPathException(pde.getMessage(), pde);
                }

    DocumentImpl docImage = null;
    BinaryDocument binImage = null;
    @SuppressWarnings("unused")
    BinaryDocument doc = null;
    BufferedImage bImage = null;
    @SuppressWarnings("unused")
    byte[] imgData = null;
    Image image = null;
    ByteArrayOutputStream os = null;

                try {
    Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);

    while (i.hasNext()) {
      docImage = (DocumentImpl) i.next();
      // is not already existing??
      if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(
          existingThumbsArray, docImage, prefix)))) {
        if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
          // TODO maybe extends for gifs too.
          if (docImage.getMetadata().getMimeType().startsWith(
              "image/jpeg")) {

            binImage = (BinaryDocument) docImage;

            // get a byte array representing the image

            try {
                                                   InputStream is = dbbroker.getBinaryResource(binImage);
              image = ImageIO.read(is);
            } catch (IOException ioe) {
              throw new XPathException(this,ioe.getMessage());
            }

            try {
              bImage = ImageModule.createThumb(image, maxThumbHeight,
                  maxThumbWidth);
            } catch (Exception e) {
              throw new XPathException(this, e.getMessage());
            }

            if (isSaveToDataBase) {
              os = new ByteArrayOutputStream();
              try {
                ImageIO.write(bImage, "jpg", os);
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              try {
                doc = thumbCollection.addBinaryResource(
                    transaction, dbbroker,
                    XmldbURI.create(prefix
                        + docImage.getFileURI()), os
                        .toByteArray(), "image/jpeg");
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              // result.add(new
              // StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
              // + docImage.getFileURI()));
            } else {
              try {
                ImageIO
                    .write(
                        bImage,
                        "jpg",
                        new File(thumbPath.toString()
                            + "/" + prefix
                            + docImage.getFileURI()));
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              // result.add(new StringValue(
              // thumbPath.toString() + "/"
              // + THUMBPREFIX
              // + docImage.getFileURI()));
            }
          }
      } else {

        // result.add(new StringValue(""+docImage.getURI()+"|"
        // + ((existingThumbsCol != null) ? ""
        // + existingThumbsCol.getURI() : thumbDir
        // .toString()) + "/" + prefix
        // + docImage.getFileURI()));

        result.add(new StringValue(docImage.getFileURI().toString()));
      }
    }
                } catch(PermissionDeniedException pde) {
                    throw new XPathException(this, pde.getMessage(), pde);
                }
View Full Code Here

TOP

Related Classes of org.exist.dom.DocumentImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.