Get variable from config.properties thoughtout Java app - java

I'm loading variables from a config.properties in my main class but how do I call these variables though out my app in these other files? I need to get hostName from my config.properties in multiple classes and the other java files as shown in my screenshot.
This code is in my RFIDMainDlg.java file,
I want to call the value of hostName in my RFIDBase.java file
Screenshot
public static void main(String args[]) throws IOException {
Properties prop=new Properties();
FileInputStream ip= new FileInputStream("resources/config.properties");
prop.load(ip);
String hostName = prop.getProperty("hostName");
System.out.println(hostName);

You can use a Singleton like this https://www.codeproject.com/articles/189489/java-properties-example-using-singleton-pattern (sorry for the ugly website, but it's a basic concept), it's not necessary but it's one of your options.

The solution on that page worked
package rfidsample;
import java.io.*;
import java.util.Properties;
public class RFIDGetPropertyValues {
static private RFIDGetPropertyValues _instance = null;
String hostName = null;
String port = null;
String intensity = null;
String user = null;
String pass = null;
String jdbc = null;
String driver = null;
String instance = null;
protected RFIDGetPropertyValues(){
try{
InputStream file = new FileInputStream(new File("resources/config.properties")) ;
Properties props = new Properties();
props.load(file);
hostName = props.getProperty("hostName");
port = props.getProperty("port");
intensity = props.getProperty("intensity");
}
catch(Exception e){
System.out.println("error" + e);
}
}
static public RFIDGetPropertyValues instance(){
if (_instance == null) {
_instance = new RFIDGetPropertyValues();
}
return _instance;
}
}
Then anywhere in my code I was able to call
RFIDGetPropertyValues dbInfo = RFIDGetPropertyValues.instance();
String hostName = dbInfo.hostName;
String intensity = dbInfo.intensity;

Related

How to run external executable from java and save the output in txt

hi i new in java and i try to run executable (exe) of vienna packge from eclipse java,
i want it will get string and use this on the exe , and i want to save the output of the exe in txt file,
how can i do it?
String[] params = new String [2];
params[0] = "C:\\Program Files (x86)\\ViennaRNA Package\\RNAup.exe";
params[1] = "GHHI";
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
out.print(Runtime.getRuntime().exec(params));
}
tnx
Runtime.getRuntim().exec(...); returns an instance of Process. Process has a method getOutputStream. Use this method to get the stream.
Once you have the stream read from it.
import java.io.*;
public class Main {
public static void main(String[] args) {
String[] params = new String[2];
params[0] = "C:\\Program Files (x86)\\ViennaRNA Package\\RNAup.exe";
params[1] = "GHHI";
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
Process p = Runtime.getRuntime().exec(params);
final InputStream inputStream = p.getInputStream();
final BufferedInputStream bis = new BufferedInputStream(inputStream);
final BufferedReader br = new BufferedReader(new InputStreamReader(bis));
String line;
while((line = br.readLine()) != null) {
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can use ProcessBuilder.redirectOutput to write directly to file, eg.
import java.io.File;
public class Sample {
private static final String EXE_FOLDER = "C:\\Program Files (x86)\\ViennaRNA Package";
private static final String EXE_NAME = "RNAup.exe";
public static void main(String[] args) throws Exception {
File exeDir = new File(EXE_FOLDER);
File exeFile = exeDir.toPath().resolve(EXE_NAME).toFile();
File outFile = new File("out.txt");
File errFile = new File("err.txt");
System.out.println("exeDir="+exeDir);
System.out.println("exeFile="+exeFile);
System.out.println("outFile="+outFile.getAbsolutePath());
System.out.println("errFile="+errFile.getAbsolutePath());
String[] params = { exeFile.getAbsolutePath() , "GHHI" };
ProcessBuilder proc = new ProcessBuilder(params).directory(exeDir);
proc.redirectOutput(outFile);
proc.redirectError(errFile);
proc.start();
}
}

Spring Boot Unit Testing

I'm currently developing a Rest service in Spring Boot and am attempting to develop unit tests. My Rest controller takes the request and utilizes an Sftp Connector defined within another package to directly interface with our servers. I've defined my test cases to mock the behavior of the Sftp Connector rather than have it execute with the mock data being fed into the Rest endpoints. I've been getting a ton of error logs during testing showing that exceptions are being raised from the Sftp connector. Which makes sense if the mock data sent to the endpoints is still being fed into the connector class. I'm guessing that I'm not mocking the connector properly. I've been unsuccessful so far in locating a method to mock the connector. Is this possible? Or can I only mock the classes defined within the same package as the test class?
Controller:
package RestAPI;
import JSch.SFTP.SftpConnector;
import JSch.SFTP.fileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
#RestController
#Api( value = "Test Rest Controller", description = "Basic test for SFTP
connector with very basic operations.")
public class SftpRestController {
private Properties getProperties(){ //used to get connection values from
external file
InputStream input = null;
Properties prop = new Properties(); //new property
try{
if(System.getProperty("os.name").contains("Windows")){
input = new FileInputStream("C:\\tmp\\application.properties"); //get resources stream
}
else{
input = new FileInputStream("application.properties");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while((line = reader.readLine()) != null){
String[] split = line.split("=");
prop.put(split[0], split[1]);
}
}catch(IOException IO){
IO.printStackTrace();
}finally{
if(input != null )
try{
input.close(); //close the stream
}catch(IOException IO2){
IO2.printStackTrace();
}
}
System.out.println(prop.getProperty("sftp.port"));
return prop;
}
#ApiOperation(value = "Check that service is up and running.", response = String.class)
#RequestMapping("/")
public #ResponseBody String index(){ return "Rest Test Service is up and running";}
#ApiOperation(value = "Upload a file", response = String.class)
#PostMapping(value="/Upload") //uploads files so long as the files are defined in one structure in the form body
public #ResponseBody String multipartUpload(#RequestBody MultipartFile[] files, #RequestParam("dest") String dest){
String fileMessage = ""; //need something to send back to the caller
SftpConnector connector = new SftpConnector(this.getProperties()); //plugs properties into the constructor and opens a connection
for( int i = 0; i < files.length; i++ ){ //for each file uploaded
connector.uploadFile(files[i], dest); //call the upload method with the file reference and where it's going
fileMessage = fileMessage + files[i].getOriginalFilename() + " has been uploaded to remote directory \n";
}
connector.closeSFTPConnection(); //manually close the connection
return fileMessage;
}
#ApiOperation(value = "Downloads a file over SFTP and writes it to local file system specified", response = String.class)
#GetMapping(value="/Download")
public #ResponseBody String downloadFile(#RequestParam("fileName") String fName, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties()); //new connector
connector.downloadFile(fName, dest); //pull the specified file down
connector.closeSFTPConnection(); //close the connection
return fName + " has been downloaded to the specified local directory";
}
#ApiOperation(value = "Moves all files on a remote server from one directory to another based on file type specified", response = String.class)
#PutMapping(value="/moveFiles")
public #ResponseBody String moveFiles(#RequestParam("dir") String origin, #RequestParam("fileType") String type,#RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.moveFiles(origin, type, dest); //moves a group of files based file type from one directory to another
connector.closeSFTPConnection();
return "All files have been moved.";
}
#ApiOperation(value = "Moves a single specific file", response = String.class)
#GetMapping(value="/moveFile")
public #ResponseBody String moveFile(#RequestParam("dir") String origFile, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.moveFile(origFile, dest); //moves a single file. file name must be specified.
connector.closeSFTPConnection();
return FilenameUtils.getName(origFile) + " has been moved to " + dest;
}
#ApiOperation(value = "Gets a specified file stream and returns it as a string", response = String.class)
#GetMapping(value="/readFile")
public #ResponseBody String readFile(#RequestParam("path") String path){
String fileContents;
try{
SftpConnector connector = new SftpConnector(this.getProperties());
InputStream fis = connector.readFile(path); //gets an open file stream
fileContents = IOUtils.toString(fis,"UTF-8"); //takes a file stream, reads into variable in specified encoding
fis.close(); //closes the stream
connector.closeSFTPConnection();
}catch(IOException IO){
fileContents = IO.toString();
}
return fileContents;
}
#ApiOperation(value="Returns a list of file names as a string", response = String.class)
#GetMapping(value="/listFiles")
public #ResponseBody String fileNames(#RequestParam("dir") String origin, #RequestParam("fileType") String type){
String base = "";
try{
SftpConnector connector = new SftpConnector(this.getProperties());
Collection<fileInfo> files = connector.listFiles(origin, type); //gets a list of files with name, type, and an input stream
Iterator<fileInfo> iterator = files.iterator(); //iterator to roll over collection
while(iterator.hasNext()){ //while not empty
fileInfo thisFile = iterator.next(); //get the next fileInfo
base = base + thisFile.getName() + '\n' ; //write the name to the base string
thisFile.getFileStream().close(); //close the file stream
}
connector.closeSFTPConnection();
}catch(Exception ex){
ex.printStackTrace();
}
return base;
}
#ApiOperation(value = "Moves a file by the connectors readFile and writeFile methods rather than SFTP rename", response = String.class)
#GetMapping(value="/test")
public #ResponseBody String StreamTest(#RequestParam("file") String file, #RequestParam("dest") String dest){
SftpConnector connector = new SftpConnector(this.getProperties());
connector.writeFile(dest, FilenameUtils.getName(file), connector.readFile(file));
connector.closeSFTPConnection();
return file + " has been successfully read, and written to destination";
}
}
Sftp Connector:
package JSch.SFTP;
import com.jcraft.jsch.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
#Component
public class SftpConnector {
private String sftpHost;
private String sftpUser;
private int sftpPort;
private String sftpPass;
private Session session = null;
private ChannelSftp channelSftp = null;
public String getSftpHost() {
return sftpHost;
}
public void setSftpHost(String sftpHost) {
this.sftpHost = sftpHost;
}
public String getSftpUser() {
return sftpUser;
}
public void setSftpUser(String sftpUser) {
this.sftpUser = sftpUser;
}
public int getSftpPort() {
return sftpPort;
}
public void setSftpPort(int sftpPort) {
this.sftpPort = sftpPort;
}
public String getSftpPass() {
return sftpPass;
}
public void setSftpPass(String sftpPass) {
this.sftpPass = sftpPass;
}
public void setConnectionVars(String host, String user, String password, int port){
this.setSftpHost(host);
this.setSftpPort(port);
this.setSftpUser(user);
this.setSftpPass(password);
}
public SftpConnector(Properties prop){
this.setConnectionVars(prop.getProperty("sftp.host"), prop.getProperty("sftp.user"), prop.getProperty("sftp.pass"), Integer.parseInt(prop.getProperty("sftp.port"))); //set the connection variables
this.openSFTPConnection(); //open the sftp connection
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public ChannelSftp getChannelSftp() {
return channelSftp;
}
public void setChannelSftp(ChannelSftp channelSftp) {
this.channelSftp = channelSftp;
}
public void openSFTPConnection(){
try{
JSch jsch = new JSch(); //create the new JSch var
Session baseSession = jsch.getSession(this.getSftpUser(), this.getSftpHost(),this.getSftpPort()); //establish the ssh session info
baseSession.setPassword(this.getSftpPass()); //auth over password
baseSession.setConfig("StrictHostKeyChecking","no"); // don't require hosting key check will need to change after testing
baseSession.setConfig("PreferredAuthentications","password"); //used to omit kerberos authentication
this.setSession(baseSession); //set the session to private variable
this.getSession().connect(); //open the session
ChannelSftp tempChannel = (ChannelSftp) this.getSession().openChannel("sftp"); //create an SFTP channel
this.setChannelSftp(tempChannel); //set the channel to the private variable
this.getChannelSftp().connect(); //open the SFTP connection
}catch(JSchException e){
e.printStackTrace();
}
}
public void closeSFTPConnection(){
try{
if(this.getChannelSftp().isConnected()){ //if the channel is open so must be the session close both
this.getChannelSftp().disconnect();
this.getSession().disconnect();
}else{ //if the channel is closed check to see if the session is still open
if(this.getSession().isConnected()){
this.getSession().disconnect();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public Collection<fileInfo> listFiles(String dir, String filter){
Collection<fileInfo> files = new ArrayList<fileInfo>();
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //make sure that the JSch sessions been opened.
this.openSFTPConnection();
this.getChannelSftp().cd(dir); //set the working directory
Vector<ChannelSftp.LsEntry> fileList = this.getChannelSftp().ls(filter); //Get a listing of the files in the directory depending on a filter
Iterator<ChannelSftp.LsEntry> iterator = fileList.iterator(); //iterate over the collection
while(iterator.hasNext()) {
ChannelSftp.LsEntry entry = iterator.next();
fileInfo thisFile = new fileInfo(entry.getFilename(), entry.getAttrs().getAtimeString(), FilenameUtils.getExtension(entry.getFilename()), this.getChannelSftp().get(entry.getFilename())); //get a buffered input stream for each file.
files.add(thisFile);
}
}catch(SftpException e){
e.printStackTrace();
}
return files;
}
public InputStream readFile(String filePath){ //built to read get a file stream from a given file path
InputStream inputStream = null;
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if the channel isn't open, open it.
this.openSFTPConnection(); //open connection
inputStream = this.getChannelSftp().get(filePath); //get the stream
}catch(SftpException ex){
ex.printStackTrace();
}
return inputStream;
}
public String uploadFile(MultipartFile file, String dest){
try{
System.out.println(this.getSession().getConfig("FileUploadBaseSizeLimit"));
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if not connected open the connection
this.openSFTPConnection();
this.getChannelSftp().cd(dest); //set working dir
this.getChannelSftp().put(file.getInputStream(), dest + '/' + file.getOriginalFilename()); //get the input stream from the multipart file and put it in the destination
}catch(IOException IO){
IO.printStackTrace();
}catch(SftpException sftp){
sftp.printStackTrace();
}
return file.getOriginalFilename() + " has been uploaded";
}
public String downloadFile(String orig, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()) //if not connected
this.openSFTPConnection();
File download = new File(dest + '/' + FilenameUtils.getName(orig)); //create a new local file
OutputStream outputStream = new FileOutputStream(download); //generate an output stream to the file
byte[] buffer = new byte[10000000]; //10MB buffer instance
BufferedInputStream bufferedInputStream = new BufferedInputStream(this.getChannelSftp().get(orig)); //create a buffered input stream off the input stream from the ssh get
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); // and a buffered output stream off the output stream we created
int readCount; //integer link to the buffer
while((readCount = bufferedInputStream.read(buffer)) > 0){ //read the buffered input stream into the buffer. while it's not empty...
bufferedOutputStream.write(buffer, 0, readCount); //write the buffer to the output stream
}
bufferedInputStream.close(); //close the streams
bufferedOutputStream.close();
outputStream.close();
}catch(IOException IO){
IO.printStackTrace();
}catch(SftpException sftp){
sftp.printStackTrace();
}
return "File " + FilenameUtils.getName(orig) + " has been downloaded."; //return that we've successfully uploaded the file
}
public void writeFile(String dir, String fileName, InputStream fileIS) { //writes a file given the destination directory, fileName, and an input stream
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected() ) //if the connection isn't already open, open it
this.openSFTPConnection();
this.getChannelSftp().cd(dir); //set the working dir for better shorthand
this.getChannelSftp().put(fileIS, fileName); //put the file.
fileIS.close(); //close the file stream
}catch(SftpException e){
e.printStackTrace();
}catch(IOException IO){
IO.printStackTrace();
}
}
public void moveFile(String path, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()){ //open connection if not done already
this.openSFTPConnection();
}
this.getChannelSftp().rename(path, dest + '/' + FilenameUtils.getName(path)); //ssh command to move the file.
}catch(SftpException e){
e.printStackTrace();
}
}
public String moveFiles(String dir, String filter, String dest){
try{
if(this.getChannelSftp() == null || !this.getChannelSftp().isConnected()){ //open if not done already
this.openSFTPConnection();
}
Collection<fileInfo> files = this.listFiles(dir, filter); //invoke the listFiles method and convert to an array
Iterator<fileInfo> iterator = files.iterator(); //iterator
while( iterator.hasNext()){ //while there is another value in the collection
fileInfo thisFile = iterator.next(); // have to store the fileInfo somewhere. can't just keep calling next.
this.getChannelSftp().rename(dir + '/' + thisFile.getName(), dest + '/' + thisFile.getName());
}
this.closeSFTPConnection(); //close the connection
}catch(Exception ex){
ex.printStackTrace();
}
return "all files moved";
}
}
Test Class:
package RestAPI;
import JSch.SFTP.SftpConnector;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.FileInputStream;
import java.io.InputStream;
#RunWith(SpringRunner.class)
#WebMvcTest(value = SftpRestController.class, secure = false)
public class SftpRestUnitTest {
#Autowired
private MockMvc mockMvc;
#MockBean
SftpConnector connector ;
#Test
public void testFileUpload() throws Exception{
MockMultipartFile testFile1 = new MockMultipartFile("data", "WSUID1.csv", "application/csv", "some xml".getBytes());
Mockito.when(
connector.uploadFile(Mockito.any(),Mockito.anyString())
).thenReturn("WSUID1.csv has been uploaded");
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.multipart("/Upload").file(testFile1).param("dest", "/tmp/test")).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getContentAsString().equals("WSUID1.csv has been uploaded to remote directory \n");
}
#Test
public void testDownload() throws Exception{
String expected = "TestFile.csv has been downloaded to the specified local directory";
Mockito.when(
connector.downloadFile(Mockito.anyString(), Mockito.anyString())
).thenReturn("File TestFile.csv has been downloaded.");
RequestBuilder request = MockMvcRequestBuilders.get("/Download").param("fileName","/tmp/TestFile.csv").param("dest", "/tmp");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getContentAsString().equals(expected);
}
#Test
public void testMoveFiles() throws Exception{
Mockito.when(
connector.moveFiles(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())
).thenReturn("all files moved");
RequestBuilder request = MockMvcRequestBuilders.put("/moveFiles").param("dir","/IB_Test").param("fileType", "*.csv").param("dest", "/InternatlPgms/INTO/Archive/Receipt");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.getResponse().getStatus() == 200;
}
#Test
public void testReadFile() throws Exception{
String expected = "greetings from java test";
InputStream mockStream = Mockito.mock(FileInputStream.class);
Mockito.when(
connector.readFile(Mockito.anyString())
).thenReturn(mockStream);
RequestBuilder request = MockMvcRequestBuilders.get("/readFile").param("path", "IB_Test");
MvcResult result = mockMvc.perform(request).andReturn();
System.out.println(result.getResponse().getContentAsString());
assert result.equals(expected);
}
}
Error Logs:
The specified file is a directory.
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1337)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1290)
at JSch.SFTP.SftpConnector.readFile(SftpConnector.java:133)
at RestAPI.SftpRestController.readFile(SftpRestController.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Any help is greatly appreciated.
You create a new instance of SftpConnector in every method of your controller. It is not an injected bean so it won't be replaced with your mock.
For an example have a look at this article (section 5):
https://www.baeldung.com/spring-boot-testing

Java: Getting FileNotFoundException when trying to read from a text file even though the file exists

I'm trying to read from a .txt file that is located in src\main\java\props\engprops.txt. I'm using backslashes here because I'm using Windows.
When I try to read the file I get a FileNotFoundException.
This is my helper class. It contains a method for returning a string representation of the directory in which the file is located. Here is what it returns C:\Users\SOME_USER\workarea\Myapp\myapp\src\main\props\
private final static String APP_HOME = "\\workarea\\Translate\\translate\\src\\main";
private final static String PROPS_HOME = "\\props\\";
public static String getPropsPath() {
StringBuilder sb = new StringBuilder();
String userHome = System.getProperty("user.home");
//userHome = userHome.replace("\\", "/");
String propsHome = null;
propsHome = userHome + APP_HOME + PROPS_HOME;
return propsHome;
}
Then in my main class is where I try to read the file:
private static String stringPath = AppHelper.getPropsPath();
public static void main(String[] args) {
readFile();
}
public static void readFile() throws IOException {
FileReader fReader = new FileReader(stringPath + "engprops.txt");
BufferedReader bReader = new BufferedReader(fReader);
String line = null;
while((line = bReader.readLine()) != null) {
System.out.println(line);
}
bReader.close();
}
Inside the readFile() method, I am appending the name of the file on to the stringPath variable with the file name being engprops.txt.
Here is a snippet of the console displays after the program executes; java.io.FileNotFoundException: C:\Users\600010209\workarea\Translate\translate\src\main\props\engprops.txt (The system cannot find the file specified)
You wrote in your question that your file was located under src/main/java/props, but the props path you declared was under src/main/props.
Comment if that doesnt answer your question.

How can I create a Global FileInputStream object, which will be available to access from other class within in my project?

What I wanted to do is create a global FileInputStream object and use it a long of my application.
I have the follow class which create my FileInputStream objec and return the result of my query over the XML file:
package Engine;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import com.ximpleware.AutoPilot;
import com.ximpleware.VTDException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
public class BACENGQueryXMLbyVTD {
public String query;
public String path;
public BACENGQueryXMLbyVTD(String query, String path) {
this.query = query;
this.path = path;
}
public ArrayList query() throws IOException, VTDException {
System.out.println(path);
File f = new File(path);
//System.out.println(f);
FileInputStream fis = new FileInputStream(f);
//System.out.println(fis);
byte[] xmlContent = new byte[(int) f.length()];
fis.read(xmlContent);
VTDGen vg = new VTDGen();
vg.setDoc(xmlContent);
vg.parse(false);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
int node = 0;
ap.selectXPath(query);
int i;
ArrayList<String> interfaces = new ArrayList<String>();
while ((i = ap.evalXPath()) != -1) {
vn.push();
interfaces.add(vn.toString(i + 1));
}
ap.resetXPath();
return interfaces;
}
}
This class is called from my main class which has a loop.
for (int c = 0; c < nodeNames.size(); c++) {
String Queries2 = "/import_data/group/node[#name='" +nodeNames.get(c) + "']/interface/#network_value";
BACENGQueryXMLbyVTD getNodesInterfaces = new BACENGQueryXMLbyVTD(Queries2, TransitPath);
listOfNodeInterfaces = getNodesInterfaces.query();
}
It's working fine, however in order to reduce the consume of IO resource over my server HD. I would like to create an unique FileInputStream object and use it for each query whcih has to be executed.
Could somebody point out the way to do it?
Separate your concerns - BACENGQueryXMLbyVTD is both loading the data and executing the query.
First load the file into a byte[] outside your loop, then pass it to BACENGQueryXMLbyVTD. You might also want to pass the query as an argument to the query method.
You'll end up with a BACENGQueryXMLbyVTD that looks like this (with the disclaimer that I'm not familiar with VTD-XML, so this might the creation of objects from that API might not work exactly like this):
public class BACENGQueryXMLbyVTD
{
private byte[] doc;
public BACENGQueryXMLbyVTD(byte[] doc)
{
this.doc = doc;
}
public List<String> query(String query) throws IOException, VTDException
{
VTDGen generator = new VTDGen();
generator.setDoc(doc);
generator.parse(false);
VTDNav navigator = generator.getNav();
AutoPilot autoPilot = new AutoPilot(navigator);
autoPilot.selectXPath(query);
List<String> nodeInterfaces = new ArrayList<String>();
int i;
while ((i = autoPilot.evalXPath()) != -1)
{
navigator.push();
nodeInterfaces.add(navigator.toString(i + 1));
}
return nodeInterfaces;
}
}
That you can then call like:
byte[] xmlContent = ... //load the xml from anywhere you like, not just a file as previously.
BACENGQueryXMLbyVTD getNodesInterfaces = new BACENGQueryXMLbyVTD(xmlContent);
for (String nodeName : nodeNames)
{
String query = "/import_data/group/node[#name='" + nodeName + "']/interface/#network_value";
nodeInterfaces = getNodesInterfaces.query(query);
...
}
You might also want to switch to the standard Java XPATH APIs - they're a lot clearer and better documented than VTD-XML.

