Made the code tighter

Updated 4 files
This commit is contained in:
Keith Armstrong 2025-01-07 15:50:28 -05:00
parent 1c86d5a944
commit d5ed7a9f9e
4 changed files with 59 additions and 53 deletions

View File

@ -6,15 +6,19 @@ import java.util.Properties;
public class propertyProcessor
{
public static String getProperty(File MyFile, String varProperty) throws Exception
public static String getProperty(File MyFile, String varProperty, String thisDefault) throws Exception
{
Properties props = new Properties();
String propertyValue = "";
String propertyValue = null;
try
{
props.load(new FileInputStream(MyFile));
propertyValue = props.getProperty(varProperty);
if (propertyValue == null)
{
propertyValue = thisDefault;
}
}
catch (Exception e)
{

View File

@ -4,19 +4,17 @@ import java.io.Console;
import java.util.*;
public class userCreds {
public static String getUsername() {
public static String getUsername(String thisPrompt) {
Scanner myObj = new Scanner(System.in);
String varPrompt = "Please enter your username : ";
System.out.print(varPrompt);
System.out.print(thisPrompt);
String varEntry;
varEntry = myObj.nextLine();
return varEntry;
}
public static char[] getPassword() {
String varPrompt = "Please enter your password : ";
public static char[] getPassword(String thisPrompt) {
Console console = System.console();
char[] password = console.readPassword(varPrompt);
char[] password = console.readPassword(thisPrompt);
return password;
}
}

View File

@ -11,15 +11,13 @@ import java.util.*;
public class userVerify
{
public static void main (String[] args) {
public static void main (String[] args) throws IOException
{
System.out.println();
Scanner myObj = new Scanner(System.in); // Create a Scanner object
debugProcessor.writer(false, "");
debugProcessor.writer(true, "SOAP Client (06.01.2025 build) - Belkast Consulting (c) 2025");
debugProcessor.writer(true, "");
String varPrompt = "Please enter the name of the CSV file to validate : ";
String varFileName;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
while (true)
{
System.out.print(varPrompt);

View File

@ -3,7 +3,6 @@ package com.belkast.soap;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.*;
import javax.crypto.BadPaddingException;
import java.io.File;
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
@ -19,21 +18,18 @@ public class webService {
public static void main(String... argv) throws Exception
{
debugProcessor.writer(true, "");
debugProcessor.writer(true, "SOAP Client (06.01.2025 build) - Belkast Consulting (c) 2025");
debugProcessor.writer(true, "");
System.out.println();
// @Parameter(names = "--props",
// description = "Location of the Properties file")
// @Parameter(names = "--props",
// description = "Location of the Properties file")
// @Parameter(names = "--key",
// description = "Encryption Key (length of 16)")
// @Parameter(names = "--key",
// description = "Encryption Key (length of 16)")
// @Parameter(names = "--encrypt",
// description = "Encrypt this value using the Key")
// @Parameter(names = "--encrypt",
// description = "Encrypt this value using the Key")
// @Parameter(names = "--debug",
// description = "Write details to the screen")
// @Parameter(names = "--debug",
// description = "Write details to the screen")
jCommanderArgs args = new jCommanderArgs();
JCommander jct = new JCommander(args);
@ -41,12 +37,11 @@ public class webService {
try {
jct.parse(argv);
} catch (ParameterException ex) {
System.out.println();
jct.usage();
System.exit(0);
}
varDebug = Boolean.valueOf(args.debug);
varDebug = args.debug;
varKey = args.key;
String varToEncrypt = args.encrypt;
@ -93,17 +88,17 @@ public class webService {
String varPassword = "";
debugProcessor.writer(false, "Debug is set to : " + varDebug);
String varURL = propertyProcessor.getProperty(thisProps, "SHIM_URL");
String varURL = propertyProcessor.getProperty(thisProps, "SHIM_URL", null);
String varKeystore_LOC = propertyProcessor.getProperty(thisProps, "JAVA_KS_LOCATION", null);
String varInputFile = propertyProcessor.getProperty(thisProps, "XML_FILE", null);
String varRoleFile = propertyProcessor.getProperty(thisProps, "CSV_FILE",null);
String varKeystore_PW = propertyProcessor.getProperty(thisProps, "JAVA_KS_PASSWORD", null);
String varUseSSL = propertyProcessor.getProperty(thisProps, "SSL");
String varKeystore_LOC = propertyProcessor.getProperty(thisProps, "JAVA_KS_LOCATION");
String varKeystore_PW = propertyProcessor.getProperty(thisProps, "JAVA_KS_PASSWORD");
String varPropAuthRequired = propertyProcessor.getProperty(thisProps, "AUTH_REQUIRED", "true");
String varInputFile = propertyProcessor.getProperty(thisProps, "XML_FILE");
String varPropUseSSL = propertyProcessor.getProperty(thisProps, "SSL", "true");
String varRoleFile = propertyProcessor.getProperty(thisProps, "CSV_FILE");
String varPropEmptyColumns = propertyProcessor.getProperty(thisProps, "CSV_ALLOW_EMPTY_COLUMN_VALUES");
Boolean varEmptyColumns = Boolean.valueOf(varPropEmptyColumns);
String varPropEmptyColumns = propertyProcessor.getProperty(thisProps, "CSV_ALLOW_EMPTY_COLUMN_VALUES", "false");
try {
File checkInput = new File(varInputFile);
@ -113,6 +108,11 @@ public class webService {
System.exit(0);
}
if (varURL.isEmpty())
{
throw new RuntimeException("SHIM_URL cannot be empty");
}
File checkRole = new File(varRoleFile);
if (!checkRole.exists()) {
debugProcessor.writer(true, thisProps + " => The file specified in CSV_FILE does not exist [ " + varRoleFile + " ]");
@ -122,23 +122,26 @@ public class webService {
}
catch (Exception e)
{
debugProcessor.writer(true, "Please check that the properties file " + thisProps + " is correctly formatted!");
debugProcessor.writer(true, thisProps + " => The properties file is missing a required value!");
debugProcessor.writer(true, thisProps + " => Required values : SHIM_URL, XML_FILE, CSV_FILE");
System.out.println();
System.exit(0);
}
String varAuthProp = propertyProcessor.getProperty(thisProps, "AUTH_REQUIRED");
Boolean varAuthRequired = Boolean.valueOf(varAuthProp);
String varUsername = propertyProcessor.getProperty(thisProps, "USERNAME");
Boolean varUseSSL = Boolean.valueOf(varPropUseSSL);
Boolean varAuthRequired = Boolean.valueOf(varPropAuthRequired);
Boolean varEmptyColumns = Boolean.valueOf(varPropEmptyColumns);
String varUsername = propertyProcessor.getProperty(thisProps, "USERNAME", null);
if (varAuthRequired && varUsername == null)
{
varUsername = userCreds.getUsername();
varUsername = userCreds.getUsername("Please enter your username : ");
}
String varPasswordTemp = propertyProcessor.getProperty(thisProps, "PASSWORD");
String varPasswordTemp = propertyProcessor.getProperty(thisProps, "PASSWORD", null);
if (varAuthRequired && varPasswordTemp == null)
{
char[] varUserPW = userCreds.getPassword();
char[] varUserPW = userCreds.getPassword("Please enter your password : ");
varPasswordTemp = salter.encrypt(String.copyValueOf(varUserPW), varKey, varSalt);
}
@ -154,13 +157,13 @@ public class webService {
}
debugProcessor.writer(varDebug, "");
debugProcessor.writer(varDebug, thisProps + " => SOAP URL : " + varURL);
debugProcessor.writer(varDebug, thisProps + " => SOAP URL : " + (varURL == null ? "" : varURL));
debugProcessor.writer(varDebug, thisProps + " => Authentication required : " + varAuthRequired);
debugProcessor.writer(varDebug, thisProps + " => Use SSL : " + varUseSSL);
debugProcessor.writer(varDebug, thisProps + " => JAVA keystore : " + varKeystore_LOC);
debugProcessor.writer(varDebug, thisProps + " => JAVA keystore password : " + varKeystore_PW);
debugProcessor.writer(varDebug, thisProps + " => XML file : " + varInputFile);
debugProcessor.writer(varDebug, thisProps + " => CSV file : " + varRoleFile);
debugProcessor.writer(varDebug, thisProps + " => JAVA keystore location : " + (varKeystore_LOC == null ? "" : varKeystore_LOC));
debugProcessor.writer(varDebug, thisProps + " => JAVA keystore password : " + (varKeystore_PW == null ? "" : varKeystore_PW));
debugProcessor.writer(varDebug, thisProps + " => XML file : " + (varInputFile == null ? "" : varInputFile));
debugProcessor.writer(varDebug, thisProps + " => CSV file : " + (varRoleFile == null ? "" : varRoleFile));
debugProcessor.writer(varDebug, thisProps + " => CSV allow empty : " + varEmptyColumns);
if (!varAuthRequired)
@ -188,7 +191,7 @@ public class webService {
while (mapper1.hasNext()) {
varCount++;
debugProcessor.writer(true, "");
debugProcessor.writer(true, "processing record " + varCount);
debugProcessor.writer(true, "[" + varCount + " of " + thisCSV.size() + "] : processing started");
Map hashMap = mapper1.next();
debugProcessor.writer(varDebug, "record " + varCount + " : " + hashMap.values().toString());
@ -205,18 +208,21 @@ public class webService {
if (varDocument != null && !varDocument.isEmpty())
{
String varCredentials = soapProcessor.getCredentials(varUsername, varPassword);
Boolean isSSL = Boolean.parseBoolean(varUseSSL);
debugProcessor.writer(varDebug, "connecting to URL : " + varURL);
HttpURLConnection varConn = soapProcessor.getConnection(varCredentials, varURL, isSSL, varKeystore_LOC, varKeystore_PW);
HttpURLConnection varConn = soapProcessor.getConnection(varCredentials, varURL, varUseSSL, varKeystore_LOC, varKeystore_PW);
debugProcessor.writer(true, "[" + varCount + " of " + thisCSV.size() + "] : opened connection");
debugProcessor.writer(varDebug, "sending XML document : " + varDocument);
debugProcessor.writer(varDebug, "sending document : " + varDocument);
String response = soapProcessor.send(varConn, varInputFile, hashMap, varDocument.getBytes());
debugProcessor.writer(true, "[" + varCount + " of " + thisCSV.size() + "] : XML document sent");
debugProcessor.writer(varDebug, "response received : " + response);
if (varConn != null)
{
soapProcessor.closeConnection(varConn);
debugProcessor.writer(true, "[" + varCount + " of " + thisCSV.size() + "] : closed connection");
}
}
}