Package org.exist.collections

Examples of org.exist.collections.IndexInfo


            final CollectionConfigurationManager mgr = pool.getConfigurationManager();
            mgr.addConfiguration(txn, broker, root, COLLECTION_CONFIG);

            //store test data
            IndexInfo info = root.validateXMLResource(txn, broker, doc1uri.lastSegment(), XML1);
            root.store(txn, broker, info, XML1, false);
            info = root.validateXMLResource(txn, broker, doc2uri.lastSegment(), XML2);
            root.store(txn, broker, info, XML2, false);
            root.addBinaryResource(txn, broker, doc3uri.lastSegment(), BINARY.getBytes(), null);
View Full Code Here


   
    final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
    final Txn txn = transact.beginTransaction();
    try
    {
      final IndexInfo info = collection.validateXMLResource(txn, broker, docName, content);
      //TODO : unlock the collection here ?
      collection.store(txn, broker, info, content, false);
      transact.commit(txn);
    }
    catch(final Exception e)
View Full Code Here

            CollectionConfigurationManager mgr = pool.getConfigurationManager();
            mgr.addConfiguration(txn, broker, root, COLLECTION_CONFIG);

            System.out.println("STORING DOCUMENT....");
            IndexInfo info = root.validateXMLResource(txn, broker, doc1uri.lastSegment(), XML1);
            assertNotNull(info);
            System.out.println("STORING DOCUMENT....SECOND ROUND....");
            root.store(txn, broker, info, XML1, false);
            assertNotNull(info.getDocument());
            System.out.println("STORING DOCUMENT....DONE.");

            doc1 = info.getDocument();

            info = root.validateXMLResource(txn, broker, doc2uri.lastSegment(), XML2);
            assertNotNull(info);
            root.store(txn, broker, info, XML2, false);

            doc2 =  info.getDocument();

            System.out.println("store "+doc3uri);
            root.addBinaryResource(txn, broker, doc3uri.lastSegment(), BINARY.getBytes(), null);

            txnManager.commit(txn);
View Full Code Here

        Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
   
    //store test document
        IndexInfo info = root.validateXMLResource(transaction, broker, XmldbURI.create(documentName), testDocument);
        assertNotNull(info);
        root.store(transaction, broker, info, new InputSource(new StringReader(testDocument)), false);
       
        //commit the transaction
        transact.commit(transaction);
