Examples of VirtualTempFile


Examples of org.exist.util.VirtualTempFile

      params.add(outputProperties);
   
      try {


        VirtualTempFile vtmpfile=null;
    try {
      vtmpfile = new VirtualTempFile();
      vtmpfile.setTempPrefix("eXistRRS");
      vtmpfile.setTempPostfix(".xml");
     
      Map<?,?> table = (Map<?,?>) collection.getClient().execute("retrieveAllFirstChunk", params);
     
      long offset = ((Integer)table.get("offset")).intValue();
      byte[] data = (byte[])table.get("data");
      boolean isCompressed="yes".equals(outputProperties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
      // One for the local cached file
      Inflater dec = null;
      byte[] decResult = null;
      int decLength = 0;
      if(isCompressed) {
        dec = new Inflater();
        decResult = new byte[65536];
        dec.setInput(data);
        do {
          decLength = dec.inflate(decResult);
          vtmpfile.write(decResult,0,decLength);
        } while(decLength==decResult.length || !dec.needsInput());
      } else {
        vtmpfile.write(data);
      }
      while(offset > 0) {
        params.clear();
        params.add(table.get("handle"));
        params.add(Long.toString(offset));
        table = (Map<?,?>) collection.getClient().execute("getNextExtendedChunk", params);
        offset = Long.valueOf((String)table.get("offset")).longValue();
        data = (byte[])table.get("data");
        // One for the local cached file
        if(isCompressed) {
          dec.setInput(data);
          do {
            decLength = dec.inflate(decResult);
            vtmpfile.write(decResult,0,decLength);
          } while(decLength==decResult.length || !dec.needsInput());
        } else {
          vtmpfile.write(data);
        }
      }
      if(dec!=null)
        {dec.end();}
     
      final RemoteXMLResource res = new RemoteXMLResource( collection, handle, 0, XmldbURI.EMPTY_URI, null );
      res.setContent( vtmpfile );
      res.setProperties(outputProperties);
      return res;
    } catch (final XmlRpcException xre) {
      final byte[] data = (byte[]) collection.getClient().execute("retrieveAll", params);
      String content;
      try {
        content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));
      } catch (final UnsupportedEncodingException ue) {
        LOG.warn(ue);
        content = new String(data);
      }
      final RemoteXMLResource res = new RemoteXMLResource( collection, handle, 0,
                XmldbURI.EMPTY_URI, null );
          res.setContent( content );
          res.setProperties(outputProperties);
          return res;
    } catch (final IOException ioe) {
      throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
    } catch (final DataFormatException dfe) {
      throw new XMLDBException(ErrorCodes.VENDOR_ERROR, dfe.getMessage(), dfe);
    } finally {
      if(vtmpfile!=null) {
        try {
          vtmpfile.close();
        } catch(final IOException ioe) {
          //IgnoreIT(R)
        }
      }
    }
View Full Code Here

Examples of org.exist.util.VirtualTempFile

     
      long clen = request.getContentLength();
      final String lenstr = request.getHeader("Content-Length");
      if(lenstr!=null)
        {clen = Long.parseLong(lenstr);}
      contentBody = new VirtualTempFile();
      contentBody.setTempPrefix("existWRP");
      contentBody.setTempPostfix(".tmp");
      contentBody.write(is,clen);
      contentBody.close();
    }
View Full Code Here

