public static User authenticateKey(String login, String key) throws SearchLibException {
   usersLock.r.lock();
   try {
     User user = getUserList().get(login);
     if (user == null) return null;
     if (!user.authenticateKey(key)) return null;
     return user;
   } finally {
     usersLock.r.unlock();
   }
 }
 public static void eraseIndex(User user, String indexName)
     throws SearchLibException, NamingException, IOException {
   if (user != null && !user.isAdmin()) throw new SearchLibException("Operation not permitted");
   File indexDir = getIndexDirectory(indexName);
   Client client = null;
   synchronized (ClientCatalog.class) {
     clientsLock.r.lock();
     try {
       client = CLIENTS.get(indexDir);
     } finally {
       clientsLock.r.unlock();
     }
     if (client != null) {
       client.close();
       client.delete();
     } else FileUtils.deleteDirectory(indexDir);
     if (client != null) {
       clientsLock.w.lock();
       try {
         CLIENTS.remove(client.getDirectory());
       } finally {
         clientsLock.w.unlock();
       }
       PushEvent.eventClientSwitch.publish(client);
     }
   }
 }
示例#3
0
 @Override
 public MonitorResult monitor(String login, String key, boolean full) {
   try {
     User user = getLoggedUser(login, key);
     if (user != null)
       if (!user.isMonitoring() && !user.isAdmin()) throw new WebServiceException("Not allowed");
     ClientFactory.INSTANCE.properties.checkApi();
     return new MonitorResult(full);
   } catch (SearchLibException e) {
     throw new WebServiceException(e);
   } catch (InterruptedException e) {
     throw new WebServiceException(e);
   } catch (IOException e) {
     throw new WebServiceException(e);
   }
 }
 public static void createIndex(
     User user, String indexName, TemplateAbstract template, URI remoteURI)
     throws SearchLibException, IOException {
   if (user != null && !user.isAdmin()) throw new SearchLibException("Operation not permitted");
   ClientFactory.INSTANCE.properties.checkMaxIndexNumber();
   if (!isValidIndexName(indexName))
     throw new SearchLibException("The name '" + indexName + "' is not allowed");
   synchronized (ClientCatalog.class) {
     File indexDir = new File(StartStopListener.OPENSEARCHSERVER_DATA_FILE, indexName);
     if (indexDir.exists())
       throw new SearchLibException("directory " + indexName + " already exists");
     template.createIndex(indexDir, remoteURI);
   }
 }
 public static final Set<ClientCatalogItem> getClientCatalog(User user) throws SearchLibException {
   File[] files =
       StartStopListener.OPENSEARCHSERVER_DATA_FILE.listFiles(
           (FileFilter) DirectoryFileFilter.DIRECTORY);
   Set<ClientCatalogItem> set = new TreeSet<ClientCatalogItem>();
   if (files == null) return null;
   for (File file : files) {
     if (!file.isDirectory()) continue;
     String indexName = file.getName();
     if (!isValidIndexName(indexName)) continue;
     if (user == null || user.hasAnyRole(indexName, Role.GROUP_INDEX))
       set.add(new ClientCatalogItem(indexName));
   }
   return set;
 }
  @Override
  protected void doRequest(ServletTransaction transaction) throws ServletException {
    try {

      User user = transaction.getLoggedUser();
      if (user != null && !user.isAdmin()) throw new SearchLibException("Not permitted");

      Client client = transaction.getClient();

      String cmd = transaction.getParameterString("cmd");
      if (CALL_XML_CMD_INIT.equals(cmd)) {
        transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_INIT);
        ClientCatalog.receive_init(client);
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      if (CALL_XML_CMD_SWITCH.equals(cmd)) {
        transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_SWITCH);
        ClientCatalog.receive_switch(transaction.getWebApp(), client);
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      if (CALL_XML_CMD_MERGE.equals(cmd)) {
        transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_MERGE);
        ClientCatalog.receive_merge(transaction.getWebApp(), client);
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      if (CALL_XML_CMD_ABORT.equals(cmd)) {
        transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_ABORT);
        ClientCatalog.receive_abort(client);
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      String filePath = transaction.getParameterString(CALL_XML_CMD_FILEPATH);
      Long lastModified = transaction.getParameterLong("lastModified", 0L);
      Long length = transaction.getParameterLong("length", 0L);
      if (CALL_XML_CMD_EXISTS.equals(cmd)) {
        transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_EXISTS);
        boolean exist = ClientCatalog.receive_file_exists(client, filePath, lastModified, length);
        transaction.addXmlResponse(CALL_XML_KEY_EXISTS, Boolean.toString(exist));
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      transaction.addXmlResponse(CALL_XML_KEY_CMD, CALL_XML_CMD_FILEPATH);
      if (FilenameUtils.getName(filePath).startsWith(".")) {
        transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
        return;
      }
      filePath = FileUtils.unixToSystemPath(filePath);
      if (transaction.getParameterBoolean("type", "dir", false))
        ClientCatalog.receive_dir(client, filePath);
      else ClientCatalog.receive_file(client, filePath, lastModified, transaction.getInputStream());
      transaction.addXmlResponse(XML_CALL_KEY_STATUS, XML_CALL_KEY_STATUS_OK);
    } catch (SearchLibException e) {
      throw new ServletException(e);
    } catch (NamingException e) {
      throw new ServletException(e);
    } catch (InterruptedException e) {
      throw new ServletException(e);
    } catch (IOException e) {
      throw new ServletException(e);
    }
  }
 public static final boolean exists(User user, String indexName) throws SearchLibException {
   if (user != null && !user.isAdmin()) throw new SearchLibException("Operation not permitted");
   if (!isValidIndexName(indexName))
     throw new SearchLibException("The name '" + indexName + "' is not allowed");
   return getClientCatalog(null).contains(new ClientCatalogItem(indexName));
 }