private static String getPushTargetUrl(
     Client client, ReplicationItem replicationItem, String cmd, File sourceFile)
     throws UnsupportedEncodingException, SearchLibException, MalformedURLException,
         URISyntaxException {
   String dataPath = replicationItem.getDirectory(client).getAbsolutePath();
   String filePath = sourceFile.getAbsolutePath();
   if (!filePath.startsWith(dataPath)) throw new SearchLibException("Bad file path " + filePath);
   filePath = filePath.substring(dataPath.length());
   StringBuilder sb = new StringBuilder(replicationItem.getCachedUrl());
   if (cmd != null) {
     sb.append("&cmd=");
     sb.append(cmd);
   }
   sb.append("&filePath=");
   sb.append(URLEncoder.encode(FileUtils.systemPathToUnix(filePath), "UTF-8"));
   if (sourceFile.isDirectory()) sb.append("&type=dir");
   else {
     sb.append("&lastModified=" + sourceFile.lastModified());
     sb.append("&length=" + sourceFile.length());
   }
   return sb.toString();
 }
  @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);
    }
  }