Java tutorial
package com.pari.ic; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.apache.log4j.Priority; import org.quartz.JobExecutionContext; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import com.cisco.ca.csp.cso.icm.capture.webservices.service.ICContent; import com.cisco.ca.csp.cso.icm.capture.webservices.service.ICException; import com.cisco.ca.csp.cso.icm.capture.webservices.service.ICNuggetServiceImplPortBindingStub; import com.cisco.ca.csp.cso.icm.capture.webservices.service.ICNuggetServiceProxy; import com.cisco.ca.csp.cso.icm.capture.webservices.service.ICNuggetService_PortType; import com.cisco.ca.csp.cso.icm.capture.webservices.service.IcMetadata; import com.cisco.ca.csp.cso.icm.capture.webservices.service.IcVersionDocument; import com.cisco.ca.csp.cso.icm.capture.webservices.service.IcVersionDocumentAndRuleVersionDocument; import com.cisco.ca.csp.cso.icm.capture.webservices.service.MetaDataField; import com.cisco.ca.csp.sso.icd.encryption.IcdPackageDecryption; import com.pari.ic.ICPackage.ICSource; import com.pari.ic.ICPackage.ImportStatus; import com.pari.nm.base.so.i18nHelper.ShadowWrapper; import com.pari.nm.gui.guimessages.JobStatus; import com.pari.nm.modules.cspc.ConnectivityTypeEnum; import com.pari.nm.modules.customers.Customer; import com.pari.nm.modules.customers.CustomerInstance; import com.pari.nm.modules.customers.CustomerManager; import com.pari.nm.modules.customers.CustomerWingInstanceManager; import com.pari.nm.modules.jobs.JobMgr; import com.pari.nm.modules.jobs.JobRun; import com.pari.nm.modules.jobs.PasImportDetails; import com.pari.nm.modules.jobs.status.IJobStatus; import com.pari.nm.modules.jobs.status.JobStageConstants; import com.pari.nm.modules.session.ServerIfProviderLocalImpl; import com.pari.nm.modules.session.UpgradeIfImpl; import com.pari.nm.utils.db.DBHelper; import com.pari.nm.utils.db.ICDDBHelper; import com.pari.nm.utils.db.InventoryDBHelper; import com.pari.nm.utils.db.ServerDBHelper; import com.pari.pas.pasimport.PasHttpRequestHandler; import com.pari.policies.custom.CustomPolicyManager; import com.pari.policies.policygroups.PolicyGroupManager; import com.pari.services.def.version.custom.CustomPlatformManager; import com.pari.swim.SwimScriptManager; public class ICManager implements ICManagerIf { private static Logger logger = Logger.getLogger(ICManager.class); private static ICManager instance = null; private boolean isInitializing = false; PasHttpRequestHandler pasHttpRequestHandler = new PasHttpRequestHandler(); private Timer icRetrirevalThread; private ICManager() { logger.debug("ICManager is getting created.."); } public synchronized static ICManager getInstance() { if (instance != null) { return instance; } instance = new ICManager(); instance.initialize(); return instance; } private void initialize() { // initialize the IC poll thread isInitializing = true; updateICRetrievalFrequency(ServerDBHelper.loadICStorageServerSettings()); Connection c = null; PreparedStatement queryPs = null; PreparedStatement insertPs = null; try { c = DBHelper.getConnection(); queryPs = c.prepareStatement("select * from shadow_customers where shadow_customer_id=?"); if (!hasShadowCustomerEntry(queryPs, ICEntity.ALL_CUSTOMER_ID)) { DBHelper.executeUpdate("insert into shadow_customers (shadow_customer_id) values (" + ICEntity.ALL_CUSTOMER_ID + ")"); } insertPs = c.prepareStatement( "insert into shadow_customers (shadow_customer_id, customer_id) values (?,?)"); // In case of upgrades, shadow_customer_table may not have the entries for existing customers. // We need to populate the shadow_customer_table for missing customer entries. // At this point we cannot assume CustomerManager is initialized so it is always safe to load the // customer entries directly from database instead of CustomerManager for (Integer customer : getCustomerIds(c)) { if (!hasShadowCustomerEntry(queryPs, customer)) { insertShadowCustomerEntry(insertPs, customer); } } } catch (Exception e) { logger.warn("Error while initializing the IC Manager", e); } finally { try { queryPs.close(); } catch (Exception eee) { } try { insertPs.close(); } catch (Exception eee) { } try { DBHelper.releaseConnection(c); } catch (Exception ignore) { } isInitializing = false; } } /** * Validates if a given IC has sufficient licese for execution. In case of insufficient license, throws exception * with appropriate license expiry message. * * @param customer_id * Customer ID * @param icIdentifier * IC Identifier * * @param deviceCount * the device count * @throws Exception * Exception in case of insufficient license for IC execution */ public void validateLicense(int customer_id, String icIdentifier, int deviceCount) throws Exception { ICPackage icPackage = ICDDBHelper.getICPackageForIc(customer_id, icIdentifier); if (icPackage != null) { String licenseType = icPackage.getLicenseType(); logger.info("Validating license " + licenseType + " for IC " + icIdentifier); if (ICPackageLicense.LicenseNeverExpires == ICPackageLicense.getEnum(licenseType)) { return; } else if (ICPackageLicense.PackageUsageCount == ICPackageLicense.getEnum(licenseType)) { int icUsageCount = ICDDBHelper.getIcUsageCount(customer_id, icIdentifier); int maxCount = icPackage.getLicenseExpiryCount(); if ((icUsageCount + deviceCount) > maxCount) { logger.debug("IC Package " + icPackage.getPackageName() + " has max count " + maxCount + " but current usage count of IC " + icIdentifier + " is " + icUsageCount); throw new Exception("Insufficient License counts - maximum limit is " + maxCount + " , current usage count is " + icUsageCount + ", Required licenses - " + deviceCount); } } else if (ICPackageLicense.PackageExpiryDate == ICPackageLicense.getEnum(licenseType)) { Timestamp expiryDate = icPackage.getLicenseExpiryDate(); try { Date now = new Date(); if (!expiryDate.after(now)) { String expiryDateStr = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm") .format(expiryDate); logger.debug("IC Package " + icPackage.getPackageName() + " has expiry date of " + expiryDateStr + " which is is beyond current time. IC = " + icIdentifier); throw new Exception("License expired - IC Package expired on " + expiryDateStr); } } catch (Exception e) { throw new Exception("IC Package license not available - " + e.getLocalizedMessage()); } } else { logger.error( "Unknown license type " + licenseType + " for IC Package " + icPackage.getPackageName()); } } } private List<Integer> getCustomerIds(Connection c) throws SQLException { ResultSet rs = null; List<Integer> list = new ArrayList<Integer>(); try { rs = DBHelper.executeQuery("select customer_id from customers"); while (rs.next()) { list.add(rs.getInt(1)); } } finally { try { rs.close(); } catch (Exception e) { } } return list; } private boolean hasShadowCustomerEntry(PreparedStatement ps, int customerId) throws SQLException { ResultSet rs = null; boolean hasCustomerEntry = false; try { ps.setInt(1, customerId); rs = ps.executeQuery(); if (rs.next()) { hasCustomerEntry = true; } } finally { try { rs.close(); } catch (Exception eee) { } } return hasCustomerEntry; } private void insertShadowCustomerEntry(PreparedStatement ps, int customerId) throws SQLException { ps.setInt(1, customerId); ps.setInt(2, customerId); ps.executeUpdate(); } /** * Deletes all customer specific ICs. Deletion is best effort scenario - in case certain IC is not getting deleted * for whatever reasons then entire operation will not fail back. It will continue with next ICs that needs to be * deleted. * * This operation could be potentially long running operation. * * @param customerId * Customer ID */ public void deleteCustomerICs(int customerId) { try { deletePoliciesForCustomer(customerId); logger.info("Deleted the Policies for customer id " + customerId); } catch (Exception e) { logger.warn("Error while deleting the Policies for customer id " + customerId, e); } try { deletePolicyGroupsForCustomer(customerId); logger.info("Deleted the Policy Group for customer id " + customerId); } catch (Exception e) { logger.warn("Error while deleting the Policy Group for customer id " + customerId, e); } try { deleteSwimScriptsForCustomer(customerId); logger.info("Deleted the Swim Scripts for customer id " + customerId); } catch (Exception e) { logger.warn("Error while deleting the Swim Scripts for customer id " + customerId, e); } try { deletePlatformsForCustomer(customerId); logger.info("Deleted the Custom Platform for customer id " + customerId); } catch (Exception e) { logger.warn("Error while deleting the Custom Platform for customer id " + customerId, e); } } private void deletePlatformsForCustomer(int customerId) { CustomPlatformManager.getInstance().deletePlatformsForCustomer(customerId); } private void deletePoliciesForCustomer(int customerId) { CustomPolicyManager.getInstance().deletePoliciesForCustomer(customerId); } private void deletePolicyGroupsForCustomer(int customerId) { PolicyGroupManager.getInstance().deletePolicyGroupsForCustomer(customerId); } private void deleteSwimScriptsForCustomer(int customerId) { SwimScriptManager.getInstance().deleteScriptsForCustomer(customerId); } public static String createICKey(ICEntity entity) { return createICKey(entity.getCustomerId(), entity.getIdentifier()); } public static String createICKey(int customerId, String identifier) { String icKey = null; if (customerId != ICEntity.ALL_CUSTOMER_ID) { icKey = Character.toString('c') + customerId + "$" + identifier; } else { icKey = identifier; } return icKey; } public boolean generateSecurityKeyPairsForCustomer(int customer_id, int installation_id, boolean regenerate) { String keystore = InventoryDBHelper.getCustomerSecurityKeystore(customer_id, installation_id); if (!regenerate && keystore != null) { logger.info("Successfully created keypairs for customer/installation " + customer_id + "/" + installation_id); return false; } keystore = null; try { DBHelper.executeUpdate("delete from customer_security_keystore where customer_id=" + customer_id + " and instance_id = " + installation_id); Customer customer = CustomerManager.getInstance().getCustomerById(customer_id); CustomerInstance customer_instance = CustomerWingInstanceManager.getInstance() .getCustomerInstance(installation_id); if (customer_instance != null && customer != null) { String alias = customer.getCustomerName() + "_" + customer_instance.getInstanceName(); keystore = createSecurityKeyPairs(alias); String keystoreEncoded = encodeFile(keystore); InventoryDBHelper.insertCustomerSecurityKeystore(customer_id, installation_id, alias, keystoreEncoded, false); CustomerInstance customerInstance = CustomerWingInstanceManager.getInstance() .getCustomerInstance(installation_id); if (customerInstance != null) { customerInstance.setHasKeystore( InventoryDBHelper.getCustomerSecurityKeystore(customer_id, installation_id) != null); } return true; } logger.warn("Unable to load customer/instance for " + customer_id + "/" + installation_id); } catch (Exception e) { logger.warn("Error while generating the key pairs for customer " + customer_id + ":" + installation_id, e); } finally { if (keystore != null) { File file = new File(keystore); file.delete(); } } return false; } public String createSecurityKeyPairs(String alias) throws Exception { String keystore = alias + "_keystore"; File file = new File(keystore); if (file.exists()) { file.delete(); } String[] params = new String[] { "-genkey", "-alias", alias, "-keyalg", "RSA", "-keysize", "1024", "-validity", "730", "-keystore", keystore, "-storepass", "123456", "-dname", "cn=Cisco NCCM, ou=Cisco, o=Cisco, c=US", "-keypass", "123456", }; /* * System.out.print("keytool "); for (String param : params) { System.out.print(param + " "); } * System.out.println(); */ sun.security.tools.keytool.Main.main(params); logger.info("Successfully created keyparis for customer " + alias); return keystore; } public String getKeyStore(int customerId, int instanceId) { return InventoryDBHelper.getCustomerSecurityKeystore(customerId, instanceId); } public String getPublicKeyKeystoreAlias(int customer_id, int installation_id) { return InventoryDBHelper.getCustomerSecurityKeystoreAlias(customer_id, installation_id); } public String generatePublicKeyCertificate(int customer_id, int installation_id) throws Exception { String certFile = null; String keystore = null; try { Customer customer = CustomerManager.getInstance().getCustomerById(customer_id); CustomerInstance customer_instance = CustomerWingInstanceManager.getInstance() .getCustomerInstance(installation_id); if (customer_instance != null && customer != null) { String alias = getPublicKeyKeystoreAlias(customer_id, installation_id); String keystoreEncoded = InventoryDBHelper.getCustomerSecurityKeystore(customer_id, installation_id); keystore = decodeToFile(alias, keystoreEncoded); certFile = generatePublicKeyCertificate(alias, keystore); logger.info("Successfully created public key certificate for customer " + alias); return encodeFile(certFile); } } catch (Exception e) { logger.warn("Error while generating the key pairs for customer " + customer_id + ":" + installation_id, e); } finally { if (certFile != null) { File file = new File(certFile); file.delete(); } if (keystore != null) { File file = new File(keystore); file.delete(); } } logger.info("Unable to create public key certificate for customer/installation " + customer_id + "/" + installation_id); return null; } private String encodeFile(String filename) { File file = new File(filename); if (!file.exists()) { logger.warn("File does not exists for encoding - " + filename); return null; } FileInputStream fin = null; ByteArrayOutputStream out = null; try { fin = new FileInputStream(file); BASE64Encoder encoder = new sun.misc.BASE64Encoder(); out = new ByteArrayOutputStream(); encoder.encodeBuffer(fin, out); logger.info("File encoding successful - " + filename); return out.toString(); } catch (Exception e) { logger.warn("Error while loading the file " + filename, e); } finally { if (fin != null) { try { fin.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } logger.error("file encoding failed - " + filename); return null; } private String decodeToFile(String name, String keystoreEncoded) { BASE64Decoder decoder = new sun.misc.BASE64Decoder(); FileOutputStream out = null; String filename = null; try { byte[] decoded = decoder.decodeBuffer(keystoreEncoded); File file = new File(ICF_UPLOAD_FOLDER + File.separatorChar + name + ".keystore"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } if (file.exists()) { file.delete(); } out = new FileOutputStream(file); out.write(decoded); filename = file.getAbsolutePath(); } catch (Exception e) { logger.warn("Error while creating the decoded the file ", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { } } } return filename; } public String generatePublicKeyCertificate(String alias, String keystore) throws Exception { String certFile = alias + "_cert.cer"; File file = new File(certFile); if (file.exists()) { file.delete(); } String[] params = new String[] { "-exportcert", "-alias", alias, "-file", certFile, "-keystore", keystore, "-storepass", "123456", "-keypass", "123456", "-debug", "-v" }; runKeyTool(params); return certFile; } /** * Invoking the Keytool java has its own perils - worst of it is it calls System.exit() on certain occasions and * causes NCCM server to initiate shutdown. * * We need to replace it with keytool native process invocation instead of java to avoid nasty System.exit() * * @param params * @throws Exception */ private void runKeyTool(String[] params) throws Exception { sun.security.tools.keytool.Main.main(params); } public File decryptICPackage(int customerId, int instanceId, File inFile) throws Exception { String keystoreFile = null; try { File outFolder = new File( inFile.getParent() + File.separatorChar + "decrypted" + File.separatorChar + inFile.getName()); if (!outFolder.exists()) { outFolder.mkdirs(); } String alias = getPublicKeyKeystoreAlias(customerId, instanceId); keystoreFile = decodeToFile(alias, InventoryDBHelper.getCustomerSecurityKeystore(customerId, instanceId)); IcdPackageDecryption decryption = new IcdPackageDecryption(keystoreFile, "123456"); decryption.decryptAsZip(inFile, outFolder, alias, "123456"); File[] files = outFolder.listFiles(); if (files != null && files.length > 0) { return files[0]; } return null; } finally { try { if (keystoreFile != null) { File file = new File(keystoreFile); file.delete(); } } catch (Exception e) { } } } public synchronized void updateICRetrievalFrequency(ICStorageServerSettings settings) { if (settings == null) { logger.info("IC Retrieval settigs does not exists in database. IC polling will be disabled"); return; } if (settings.getPollType() == ICStorageServerSettings.POLL_MANUAL) { if (icRetrirevalThread != null) { icRetrirevalThread.cancel(); } logger.info("IC Retrieval settigs set to Manual Polling"); } else { if (icRetrirevalThread != null) { icRetrirevalThread.cancel(); } icRetrirevalThread = new Timer("ICPackageRetrievalTask"); logger.info("IC Retrieval settigs set to Automatic Polling, Frequency: " + settings.getPollFrequency() + " minutes"); int userId = -1; try { if (isInitializing) { userId = settings.getConfigUid(); } else { userId = ServerIfProviderLocalImpl.getInstance().getNmIf().getCurrentUser().getUserId(); } } catch (Exception e) { logger.error(e.getMessage()); } TimerTask task = new ICPackageRetrievalTask(userId); icRetrirevalThread.scheduleAtFixedRate(task, 1000 * 10, settings.getPollFrequency() * 60 * 1000); } } private Collection<CustomerInstance> getCustomers() { Collection<CustomerInstance> customerInstances = new ArrayList<CustomerInstance>(); Customer[] customers = CustomerManager.getInstance().getAllCustomers(); for (Customer customer : customers) { try { CustomerInstance[] instances = CustomerWingInstanceManager.getInstance() .getCustomerInstances(customer.getId()); customerInstances.addAll(Arrays.asList(instances)); } catch (Exception e) { logger.warn("Error loading Customer instances for " + customer.getCustomerName(), e); } } return customerInstances; } private String getCustomerName(int customerId) { Customer customer = CustomerManager.getInstance().getCustomerById(customerId); if (customer != null) { return customer.getCustomerName(); } return null; } public String getCustomerInstanceName(int instanceId) { CustomerInstance customer_instance = CustomerWingInstanceManager.getInstance() .getCustomerInstance(instanceId); if (customer_instance != null) { return customer_instance.getInstanceName(); } return null; } public void logMsg(PollJobDetails details, String log, JobStageConstants.PollingStages stage, int loggerType, Exception e) { if (details != null) { details.jobStatus.setState(details.jobId, stage, 0, log); JobRun.logJob(details.jobId, details.jobRunId, log); } if (loggerType == Priority.INFO_INT) { logger.info(log); } else if (loggerType == Priority.DEBUG_INT) { logger.debug(log); } else if (loggerType == Priority.WARN_INT) { if (e != null) { logger.warn(log, e); } else { logger.warn(log); } } else if (loggerType == Priority.ERROR_INT) { if (e != null) { logger.error(log, e); } else { logger.error(log); } } } public static class PollJobDetails { public int jobId; public int jobRunId; public IJobStatus jobStatus; public int jobState = JobStatus.RUNNING; public JobExecutionContext context; public volatile boolean jobCancelled = false; } @Override public void pollICPackages(String type, PollJobDetails details) throws InterruptedException { String log = null; log = ShadowWrapper.getString("ICF_Polling_Started") + " - " + type; logMsg(details, log, JobStageConstants.PollingStages.STARTED, Priority.INFO_INT, null); try { if (details.jobCancelled) { return; } Collection<ICPackage> packages = null; ICStorageServerSettings settings = ServerDBHelper.loadICStorageServerSettings(); if (settings == null) { details.jobState = JobStatus.FAILED; log = ShadowWrapper.getString("ICF_Polling_Loading_Error"); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, null); return; } try { if (details.jobCancelled) { return; } packages = retriveICPackages(settings, details); } catch (Exception e) { details.jobState = JobStatus.FAILED; log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_Retriving_Error"), e.getMessage()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, e); return; } if (packages == null || packages.isEmpty()) { details.jobState = JobStatus.SUCCESS; log = ShadowWrapper.getString("ICF_Polling_no_pkg"); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); return; } log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_pkg_size"), packages.size()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_to_download"), packages.size()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); for (ICPackage pkg : packages) { if (details.jobCancelled) { return; } log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_Package"), pkg.getCustomerName()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); pkg.setSource(ICSource.ICD_AUTO_IMPORT); ICPackage license_info = ICDDBHelper.getLicenseInfo(pkg.getPackageId(), pkg.getPackageVersion(), pkg.getCustomerId()); String filePath = null; try { if (details.jobCancelled) { return; } /* * Check for Delete Request */ if (pkg.isDelete_flag()) { pkg.getPackageImporter().deletePackage(pkg, details); continue; } else if (license_info != null) { if ((pkg.getLicenseType() != null) && (license_info.getLicenseType() != null) && (pkg.getLicenseType().equalsIgnoreCase(license_info.getLicenseType()))) { if (!(pkg.getLicenseExpiryCount() == license_info.getLicenseExpiryCount())) { ICPackage oldPackage = ICDDBHelper.getExistingPackageInfo(pkg.getCustomerId(), pkg.getIcType().toString()); pkg.setIcCount(oldPackage.getIcCount()); pkg.setIcImported(oldPackage.getIcImported()); pkg.setPackageName(oldPackage.getPackageName()); pkg.setPackageVersion(oldPackage.getPackageVersion()); pkg.setIcType(oldPackage.getIcType()); pkg.setImportStatus(oldPackage.getImportStatus()); log = ShadowWrapper.getString("ICF_Same_Version_Import"); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, null); pkg.savePackageInfo(); } else if ((pkg.getLicenseExpiryDate() != null) && (license_info.getLicenseExpiryDate() != null) && (!(pkg .getLicenseExpiryDate().equals(license_info.getLicenseExpiryDate())))) { ICPackage oldPackage = ICDDBHelper.getExistingPackageInfo(pkg.getCustomerId(), pkg.getIcType().toString()); pkg.setIcCount(oldPackage.getIcCount()); pkg.setIcImported(oldPackage.getIcImported()); pkg.setPackageName(oldPackage.getPackageName()); pkg.setPackageVersion(oldPackage.getPackageVersion()); pkg.setIcType(oldPackage.getIcType()); pkg.setImportStatus(oldPackage.getImportStatus()); log = ShadowWrapper.getString("ICF_Same_Version_Import"); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, null); pkg.savePackageInfo(); } continue; } } if (details.jobCancelled) { return; } filePath = retriveICPackageFile(settings, pkg, details); } catch (Exception e) { details.jobState = JobStatus.FAILED; log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_download_error"), pkg.getPackageName()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, e); continue; } try { log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_downloading"), pkg.getPackageName()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); if (details.jobCancelled) { return; } ICPackage.ImportStatus importStatus = importICPackage(pkg, filePath, false); log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_auto_import"), pkg.getPackageName(), importStatus.name()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); } catch (Exception e) { details.jobState = JobStatus.FAILED; log = MessageFormat.format(ShadowWrapper.getString("ICF_Polling_auto_import_error"), pkg.getPackageName(), e.getMessage()); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, e); } } if (details.jobState != JobStatus.FAILED) { details.jobState = JobStatus.SUCCESS; } } finally { if (details.jobCancelled) { return; } log = ShadowWrapper.getString("ICF_Polling_completed") + " - " + type; logMsg(details, log, JobStageConstants.PollingStages.COMPLETED, Priority.INFO_INT, null); JobRun.logJobCompletionStatus(details.jobId, details.jobRunId, ((details.jobState == JobStatus.SUCCESS) ? true : false)); details.context.setResult("Completed"); } } @Override public String testICStorageServerSettings(ICStorageServerSettings settings) throws Exception { String msg = ""; if (settings.getPollType() == 1 && settings.getPollFrequency() < 60) { msg = "Invalid IC Package Poll Frequency value. Minimum allowed value is 60."; logger.error(msg); return msg; } if (settings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY) { // For Standalone NCCM, send the request via Connectivity String tegHost = settings.getTegHost(); if (tegHost == null) { msg = "TEG HOST is not configured"; logger.error(msg); return msg; } String tegUrl = "http://" + tegHost + "/NccmCollector/ICDownloadServlet?"; logger.info("URL to TEG :" + tegUrl); // Sample URL - // http://172.21.136.202:8080/NccmCollector/ICDownloadServlet?GET_IC_PACK=FALSE tegUrl = tegUrl + "GET_IC_PACK=FALSE"; DefaultHttpClient httpclient = null; String request = new URL(getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/uas/testConnection") .toString(); request = request + "&&&"; request = request + settings.getUserId(); request = request + "&&&"; request = request + settings.getPassword(); logger.info("Request URL for package query : " + request); String responseStr = ""; try { httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); tegUrl = tegUrl.replaceAll(" ", "%20"); logger.info("Posting request to url: " + tegUrl); HttpPost httppost = new HttpPost(tegUrl); httppost.setEntity(new StringEntity(request, null, null)); httppost.setHeader("Content-type", "text/xml"); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); responseStr = ""; if (resEntity != null) { responseStr = EntityUtils.toString(resEntity); } logger.info("Response for Package query :" + responseStr); if (statusCode != 200) { msg = responseStr + ". HTTP Status:" + response.getStatusLine() + ". Response:" + responseStr; logger.error(msg); return msg; } else { msg = "Successfully connected to UAS"; } } catch (Exception e) { if (responseStr != null) { msg = responseStr + ". " + e.getMessage(); } else { msg = e.getMessage(); } logger.error(msg); return msg; } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return msg; } String responseStr = null; HttpClient client = null; try { client = new DefaultHttpClient(); getSecuredHttpClient(client); HttpGet request = new HttpGet( getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/uas/testConnection"); ((AbstractHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUserId(), settings.getPassword())); HttpResponse response = client.execute(request); HttpEntity resEntity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); responseStr = ""; if (resEntity != null) { responseStr = EntityUtils.toString(resEntity); } logger.info("Response for Package query :" + responseStr); if (statusCode != 200) { msg = responseStr + ". HTTP Status:" + response.getStatusLine() + ". Response:" + responseStr; logger.error(msg); return msg; } else { msg = "Successfully connected to UAS"; } } catch (Exception e) { if (responseStr != null) { msg = responseStr + ". " + e.getMessage(); } else { msg = e.getCause().getMessage(); } logger.error(msg); return msg; } finally { if (client != null) { try { client.getConnectionManager().shutdown(); } catch (Exception e) { } } } return msg; } private Collection<ICPackage> retriveICPackagesForCustomerInstance(String customer, String instance, ICStorageServerSettings settings) throws Exception { if (settings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY) { // For Standalone NCCM, send the request via Connectivity String tegHost = settings.getTegHost(); if (tegHost == null) { logger.error("TEG HOST is not configured.... "); return Collections.emptyList(); } String tegUrl = "http://" + tegHost + "/NccmCollector/ICDownloadServlet?"; logger.info("URL to TEG :" + tegUrl); // Sample URL - // http://172.21.136.202:8080/NccmCollector/ICDownloadServlet?GET_IC_PACK=FALSE tegUrl = tegUrl + "GET_IC_PACK=FALSE"; DefaultHttpClient httpclient = null; String request = new URL(getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/query/newPackages/" + customer + "/" + instance).toString(); request = request + "&&&"; request = request + settings.getUserId(); request = request + "&&&"; request = request + settings.getPassword(); logger.info("Request URL for package query : " + request); String responseStr = null; try { httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); tegUrl = tegUrl.replaceAll(" ", "%20"); logger.info("Posting request to url: " + tegUrl); HttpPost httppost = new HttpPost(tegUrl); httppost.setEntity(new StringEntity(request, null, null)); httppost.setHeader("Content-type", "text/xml"); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); responseStr = ""; if (resEntity != null) { responseStr = EntityUtils.toString(resEntity); } logger.info("Response for Package query :" + responseStr); if (statusCode != 200) { throw new Exception("HTTP Status:" + response.getStatusLine() + ". Response:" + responseStr); } } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return ICPackage.readPackages(responseStr); } String responseStr = null; HttpClient client = null; try { client = new DefaultHttpClient(); getSecuredHttpClient(client); HttpGet request = new HttpGet(getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/query/newPackages/" + customer + "/" + instance); ((AbstractHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUserId(), settings.getPassword())); HttpResponse response = client.execute(request); HttpEntity resEntity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); responseStr = ""; if (resEntity != null) { responseStr = EntityUtils.toString(resEntity); } logger.debug("Response for Package query :" + responseStr); if (statusCode != 200) { logger.error("HTTP Status:" + response.getStatusLine() + ". Response:" + responseStr); throw new Exception("HTTP Status:" + response.getStatusLine() + ". Response:" + responseStr); } } finally { if (client != null) { try { client.getConnectionManager().shutdown(); } catch (Exception e) { } } } return ICPackage.readPackages(responseStr); } // https certificate public DefaultHttpClient getSecuredHttpClient(HttpClient httpClient) throws Exception { final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {}; try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return _AcceptedIssuers; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } }; ctx.init(null, new TrustManager[] { tm }, new SecureRandom()); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = httpClient.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); return new DefaultHttpClient(ccm, httpClient.getParams()); } catch (Exception e) { throw e; } } private Collection<ICPackage> retriveICPackages(ICStorageServerSettings settings, PollJobDetails details) throws Exception { Collection<ICPackage> allPackages = new ArrayList<ICPackage>(); Collection<CustomerInstance> customers = getCustomers(); String log = "System has " + customers.size() + " customer instances"; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); for (CustomerInstance customerInstance : customers) { String customerName = getCustomerName(customerInstance.getCustomerId()); String instanceName = customerInstance.getInstanceName(); Collection<ICPackage> customerPackages; try { customerPackages = retriveICPackagesForCustomerInstance(customerName, instanceName, settings); } catch (Exception e) { log = "Error while retriving the packages for " + customerName + ":" + instanceName; // + " - "+ e.getLocalizedMessage(); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.WARN_INT, e); continue; } logger.info( "Retrived " + customerPackages.size() + " packages for " + customerName + ":" + instanceName); for (ICPackage pkg : customerPackages) { pkg.setCustomerId(customerInstance.getCustomerId()); pkg.setInstanceId(customerInstance.getInstanceId()); pkg.setInstance_name(customerInstance.getInstanceName()); pkg.setCustomerName(customerName); } allPackages.addAll(customerPackages); } return allPackages; } private String retriveICPackageFile(ICStorageServerSettings settings, ICPackage pkg, PollJobDetails details) throws Exception { String log = null; InputStream inStream = null; HttpClient httpclient = null; Customer customer = CustomerManager.getInstance().getCustomerById(pkg.getCustomerId()); String customerName = customer.getCustomerName(); if (settings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY) { // DefaultHttpClient httpclient = null; // For Standalone NCCM, send the request via Connectivity String tegHost = settings.getTegHost(); if (tegHost == null) { log = "TEG URL is not configured.... "; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); return null; } String tegUrl = "http://" + tegHost + "/NccmCollector/ICDownloadServlet?"; log = "URL to TEG : " + tegUrl; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); // Sample URL - // http://172.21.136.202:8090/NccmCollector/ICDownloadServlet?GET_IC_PACK=TRUE&PACK_ID=1234 tegUrl = tegUrl + "GET_IC_PACK=TRUE"; tegUrl = tegUrl + "&"; tegUrl = tegUrl + "PACK_ID" + "=" + pkg.getPackageId(); String request = new URL(getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/getFile/forPackage/" + pkg.getPackageId() + "/" + pkg.getPackageVersion() + "/" + customerName + "/" + pkg.getInstance_name()).toString(); request = request + "&&&"; request = request + settings.getUserId(); request = request + "&&&"; request = request + settings.getPassword(); log = "Request URL package download : " + request; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); try { httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); tegUrl = tegUrl.replaceAll(" ", "%20"); log = "Posting request to url: " + tegUrl; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); HttpPost httppost = new HttpPost(tegUrl); httppost.setEntity(new StringEntity(request, null, null)); httppost.setHeader("Content-type", "text/xml"); HttpResponse response = httpclient.execute(httppost); log = "Response from HTTP Client in retriveICPackageFile : " + response.toString(); logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.INFO_INT, null); inStream = response.getEntity().getContent(); } catch (Exception e) { log = "Error while posting request to TEG... "; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.ERROR_INT, e); } } else { try { httpclient = new DefaultHttpClient(); this.pasHttpRequestHandler.getSecuredHttpClient(httpclient); HttpGet request = new HttpGet(getFullUrl(settings.getServerHost()) + "/NetworkManagerWS/getFile/forPackage/" + pkg.getPackageId() + "/" + pkg.getPackageVersion() + "/" + customerName + "/" + pkg.getInstance_name()); ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUserId(), settings.getPassword())); HttpResponse response = httpclient.execute(request); inStream = response.getEntity().getContent(); } catch (Exception e) { log = "Error while posting request to retrieve package... "; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.ERROR_INT, e); } } try { if (inStream != null) { ReadableByteChannel rbc = Channels.newChannel(inStream); String filePath = ICF_UPLOAD_FOLDER + File.separatorChar + pkg.getPackageId(); File file = new File(filePath); if (!file.getParentFile().exists()) { // ensure parent folder exists file.getParentFile().mkdir(); } if (file.exists()) { file.delete(); } FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } finally { try { if (fos != null) { fos.close(); } } catch (Exception ignore) { log = "Error while closing FileOutputStream : "; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.ERROR_INT, ignore); } try { inStream.close(); } catch (Exception ignore) { log = "Error while closing inStream : "; logMsg(details, log, JobStageConstants.PollingStages.RUNNING, Priority.ERROR_INT, ignore); } } return filePath; } } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return null; } @Override public ICStorageServerSettings loadICStorageServerSettings() throws Exception { return ServerDBHelper.loadICStorageServerSettings(); } @Override public void saveICStorageServerSettings(ICStorageServerSettings icStorageSettings) throws Exception { ICStorageServerSettings currentSettings = loadICStorageServerSettings(); if (icStorageSettings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY && icStorageSettings.getConfigUid() == 0) { saveServerHost(icStorageSettings.getServerHost()); } else { ServerDBHelper.updateICStorageServerSettings(icStorageSettings); } if (currentSettings == null || currentSettings.getPollType() != icStorageSettings.getPollType() || (icStorageSettings.getPollType() != 0 && icStorageSettings.getPollFrequency() != currentSettings.getPollFrequency()) || icStorageSettings.getConfigUid() != currentSettings.getConfigUid()) { updateICRetrievalFrequency(icStorageSettings); } } @Override public ICCServerSettings loadICCServerSettings() throws Exception { return ServerDBHelper.loadICCServerSettings(); } @Override public void saveICCServerSettings(ICCServerSettings iccServerSettings) throws Exception { ICCServerSettings currentSettings = loadICCServerSettings(); if (currentSettings == null || !currentSettings.equals(iccServerSettings)) { ServerDBHelper.updateICCServerSettings(iccServerSettings); } else { logger.debug("No change in icc server setting"); } } @Override public String testICCServerSettings(ICCServerSettings iccServerSettings) { try { ICNuggetServiceProxy nuggetServiceProxy = new ICNuggetServiceProxy(iccServerSettings.getServiceUrl()); ICNuggetService_PortType nuggetservicePortType = nuggetServiceProxy.getICNuggetService_PortType(); ICNuggetServiceImplPortBindingStub nuggetStub = (ICNuggetServiceImplPortBindingStub) nuggetservicePortType; nuggetStub.setUsername(iccServerSettings.getUserId()); nuggetStub.setPassword(iccServerSettings.getPassword()); nuggetStub.getICTypes(); return "Connection Successful"; } catch (ICException e) { logger.error("Error while creating nugget - " + e.getFaultString()); return "Connection Failed. " + e.getFaultString(); } catch (Exception e) { logger.error("Error while creating nugget - " + e.getMessage()); return "Connection Failed. "; } } @Override public ICPackage.ImportStatus importICPackage(int customerId, int instanceId, String filePath, boolean forceImport) throws Exception { ICPackage pkg = new ICPackage(); pkg.setPackageId(-1); pkg.setCustomerId(customerId); pkg.setInstanceId(instanceId); pkg.setSource(ICSource.ICD_MANUAL_IMPORT); return importICPackage(pkg, filePath, forceImport); } private ICPackage.ImportStatus importICPackage(ICPackage pkg, String filePath, boolean forceImport) throws Exception { File file = new File(filePath); if (!file.exists()) { // log return ICPackage.ImportStatus.IMPORT_FAILED; } try { return ICPackage.importICPackage(pkg, file, forceImport); } catch (Exception e) { // IC Package log is already updated. No need to update it here again logger.warn("Error while importing the IC Package " + pkg.getPackageName(), e); throw new Exception(e.getLocalizedMessage()); } finally { try { file.delete(); } catch (Exception ignore) { } } } private class ICPackageRetrievalTask extends TimerTask { int uid = -1; ICPackageRetrievalTask(int uid) { this.uid = uid; } @Override public void run() { long startTime = System.currentTimeMillis(); String pollingType = ShadowWrapper.getString("ICF_Polling_Automatic"); logger.info("Automatic IC retrieval initiated "); // IC-Polling try { JobMgr.getInstance().scheduleICPollingJob(pollingType, uid); } catch (Exception e) { logger.warn("Error while polling the IC Packages - " + e.getMessage(), e); } // PAS Polling try { logger.info(">> PAS Rule Package autopolling begins...."); PasImportDetails pasImport = new PasImportDetails(); pasImport.setAutoPolling(true); Date d = new Date(); pasImport.setJobName("pas_rule_polling_" + d.getTime()); UpgradeIfImpl.getInstance().importPasPackge(pasImport, uid); } catch (Exception e) { logger.warn("Error while polling the PAS Packages - " + e.getMessage(), e); } logger.info("Automatic IC/PAS retrieval completed in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds"); } } @Override public void discardICPackage(ICPackage pkg) { ImportStatus status = ImportStatus.IMPORT_DISCARDED; String log = MessageFormat.format(ShadowWrapper.getString("ICF_Discard_Option"), pkg.getPackageName()); logger.debug(log); pkg.setImportStatus(status); ICDDBHelper.saveICPackageLogs(pkg); ICDLogger.saveICImportLog(pkg.getPackageLogId(), "", "", pkg.getIcType().toString(), "Package is Discarded", "Import Discarded"); } @Override public void importRequested(ICPackage pkg) { ImportStatus status = ImportStatus.IMPORT_FAILED; String filePath = null; logger.debug("Import option is selected for package: " + pkg.getPackageName()); ICStorageServerSettings settings = ServerDBHelper.loadICStorageServerSettings(); if (settings == null) { // Log in the Import log about the missing settings. ICDLogger.saveICImportLog(pkg.getPackageLogId(), "", "", "", "IC Retrieval settigs does not exists in database. IC polling will be disabled", "Import Failed"); pkg.setImportStatus(status); pkg.savePackageInfo(); return; } try { filePath = retriveICPackageFile(settings, pkg, null); ICPackage.importICPackage(pkg, new File(filePath), true); // if (filePath != null) // { // try // { // ICPackage oldPkg = ICDDBHelper.getExistingPackageInfo(pkg.getCustomerId(), pkg.getIcType() // .toString()); // if (oldPkg != null) // { // List<String> result; // result = SwimICImporter.getInstance().deleteAction(oldPkg, true); // if (!(result.isEmpty())) // { // // oldPkg.setImportStatus(ImportStatus.PARTIALLY_DELETED); // oldPkg.savePackageInfo(); // pkg.setImportStatus(status); // pkg.savePackageInfo(); // return; // // } // // oldPkg.setImportStatus(ImportStatus.PACKAGE_OBSOLETE); // oldPkg.savePackageInfo(); // } // ICPackage.ImportStatus importStatus = importICPackage(pkg, filePath, false); // pkg.setImportStatus(importStatus); // // } // catch (Exception e) // { // pkg.setImportStatus(status); // pkg.savePackageInfo(); // ICDLogger.saveICImportLog(pkg.getPackageLogId(), "", "", "", // "Error in retrieving the filePath for given package ", "Import Failed"); // } // } } catch (Exception e) { pkg.setImportStatus(status); pkg.savePackageInfo(); ICDLogger.saveICImportLog(pkg.getPackageLogId(), "", "", "", "Error in retrieving the given package " + e.getMessage(), "Import Failed"); } } public String updateCustomerDetails() throws Exception { ICStorageServerSettings settings = ServerDBHelper.loadICStorageServerSettings(); List<CustomerInstance> customers = new ArrayList(getCustomers()); String customerName = null; String responseStr = ""; BufferedReader input = null; if (settings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY) { // For Standalone NCCM, send the request via Connectivity String tegHost = settings.getTegHost(); if (tegHost == null) { logger.warn("TEG HOST is not configured.... "); } if (customers.size() > 0) { StringBuffer sb = new StringBuffer( "http://" + tegHost + "/NccmCollector/NCCMRegistration?size=" + customers.size()); for (int i = 1; i <= customers.size(); i++) { CustomerInstance customerInstance = customers.get(i - 1); customerName = getCustomerName(customerInstance.getCustomerId()); sb.append("customer" + i).append(customerName); } String request = sb.toString(); logger.info("URL to TEG :" + request); try { URL url = new URL(request); URLConnection connection = url.openConnection(); input = new BufferedReader(new InputStreamReader(connection.getInputStream())); responseStr = input.readLine(); if ("error".contains(responseStr)) { logger.warn("Error updating customer details to TEG"); return null; } } catch (Exception e) { logger.warn("Exception in updating customer details to TEG"); e.printStackTrace(); } finally { if (input != null) { input.close(); } } } } return responseStr; } public String getServerHost() { ICStorageServerSettings settings = ServerDBHelper.loadICStorageServerSettings(); String serverHost = ""; BufferedReader input = null; if (settings.getConnectivityType() == ConnectivityTypeEnum.CONNECTIVITY) { // For Standalone NCCM, send the request via Connectivity String tegHost = settings.getTegHost(); if (tegHost == null) { logger.warn("TEG HOST is not configured.... "); return null; } try { URL url = new URL("http://" + tegHost + "/NccmCollector/GetUASAddress"); URLConnection connection = url.openConnection(); input = new BufferedReader(new InputStreamReader(connection.getInputStream())); serverHost = input.readLine(); if ("fail".equals(serverHost)) { logger.warn("Error retrieving UAS server host from TEG"); return null; } input.close(); } catch (Exception e) { logger.warn("Exception retrieving UAS server host from TEG"); e.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } } } return serverHost; } private void saveServerHost(String serverHost) { try { ServerDBHelper.updateServerHost(serverHost); } catch (Exception e) { e.printStackTrace(); logger.error("Error in updating the server Host", e); } } /** * This method is to form url with protocol * * @param str * @return String */ public static String getFullUrl(String str) { if (str != null && !str.toLowerCase().startsWith("http")) { str = "https://" + str; } return str; } public String validateAndPrepareIcPushDetails(ICPushDetails icPushDetails) throws Exception { String validation = ""; List<String> list = new ArrayList<String>(); if (icPushDetails.getTitle() == null) { list.add("IC Title"); } if (icPushDetails.getIcType() == null) { list.add("IC Type"); } if (icPushDetails.getCreator() == null) { list.add("IC Creator"); } if (icPushDetails.getRuleXml() == null)// handle zip also { list.add("Rule Content"); } if (icPushDetails.getDescription() == null) { icPushDetails.setDescription(icPushDetails.getTitle());// description - title } if (icPushDetails.getIcContentSummary() == null) { icPushDetails.setIcContentSummary("Summary - " + icPushDetails.getTitle());// summary - title } if (icPushDetails.getOwner() == null) { icPushDetails.setOwner(icPushDetails.getCreator()); } if (icPushDetails.getVersion() == null) { icPushDetails.setVersion("1.0.0"); } if (icPushDetails.getIcStatus() == null) { icPushDetails.setIcStatus("Published"); } if (icPushDetails.getGroup() == null) { icPushDetails.setGroup("Proactive and Premptive (NCCM Capabilities)"); } if (icPushDetails.getTrustLevel() == null) { icPushDetails.setTrustLevel("Bronze"); } if (!list.isEmpty()) { validation = StringUtils.join(list, ", ") + " cannot be empty"; } return validation; } @Override public String icPush(ICPushDetails icPushDetails) throws Exception { String message = ""; try { message = validateAndPrepareIcPushDetails(icPushDetails); if (message != null && !message.isEmpty()) { throw new Exception(message.trim()); } ICCServerSettings iccServerSettings = loadICCServerSettings(); if (iccServerSettings == null) { throw new Exception("_API_ICC_SERVER_SETTINGS_NOT_CONFIGURED"); } ICNuggetServiceProxy nuggetServiceProxy = new ICNuggetServiceProxy(iccServerSettings.getServiceUrl()); ICNuggetService_PortType nuggetservicePortType = nuggetServiceProxy.getICNuggetService_PortType(); ICNuggetServiceImplPortBindingStub nuggetStub = (ICNuggetServiceImplPortBindingStub) nuggetservicePortType; nuggetStub.setUsername(iccServerSettings.getUserId()); nuggetStub.setPassword(iccServerSettings.getPassword()); try { nuggetStub.getICTypes(); } catch (ICException e) { logger.error("Error while creating nugget - " + e.getFaultString()); throw new Exception("_API_ICC_SERVER_SETTINGS_CONNECTION_FAILED:" + " "); } catch (Exception e) { logger.error("Error while creating nugget - " + e.getMessage()); throw new Exception("_API_ICC_SERVER_SETTINGS_CONNECTION_FAILED:" + " "); } IcVersionDocument document = new IcVersionDocument(); ICContent tempICContent = new ICContent(); document.setTitle(icPushDetails.getTitle()); tempICContent.setSummary(icPushDetails.getIcContentSummary()); tempICContent.setDescription(icPushDetails.getDescription()); document.setIcContent(tempICContent); document.setVersionLabel(icPushDetails.getVersion()); IcMetadata tempMetadata = new IcMetadata(); tempMetadata.setIcCreator(icPushDetails.getCreator()); tempMetadata.setIcOwner(icPushDetails.getOwner()); tempMetadata.setIcType(icPushDetails.getIcType()); tempMetadata.setIcStatus(icPushDetails.getIcStatus()); List<MetaDataField> tempFields = new ArrayList<MetaDataField>(); MetaDataField newField = new MetaDataField(); newField = new MetaDataField(); newField.setName("Group"); newField.setValue(icPushDetails.getGroup()); tempFields.add(newField); MetaDataField newField2 = new MetaDataField(); newField2.setName("IC Trust Level"); newField2.setValue(icPushDetails.getTrustLevel()); tempFields.add(newField2); MetaDataField[] array = tempFields.toArray(new MetaDataField[tempFields.size()]); tempMetadata.setMetaDataFields(array); document.setMetadata(tempMetadata); Long nuggetId = null; IcVersionDocumentAndRuleVersionDocument icVersionDocumentAndRuleVersionDocument = new IcVersionDocumentAndRuleVersionDocument(); icVersionDocumentAndRuleVersionDocument.setIcVersionDocument(document); icVersionDocumentAndRuleVersionDocument.setRuleXmlContent(icPushDetails.getRuleXml()); try { nuggetId = nuggetStub.createICAndRule(icVersionDocumentAndRuleVersionDocument); message = "success"; logger.info("Created Nugget with ID " + nuggetId); } catch (ICException e) { message = "Error while pushing " + icPushDetails.getTitle() + " IC Into ICC. " + e.getFaultString(); logger.error("Error while creating nugget" + e.getMessage()); StringBuilder messageBuilder = new StringBuilder(" "); messageBuilder.append(icPushDetails.getTitle()).append(": ").append(e.getFaultString()); throw new Exception(messageBuilder.toString()); } } catch (Exception e) { logger.error(e.getMessage()); message = "Error while pushing " + icPushDetails.getTitle() + " IC Into ICC. " + e.getMessage(); throw e; } return message; } }