Examples of org.exist.util.VirtualTempFile

      }
    }

    public void setContentAsDOM(Node root) throws XMLDBException {
      try {
        final VirtualTempFile vtmpfile = new VirtualTempFile();
        vtmpfile.setTempPrefix("eXistRXR");
        vtmpfile.setTempPostfix(".xml");
       
        final OutputStreamWriter osw=new OutputStreamWriter(vtmpfile,"UTF-8");
      final DOMSerializer xmlout = new DOMSerializer(osw, getProperties());
      try {
          switch (root.getNodeType()) {
            case Node.ELEMENT_NODE :
            xmlout.serialize((Element) root);
            break;
            case Node.DOCUMENT_FRAGMENT_NODE :
            xmlout.serialize((DocumentFragment) root);
            break;
            case Node.DOCUMENT_NODE :
            xmlout.serialize((Document) root);
            break;
            default :
              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "invalid node type");
        }
      } catch (final TransformerException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
      } finally {
        try {
          osw.close();
        } catch(final IOException ioe) {
          // IgnoreIT(R)
        }
        try {
          vtmpfile.close();
        } catch(final IOException ioe) {
          // IgnoreIT(R)
        }
      }
      setContent(vtmpfile);
View Full Code Here

Examples of org.exist.util.VirtualTempFile

      super();
    }

    public void startDocument() throws SAXException {
      try {
        vtmpfile = new VirtualTempFile();
        vtmpfile.setTempPrefix("eXistRXR");
        vtmpfile.setTempPostfix(".xml");
         
          writer=new OutputStreamWriter(vtmpfile,"UTF-8");
        setOutput(writer, emptyProperties);
View Full Code Here

Examples of org.exist.util.VirtualTempFile

                    try {
                        if(LOG.isDebugEnabled()) {
                            LOG.debug(String.format("Serializing XML to virtual file (%s)", resourceXmldbUri));
                        }

                        vtf = new VirtualTempFile();
                        existDocument.stream(vtf);
                        vtf.close();

                    } catch (Exception ex) {
                        LOG.error(ex);
View Full Code Here

Examples of org.exist.util.VirtualTempFile

        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);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Document created sucessfully");
            }


        } catch (EXistException | SAXException e) {
            LOG.error(e);
            txnManager.abort(txn);
            throw new IOException(e);

        } catch (LockException e) {
            LOG.error(e);
            txnManager.abort(txn);
            throw new PermissionDeniedException(xmldbUri + "");

        } catch (IOException | PermissionDeniedException e) {
            LOG.error(e);
            txnManager.abort(txn);
            throw e;

        } finally {

            if (vtf != null) {
                vtf.delete();
            }

            // TODO: check if can be done earlier
            if (collection != null) {
                collection.release(Lock.WRITE_LOCK);
View Full Code Here

Examples of org.exist.util.VirtualTempFile

          // IgnoreIT(R)
        }
        setExtendendContentLength(vfile.length());
        wasSet=true;
    } else if(value instanceof File) {
      vfile = new VirtualTempFile((File) value);
      setExtendendContentLength(vfile.length());
      wasSet=true;
    } else if (value instanceof InputSource) {
      inputSource = (InputSource) value;
        wasSet=true;
    } else if(value instanceof byte[]) {
      vfile = new VirtualTempFile((byte[])value);
      setExtendendContentLength(vfile.length());
      wasSet=true;
    } else if(value instanceof String) {
            vfile = new VirtualTempFile(((String)value).getBytes(UTF_8));
            setExtendendContentLength(vfile.length());
            wasSet=true;
    }
   
    return wasSet;
View Full Code Here

