Create config file at runtime in Java exported WAR - java

I've made a web project with eclipse to run my webservice on Tomcat v7. I have a config file that have to define some variables that should be changed while webserver is up without need of restart it. The problem is: where I can put this file?
At the moment I've put it into the "./" dir, but seems that when I export the WAR file and install it on the webserver it doesn't works. Is there a way to create this file and modify it runtime inside the WAR?
Here is part of the code of the .class file that accesses the config file
public class ToolConfigurations {
private static final Logger log = LogManager.getLogger(ToolConfigurations.class); //Oggetto logging
private static ToolConfigurations instance = null; //verificarne la necessità
private static String defaultServerName = null;
private static String defaultDbName = "postgres";
private static String defaultDbUser = "postgres";
private static String defaultDbPass = "password";
private static int defaultDbMaxConnNum = 10;
private static String configFilePath = ".\\";
private static String configFileName = "tool.properties";
private String serverName = null;
private String dbName = null;
private String dbUser = null;
private String d`enter code here`bPass = null;
private int dbMaxConnNum = -1;
private ToolConfigurations() throws Exception {
File file = new File(configFilePath + configFileName);
if(file.exists()) {
//file configurazione esiste
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String line = null;
while((line = br.readLine()) != null) { //Leggo riga per riga il file
String[] values = line.split(" ");
switch (values[0]) {
case "serverName":
serverName = values[1];
break;
case "dbName":
dbName = values[1];
break;
case "dbUser":
dbUser = values[1];
break;
case "dbPass":
dbPass = values[1];
break;
case "dbMaxConnNum":
dbMaxConnNum = Integer.parseInt(values[1]);
break;
default:
log.warn("Elemento inaspettato nel file di configurazione: " + values[0]);
break;
}
}
br.close();
in.close();
}else {
if(file.createNewFile() == false) {
//Errore creazione file
log.error("Errore creazione file di configurazione");
throw new Exception("Errore creazione file configurazione");
}
//CREO FILE CONFIGURAZIONE CON IMPOSTAZIONI DI DEFAULT
FileWriter fw = new FileWriter(file);
fw.write("dbName " + defaultDbName + "\r\n");
fw.write("dbUser " + defaultDbUser + "\r\n");
fw.write("dbPass " + defaultDbPass + "\r\n");
fw.write("dbMaxConnNum " + defaultDbMaxConnNum + "\r\n");
fw.flush();
fw.close();
log.warn("File di configurazione non trovato. Path: " + file.getAbsolutePath() + ". Creato nuovo file con configurazioni di default.");
//CARICO IMPOSTAZIONI DI DEFAULT
dbName = defaultDbName;
dbUser = defaultDbUser;
dbPass = defaultDbPass;
dbMaxConnNum = defaultDbMaxConnNum;
}
}
public static synchronized ToolConfigurations getInstance() throws Exception {
if(instance == null) {
instance = new ToolConfigurations();
}
return instance;
}
...

In web server, files other than .class are directly taken in charge by the server. You don't need to reload the server.
But try using .properties files and check the structure of the WAR when deployed on the server.

The best solution I found was to a ClassLoader to search for the config file that I have previously created into the main directory.
The example of use is:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream fileInput = classLoader.getResourceAsStream(configFileName);
if(fileInput == null){
//File not found
}else{
//File found
InputStreamReader sr = new InputStreamReader(fileInput);
BufferedReader br = new BufferedReader(sr);
try {
while((line = br.readLine()) != null) {
//Read file line by line
}
}catch (IOException e) {
throw new IOException("Error parsing file: " + e.getMessage());
}
}

Related

Unit testing on local file throws java.io.FileNotFoundException in macbook

response =
File("src\\test\\java\\com\\resources\\products\\response.json").bufferedReader()
.use { it.readText() }
I have this line of code for mocking response and it is working fine on pc but throws not found on macbook? Any solutions? Thanks.
You can create assets directory in unit test dir(java unit test), and put your response.json into it. and then you can read this file with these code below:
private static File readAssetsFile(String fileName) {
// create file for assets file
final String dir = System.getProperty("user.dir");
File assetsDir = new File(dir, File.separator + "src/test/assets/");
return new File(assetsDir, fileName);
}
private static String streamToString(InputStream inputStream)
throws IOException {
if (inputStream == null) {
return "";
}
StringBuilder sBuilder = new StringBuilder();
String line;
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
while ((line = bufferedReader.readLine()) != null) {
sBuilder.append(line).append("\n");
}
return sBuilder.toString();
}
// YOUR TESTCASE
#Test
public void testReadResponseJson() throws IOException {
File respFile = readAssetsFile("response.json");
String respText = streamToString(new FileInputStream(respFile));
assertEquals("{}", respText);
}
I have solved with:
val home = System.getProperty("user.home")
val file = File(home + File.separator + "Desktop" + File.separator + "response.json")
Desktop directory for the sample. I will give the absolute path step by step.

Simplest way to read a file using jcifs

I am trying to read a file from a network share using the external jcifs library. Most sample codes I can find for reading files are quite complex, potentially unnecessarily so. I have found a simple way to write to a file as seen below. Is there a way to read a file using similar syntax?
SmbFile file= null;
try {
String url = "smb://"+serverAddress+"/"+sharename+"/TEST.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
file = new SmbFile(url, auth);
SmbFileOutputStream out= new SmbFileOutputStream(file);
out.write("test string".getBytes());
out.flush();
out.close();
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "ERROR: "+e);
}
SmbFile file = null;
byte[] buffer = new byte[1024];
try {
String url = "smb://"+serverAddress+"/"+sharename+"/TEST.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
file = new SmbFile(url, auth);
try (SmbFileInputStream in = new SmbFileInputStream(file)) {
int bytesRead = 0;
do {
bytesRead = in.read(buffer)
// here you have "bytesRead" in buffer array
}
while (bytesRead > 0);
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "ERROR: "+e);
}
or even better, assuming that you're working with text files - using BufferedReader from Java SDK:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(file)))) {
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
}
}
And write with:
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new SmbFileOutputStream(file)))) {
String toWrite = "xxxxx";
writer.write(toWrite, 0, toWrite.length());
}
try {
String url = "smb://" + serverAddress + "/" + sharename + "/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(DOMAIN, USER_NAME, PASSWORD);
String fileContent = IOUtils.toString(new SmbFileInputStream(new SmbFile(url, auth)), StandardCharsets.UTF_8.name());
System.out.println(fileContent);
} catch (Exception e) {
System.err.println("ERROR: " + e.getMessage());
}
I could read some pdf files using this:
private final Singleton<CIFSContext> contextoDdetran = new Singleton<>() {
#Override
public CIFSContext inicializar() {
NtlmPasswordAuthenticator autenticador = new NtlmPasswordAuthenticator(smbDomain, smbUser, smbPassword);
return SingletonContext.getInstance().withCredentials(autenticador);
}
};
public byte[] readSmbFile(String fileName) {
try {
SmbFile file = new SmbFile(fileName, this.contextoDdetran.get());
return file.getInputStream().readAllBytes();
} catch(Exception e) {
final String msgErro = String.format("Error reading file '%s': %s", fileName, e.getMessage());
logger.error(msgErro, e);
throw new IllegalStateException(msgErro);
}
}

