/** @see org.jlibrary.core.repository.RepositoryService#exportRepository(Ticket) */
  public byte[] exportRepository(Ticket ticket)
      throws RepositoryNotFoundException, RepositoryException, SecurityException {

    try {
      javax.jcr.Session session = SessionManager.getInstance().getSession(ticket);
      if (session == null) {
        throw new RepositoryException("Session has expired. Please log in again.");
      }
      javax.jcr.Node root = JCRUtils.getRootNode(session);
      if (!JCRSecurityService.canRead(root, ticket.getUser().getId())) {
        throw new SecurityException(SecurityException.NOT_ENOUGH_PERMISSIONS);
      }

      ByteArrayOutputStream baos1 = null;
      byte[] rootContent;
      try {
        baos1 = new ByteArrayOutputStream();
        session.exportSystemView(JCRUtils.getRootNode(session).getPath(), baos1, false, false);
        rootContent = baos1.toByteArray();
      } finally {
        if (baos1 != null) {
          baos1.close();
        }
      }
      byte[] systemContent;
      ByteArrayOutputStream baos2 = null;
      try {
        baos2 = new ByteArrayOutputStream();
        session.exportSystemView(JCRUtils.getSystemNode(session).getPath(), baos2, false, false);
        systemContent = baos2.toByteArray();
      } finally {
        if (baos2 != null) {
          baos2.close();
        }
      }

      String tag = String.valueOf(rootContent.length) + "*";
      byte[] header = tag.getBytes();

      byte[] content = new byte[header.length + rootContent.length + systemContent.length];
      System.arraycopy(header, 0, content, 0, header.length);
      System.arraycopy(rootContent, 0, content, header.length, rootContent.length);
      System.arraycopy(
          systemContent, 0, content, header.length + rootContent.length, systemContent.length);

      return zipContent(content);
    } catch (PathNotFoundException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (javax.jcr.RepositoryException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    }
  }
  public void exportRepository(Ticket ticket, OutputStream stream)
      throws RepositoryNotFoundException, RepositoryException, SecurityException {

    try {
      javax.jcr.Session session = SessionManager.getInstance().getSession(ticket);
      if (session == null) {
        throw new RepositoryException("Session has expired. Please log in again.");
      }
      javax.jcr.Node root = JCRUtils.getRootNode(session);
      if (!JCRSecurityService.canRead(root, ticket.getUser().getId())) {
        throw new SecurityException(SecurityException.NOT_ENOUGH_PERMISSIONS);
      }

      // Create a temporary file with content
      File tempRoot = File.createTempFile("tmp", "jlib");
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(tempRoot);
        session.exportSystemView(JCRUtils.getRootNode(session).getPath(), fos, false, false);
      } finally {
        if (fos != null) {
          fos.close();
        }
      }

      // Will wrap compression around the given stream
      ZipOutputStream zos = null;
      try {
        zos = new ZipOutputStream(stream);
        zos.setComment("jLibrary ZIP archive");
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setEncoding("UTF-8");
        zos.setLevel(Deflater.DEFAULT_COMPRESSION);

        // create and initialize a zipentry for it
        ZipEntry entry = new ZipEntry("jlibrary");
        entry.setTime(System.currentTimeMillis());
        zos.putNextEntry(entry);
        // write header
        String tag = String.valueOf(tempRoot.length()) + "*";
        byte[] header = tag.getBytes();
        zos.write(header);

        // write root
        FileInputStream fis = null;
        try {
          fis = new FileInputStream(tempRoot);
          IOUtils.copy(fis, zos);
        } finally {
          if (fis != null) {
            fis.close();
          }
        }
        // Delete root file
        tempRoot.delete();

        session.exportSystemView(JCRUtils.getSystemNode(session).getPath(), zos, false, false);
      } finally {
        if (zos != null) {
          zos.closeEntry();
          zos.close();
        }
      }

    } catch (PathNotFoundException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (javax.jcr.RepositoryException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    }
  }