Package org.exist.util

Examples of org.exist.util.MimeType


         
          return false;
        }
      }

      MimeType mimeType = MimeTable.getInstance().getContentTypeFor(fileName);
 
      if (mimeType == null) {
        mimeType = MimeType.BINARY_TYPE;
      }
     
      tm = db.getTransactionManager();
      final Txn transaction = tm.beginTransaction();
 
      InputStream is = null;
      try {
        if (mimeType.isXMLType()) {
          // store as xml resource
          final String str = "<empty/>";
          final IndexInfo info = collection.validateXMLResource(transaction, broker, fileName, str);
          info.getDocument().getMetadata().setMimeType(mimeType.getName());
          info.getDocument().getPermissions().setMode(DEFAULT_RESOURCE_PERM);
          collection.store(transaction, broker, info, str, false);
 
        } else {
          // store as binary resource
          is = new ByteArrayInputStream("".getBytes(UTF_8));
         
          final BinaryDocument blob = new BinaryDocument(db, collection, fileName);
 
          blob.getPermissions().setMode(DEFAULT_RESOURCE_PERM);

          collection.addBinaryResource(transaction, broker, blob, is,
              mimeType.getName(), 0L , new Date(), new Date());
 
        }
        tm.commit(transaction);
      } catch (final Exception e) {
        tm.abort(transaction);
View Full Code Here


            return;
       
        TransactionManager txManager = db.getTransactionManager();
        Txn txn = null;
        try {
            MimeType mime = getMimeTable().getContentTypeFor( file.getName() );
            if (mime != null && mime.isXMLType()) {
                txn = txManager.beginTransaction();

                IndexInfo info = col.validateXMLResource(txn, broker,
                        XmldbURI.create(file.getName()),
                        new InputSource(new FileInputStream(file))
View Full Code Here

        return;
     
      if (!(file.exists() && file.canRead()))
        return;
     
        MimeType mime = getMimeTable().getContentTypeFor( file.getName() );
        if (mime != null && mime.isXMLType()) {
          IndexInfo info = col.validateXMLResource(null, broker,
                XmldbURI.create(file.getName()),
                new InputSource(new FileInputStream(file))
              );
            //info.getDocument().getMetadata().setMimeType();
View Full Code Here


                // determine the type of the response document
                final Header responseContentType = method.getResponseHeader("Content-Type");

                final MimeType responseMimeType = getResponseMimeType(responseContentType);
                if(responseContentType != null) {
                    builder.addAttribute(new QName("mimetype", null, null), responseContentType.getValue());
                }

                //try and parse the response as XML
                try {
                    //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread
                    final InputStream shieldedInputStream = new CloseShieldInputStream(cfis);
                    responseNode = (NodeImpl)ModuleUtils.streamToXML(context, shieldedInputStream);
                    builder.addAttribute(new QName("type", null, null ), "xml");
                    responseNode.copyTo(null, new DocumentBuilderReceiver(builder));
                } catch(final SAXException se) {
                    // could not parse to xml
                    // not an error in itself, it will be treated either as HTML,
                    // text or binary here below
                    final String msg = "Request for URI '"
                        + method.getURI().toString()
                        + "' Could not parse http response content as XML (will try html, text or fallback to binary): "
                        + se.getMessage();
                    if(logger.isDebugEnabled()) {
                        logger.debug(msg, se);
                    } else {
                        logger.info(msg);
                    }
                } catch(final IOException ioe) {
                    final String msg = "Request for URI '" + method.getURI().toString() + "' Could not read http response content: " + ioe.getMessage();
                    logger.error(msg, ioe);
                    throw new XPathException(msg, ioe);
                }

                if(responseNode == null) {
                    //response is NOT parseable as XML

                    //is it a html document?
                    if(responseMimeType.getName().equals(MimeType.HTML_TYPE.getName())) {

                        //html document
                        try {

                            //reset the stream to the start, as we need to reuse since attempting to parse to XML
                            cfis.reset();

                            //parse html to xml(html)
                           
                            //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread
                            final InputStream shieldedInputStream = new CloseShieldInputStream(cfis);
                           
                            responseNode = (NodeImpl)ModuleUtils.htmlToXHtml(context, method.getURI().toString(), new InputSource(shieldedInputStream), parserFeatures, parserProperties).getDocumentElement();
                            builder.addAttribute(new QName("type", null, null), "xhtml" );
                            responseNode.copyTo(null, new DocumentBuilderReceiver(builder));
                        } catch(final URIException ue) {
                            throw new XPathException(this, ue.getMessage(), ue);
                        } catch(final SAXException se) {
                            //could not parse to xml(html)
                            logger.debug("Could not parse http response content from HTML to XML: " + se.getMessage(), se);
                        }
                    }
                }

                if(responseNode == null) {

                    //reset the stream to the start, as we need to reuse since attempting to parse to HTML->XML
                    cfis.reset();

                    if(responseMimeType.getName().startsWith("text/")) {

                        // Assume it's a text body and URL encode it
                        builder.addAttribute(new QName("type", null, null), "text");
                        builder.addAttribute(new QName("encoding", null, null), "URLEncoded");
                       
View Full Code Here

                    //strip off any charset encoding info
                    if(contentType.indexOf(";") > -1) {
                        contentType = contentType.substring(0, contentType.indexOf(";"));
                    }

                    MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
                    if(mimeType != null && !mimeType.isXMLType()) {

                        //binary data
                        try {
                           
                            final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
View Full Code Here

     * @param   responseHeaderContentType  The HTTP Response Header containing the Content-Type of the Response.
     *
     * @return  The corresponding eXist MimeType
     */
    protected MimeType getResponseMimeType(final Header responseHeaderContentType) {
        MimeType returnMimeType = MimeType.BINARY_TYPE;

        if(responseHeaderContentType != null) {

            if(responseHeaderContentType.getName().equals("Content-Type")) {

                final String responseContentType = responseHeaderContentType.getValue();
                int contentTypeEnd = responseContentType.indexOf(";");
                if(contentTypeEnd == -1) {
                    contentTypeEnd = responseContentType.length();
                }

                final String responseMimeType = responseContentType.substring(0, contentTypeEnd);
                final MimeTable mimeTable = MimeTable.getInstance();
                final MimeType mimeType = mimeTable.getContentType(responseMimeType);

                if(mimeType != null) {
                    returnMimeType = mimeType;
                }
            }
View Full Code Here

        File existDir = existHome==null ? new File(".") : new File(existHome);
        File samples = new File(existDir,"samples/shakespeare");
        File[] files = samples.listFiles();
        MimeTable mimeTab = MimeTable.getInstance();
        for (int j = 0; j < files.length; j++) {
            MimeType mime = mimeTab.getContentTypeFor(files[j].getName());
            if(mime != null && mime.isXMLType()) {
                Resource resource = collection.createResource(files[j].getName(), "XMLResource");
                resource.setContent(files[j]);
                collection.storeResource(resource);
            }
        }
View Full Code Here

            File file = new File(directory);
            File[] files = file.listFiles();
            MimeTable mimeTab = MimeTable.getInstance();
            for (int j = 0; j < files.length; j++) {
                MimeType mime = mimeTab.getContentTypeFor(files[j].getName());
                if(mime != null && mime.isXMLType()) {
                    System.out.println("Storing document " + files[j].getName());
                    InputSource is = new InputSource(files[j].getAbsolutePath());
                    IndexInfo info =
                            root.validateXMLResource(transaction, broker, XmldbURI.create(files[j].getName()), is);
                    assertNotNull(info);
View Full Code Here

            LOG.debug(String.format("Create '%s' in '%s'", newName, xmldbUri));

        XmldbURI newNameUri = XmldbURI.create(newName);

        // Get mime, or NULL when not available
        MimeType mime = MimeTable.getInstance().getContentTypeFor(newName);
        if (mime == null) {
            mime = MimeType.BINARY_TYPE;
        }

        // References to the database
        DBBroker broker = null;
        Collection collection = null;

        // create temp file and store. Existdb needs to read twice from a stream.
        BufferedInputStream bis = new BufferedInputStream(is);

        VirtualTempFile vtf = new VirtualTempFile();

        BufferedOutputStream bos = new BufferedOutputStream(vtf);

        // Perform actual copy
        IOUtils.copy(bis, bos);
        bis.close();
        bos.close();
        vtf.close();

        // To support LockNullResource, a 0-byte XML document can received. Since 0-byte
        // XML documents are not supported a small file will be created.
        if (mime.isXMLType() && vtf.length() == 0L) {

            if(LOG.isDebugEnabled())
                LOG.debug(String.format("Creating dummy XML file for null resource lock '%s'", newNameUri));

            vtf = new VirtualTempFile();
            IOUtils.write("<null_resource/>", vtf);
            vtf.close();
        }

        // Start transaction
        TransactionManager txnManager = brokerPool.getTransactionManager();
        Txn txn = txnManager.beginTransaction();

        try {
            broker = brokerPool.get(subject);

            // Check if collection exists. not likely to happen since availability is checked
            // by ResourceFactory
            collection = broker.openCollection(xmldbUri, Lock.WRITE_LOCK);
            if (collection == null) {
                LOG.debug(String.format("Collection %s does not exist", xmldbUri));
                txnManager.abort(txn);
                throw new CollectionDoesNotExistException(xmldbUri + "");
            }


            if (mime.isXMLType()) {

                if(LOG.isDebugEnabled())
                    LOG.debug(String.format("Inserting XML document '%s'", mime.getName()));

                // Stream into database
                VirtualTempFileInputSource vtfis = new VirtualTempFileInputSource(vtf);
                IndexInfo info = collection.validateXMLResource(txn, broker, newNameUri, vtfis);
                DocumentImpl doc = info.getDocument();
                doc.getMetadata().setMimeType(mime.getName());
                collection.store(txn, broker, info, vtfis, false);

            } else {

                if(LOG.isDebugEnabled())
                    LOG.debug(String.format("Inserting BINARY document '%s'", mime.getName()));

                // Stream into database
                InputStream fis = vtf.getByteStream();
                bis = new BufferedInputStream(fis);
                collection.addBinaryResource(txn, broker, newNameUri, bis, mime.getName(), length);
                bis.close();
            }

            // Commit change
            txnManager.commit(txn);
View Full Code Here

        currentFile.setLastModified(SVNDate.parseDate(date).getTime());
      }

      XmldbURI fileName = XmldbURI.create(path).lastSegment();

      MimeType mimeType = MimeTable.getInstance().getContentTypeFor(
          fileName);
      // unknown mime type, here preferred is to do nothing
      if (mimeType == null) {
        // TODO: report error? path +
        // " - 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;
      }

      InputStream is = null;
      try {
        if (mimeType.isXMLType()) {
          // store as xml resource
          is = new FileInputStream(currentFile);
          IndexInfo info = currentDirectory.validateXMLResource(
              transaction, broker, fileName, new InputSource(
                  new InputStreamReader(is)));
//                  new InputStreamReader(is, charset)));
          is.close();
          info.getDocument().getMetadata().setMimeType(
              mimeType.getName());
          is = new FileInputStream(currentFile);
          currentDirectory
              .store(transaction, broker, info, new InputSource(
                  new InputStreamReader(is)), false);
//                  new InputStreamReader(is, charset)), false);
          is.close();

        } else {
          // store as binary resource
          is = new FileInputStream(currentFile);

          BinaryDocument doc = currentDirectory.addBinaryResource(
              transaction, broker, fileName, is,
              mimeType.getName(), currentFile.length(), new Date(), new Date());

        }
      } catch (FileNotFoundException e) {
        SVNErrorMessage err = SVNErrorMessage.create(
            SVNErrorCode.IO_ERROR, "error: ."); // TODO: error
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.