how to create a powershell remote session from inside a Java program?

I have the following code to connect to a remote machine and execute commands.It is working if I create a new session for each Invoke-Command call to the remote machine. I don't want to create a new session each time I use Invoke-Command as that will not scale for thousands of commands on hundreds of machines concurrently and session creation itself is a big overhead. I need a way so that I can reuse the same session object in the $session powershell variable for multiple Invoke-Command calls to the remote machines.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class PowerShellSession {
private static String subModule = "PowerShellSession";
String targetIpAddress;
String username;
String password;
public static Object connectPShellLock = new Object();
public PowerShellSession() {}
public void exec(String cmd, String credentials) {
String ex = "Invoke-Command -Session $session -ScriptBlock {" + cmd + "} -Computer " + targetIpAddress;
String[] args = new String[] { "powershell", ex};
try {
execRemote(args);
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String command = "Exit-PSSession";
String[] args = new String[] { "powershell", command};
try {
execRemote(args);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getCredentials(String domain, String userName,
String password) throws IOException {
String creds = "$PlainPassword ='" + password
+ "'; $SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force;"
+ "$mycred = new-object -typename System.Management.Automation.PSCredential('" + userName + "', $SecurePassword);";
creds += "$session = New-PSSession -ComputerName " + domain + " -Credential $mycred;";
String[] args = new String[] { "powershell", creds};
execRemote(args);
return creds;
}
private void execRemote(String[] arguments) throws IOException {
ProcessBuilder builder = new ProcessBuilder(arguments);
builder.redirectErrorStream(true);
Process process = builder.start();
doProcessIO(process);
}
// Do the IO for a passed process
private void doProcessIO(Process p) throws IOException {
p.getOutputStream().close();
String line;
System.out.println("Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
System.out.println("Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println(line);
}
stderr.close();
// System.out.println("Done");
}
public static void main(String[] args) throws IOException {
PowerShellSession psSession = new PowerShellSession();
String credentials = psSession.getCredentials("9.120.241.195", "username", "password");
psSession.targetIpAddress = "9.120.241.195";
if(!credentials.equals("")) {
Scanner input = new Scanner(System.in);
while(true) {
System.out.print("PS C:\\Windows\\system32> ");
String cmd = input.nextLine();
if(cmd.equals("q") || cmd.equals("e") || cmd.equals("quit") || cmd.equals("exit")) break;
psSession.username = "username";
psSession.password = "password";
psSession.exec(cmd, "");
}
System.out.println("Finished PowerShell remote session.");
input.close();
}
psSession.close();
}
}
See there are lot of logics involves in this which can help you.
Your session invoking is fine; But you cannot directly run a PS command like that. You have to invoke the powershell.exe first then you have to give the respective remote commands what you want to execute.
Finally you have execute the command you will prepare. Let me share you a sample code:
public String executeScript(String psFileName, Systems system) throws NMAException {
Runtime runtime = Runtime.getRuntime();
String filePath = ApplicationProperties.getPropertyValue("powershell.scripts.location");
String command;
switch (psFileName) {
case "TerminalServersSystemInfo.ps1":
command = POWERSHELL + filePath + psFileName + " " + system.getPassword() + " " + system.getUserName()
+ " " + system.getSystemName();
break;
case "SQLServerInfo.ps1":
command = POWERSHELL + filePath + psFileName + " " + system.getSystemName() + " "
+ system.getUserName() + " " + system.getPassword();
break;
case "MyPS.ps1":
{
command = POWERSHELL + filePath + psFileName + " " + system.getSystemName() + " "
+ system.getUserName()
+ " " + system.getPassword() + " " + system.getDatabaseName();
break;
}
default:
throw new NMAException("not available");
}
Here is how you should form the command object in Java and then you should execute this:
powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command {Invoke-command ......}
For triggering a PS file you can use the -Filepath switch.
Next this will help you in executing that:
proc = runtime.exec(command);
proc.getOutputStream().close();
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
proc.getOutputStream().close();
LOGGER.info("Command: " + command);
LOGGER.info("Result:" + sb.toString());
return sb.toString();
Hope it gives you a set off.
public class PowerShellSession {
private static String subModule = "PowerShellSession";
String targetIpAddress;
String username;
String password;
public static Object connectPShellLock = new Object();
public PowerShellSession() {}
public void exec(String cmd, String credentials) {
String ex = credentials +" Invoke-Command -ScriptBlock {" + cmd + "} -ComputerName " + targetIpAddress +" -Credential $mycred";
String[] args = new String[] { "powershell", ex};
try {
execRemote(args);
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String command = "Exit-PSSession";
String[] args = new String[] { "powershell", command};
try {
execRemote(args);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getCredentials(String domain, String userName,
String password) throws IOException {
String creds = "$Username = '"+userName+"';$PlainPassword ='" + password
+ "'; $SecurePassword = ConvertTo-SecureString -AsPlainText $PlainPassword -Force;"
+ "$mycred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword;";
//creds += "$session = New-PSSession -ComputerName " + domain + " -Credential $mycred;";
String[] args = new String[] { "powershell", creds};
execRemote(args);
return creds;
}
private void execRemote(String[] arguments) throws IOException {
ProcessBuilder builder = new ProcessBuilder(arguments);
builder.redirectErrorStream(true);
Process process = builder.start();
doProcessIO(process);
}
// Do the IO for a passed process
private void doProcessIO(Process p) throws IOException {
p.getOutputStream().close();
String line;
System.out.println("Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
System.out.println("Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println(line);
}
stderr.close();
System.out.println("Done");
}
public static void main(String[] args) throws IOException {
PropertiesFileReader propReader = new PropertiesFileReader(System.getProperty("user.dir")+"/cred.properties");
String user = propReader.getPropertyData("user");
String pass = propReader.getPropertyData("pass");
String ip_add = propReader.getPropertyData("ip");
PowerShellSession psSession = new PowerShellSession();
String credentials = psSession.getCredentials(ip_add, user, pass);
psSession.targetIpAddress = ip_add;//;
String cmdd = propReader.getPropertyData("command");//"Get-Culture";
if(!credentials.equals("")) {
psSession.exec(cmdd, credentials);
System.out.println("Finished PowerShell remote session.");
}
psSession.close();
}
}

Java specific image downloader with multiple URL's

I can't figure out how to reference a specific line of text in a txt file. I need a specific image from a specific list of URL's and I am trying to generate the URL's by concatenating a URL prefix and a search number from a list of numbers in a txt file. I can't figure out how to reference the txt file and get a string from a line number.
package getimages;
public class ExtractAllImages {
public static int url_to_get = 1;
public static String urlupc;
public static String urlpre = "http://urlineedimagefrom/searchfolder/";
public static String url2 = "" + urlpre + urlupc + "";
public static void main(String args[]) throws Exception {
while(url_to_get > 2622){
String line = Files.readAllLines(Paths.get("file_on_my_desktop.txt")).get(url_to_get);
urlupc = line;
url2 = "" + urlpre + urlupc + "";
String webUrl = url2;
URL url = new URL(webUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
AttributeSet attributes = iterator.getAttributes();
String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);
if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
try {
downloadImage(webUrl, imgSrc);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
}
public static String right(String value, int length) {
return value.substring(value.length() - length);}
private static void downloadImage(String url, String imgSrc) throws IOException {
BufferedImage image = null;
try {
if (!(imgSrc.startsWith("http"))) {
url = url + imgSrc;
} else {
url = imgSrc;
}
String webUrl = url2;
String imagename = right(webUrl , 12);
imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
String imageFormat = null;
imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
String imgPath = null;
imgPath = "C:/Users/Noah/Desktop/photos/" + urlupc + ".jpg";
URL imageUrl = new URL(url);
image = ImageIO.read(imageUrl);
if (image != null) {
File file = new File(imgPath);
ImageIO.write(image, imageFormat, file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
My error is in setting the String line, My txt file has 2622 lines and I can't reference the file on my desktop and im not sure how to set the file path to my desktop? Sorry I'm not good at java.
Thanks for any help.
From what I understood you don't know how to access a file that is on your Desktop. Try doing this:
String path = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\" + fileName + ".txt";
Let me explain. We are using
System.getProperty("user.name")
to get the name of the user, if you don't want to use it then you can replace it with the name of your user. Then we are using
"\\Desktop\\"
to acess the Desktop, and finally we are adding the
fileName + ".txt"
to access the file we want that has the extension '.txt'.

Java file encoding conversion from ANSI to UTF8

I have a requirement to change the encoding of a file from ANSI(windows-1252) to UTF8. I wrote below program to do it through java. This program converts the characters to UTF8, but when I opened the file in notepad++ the encoding type was displayed as ANSI as UTF8. This gives me error when I import this file in access db. A file with UTF8 encoding only is desired. Also the requirement is to convert the file without opening it in any editor.
public class ConvertFromAnsiToUtf8 {
private static final char BYTE_ORDER_MARK = '\uFEFF';
private static final String ANSI_CODE = "windows-1252";
private static final String UTF_CODE = "UTF8";
private static final Charset ANSI_CHARSET = Charset.forName(ANSI_CODE);
public static void main(String[] args) {
List<File> fileList;
File inputFolder = new File(args[0]);
if (!inputFolder.isDirectory()) {
return;
}
File parentDir = new File(inputFolder.getParent() + "\\"
+ inputFolder.getName() + "_converted");
if (parentDir.exists()) {
return;
}
if (parentDir.mkdir()) {
} else {
return;
}
fileList = new ArrayList<File>();
for (final File fileEntry : inputFolder.listFiles()) {
fileList.add(fileEntry);
}
InputStream in;
Reader reader = null;
Writer writer = null;
try {
for (File file : fileList) {
in = new FileInputStream(file.getAbsoluteFile());
reader = new InputStreamReader(in, ANSI_CHARSET);
OutputStream out = new FileOutputStream(
parentDir.getAbsoluteFile() + "\\"
+ file.getName());
writer = new OutputStreamWriter(out, UTF_CODE);
writer.write(BYTE_ORDER_MARK);
char[] buffer = new char[10];
int read;
while ((read = reader.read(buffer)) != -1) {
System.out.println(read);
writer.write(buffer, 0, read);
}
}
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Any pointers will be helpful.
Thanks,
Ashish
The posted code correctly transcodes from windows-1252 to UTF-8.
The Notepad++ message is confusing because "ANSI as UTF-8" has no obvious meaning; it appears to be an open defect in Notepad++. I believe Notepad++ means UTF-8 without BOM (see the encoding menu.)
Microsoft Access, being a Windows program, probably expects UTF-8 files to start with a byte-order-mark (BOM).
You can inject a BOM into the document by writing the code point U+FEFF at the start of the file:
import java.io.*;
import java.nio.charset.*;
public class Ansi1252ToUtf8 {
private static final char BYTE_ORDER_MARK = '\uFEFF';
public static void main(String[] args) throws IOException {
Charset windows1252 = Charset.forName("windows-1252");
try (InputStream in = new FileInputStream(args[0]);
Reader reader = new InputStreamReader(in, windows1252);
OutputStream out = new FileOutputStream(args[1]);
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
writer.write(BYTE_ORDER_MARK);
char[] buffer = new char[1024];
int read;
while ((read = reader.read(buffer)) != -1) {
writer.write(buffer, 0, read);
}
}
}
}
On Windows 7 (64-Bit), running Java 8, I had to close every file. Otherwise, files get truncated to multiples of 4 kB. It is not enough to close the last set of files, I had to close every file to get the desired result. Posting my adapted version that adds error messages:
import java.io.*;
import java.nio.charset.*;
import java.util.ArrayList;
public class ConvertFromAnsiToUtf8 {
private static final char BYTE_ORDER_MARK = '\uFEFF';
private static final String ANSI_CODE = "windows-1252";
private static final String UTF_CODE = "UTF8";
private static final Charset ANSI_CHARSET = Charset.forName(ANSI_CODE);
private static final String PATH_SEP = "\\";
private static final boolean WRITE_BOM = false;
public static void main(String[] args)
{
if (args.length != 2) {
System.out.println("Please name a source and a target directory");
return;
}
File inputFolder = new File(args[0]);
if (!inputFolder.isDirectory()) {
System.out.println("Input folder " + inputFolder + " does not exist");
return;
}
File outputFolder = new File(args[1]);
if (outputFolder.exists()) {
System.out.println("Folder " + outputFolder + " exists - aborting");
return;
}
if (outputFolder.mkdir()) {
System.out.println("Placing converted files in " + outputFolder);
} else {
System.out.println("Output folder " + outputFolder + " exists - aborting");
return;
}
ArrayList<File> fileList = new ArrayList<File>();
for (final File fileEntry : inputFolder.listFiles()) {
fileList.add(fileEntry);
}
InputStream in;
Reader reader = null;
Writer writer = null;
int converted = 0;
try {
for (File file : fileList) {
try {
in = new FileInputStream(file.getAbsoluteFile());
reader = new InputStreamReader(in, ANSI_CHARSET);
OutputStream out = new FileOutputStream(outputFolder.getAbsoluteFile() + PATH_SEP + file.getName());
writer = new OutputStreamWriter(out, UTF_CODE);
if (WRITE_BOM)
writer.write(BYTE_ORDER_MARK);
char[] buffer = new char[1024];
int read;
while ((read = reader.read(buffer)) != -1) {
writer.write(buffer, 0, read);
}
++converted;
} finally {
reader.close();
writer.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(converted + " files converted");
}
}

Categories

Resources