View Full Code Here

              txnManager = pool.getTransactionManager();
              assertNotNull(txnManager);
              txn = txnManager.beginTransaction();
              assertNotNull(txn);

          IndexInfo info = col.validateXMLResource(txn, broker, doc1uri.lastSegment(), XML1);
              assertNotNull(info);
              col.store(txn, broker, info, XML1, false);
             
              //XXX: need to simulate unfinished transaction & crash
              txnManager.commit(txn);

          } catch (Exception e) {
              e.printStackTrace();
              txnManager.abort(txn);
              fail(e.getMessage());
          }
         
        } finally {
          pool.release(broker);
        }
         
        shutdown();
      startDB();

      md = MetaData.get();
      assertNotNull(md);
     
      try {
       
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            assertNotNull(broker);

            Collection root = broker.getCollection(col1uri);
          assertNotNull(root);

          TransactionManager txnManager = null;
          Txn txn = null;
          try {
              txnManager = pool.getTransactionManager();
              assertNotNull(txnManager);
              txn = txnManager.beginTransaction();
              assertNotNull(txn);

           
          IndexInfo info = root.validateXMLResource(txn, broker, doc1uri.lastSegment(), XML1);
              assertNotNull(info);
              root.store(txn, broker, info, XML1, false);

              txnManager.commit(txn);
             
View Full Code Here

 
              CollectionConfigurationManager mgr = pool.getConfigurationManager();
              mgr.addConfiguration(txn, broker, root, COLLECTION_CONFIG);
 
              System.out.println("store "+doc1uri);
              IndexInfo info = root.validateXMLResource(txn, broker, doc1uri.lastSegment(), XML1);
              assertNotNull(info);
              root.store(txn, broker, info, XML1, false);
 
              txnManager.commit(txn);
          } catch (Exception e) {
              e.printStackTrace();
              txnManager.abort(txn);
              fail(e.getMessage());
          }
           
        Metas docMD = md.getMetas(doc1uri);
        assertNotNull(docMD);
       
        String uuid = docMD.getUUID();

            try {
              txn = txnManager.beginTransaction();
              assertNotNull(txn);
              System.out.println("Transaction started ...");
 
              root = broker.getOrCreateCollection(txn, col1uri);
              assertNotNull(root);
              broker.saveCollection(txn, root);
 
              CollectionConfigurationManager mgr = pool.getConfigurationManager();
              mgr.addConfiguration(txn, broker, root, COLLECTION_CONFIG);
 
              System.out.println("store "+doc1uri);
              IndexInfo info = root.validateXMLResource(txn, broker, doc1uri.lastSegment(), wrongXML);
              assertNotNull(info);
              root.store(txn, broker, info, wrongXML, false);
 
              txnManager.commit(txn);
          } catch (Exception e) {
View Full Code Here

//      return doc;
//  }
 
  private static DocumentImpl storeDocument(Txn txn, DBBroker broker, Collection col, XmldbURI uri, String data) throws TriggerException, EXistException, PermissionDeniedException, SAXException, LockException, IOException {
        System.out.println("STORING DOCUMENT....");
        IndexInfo info = col.validateXMLResource(txn, broker, uri.lastSegment(), data);
        assertNotNull(info);
        System.out.println("STORING DOCUMENT....SECOND ROUND....");
        col.store(txn, broker, info, data, false);
        assertNotNull(info.getDocument());
        System.out.println("STORING DOCUMENT....DONE.");

        return info.getDocument();
  }
View Full Code Here

            if(res.file != null) {
                uri = res.file.toURI().toASCIIString();
            }
           
            Collection collection = null;
            final IndexInfo info;
            try {
                collection = broker.openCollection(path, Lock.WRITE_LOCK);
                if(collection == null) {
                    transact.abort(txn);
                    throw new XMLDBException(ErrorCodes.INVALID_COLLECTION, "Collection " + path + " not found");
                }
                for(final Observer observer : observers) {
                    collection.addObserver(observer);
                }
                if (uri != null || res.inputSource!=null) {
                    setupParser(collection, res);
                    info = collection.validateXMLResource(txn, broker, resURI, (uri != null) ? new InputSource(uri) : res.inputSource);
                } else if (res.root != null) {
                    info = collection.validateXMLResource(txn, broker, resURI, res.root);
                } else {
                    info = collection.validateXMLResource(txn, broker, resURI, res.content);
                }
                //Notice : the document should now have a Lock.WRITE_LOCK update lock
                //TODO : check that no exception occurs in order to allow it to be released
                info.getDocument().getMetadata().setMimeType(res.getMimeType());
                if (res.datecreated  != null) {
                    info.getDocument().getMetadata().setCreated(res.datecreated.getTime());
                }
                if (res.datemodified != null) {
                    info.getDocument().getMetadata().setLastModified(res.datemodified.getTime());
                }
            } finally {
                if(collection != null) {
                    collection.release(Lock.WRITE_LOCK);
                }
View Full Code Here

          selfLink.setAttribute("rel", "self");
          selfLink.setAttribute("type", Atom.MIME_TYPE);
          selfLink.setAttribute("href", "#");
          root.appendChild(selfLink);

          final IndexInfo info = collection.validateXMLResource(transaction, broker, FEED_DOCUMENT_URI, doc);
          setPermissions(broker, root, info.getDocument());
          // TODO : We should probably unlock the collection here
          collection.store(transaction, broker, info, doc, false);
          transact.commit(transaction);
          response.setStatusCode(204);
          response.setHeader("Location", request.getModuleBase() + request.getPath());

        } catch (final IOException ex) {
          transact.abort(transaction);
          throw new EXistException("IO error: " + ex.getMessage(), ex);
        } catch (final TriggerException ex) {
          transact.abort(transaction);
          throw new EXistException("Trigger failed: " + ex.getMessage(), ex);
        } catch (final SAXException ex) {
          transact.abort(transaction);
          throw new EXistException("SAX error: " + ex.getMessage(), ex);
        } catch (final LockException ex) {
          transact.abort(transaction);
          throw new EXistException("Cannot acquire write lock.", ex);
        } finally {
                    transact.close(transaction);
                }
      } else if ("entry".equals(root.getLocalName())) {

        if (collection == null) {
          throw new BadRequestException("Collection "
              + request.getPath() + " does not exist.");
        }

        LOG.debug("Adding entry to " + request.getPath());
        DocumentImpl feedDoc = null;
        feedDoc = collection.getDocument(broker, FEED_DOCUMENT_URI);

        if (!feedDoc.getPermissions().validate(broker.getSubject(), Permission.WRITE))
          {throw new PermissionDeniedException(
              "Permission denied to update feed " + collection.getURI());}

        final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
        final Txn transaction = transact.beginTransaction();
        final String uuid = UUIDGenerator.getUUID();
        final String id = "urn:uuid:" + uuid;
        final Element publishedE = DOM.replaceTextElement(root,
            Atom.NAMESPACE_STRING, "published", currentDateTime, true, true);
        DOM.replaceTextElement(root, Atom.NAMESPACE_STRING, "updated", currentDateTime, true, true);
        DOM.replaceTextElement(root, Atom.NAMESPACE_STRING, "id", id, true, true);

        Element editLink = findLink(root, "edit");
        final Element editLinkSrc = findLink(root, "edit-media");
        if (editLink != null || editLinkSrc != null) {
          throw new BadRequestException(
              "An edit link relation cannot be specified in the entry.");
        }
        editLink = doc.createElementNS(Atom.NAMESPACE_STRING, "link");
        editLink.setAttribute("rel", "edit");
        editLink.setAttribute("type", Atom.MIME_TYPE);
        editLink.setAttribute("href", "?id=" + id);
        final Node next = publishedE.getNextSibling();
        if (next == null) {
          root.appendChild(editLink);
        } else {
          root.insertBefore(editLink, next);
        }

        try {
          // get the feed
          LOG.debug("Acquiring lock on feed document...");
          final ElementImpl feedRoot = (ElementImpl) feedDoc.getDocumentElement();

          // Lock the feed
          feedDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);

          // Append the entry
          collection = broker.getOrCreateCollection(transaction, pathUri.append(ENTRY_COLLECTION_URI));
          setPermissions(broker, root, collection);
          broker.saveCollection(transaction, collection);
          final XmldbURI entryURI = entryURI(uuid);
          final DocumentImpl entryDoc = collection.getDocument(broker, entryURI);
          if (entryDoc != null) {
            throw new PermissionDeniedException("Entry with " + id
                + " already exists.");
          }
          final IndexInfo info = collection.validateXMLResource(transaction, broker, entryURI, doc);
          setPermissions(broker, root, info.getDocument());
          // TODO : We should probably unlock the collection here
          collection.store(transaction, broker, info, doc, false);

          // Update the updated element
          DOMDB.replaceTextElement(transaction, feedRoot,
              Atom.NAMESPACE_STRING, "updated", currentDateTime,
              true);

          // Store the changes
          LOG.debug("Storing change...");
          broker.storeXMLResource(transaction, feedDoc);
          transact.commit(transaction);

          LOG.debug("Done!");

          //XXX: response outside of try-block
          response.setStatusCode(201);
          response.setHeader("Location", request.getModuleBase()
              + request.getPath() + "?id=" + id);
          getEntryById(broker, request.getPath(), id, response);
          /*
           * response.setContentType(Atom.MIME_TYPE+"; charset="+charset
           * ); OutputStreamWriter w = new
           * OutputStreamWriter(response.getOutputStream(),charset);
           * Transformer identity =
           * TransformerFactory.newInstance().newTransformer();
           * identity.transform(new DOMSource(doc),new
           * StreamResult(w)); w.flush(); w.close();
           */
        } catch (final IOException ex) {
          transact.abort(transaction);
          throw new EXistException("IO error: " + ex.getMessage(), ex);
        } catch (final TriggerException ex) {
          transact.abort(transaction);
          throw new EXistException("Trigger failed: "
              + ex.getMessage(), ex);
        } catch (final SAXException ex) {
          transact.abort(transaction);
          throw new EXistException("SAX error: " + ex.getMessage(),
              ex);
        } catch (final LockException ex) {
          transact.abort(transaction);
          throw new EXistException("Cannot acquire write lock.", ex);
          /*
           * } catch (IOException ex) { throw new
           * EXistException("Internal error while serializing result."
           * ,ex); } catch (TransformerException ex) { throw new
           * EXistException("Serialization error.",ex);
           */
        } finally {
                    transact.close(transaction);
          if (feedDoc != null)
            {feedDoc.getUpdateLock().release(Lock.WRITE_LOCK);}
        }
      } else {
        throw new BadRequestException(
            "Unexpected element: {http://www.w3.org/2005/Atom}" + root.getLocalName());
      }

    } else {
      if (collection == null)
        {throw new BadRequestException("Collection " + request.getPath() + " does not exist.");}

      final DocumentImpl feedDoc = collection.getDocument(broker, FEED_DOCUMENT_URI);
      if (feedDoc == null)
        {throw new BadRequestException("Feed at " + request.getPath() + " does not exist.");}

      if (!feedDoc.getPermissions().validate(broker.getSubject(), Permission.WRITE))
        {throw new PermissionDeniedException(
            "Permission denied to update feed " + collection.getURI());}

      String filename = request.getHeader("Slug");
      if (filename == null) {
        final String ext = MimeTable.getInstance().getPreferredExtension(mime);
        int count = 1;
        while (filename == null) {
          filename = "resource" + count + ext;
         
          if (collection.getDocument(broker, XmldbURI.create(filename)) != null)
            {filename = null;}

          count++;
        }
      }

      final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
      final Txn transaction = transact.beginTransaction();
      try {
        final XmldbURI docUri = XmldbURI.create(filename);
        if (collection.getDocument(broker, docUri) != null) {
          transact.abort(transaction);
          throw new BadRequestException("Resource " + docUri + " already exists in collection " + pathUri);
        }

        final File tempFile = storeInTemporaryFile(request.getInputStream(), request.getContentLength());

        if (mime.isXMLType()) {
          InputStream is = new FileInputStream(tempFile);
         
          final IndexInfo info = collection.validateXMLResource(
              transaction, broker, docUri,
              new InputSource(new InputStreamReader(is, charset)));
         
          is.close();
          info.getDocument().getMetadata().setMimeType(contentType);
          is = new FileInputStream(tempFile);
         
          collection.store(transaction, broker, info,
              new InputSource(new InputStreamReader(is, charset)), false);
         
          is.close();
        } else {
          final FileInputStream is = new FileInputStream(tempFile);
          collection.addBinaryResource(transaction, broker, docUri, is, contentType, tempFile.length());
          is.close();
        }

        try {
          LOG.debug("Acquiring lock on feed document...");
          feedDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);
         
          String title = request.getHeader("Title");
          if (title == null)
            {title = filename;}

          final String created = DateFormatter.toXSDDateTime(new Date());
          final ElementImpl feedRoot = (ElementImpl) feedDoc.getDocumentElement();
          DOMDB.replaceTextElement(transaction, feedRoot, Atom.NAMESPACE_STRING, "updated", created, true);
          final String uuid = UUIDGenerator.getUUID();
          final String id = "urn:uuid:" + uuid;
          final Element mediaEntry = generateMediaEntry(id, created, title, filename, mime.getName());

          collection = broker.getOrCreateCollection(transaction, pathUri.append(ENTRY_COLLECTION_URI));
          broker.saveCollection(transaction, collection);
          final XmldbURI entryURI = entryURI(uuid);

          final DocumentImpl entryDoc = collection.getDocument(broker, entryURI);
          if (entryDoc != null)
            {throw new PermissionDeniedException("Entry with " + id + " already exists.");}

          final IndexInfo info = collection.validateXMLResource(transaction, broker, entryURI, mediaEntry);
          // TODO : We should probably unlock the collection here
          collection.store(transaction, broker, info, mediaEntry, false);
          // Update the updated element
          DOMDB.replaceTextElement(
              transaction, feedRoot, Atom.NAMESPACE_STRING, "updated",
View Full Code Here

            request.getContentLength());

        if (mime.isXMLType()) {
          InputStream is = new FileInputStream(tempFile);
         
          final IndexInfo info = collection.validateXMLResource(
              transaction, broker, docUri,
              new InputSource(new InputStreamReader(is, charset)));
         
          is.close();
         
          info.getDocument().getMetadata().setMimeType(contentType);
         
          is = new FileInputStream(tempFile);
         
          collection.store(transaction, broker, info,
            new InputSource(new InputStreamReader(is, charset)), false);
View Full Code Here

TOP

Related Classes of org.exist.collections.IndexInfo

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.