Package org.exist.util

Examples of org.exist.util.MimeType


                name = file.getName();
                path = file.getParent();
               
                Collection target = (path==null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
               
                  MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                 
                try{
                  NodeValue content = ModuleUtils.streamToXML(context, new ByteArrayInputStream(baos.toByteArray()));
                  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(baos.toByteArray());
                    }
               
                if (resource != null){
                  if (mime != null){
                    ((EXistResource)resource).setMimeType(mime.getName());
                  }
                  target.storeResource(resource);
                }
                 
                }
View Full Code Here


    }
   
    private void storeBinary(final String fileName) throws XMLDBException {
        final File file = new File(fileName);
        if (file.canRead()) {
            final MimeType mime = MimeTable.getInstance().getContentTypeFor(file.getName());
            final BinaryResource resource = (BinaryResource) current.createResource(file.getName(), "BinaryResource");
            resource.setContent(file);
            ((EXistResource)resource).setMimeType(mime == null ? "application/octet-stream" : mime.getName());
            current.storeResource(resource);
        }
    }
View Full Code Here

        Collection c;
        Resource document;
        CollectionManagementServiceImpl mgtService;
        //The XmldbURIs here aren't really used...
        XmldbURI next;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            next = base.append(files[i].getName());
            try {
                if (files[i].isDirectory()) {
                    messageln("entering directory " + files[i].getAbsolutePath());
                    c = collection.getChildCollection(files[i].getName());
                    if (c == null) {
                        mgtService = (CollectionManagementServiceImpl) collection.getService("CollectionManagementService", "1.0");
                        c = mgtService.createCollection(URIUtils.encodeXmldbUriFor(files[i].getName()));
                    }
                   
                    if (c instanceof Observable && verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) c).addObserver(observer);
                    }
                    findRecursive(c, files[i], next);
                } else {
                    final long start1 = System.currentTimeMillis();
                    mimeType = MimeTable.getInstance().getContentTypeFor(files[i].getName());
                    if(mimeType == null) {
                        messageln("File " + files[i].getName() + " has an unknown suffix. Cannot determine file type.");
      mimeType = MimeType.BINARY_TYPE;
                    }
                    message("storing document " + files[i].getName() + " (" + i + " of " + files.length + ") " + "...");
                    document = collection.createResource(URIUtils.urlEncodeUtf8(files[i].getName()), mimeType.getXMLDBType());
                    document.setContent(files[i]);
                    ((EXistResource)document).setMimeType(mimeType.getName());
                    collection.storeResource(document);
                    ++filesCount;
                    messageln(" " + files[i].length() + " bytes in " + (System.currentTimeMillis() - start1) + "ms.");
                }
            } catch (final URISyntaxException e) {
View Full Code Here

            files = DirectoryScanner.scanDir(fileName);
        }
       
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                continue;
            }
            final long start = System.currentTimeMillis();
            mimeType = MimeTable.getInstance().getContentTypeFor(files[i].getName());
            if(mimeType == null) {
                mimeType = MimeType.BINARY_TYPE;
            }
            document = current.createResource(files[i].getName(), mimeType.getXMLDBType());
            message("storing document " + files[i].getName() + " (" + (i + 1) + " of " + files.length + ") ...");
            document.setContent(files[i]);
            ((EXistResource)document).setMimeType(mimeType.getName());
            current.storeResource(document);
            messageln("done.");
            messageln("parsing " + files[i].length() + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
            bytes += files[i].length();
        }
View Full Code Here

        Collection c;
        Resource document;
        CollectionManagementServiceImpl mgtService;
        //The XmldbURIs here aren't really used...
        XmldbURI next;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            next = base.append(files[i].getName());
            try {
                if (files[i].isDirectory()) {
                    messageln("entering directory " + files[i].getAbsolutePath());
                    c = collection.getChildCollection(files[i].getName());
                    if (c == null) {
                        mgtService = (CollectionManagementServiceImpl) collection.getService("CollectionManagementService", "1.0");
                        c = mgtService.createCollection(URIUtils.encodeXmldbUriFor(files[i].getName()));
                    }
                    if (c instanceof Observable && verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) c).addObserver(observer);
                    }
                    findGZipRecursive(c, files[i], next);
                } else {
                    final long start1 = System.currentTimeMillis();
                    final String compressedName = files[i].getName();
                    String localName = compressedName;
                    final String[] cSuffix = {".gz",".Z"};
                    boolean isCompressed = false;
                    for(int isuf = 0; isuf < cSuffix.length; isuf++) {
                      final String suf = cSuffix[isuf];
                      if(localName.endsWith(suf)) {
                        // Removing compressed prefix to validate
                        localName=compressedName.substring(0, localName.length()-suf.length());
                        isCompressed=true;
                        break;
                      }
                    }
                    mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                    if(mimeType == null) {
                        messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
                      mimeType = MimeType.BINARY_TYPE;
        }
                    message("storing document " + compressedName + " (" + i + " of " + files.length + ") " + "...");
                    document = collection.createResource(URIUtils.urlEncodeUtf8(compressedName), mimeType.getXMLDBType());
                    document.setContent(isCompressed?new GZIPInputSource(files[i]):files[i]);
                    ((EXistResource)document).setMimeType(mimeType.getName());
                    collection.storeResource(document);
                    ++filesCount;
                    messageln(" " + files[i].length() + (isCompressed?" compressed":"") + " bytes in "
                          + (System.currentTimeMillis() - start1) + "ms.");
                }
