Exemplo n.º 1
0
  @Override
  public List<String> getEnabledExtensions() throws OKMException {
    List<String> extensions = new ArrayList<String>();
    Session session = null;

    try {
      Profile up = new Profile();
      session = JCRUtils.getSession();
      UserConfig uc = UserConfigDAO.findByPk(session, session.getUserID());
      up = uc.getProfile();
      extensions = new ArrayList<String>(up.getMisc().getExtensions());
    } catch (LoginException e) {
      log.error(e.getMessage(), e);
      throw new OKMException(
          ErrorCode.get(ErrorCode.ORIGIN_OKMGeneralService, ErrorCode.CAUSE_Repository),
          e.getMessage());
    } catch (javax.jcr.RepositoryException e) {
      log.error(e.getMessage(), e);
      throw new OKMException(
          ErrorCode.get(ErrorCode.ORIGIN_OKMGeneralService, ErrorCode.CAUSE_Repository),
          e.getMessage());
    } catch (DatabaseException e) {
      log.warn(e.getMessage(), e);
      throw new OKMException(
          ErrorCode.get(ErrorCode.ORIGIN_OKMGeneralService, ErrorCode.CAUSE_Database),
          e.getMessage());
    } finally {
      JCRUtils.logout(session);
    }

    return extensions;
  }
  public void importRepository(Ticket ticket, String name, InputStream inputStream)
      throws RepositoryAlreadyExistsException, RepositoryException, SecurityException {

    try {

      if (!ticket.getUser().isAdmin()) {
        throw new SecurityException(SecurityException.NOT_ENOUGH_PERMISSIONS);
      }

      javax.jcr.Session systemSession = SessionManager.getInstance().getSystemSession(ticket);
      WorkspaceImpl workspace = checkWorkspaceExists(name, systemSession);
      // Always change to lowercase
      name = name.toLowerCase();
      workspace.createWorkspace(name);

      javax.jcr.Repository repository = SessionManager.getInstance().getRepository();
      SimpleCredentials creds = new SimpleCredentials("username", "password".toCharArray());

      systemSession = repository.login(creds, name);

      // Copy to temp file. We cannot wrap to zip input stream due to incompatibilities
      // between apache implementation and java.util implementation
      File tempFile = File.createTempFile("jlib", "tmp");
      tempFile.deleteOnExit();
      FileOutputStream fos = new FileOutputStream(tempFile);
      IOUtils.copy(inputStream, fos);
      fos.flush();
      fos.close();

      ZipFile archive = null;
      try {
        archive = new ZipFile(tempFile);
      } catch (IOException ioe) {
        logger.warn("[JCRImportService] Trying to import non zipped repository");
        // probably this will be an old repository, so we will return the
        // content to let the process try to import it
        return;
      }

      // do our own buffering; reuse the same buffer.
      byte[] buffer = new byte[16384];
      ZipEntry entry = archive.getEntry("jlibrary");

      // get a stream of the archive entry's bytes
      InputStream zis = archive.getInputStream(entry);

      // ZipInputStream zis = new ZipInputStream(inputStream);
      byte[] smallBuffer = new byte[32];
      int i = 0;
      boolean tagFound = false;
      while (!tagFound) {
        byte next = (byte) zis.read();
        if (next == '*') {
          tagFound = true;
        } else {
          smallBuffer[i] = next;
          i++;
        }
      }

      byte[] header = new byte[i];
      System.arraycopy(smallBuffer, 0, header, 0, i);
      String lengthString = new String(header);
      int contentLength = Integer.parseInt(lengthString.substring(0, lengthString.length()));

      InputStream wrapzis = new ImportInputStream(zis, contentLength);
      systemSession.importXML("/", wrapzis, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

      // Reopen the stream. importXML closes it
      zis = archive.getInputStream(entry);
      zis.skip(i + 1 + contentLength);

      // Now import the remaining info
      systemSession.importXML("/", zis, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

      tempFile.delete();

      checkCustomProperties(systemSession);

      systemSession.save();

      // Finally check versions compatibility
      VersionChecker checker = new VersionChecker();
      checker.checkSession(systemSession);
    } catch (ConfigurationException ce) {
      // TODO: Remove this catch block when Jackrabbit supports workspace deletes
      throw new RecentlyRemovedRepositoryException();
    } catch (RepositoryAlreadyExistsException raee) {
      throw raee;
    } catch (SecurityException se) {
      throw se;
    } catch (AccessDeniedException e) {
      logger.error(e.getMessage(), e);
      throw new SecurityException(e);
    } catch (LoginException e) {
      logger.error(e.getMessage(), e);
      throw new SecurityException(e);
    } catch (RepositoryException re) {
      throw re;
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    }
  }
  public void importRepository(Ticket ticket, byte[] zippedContent, String name)
      throws RepositoryAlreadyExistsException, RepositoryException, SecurityException {

    try {

      if (!ticket.getUser().isAdmin()) {
        throw new SecurityException(SecurityException.NOT_ENOUGH_PERMISSIONS);
      }

      javax.jcr.Session systemSession = SessionManager.getInstance().getSystemSession(ticket);
      WorkspaceImpl workspace = checkWorkspaceExists(name, systemSession);
      // Always change to lowercase
      name = name.toLowerCase();
      workspace.createWorkspace(name);

      javax.jcr.Repository repository = SessionManager.getInstance().getRepository();
      SimpleCredentials creds = new SimpleCredentials("username", "password".toCharArray());

      systemSession = repository.login(creds, name);

      byte[] content = unzipContent(zippedContent);

      // First read the header
      int i = 0;
      while (content[i] != '*') {
        i++;
      }
      i++;
      byte[] header = new byte[i];
      System.arraycopy(content, 0, header, 0, i);
      String lengthString = new String(header);
      int contentLength = Integer.parseInt(lengthString.substring(0, lengthString.length() - 1));

      // Now, load root content

      byte[] rootContent = new byte[contentLength];
      System.arraycopy(content, i, rootContent, 0, contentLength);
      rootContent = filterForCompatibility(rootContent);
      ByteArrayInputStream bais1 = new ByteArrayInputStream(rootContent);

      // Now load system content
      byte[] systemContent = new byte[content.length - contentLength - header.length];
      System.arraycopy(
          content, header.length + contentLength, systemContent, 0, systemContent.length);
      systemContent = filterForCompatibility(systemContent);
      ByteArrayInputStream bais2 = new ByteArrayInputStream(systemContent);

      systemSession.importXML("/", bais1, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

      systemSession.importXML("/", bais2, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

      checkCustomProperties(systemSession);

      bais1.close();
      bais2.close();

      systemSession.save();

      // Finally check versions compatibility
      VersionChecker checker = new VersionChecker();
      checker.checkSession(systemSession);
    } catch (ConfigurationException ce) {
      // TODO: Remove this catch block when Jackrabbit supports workspace deletes
      throw new RecentlyRemovedRepositoryException();
    } catch (RepositoryAlreadyExistsException raee) {
      throw raee;
    } catch (SecurityException se) {
      throw se;
    } catch (AccessDeniedException e) {
      logger.error(e.getMessage(), e);
      throw new SecurityException(e);
    } catch (LoginException e) {
      logger.error(e.getMessage(), e);
      throw new SecurityException(e);
    } catch (RepositoryException re) {
      throw re;
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    }
  }