/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.issue.tracker.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.core.util.CryptoUtil;
import org.wso2.carbon.issue.tracker.adapter.api.GenericCredentials;
import org.wso2.carbon.issue.tracker.adapter.exceptions.IssueTrackerException;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import sun.misc.BASE64Decoder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Class to handle registry resources. this is used to persist account credentials.
*/
public class RegistryResourceHandler {
private static final Log log = LogFactory.getLog(RegistryResourceHandler.class);
/**
* method to persist credentials
*
* @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);
}
if (registry.resourceExists(path)) {
resource = registry.get(path);
} else {
resource = registry.newResource();
}
} catch (RegistryException e) {
ExceptionHandler.handleException("Error accessing registry", e, log);
}
GenericCredentials credentials;
credentials = accountInfo.getCredentials();
if (resource != null) {
resource.addProperty(IssueTrackerConstants.ACCOUNT_KEY, accountInfo.getKey());
resource.addProperty(IssueTrackerConstants.ISSUE_TRACKER_URL, credentials.getUrl());
resource.addProperty(IssueTrackerConstants.ACCOUNT_USERNAME, credentials.getUsername());
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
byte[] bytes = (credentials.getPassword()).getBytes();
try {
byte[] encrypted = CryptoUtil.getDefaultCryptoUtil().encrypt(bytes);
String base64String = new sun.misc.BASE64Encoder().encode(encrypted);
resource.addProperty(IssueTrackerConstants.ACCOUNT_PASSWORD, base64String);
} catch (org.wso2.carbon.core.util.CryptoException e) {
ExceptionHandler.handleException("Error accessing registry", e, log);
}
}
try {
registry.put(path, resource);
} catch (RegistryException e) {
ExceptionHandler.handleException("Error while persisting accountInfo", e, log);
}
}
/**
* method to get stored accounts from registry
*
* @param registry Registry
* @return list of AccountInfo
* @throws IssueTrackerException thrown if unable to obtain resources from registry
*/
public static List<AccountInfo> getAccounts(Registry registry) throws IssueTrackerException {
List<AccountInfo> accounts = new ArrayList<AccountInfo>();
try {
if (registry.resourceExists(IssueTrackerConstants.ISSUE_TRACKERS_RESOURCE_PATH)) {
Collection collection =
(Collection) registry.get(IssueTrackerConstants.ISSUE_TRACKERS_RESOURCE_PATH);
if (null != collection) {
String[] paths = collection.getChildren();
for (String path : paths) {
AccountInfo accountInfo = new AccountInfo();
GenericCredentials credentials = new GenericCredentials();
Resource resource = registry.get(path);
List<String> accountPropertySet= resource.getPropertyValues(IssueTrackerConstants.ACCOUNT_KEY);
accountInfo.setKey(accountPropertySet.get(accountPropertySet.size()-1));
List<String> urlPropertySet = resource.getPropertyValues(IssueTrackerConstants.ISSUE_TRACKER_URL);
String url = urlPropertySet.get(urlPropertySet.size()-1);
List<String> usernamePropertySet= resource.getPropertyValues(IssueTrackerConstants.ACCOUNT_USERNAME);
String username = usernamePropertySet.get(usernamePropertySet.size()-1);
List<String> passwordPropertySet = resource.getPropertyValues(IssueTrackerConstants.ACCOUNT_PASSWORD);
String encryptedPassword = passwordPropertySet.get(passwordPropertySet.size()-1);
byte[] base64DecodedBytes = (new BASE64Decoder()).decodeBuffer(encryptedPassword);
String password = new String(CryptoUtil.getDefaultCryptoUtil().decrypt(base64DecodedBytes));
String isAutoReportingEnabled = resource.getProperty(IssueTrackerConstants.AUTO_REPORTING);
if (null != isAutoReportingEnabled &&
IssueTrackerConstants.IS_AUTO_REPORTING_ENABLED.equals(isAutoReportingEnabled)) {
accountInfo.setAutoReportingEnable(true);
AutoReportingSettings settings=new AutoReportingSettings();
String projectName=resource.getProperty(IssueTrackerConstants.AUTO_REPORTING_PROJECT);
settings.setProjectName(projectName);
String priority = resource.getProperty(IssueTrackerConstants.AUTO_REPORTING_PRIORITY);
settings.setPriority(priority);
String type = resource.getProperty(IssueTrackerConstants.AUTO_REPORTING_ISSUE_TYPE);
settings.setIssueType(type);
accountInfo.setAutoReportingSettings(settings);
} else {
accountInfo.setAutoReportingEnable(false);
}
if (!"".equals(url) && !"".equals(username) && !"".equals(password)) {
credentials.setUrl(url);
credentials.setUsername(username);
credentials.setPassword(password);
accountInfo.setCredentials(credentials);
accounts.add(accountInfo);
}
}
}
}
} catch (RegistryException e) {
ExceptionHandler.handleException("Error getting resources from registry", e, log);
} catch (CryptoException e) {
ExceptionHandler.handleException("Error decrypting password", e, log);
} catch (IOException e) {
ExceptionHandler.handleException("Error decoding password", e, log);
}
return accounts;
}
}