コード例 #1
0
  /**
   * method to persist credentials of a jira account
   *
   * @param registry Registry
   * @param accountInfo AccountInfo
   * @throws IssueTrackerException thrown id unable to store resources in the registry
   */
  public static void persistCredentials(Registry registry, AccountInfo accountInfo)
      throws IssueTrackerException {

    Resource resource = null;

    String path = IssueTrackerConstants.ISSUE_TRACKERS_RESOURCE_PATH + accountInfo.getKey();

    try {
      // if the collection does not exist create one
      if (!registry.resourceExists(IssueTrackerConstants.ISSUE_TRACKERS_RESOURCE_PATH)) {
        Collection collection = registry.newCollection();
        registry.put(IssueTrackerConstants.ISSUE_TRACKERS_RESOURCE_PATH, collection);
      }

      // get registry resource
      if (registry.resourceExists(path)) {
        resource = registry.get(path);
      } else {
        resource = registry.newResource();
      }
    } catch (RegistryException e) {
      ExceptionHandler.handleException("Error accessing registry", e, log);
    }

    // get credentials from account info
    GenericCredentials credentials;
    credentials = accountInfo.getCredentials();

    // set properties of the registry resources
    if (resource != null) {
      resource.addProperty(IssueTrackerConstants.ACCOUNT_KEY, accountInfo.getKey());
      resource.addProperty(IssueTrackerConstants.ISSUE_TRACKER_URL, credentials.getUrl());
      resource.addProperty(IssueTrackerConstants.ACCOUNT_LOGIN_USERNAME, credentials.getUsername());
      resource.addProperty(IssueTrackerConstants.ACCOUNT_EMAIL, accountInfo.getEmail());
      resource.addProperty(IssueTrackerConstants.ACCOUNT_UID, accountInfo.getUid());
      resource.addProperty(
          IssueTrackerConstants.HAS_SUPPORT_ACCOUNT,
          String.valueOf(accountInfo.isHasSupportAccount()));

      // set properties related with automatic reporting
      if (accountInfo.isAutoReportingEnable()) {

        AutoReportingSettings settings = accountInfo.getAutoReportingSettings();
        resource.addProperty(
            IssueTrackerConstants.AUTO_REPORTING, IssueTrackerConstants.IS_AUTO_REPORTING_ENABLED);
        resource.addProperty(
            IssueTrackerConstants.AUTO_REPORTING_PROJECT, settings.getProjectName());
        resource.addProperty(IssueTrackerConstants.AUTO_REPORTING_PRIORITY, settings.getPriority());
        resource.addProperty(
            IssueTrackerConstants.AUTO_REPORTING_ISSUE_TYPE, settings.getIssueType());

      } else {
        resource.addProperty(
            IssueTrackerConstants.AUTO_REPORTING, IssueTrackerConstants.IS_AUTO_REPORTING_DISABLED);
      }

      // encrypt and store password
      String password = credentials.getPassword();

      if (null != password && !"".equals(password)) {
        byte[] bytes = (password).getBytes();

        try {
          String base64String = CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(bytes);
          resource.addProperty(
              IssueTrackerConstants.ACCOUNT_PASSWORD_HIDDEN_PROPERTY, base64String);
        } catch (org.wso2.carbon.core.util.CryptoException e) {
          ExceptionHandler.handleException("Error accessing registry", e, log);
        }
      }
    }

    // put resource to registry
    try {
      registry.put(path, resource);
    } catch (RegistryException e) {
      ExceptionHandler.handleException("Error while persisting accountInfo", e, log);
    }
  }