/** @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);
    }
  }
  private void checkCustomProperties(Session session) throws javax.jcr.RepositoryException {

    NodeTypeManager nodetypeManager = new NodeTypeManager();
    // Check and create required custom properties
    javax.jcr.Node systemNode = JCRUtils.getSystemNode(session);
    if (systemNode.hasNode(JLibraryConstants.JLIBRARY_CUSTOM_PROPERTIES)) {
      javax.jcr.Node propertiesNode =
          systemNode.getNode(JLibraryConstants.JLIBRARY_CUSTOM_PROPERTIES);
      NodeIterator it = propertiesNode.getNodes();
      while (it.hasNext()) {
        javax.jcr.Node propsNode = (javax.jcr.Node) it.next();
        CustomPropertyDefinition propdef = new CustomPropertyDefinition();
        propdef.setName(
            propsNode.getProperty(JLibraryConstants.JLIBRARY_PROPERTY_NAME).getString());
        propdef.setType(
            (int) propsNode.getProperty(JLibraryConstants.JLIBRARY_PROPERTY_TYPE).getLong());
        propdef.setMultivalued(
            propsNode.getProperty(JLibraryConstants.JLIBRARY_PROPERTY_MULTIVALUED).getBoolean());
        propdef.setAutocreated(
            propsNode.getProperty(JLibraryConstants.JLIBRARY_PROPERTY_AUTOCREATED).getBoolean());
        if (propsNode.hasProperty(JLibraryConstants.JLIBRARY_PROPERTY_DEFAULT)) {
          propdef.setDefaultValues(
              propsNode.getProperty(JLibraryConstants.JLIBRARY_PROPERTY_DEFAULT).getValues());
        }

        logger.info("Registering property : " + propdef);
        nodetypeManager.registerCustomProperty(session, propdef);
      }
    }
  }
  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);
    }
  }