Examples of org.exist.util.VirtualTempFile

            params.add(path.toString());
        }
        params.add(properties);
       
        try {
            final VirtualTempFile vtmpfile = new VirtualTempFile();
            vtmpfile.setTempPrefix("eXistARR");
            vtmpfile.setTempPostfix("XMLResource".equals(getResourceType())?".xml":".bin");

            Map<?,?> table = (Map<?,?>) parent.getClient().execute(command, params);
           
            final String method;
            final boolean useLongOffset;
            if(table.containsKey("supports-long-offset") && (Boolean)(table.get("supports-long-offset"))) {
                useLongOffset = true;
                method = "getNextExtendedChunk";
            } else {
                useLongOffset = false;
                method = "getNextChunk";
            }
           
            long offset = ((Integer)table.get("offset")).intValue();
            byte[] data = (byte[])table.get("data");
            final boolean isCompressed = "yes".equals(properties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
               
            // One for the local cached file
            Inflater dec = null;
            byte[] decResult = null;
            int decLength;
            if(isCompressed) {
                dec = new Inflater();
                decResult = new byte[65536];
                dec.setInput(data);
                do {
                    decLength = dec.inflate(decResult);
                    vtmpfile.write(decResult,0,decLength);
                    // And other for the stream where we want to save it!
                    if(os != null) {
                        os.write(decResult, 0, decLength);
                    }
                } while(decLength == decResult.length || !dec.needsInput());
               
            } else {
                vtmpfile.write(data);
                // And other for the stream where we want to save it!
                if(os != null) {
                    os.write(data);
                }
            }
               
            while(offset > 0) {
                params.clear();
                params.add(table.get("handle"));
                params.add(useLongOffset?Long.toString(offset):Integer.valueOf((int)offset));
                table = (Map<?,?>) parent.getClient().execute(method, params);
                offset = useLongOffset?Long.valueOf((String)table.get("offset")).longValue():((Integer)table.get("offset")).longValue();
                data = (byte[])table.get("data");

                // One for the local cached file
                if(isCompressed) {
                    dec.setInput(data);
                    do {
                        decLength = dec.inflate(decResult);
                        vtmpfile.write(decResult,0,decLength);
                        // And other for the stream where we want to save it!
                        if(os != null) {
                            os.write(decResult, 0, decLength);
                        }
                    } while(decLength == decResult.length || !dec.needsInput());
                } else {
                    vtmpfile.write(data);
                    // And other for the stream where we want to save it!
                     if(os != null) {
                        os.write(data);
                    }
                }
View Full Code Here

Examples of org.exist.util.VirtualTempFile

            return;
        }

        final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
        Txn transaction = null;
        VirtualTempFile vtempFile = null;
        try {
            // fourth, process the request
            InputStream is = request.getInputStream();
            long len = request.getContentLength();
            final String lenstr = request.getHeader("Content-Length");
            if (lenstr != null) {
                len = Long.parseLong(lenstr);
            }
            // put may send a lot of data, so save it
            // to a temporary file first.

            vtempFile = new VirtualTempFile();
            vtempFile.setTempPrefix("existSRV");
            vtempFile.setTempPostfix(".tmp");
            vtempFile.write(is, len);
            vtempFile.close();

            final XmldbURI docUri = path.lastSegment();
            final XmldbURI collUri = path.removeLastSegment();

            if (docUri == null || collUri == null) {
                //transact.abort(transaction);
                throw new BadRequestException("Bad path: " + path);
            }
            // TODO : use getOrCreateCollection() right now ?
            Collection collection = broker.getCollection(collUri);
            if (collection == null) {
                LOG.debug("creating collection " + collUri);
                transaction = transact.beginTransaction();
                collection = broker.getOrCreateCollection(transaction, collUri);
                broker.saveCollection(transaction, collection);
            }
            MimeType mime;
            String contentType = request.getContentType();
            String charset = null;
            if (contentType != null) {
                final int semicolon = contentType.indexOf(';');
                if (semicolon > 0) {
                    contentType = contentType.substring(0, semicolon).trim();
                    final int equals = contentType.indexOf('=', semicolon);
                    if (equals > 0) {
                        final String param = contentType.substring(semicolon + 1,
                                equals).trim();
                        if (param.compareToIgnoreCase("charset=") == 0) {
                            charset = param.substring(equals + 1).trim();
                        }
                    }
                }
                mime = MimeTable.getInstance().getContentType(contentType);
            } else {
                mime = MimeTable.getInstance().getContentTypeFor(docUri);
                if (mime != null) {
                    contentType = mime.getName();
                }
            }
            if (mime == null) {
                mime = MimeType.BINARY_TYPE;
                contentType = mime.getName();
            }

            if (transaction == null) {
                transaction = transact.beginTransaction();
            }

            if (mime.isXMLType()) {
                final InputSource vtfis = new VirtualTempFileInputSource(vtempFile, charset);

                final IndexInfo info = collection.validateXMLResource(transaction, broker, docUri, vtfis);
                info.getDocument().getMetadata().setMimeType(contentType);
                collection.store(transaction, broker, info, vtfis, false);
                response.setStatus(HttpServletResponse.SC_CREATED);
            } else {

                is = vtempFile.getByteStream();
                try {
                    collection.addBinaryResource(transaction, broker, docUri, is,
                            contentType, vtempFile.length());
                } finally {
                    is.close();
                }
                response.setStatus(HttpServletResponse.SC_CREATED);
            }

            transact.commit(transaction);
        } catch (final SAXParseException e) {
            transact.abort(transaction);
            throw new BadRequestException("Parsing exception at "
                    + e.getLineNumber() + "/" + e.getColumnNumber() + ": "
                    + e.toString());
        } catch (final TriggerException e) {
            transact.abort(transaction);
            throw new PermissionDeniedException(e.getMessage());
        } catch (SAXException e) {
            transact.abort(transaction);
            Exception o = e.getException();
            if (o == null) {
                o = e;
            }
            throw new BadRequestException("Parsing exception: " + o.getMessage());
        } catch (final EXistException e) {
            transact.abort(transaction);
            throw new BadRequestException("Internal error: " + e.getMessage());
        } catch (final LockException e) {
            transact.abort(transaction);
            throw new PermissionDeniedException(e.getMessage());
        } finally {
            transact.close(transaction);
            if (vtempFile != null) {
                vtempFile.delete();
            }
        }
        return;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.