Getting IllegalAccessError

When i am using this code it gives error:
2011-08-10 13:18:13.368::WARN: EXCEPTION
java.lang.IllegalAccessError: tried to access method org.mortbay.util.Utf8StringBuffer.(I)V from class org.mortbay.jetty.HttpURI
Code:
package com.google.api.client.sample.docs.v3;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.googleapis.auth.oauth.GoogleOAuthAuthorizeTemporaryTokenUrl;
import com.google.api.client.googleapis.auth.oauth.GoogleOAuthGetAccessToken;
import com.google.api.client.googleapis.auth.oauth.GoogleOAuthGetTemporaryToken;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.sample.docs.v3.model.DocsUrl;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.net.URI;
public class Auth {
private static final String APP_NAME ="Google Documents List Data API Java Client Sample";
private static OAuthHmacSigner signer;
private static OAuthCredentialsResponse credentials;
static void authorize(HttpTransport transport) throws Exception {
// callback server
LoginCallbackServer callbackServer = null;
String verifier = null;
String tempToken = null;
try {
callbackServer = new LoginCallbackServer();
callbackServer.start();
// temporary token
GoogleOAuthGetTemporaryToken temporaryToken =new GoogleOAuthGetTemporaryToken();
signer = new OAuthHmacSigner();
signer.clientSharedSecret = "anonymous";
temporaryToken.signer = signer;
temporaryToken.consumerKey = "in.gappsdemo.in";
//temporaryToken.scope ="https://apps-apis.google.com/a/feeds/user/";
temporaryToken.scope = DocsUrl.ROOT_URL;
temporaryToken.displayName = APP_NAME;
temporaryToken.callback = callbackServer.getCallbackUrl();
System.out.println("temporaryToken.callback: "+temporaryToken.callback);
OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
signer.tokenSharedSecret = tempCredentials.tokenSecret;
System.out.println("signer.tokenSharedSecret: " + signer.tokenSharedSecret);
// authorization URL
GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl =new GoogleOAuthAuthorizeTemporaryTokenUrl();
authorizeUrl.temporaryToken = tempToken = tempCredentials.token;
String authorizationUrl = authorizeUrl.build();
System.out.println("Go to this authorizationUrl: " + authorizationUrl);
// launch in browser
boolean browsed = false;
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Action.BROWSE)) {
System.out.println("In if browsed condition:");
desktop.browse(URI.create(authorizationUrl));
browsed = true;
}
}
if (!browsed) {
String browser = "google-chrome";
Runtime.getRuntime().exec(new String[] {browser, authorizationUrl});
System.out.println("In if !browsed condition1:");
}
System.out.println("tempToken: "+tempToken);
verifier = callbackServer.waitForVerifier(tempToken);
System.out.println("GoogleOAuthGetAccessToken: ");
} finally {
System.out.println("server Stop:");
if (callbackServer != null) {
System.out.println("server Stop:");
callbackServer.stop();
}
}
System.out.println("GoogleOAuthGetAccessToken: ");
GoogleOAuthGetAccessToken accessToken = new GoogleOAuthGetAccessToken();
accessToken.temporaryToken = tempToken;
accessToken.signer = signer;
accessToken.consumerKey = "in.gappsdemo.in";
accessToken.verifier = verifier;
credentials = accessToken.execute();
signer.tokenSharedSecret = credentials.tokenSecret;
System.out.println("signer.tokenSharedSecret: ");
createOAuthParameters().signRequestsUsingAuthorizationHeader(transport);
}
static void revoke() {
if (credentials != null) {
try {
GoogleOAuthGetAccessToken.revokeAccessToken(createOAuthParameters());
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
private static OAuthParameters createOAuthParameters() {
OAuthParameters authorizer = new OAuthParameters();
authorizer.consumerKey = "in.gappsdemo.in";
authorizer.signer = signer;
authorizer.token = credentials.token;
return authorizer;
}
}
Here are two potentially related, posts. Notice you're getting an Error, not an Exception, so that is interesting. You might try running your JVM with -verbose to see where each class is getting loaded from, to see if maybe you're compiling with one class/jar, but accidentally running with another.
Also notice from the error that the packages are slightly different "org.mortbay.util" vs. "org.mortbay.jetty", so the UTF8StringBuffer constructor would need to be more visible. But again, normally a compiler would catch that.
I realize this isn't a full answer, to be sure, but at least a couple places to start looking.

Categories