View Full Code Here

            files = DirectoryScanner.scanDir(fileName);
        }
       
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                continue;
            }
            final long start = System.currentTimeMillis();
            final String compressedName = files[i].getName();
            String localName = compressedName;
            final String[] cSuffix = {".gz",".Z"};
            boolean isCompressed = false;
            for(int isuf = 0; isuf < cSuffix.length; isuf++) {
              final String suf = cSuffix[isuf];
              if(localName.endsWith(suf)) {
                    // Removing compressed prefix to validate
                    localName = compressedName.substring(0, localName.length()-suf.length());
                    isCompressed = true;
                    break;
              }
            }
            mimeType = MimeTable.getInstance().getContentTypeFor(localName);
            if(mimeType == null) {
                mimeType = MimeType.BINARY_TYPE;
            }
            document = current.createResource(compressedName,mimeType.getXMLDBType());
            message("storing document " + compressedName + " (" + (i + 1)
            + " of " + files.length + ") ...");
            document.setContent(isCompressed?new GZIPInputSource(files[i]):files[i]);
            ((EXistResource)document).setMimeType(mimeType.getName());
            current.storeResource(document);
            messageln("done.");
            messageln("parsing " + files[i].length() + (isCompressed?" compressed":"") + " bytes took "
                    + (System.currentTimeMillis() - start) + "ms." + EOL);
            bytes += files[i].length();
