String getContent(HttpMethod httpMethod) {
   StringBuilder contentBuilder = new StringBuilder();
   if (isZipContent(httpMethod)) {
     InputStream is = null;
     GZIPInputStream gzin = null;
     InputStreamReader isr = null;
     BufferedReader br = null;
     try {
       is = httpMethod.getResponseBodyAsStream();
       gzin = new GZIPInputStream(is);
       isr =
           new InputStreamReader(
               gzin,
               ((HttpMethodBase) httpMethod).getResponseCharSet()); // ���ö�ȡ���ı����ʽ���Զ������
       br = new BufferedReader(isr);
       char[] buffer = new char[4096];
       int readlen = -1;
       while ((readlen = br.read(buffer, 0, 4096)) != -1) {
         contentBuilder.append(buffer, 0, readlen);
       }
     } catch (Exception e) {
       log.error("Unzip fail", e);
     } finally {
       try {
         br.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         isr.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         gzin.close();
       } catch (Exception e1) {
         // ignore
       }
       try {
         is.close();
       } catch (Exception e1) {
         // ignore
       }
     }
   } else {
     String content = null;
     try {
       content = httpMethod.getResponseBodyAsString();
     } catch (Exception e) {
       log.error("Fetch config error:", e);
     }
     if (null == content) {
       return null;
     }
     contentBuilder.append(content);
   }
   return contentBuilder.toString();
 }
    public static String toString(MapMarker... markers) {
      if (markers.length > 0) {
        StringBuilder sb = new StringBuilder();

        sb.append(MarkersKey).append("=");

        for (int i = 0; i < markers.length; i++) {
          sb.append(markers[i].toString());
          if (i != markers.length - 1) sb.append(MarkerSeparator);
        }

        return sb.toString();
      } else {
        return "";
      }
    }
  private String getSuccess(
      String dataId, String group, CacheData cacheData, HttpMethod httpMethod) {
    String configInfo = Constants.NULL;
    configInfo = getContent(httpMethod);
    if (null == configInfo) {
      throw new RuntimeException("RP_OK configInfo is null");
    }

    Header md5Header = httpMethod.getResponseHeader(Constants.CONTENT_MD5);
    if (null == md5Header) {
      throw new RuntimeException("RP_OK not contain MD5, " + configInfo);
    }
    String md5 = md5Header.getValue();
    if (!checkContent(configInfo, md5)) {
      throw new RuntimeException(
          "MD5 verify error,DataID:["
              + dataId
              + "]ConfigInfo:["
              + configInfo
              + "]MD5:["
              + md5
              + "]");
    }

    Header lastModifiedHeader = httpMethod.getResponseHeader(Constants.LAST_MODIFIED);
    if (null == lastModifiedHeader) {
      throw new RuntimeException("RP_OK result not contain lastModifiedHeader");
    }
    String lastModified = lastModifiedHeader.getValue();

    cacheData.setMd5(md5);
    cacheData.setLastModifiedHeader(lastModified);

    changeSpacingInterval(httpMethod);

    String key = makeCacheKey(dataId, group);
    contentCache.put(key, configInfo);

    StringBuilder buf = new StringBuilder();
    buf.append("dataId=").append(dataId);
    buf.append(" ,group=").append(group);
    buf.append(" ,content=").append(configInfo);
    dataLog.info(buf.toString());

    return configInfo;
  }
  private String getProbeUpdateString() {
    StringBuilder probeModifyBuilder = new StringBuilder();
    for (Entry<String, ConcurrentHashMap<String, CacheData>> cacheDatasEntry :
        this.cache.entrySet()) {
      String dataId = cacheDatasEntry.getKey();
      ConcurrentHashMap<String, CacheData> cacheDatas = cacheDatasEntry.getValue();
      if (null == cacheDatas) {
        continue;
      }
      for (Entry<String, CacheData> cacheDataEntry : cacheDatas.entrySet()) {
        final CacheData data = cacheDataEntry.getValue();
        if (!data.isUseLocalConfigInfo()) {
          probeModifyBuilder.append(dataId).append(WORD_SEPARATOR);

          if (null != cacheDataEntry.getValue().getGroup()
              && Constants.NULL != cacheDataEntry.getValue().getGroup()) {
            probeModifyBuilder.append(cacheDataEntry.getValue().getGroup()).append(WORD_SEPARATOR);
          } else {
            probeModifyBuilder.append(WORD_SEPARATOR);
          }

          if (null != cacheDataEntry.getValue().getMd5()
              && Constants.NULL != cacheDataEntry.getValue().getMd5()) {
            probeModifyBuilder.append(cacheDataEntry.getValue().getMd5()).append(LINE_SEPARATOR);
          } else {
            probeModifyBuilder.append(LINE_SEPARATOR);
          }
        }
      }
    }
    String probeModifyString = probeModifyBuilder.toString();
    return probeModifyString;
  }
 String getUriString(String dataId, String group) {
   StringBuilder uriBuilder = new StringBuilder();
   uriBuilder.append(Constants.HTTP_URI_FILE);
   uriBuilder.append("?");
   uriBuilder.append(Constants.DATAID).append("=").append(dataId);
   if (null != group) {
     uriBuilder.append("&");
     uriBuilder.append(Constants.GROUP).append("=").append(group);
   }
   return uriBuilder.toString();
 }
  public String getURI(double lat, double lon, int sizeW, int sizeH, int zoom) {
    _validateParams(sizeW, sizeH, zoom);

    // generate the URI
    StringBuilder sb = new StringBuilder();
    sb.append(GmapStaticURI);

    // center key
    sb.append("?").append(CenterKey).append("=").append(lat).append(",").append(lon);

    // zoom key
    sb.append("&").append(ZoomKey).append("=").append(zoom);

    // size key
    sb.append("&").append(SizeKey).append("=").append(sizeW).append(SizeSeparator).append(sizeH);

    // markers key
    sb.append("&").append(MarkerUtils.toString(new MapMarker(lat, lon)));

    // maps key
    sb.append("&").append(GmapLicenseKey).append("=").append(GmapLicense);

    return sb.toString();
  }
  // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  // param handling and uri generation
  // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  public String getURI(double lat, double lon, int sizeW, int sizeH, MapMarker... markers) {
    _validateParams(sizeW, sizeH, ZoomDefault);

    // generate the URI
    StringBuilder sb = new StringBuilder();
    sb.append(GmapStaticURI);

    // size key
    sb.append("?").append(SizeKey).append("=").append(sizeW).append(SizeSeparator).append(sizeH);

    // markers key
    sb.append("&").append(MarkerUtils.toString(markers));

    // maps key
    sb.append("&").append(GmapLicenseKey).append("=").append(GmapLicense);

    return sb.toString();
  }
  @Override
  public void deployItemsToTarget(
      String site, List<PublishingSyncItem> filteredItems, PublishingTargetItem target)
      throws ContentNotFoundForPublishingException, UploadFailedException {
    LOGGER.debug(
        "Start deploying items for site \"{0}\", target \"{1}\", number of items \"{2}\"",
        site, target.getName(), filteredItems.size());
    URL requestUrl = null;
    try {
      requestUrl = new URL(target.getServerUrl());
    } catch (MalformedURLException e) {
      LOGGER.error("Invalid server URL for target {0}", target.getName());
      throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e);
    }

    ByteArrayPartSource baps = null;
    PartSource metadataPart = null;
    StringPart stringPart = null;
    FilePart filePart = null;

    int numberOfBuckets = filteredItems.size() / target.getBucketSize() + 1;
    Iterator<PublishingSyncItem> iter = filteredItems.iterator();
    LOGGER.debug(
        "Divide all deployment items into {0} bucket(s) for  target {1}",
        numberOfBuckets, target.getName());
    List<DeploymentEventItem> eventItems = new ArrayList<DeploymentEventItem>();
    for (int bucketIndex = 0; bucketIndex < numberOfBuckets; bucketIndex++) {
      int cntFiles = 0;
      StringBuilder sbDeletedFiles = new StringBuilder();
      List<Part> formParts = new ArrayList<Part>();

      formParts.add(new StringPart(PASSWORD_REQUEST_PARAMETER, target.getPassword()));
      formParts.add(new StringPart(TARGET_REQUEST_PARAMETER, target.getTarget()));
      String siteId = target.getSiteId();
      if (StringUtils.isEmpty(siteId)) {
        siteId = site;
      }
      formParts.add(new StringPart(SITE_REQUEST_PARAMETER, siteId));

      LOGGER.debug(
          "Preparing deployment items (bucket {0}) for target {1}",
          bucketIndex + 1, target.getName());

      int loopSize =
          (filteredItems.size() - (bucketIndex * target.getBucketSize()) > target.getBucketSize())
              ? target.getBucketSize()
              : filteredItems.size() - bucketIndex * target.getBucketSize();
      for (int j = 0; j < loopSize; j++) {
        if (iter.hasNext()) {

          PublishingSyncItem item = iter.next();
          LOGGER.debug(
              "Parsing \"{0}\" , site \"{1}\"; for publishing on target \"{2}\"",
              item.getPath(), item.getSite(), target.getName());
          DeploymentEventItem eventItem = new DeploymentEventItem();
          eventItem.setSite(item.getSite());
          eventItem.setPath(item.getPath());
          eventItem.setUser(item.getUser());
          eventItem.setDateTime(new Date());

          if (item.getAction() == PublishingSyncItem.Action.DELETE) {
            eventItem.setState(DeploymentEventItem.STATE_DELETED);
            if (sbDeletedFiles.length() > 0) {
              sbDeletedFiles.append(FILES_SEPARATOR).append(item.getPath());
            } else {
              sbDeletedFiles.append(item.getPath());
            }
            if (item.getPath().endsWith("/" + _indexFile)) {
              sbDeletedFiles
                  .append(FILES_SEPARATOR)
                  .append(item.getPath().replace("/" + _indexFile, ""));
            }
          } else {

            if (item.getAction() == PublishingSyncItem.Action.NEW) {
              eventItem.setState(DeploymentEventItem.STATE_NEW);
            } else if (item.getAction() == PublishingSyncItem.Action.MOVE) {
              eventItem.setState(DeploymentEventItem.STATE_MOVED);
            } else {
              eventItem.setState(DeploymentEventItem.STATE_UPDATED);
            }

            LOGGER.debug("Get content for \"{0}\" , site \"{1}\"", item.getPath(), item.getSite());
            InputStream input =
                _contentRepository.getContent(site, null, item.getEnvironment(), item.getPath());
            try {
              if (input == null || input.available() < 0) {
                if (!_contentRepository.isFolder(site, item.getPath())
                    && _contentRepository.contentExists(site, item.getPath())) {
                  baps = null;
                  stringPart = null;
                  filePart = null;
                  formParts = null;
                  throw new ContentNotFoundForPublishingException(
                      site, target.getName(), item.getPath());
                } else {
                  // Content does not exist - skip deploying file
                  continue;
                }
              }
            } catch (IOException err) {
              LOGGER.error(
                  "Error reading input stream for content at path: "
                      + item.getPath()
                      + " site: "
                      + item.getSite());
              if (_contentRepository.contentExists(site, item.getPath())) {
                baps = null;
                stringPart = null;
                filePart = null;
                formParts = null;
                throw new ContentNotFoundForPublishingException(
                    site, target.getName(), item.getPath());
              } else {
                // Content does not exist - skip deploying file
                continue;
              }
            }
            String fileName = _contentRepository.getFilename(site, item.getPath());

            byte[] byteArray = null;

            try {
              byteArray = IOUtils.toByteArray(input);
            } catch (IOException e) {
              LOGGER.error("Error while converting input stream to byte array", e);
              baps = null;
              stringPart = null;
              filePart = null;
              formParts = null;
              if (_contentRepository.contentExists(site, item.getPath())) {
                throw new ContentNotFoundForPublishingException(
                    site, target.getName(), item.getPath());
              } else {
                // Content does not exist - skip deploying file
                continue;
              }
            } finally {
              IOUtils.closeQuietly(input);
              input = null;
            }
            baps = new ByteArrayPartSource(fileName, byteArray);

            LOGGER.debug(
                "Create http request parameters for \"{0}\" , site \"{1}\"; publishing on target \"{2}\"",
                item.getPath(), item.getSite(), target.getName());
            int idx = item.getPath().lastIndexOf("/");
            String relativePath = item.getPath().substring(0, idx + 1) + fileName;
            stringPart =
                new StringPart(CONTENT_LOCATION_REQUEST_PARAMETER + cntFiles, relativePath);
            formParts.add(stringPart);
            filePart = new FilePart(CONTENT_FILE_REQUEST_PARAMETER + cntFiles, baps);
            formParts.add(filePart);
            if (item.getAction() == PublishingSyncItem.Action.MOVE) {
              if (item.getOldPath() != null
                  && !item.getOldPath().equalsIgnoreCase(item.getPath())) {
                LOGGER.debug(
                    "Add old path to be deleted for MOVE action (\"{0}\")", item.getOldPath());
                eventItem.setOldPath(item.getOldPath());
                if (sbDeletedFiles.length() > 0) {
                  sbDeletedFiles.append(",").append(item.getOldPath());
                } else {
                  sbDeletedFiles.append(item.getOldPath());
                }
                if (item.getOldPath().endsWith("/" + _indexFile)) {
                  sbDeletedFiles
                      .append(FILES_SEPARATOR)
                      .append(item.getOldPath().replace("/" + _indexFile, ""));
                }
              }
            }

            if (target.isSendMetadata()) {
              LOGGER.debug(
                  "Adding meta data for content \"{0}\" site \"{0}\"",
                  item.getPath(), item.getSite());
              InputStream metadataStream = null;
              try {
                metadataStream = _contentRepository.getMetadataStream(site, item.getPath());
                metadataPart =
                    new ByteArrayPartSource(
                        fileName + ".meta", IOUtils.toByteArray(metadataStream));
                formParts.add(
                    new FilePart(METADATA_FILE_REQUEST_PARAMETER + cntFiles, metadataPart));
              } catch (IOException e) {
                LOGGER.error("Error while creating input stream with content metadata", e);
                baps = null;
                stringPart = null;
                filePart = null;
                formParts = null;
              } finally {
                IOUtils.closeQuietly(metadataStream);
                metadataPart = null;
              }
            }
          }
          cntFiles++;
          eventItems.add(eventItem);
        }
      }

      if (sbDeletedFiles.length() > 0) {
        formParts.add(new StringPart(DELETED_FILES_REQUEST_PARAMETER, sbDeletedFiles.toString()));
      }
      LOGGER.debug(
          "Create http request to deploy bucket {0} for target {1}",
          bucketIndex + 1, target.getName());

      PostMethod postMethod = null;
      HttpClient client = null;
      try {

        LOGGER.debug("Create HTTP Post Method");
        postMethod = new PostMethod(requestUrl.toString());
        postMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
        Part[] parts = new Part[formParts.size()];
        for (int i = 0; i < formParts.size(); i++) parts[i] = formParts.get(i);
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        client = new HttpClient();

        LOGGER.debug("Execute HTTP POST request \"{0}\"", postMethod.getURI());
        int status = client.executeMethod(postMethod);
        if (status == HttpStatus.SC_OK) {
          LOGGER.info(
              "Successfully deployed bucket number {0} on target {1}",
              bucketIndex + 1, target.getName());
        } else {
          LOGGER.error(
              "Deployment failed for bucket number {0} on target {1}. Deployment agent returned status {2}",
              bucketIndex + 1, target.getName(), HttpStatus.getStatusText(status));
          throw new UploadFailedException(site, target.getName(), target.getServerUrl());
        }
      } catch (HttpException e) {
        LOGGER.error(
            "Publish failed for target {0} due to http protocol exception", target.getName());
        throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e);
      } catch (IOException e) {
        LOGGER.error(
            "Publish failed for target {0} due to I/O (transport) exception", target.getName());
        throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e);
      } finally {
        LOGGER.debug("Release http connection and release resources");
        if (client != null) {
          HttpConnectionManager mgr = client.getHttpConnectionManager();
          if (mgr instanceof SimpleHttpConnectionManager) {
            ((SimpleHttpConnectionManager) mgr).shutdown();
          }
        }
        if (postMethod != null) {
          postMethod.releaseConnection();
          postMethod = null;
          client = null;
        }
        baps = null;
        stringPart = null;
        filePart = null;
        formParts = null;
      }
    }

    LOGGER.debug(
        "Publishing deployment event for target \"{0}\" with \"{1}\" items.",
        target.getName(), eventItems.size());
    _contentRepository.publishDeployEvent(target.getName(), eventItems);

    LOGGER.info("Deployment successful on target {0}", target.getName());
    LOGGER.debug(
        "Finished deploying items for site \"{0}\", target \"{1}\", number of items \"{2}\"",
        site, target.getName(), filteredItems.size());
  }
  private void init() {
    // Register the trust manager to use when using HTTPS
    Protocol easyhttps =
        new Protocol("https", (ProtocolSocketFactory) new SSLProtocolSocketFactory(this), 443);
    Protocol.registerProtocol("https", easyhttps);

    // Convert XML based provider setup to Database based
    JiveGlobals.migrateProperty("clearspace.uri");
    JiveGlobals.migrateProperty("clearspace.sharedSecret");

    // Make sure that all Clearspace components are set up, unless they were overridden
    // Note that the auth provider is our way of knowing that we are set up with Clearspace,
    // so don't bother checking to set it.
    if (isEnabled()) {
      if (JiveGlobals.getProperty("provider.user.className") == null) {
        JiveGlobals.setProperty(
            "provider.user.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceUserProvider");
      }
      if (JiveGlobals.getProperty("provider.group.className") == null) {
        JiveGlobals.setProperty(
            "provider.group.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceGroupProvider");
      }
      if (JiveGlobals.getProperty("provider.vcard.className") == null) {
        JiveGlobals.setProperty(
            "provider.vcard.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceVCardProvider");
      }
      if (JiveGlobals.getProperty("provider.lockout.className") == null) {
        JiveGlobals.setProperty(
            "provider.lockout.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceLockOutProvider");
      }
      if (JiveGlobals.getProperty("provider.securityAudit.className") == null) {
        JiveGlobals.setProperty(
            "provider.securityAudit.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceSecurityAuditProvider");
      }
      if (JiveGlobals.getProperty("provider.admin.className") == null) {
        JiveGlobals.setProperty(
            "provider.admin.className",
            "org.jivesoftware.openfire.clearspace.ClearspaceAdminProvider");
      }
    }

    this.uri = properties.get("clearspace.uri");
    if (uri != null) {
      if (!this.uri.endsWith("/")) {
        this.uri = this.uri + "/";
      }
      // Updates the host/port attributes based on the uri
      updateHostPort();
    }
    sharedSecret = properties.get("clearspace.sharedSecret");

    // Creates the cache maps
    userIDCache = new DefaultCache<String, Long>("clearspace.userid", 1000, JiveConstants.DAY);
    groupIDCache = new DefaultCache<String, Long>("clearspace.groupid", 1000, JiveConstants.DAY);
    usernameCache = new DefaultCache<Long, String>("clearspace.username", 1000, JiveConstants.DAY);

    if (Log.isDebugEnabled()) {
      StringBuilder buf = new StringBuilder();
      buf.append("Created new ClearspaceManager() instance, fields:\n");
      buf.append("\t URI: ").append(uri).append("\n");
      buf.append("\t sharedSecret: ").append(sharedSecret).append("\n");

      Log.debug("ClearspaceManager: " + buf.toString());
    }

    // Init nonce cache
    nonceCache = CacheFactory.createCache("Clearspace SSO Nonce");
    // Init nonce generator
    nonceGenerator = new Random();
  }
  @Override
  public BatchHttpResult getConfigureInformationBatch(
      List<String> dataIds, String group, int timeout) {
    if (dataIds == null) {
      log.error("dataId list cannot be null,group=" + group);
      return new BatchHttpResult(HttpStatus.SC_BAD_REQUEST);
    }
    if (group == null) {
      group = Constants.DEFAULT_GROUP;
    }

    StringBuilder dataIdBuilder = new StringBuilder();
    for (String dataId : dataIds) {
      dataIdBuilder.append(dataId).append(Constants.LINE_SEPARATOR);
    }
    String dataIdStr = dataIdBuilder.toString();

    PostMethod post = new PostMethod(Constants.HTTP_URI_FILE_BATCH);
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout);

    BatchHttpResult response = null;
    try {
      NameValuePair dataIdValue = new NameValuePair("dataIds", dataIdStr);
      NameValuePair groupValue = new NameValuePair("group", group);

      post.setRequestBody(new NameValuePair[] {dataIdValue, groupValue});

      httpClient
          .getHostConfiguration()
          .setHost(
              diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
              this.diamondConfigure.getPort());
      int status = httpClient.executeMethod(post);
      String responseMsg = post.getResponseBodyAsString();

      if (status == HttpStatus.SC_OK) {
        String json = null;
        try {
          json = responseMsg;

          List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
          Object resultObj =
              JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {});
          if (!(resultObj instanceof List<?>)) {
            throw new RuntimeException(
                "batch query deserialize type error, not list, json=" + json);
          }
          List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
          for (ConfigInfoEx configInfoEx : resultList) {
            configInfoExList.add(configInfoEx);
          }

          response = new BatchHttpResult(configInfoExList);
          log.info(
              "batch query success,dataIds=" + dataIdStr + ",group=" + group + ",json=" + json);
        } catch (Exception e) {
          response = new BatchHttpResult(Constants.BATCH_OP_ERROR);
          log.error(
              "batch query deserialize error,dataIdStr="
                  + dataIdStr
                  + ",group="
                  + group
                  + ",json="
                  + json,
              e);
        }

      } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
        response = new BatchHttpResult(HttpStatus.SC_REQUEST_TIMEOUT);
        log.error(
            "batch query timeout, socket timeout(ms):"
                + timeout
                + ",dataIds="
                + dataIdStr
                + ",group="
                + group);
      } else {
        response = new BatchHttpResult(status);
        log.error(
            "batch query fail, status:"
                + status
                + ", response:"
                + responseMsg
                + ",dataIds="
                + dataIdStr
                + ",group="
                + group);
      }
    } catch (HttpException e) {
      response = new BatchHttpResult(Constants.BATCH_HTTP_EXCEPTION);
      log.error("batch query http exception,dataIds=" + dataIdStr + ",group=" + group, e);
    } catch (IOException e) {
      response = new BatchHttpResult(Constants.BATCH_IO_EXCEPTION);
      log.error("batch query io exception, dataIds=" + dataIdStr + ",group=" + group, e);
    } finally {
      post.releaseConnection();
    }

    return response;
  }