View Full Code Here

                ((Observable) current).addObserver(observer);
            }

            final long start0 = System.currentTimeMillis();
            long bytes = 0;
            MimeType mimeType;
            final Enumeration<? extends ZipEntry> e = zfile.entries();
            int number = 0;
       
            Collection base = current;
            String baseStr="";
            while(e.hasMoreElements()) {
                number++;
                final ZipEntry ze=e.nextElement();
                final String zeName = ze.getName().replace('\\','/');
                final String[] pathSteps = zeName.split("/");
                String currStr = pathSteps[0];
                for(int i=1;i<pathSteps.length-1;i++) {
                    currStr += "/"+pathSteps[i];
                }
                if(!baseStr.equals(currStr)) {
                    base=current;
                    for(int i=0;i<pathSteps.length-1;i++) {
                        Collection c = base.getChildCollection(pathSteps[i]);
                        if (c == null) {
                            final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) base.getService("CollectionManagementService","1.0");
                            c = mgtService.createCollection(URIUtils.encodeXmldbUriFor(pathSteps[i]));
                        }
                        base=c;
                    }
                    if (base instanceof Observable && verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) base).addObserver(observer);
                    }
                    baseStr=currStr;
                    messageln("entering directory " + baseStr);
                }
                if (!ze.isDirectory()) {
                    final String localName=pathSteps[pathSteps.length-1];
                    final long start = System.currentTimeMillis();
                    mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                    if(mimeType == null) {
                        mimeType = MimeType.BINARY_TYPE;
                    }
                    document = base.createResource(localName,mimeType.getXMLDBType());
                    message("storing Zip-entry document " + localName + " (" + (number)
                                    + " of " + zfile.size() + ") ...");
                    document.setContent(new ZipEntryInputSource(zfile,ze));
                    ((EXistResource)document).setMimeType(mimeType.getName());
                    base.storeResource(document);
                    messageln("done.");
                    messageln("parsing " + ze.getSize() + " bytes took "
                                    + (System.currentTimeMillis() - start) + "ms." + EOL);
                    bytes += ze.getSize();
View Full Code Here

        if (file.isFile()) {
            upload.reset();
            upload.setCurrent(file.getName());
            upload.setCurrentSize(file.length());
           
            MimeType mimeType = MimeTable.getInstance().getContentTypeFor(file.getName());
            // unknown mime type, here prefered is to do nothing
            if(mimeType == null) {
                upload.showMessage(file.getAbsolutePath() +
                    " - unknown suffix. No matching mime-type found in : " +
                    MimeTable.getInstance().getSrc());
               
                // if some one prefers to store it as binary by default, but dangerous
                mimeType = MimeType.BINARY_TYPE;
            }
           
            try {
                final Resource res = collection.createResource(filenameUri.toString(), mimeType.getXMLDBType());
                ((EXistResource) res).setMimeType(mimeType.getName());
                res.setContent(file);
                collection.storeResource(res);
                ++filesCount;
                this.totalLength += file.length();
                upload.setStoredSize(this.totalLength);
View Full Code Here

        final TransactionManager mgr = broker.getBrokerPool().getTransactionManager();
        for (final File file : files) {
            if (inRootDir && "repo.xml".equals(file.getName()))
                {continue;}
            if (!file.isDirectory()) {
                MimeType mime = mimeTab.getContentTypeFor(file.getName());
                if (mime == null)
                    {mime = MimeType.BINARY_TYPE;}
                final XmldbURI name = XmldbURI.create(file.getName());

                final Txn txn = mgr.beginTransaction();
                try {
                    if (mime.isXMLType()) {
                        final InputSource is = new InputSource(file.toURI().toASCIIString());
                        final IndexInfo info = targetCollection.validateXMLResource(txn, broker, name, is);
                        info.getDocument().getMetadata().setMimeType(mime.getName());
                        final Permission permission = info.getDocument().getPermissions();
                        setPermissions(false, mime, permission);

                        targetCollection.store(txn, broker, info, is, false);
                    } else {
                        final long size = file.length();
                        final FileInputStream is = new FileInputStream(file);
                        final BinaryDocument doc =
                                targetCollection.addBinaryResource(txn, broker, name, is, mime.getName(), size);
                        is.close();

                        final Permission permission = doc.getPermissions();
                        setPermissions(false, mime, permission);
                        doc.getMetadata().setMimeType(mime.getName());
                        broker.storeXMLResource(txn, doc);
                    }
                    mgr.commit(txn);
                } catch (final Exception e) {
                    mgr.abort(txn);
View Full Code Here

       
        final MimeTable mimeTable = MimeTable.getInstance();
       
        final boolean isXML = mimeTable.isXMLContent(newName.toString());
       
        final MimeType mimeType = mimeTable.getContentTypeFor(newName);
       
      if ( mimeType != null && !mimeType.getName().equals( doc.getMetadata().getMimeType()) ) {
            doc.getMetadata().setMimeType(mimeType.getName());
            broker.storeXMLResource(txn, doc);

            doc = source.getDocument(broker, uri.lastSegment());
        }
       
      if (isXML) {
            if (doc.getResourceType() == DocumentImpl.XML_FILE) {
              //XML to XML
                //move to same type as it
                broker.moveResource(txn, doc, destination, newName);

            } else {
                //convert BINARY to XML
               
                final File file = broker.getBinaryFile((BinaryDocument) doc);

                FileInputSource is = new FileInputSource(file);
               
                final IndexInfo info = destination.validateXMLResource(txn, broker, newName, is);
                info.getDocument().getMetadata().setMimeType(mimeType.getName());

                is = new FileInputSource(file);
                destination.store(txn, broker, info, is, false);
               
                source.removeBinaryResource(txn, broker, doc);
            }
        } else {
            if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
              //BINARY to BINARY

              //move to same type as it
                broker.moveResource(txn, doc, destination, newName);
               
            } else {
                //convert XML to BINARY
                // xml file
                final Serializer serializer = broker.getSerializer();
                serializer.setUser(broker.getSubject());
                serializer.setProperties(XML_OUTPUT_PROPERTIES);
               
                File tempFile = null;
                FileInputStream is = null;
        try {
                    tempFile = File.createTempFile("eXist-resource-", ".xml");
                    tempFile.deleteOnExit();
                   
                    final Writer w = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8");

                    serializer.serialize(doc, w);
                    w.flush();
                    w.close();
                   
                    is = new FileInputStream(tempFile);
                   
                    final DocumentMetadata meta = doc.getMetadata();
                   
                    final Date created = new Date(meta.getCreated());
                    final Date lastModified = new Date(meta.getLastModified());
   
                    BinaryDocument binary = destination.validateBinaryResource(txn, broker, newName, is, mimeType.getName(), -1, created, lastModified);
                   
                    binary = destination.addBinaryResource(txn, broker, binary, is, mimeType.getName(), -1, created, lastModified);
                   
                    source.removeXMLResource(txn, broker, doc.getFileURI());
                   
                } finally {
                  if (is != null)
View Full Code Here

TOP

Related Classes of org.exist.util.MimeType

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.