Following JAVA functions can be used to execute SFTP(Secure FTP).
public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {
boolean put = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String filepath = localDirectory + fileName;
File file = new File(filepath);
if (!file.exists()) {
throw new RuntimeException("Error. Local file not found.");
}
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
put = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return put;
}
public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {
boolean get = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
get = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return get;
}
public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
boolean delete = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
if (remoteFile.exists()) {
if (remoteFile.delete()) {
delete = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return delete;
}
public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {
Collection fileCollection = new ArrayList();
StandardFileSystemManager manager = new StandardFileSystemManager();
FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
FileObject[] children = localFileObject.getChildren();
for (int i = 0; i < children.length; i++) {
fileCollection.add(children[ i].getName().getBaseName());
}
return fileCollection;
}
public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
StandardFileSystemManager manager = new StandardFileSystemManager();
boolean isExist = false;
try {
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
isExist = remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
return isExist;
}
Tuesday, October 28, 2014
Monday, October 27, 2014
Password Authenticate with SALT value
In password authentication, most developers use hash value generation. Hash value has a unique value for every word. If two users have same password, both has same hash value for their password. The solution is to generate salt value for password. Before proceeding hash generating, password is appended with salt value. The salt value is not a secret value. For validation, hash value and salt value must be known from the developer.
Following article for user authentication described clearly with sources..
https://crackstation.net/hashing-security.htm#normalhashing
Following article for user authentication described clearly with sources..
https://crackstation.net/hashing-security.htm#normalhashing
Wednesday, October 1, 2014
Cron to STOP/ START Program
In Linux, you can use crontab to stop/ start applications.
Following shell script (stop.sh) can be used to grep your program & kill it.
Following shell script (stop.sh) can be used to grep your program & kill it.
#!/bin/bash
PGID=`ps -eaf | grep YourProgram | grep YourName | grep -v grep | awk '{print $2}'`
if [ -n $PGID ]
then
/bin/kill -9 $PGID
else
echo "Not Running"
fi
After stopping, you can run the following program(start.sh) to restart the application. This will grep to check whether application runs or not. If runs, shell terminates with "Already Running" error. Else, it will run the application with nohup and send a mail to owner using mailx linux API.
#!/bin/bash
PRGHOME=/home/sujith/ReminderSMS
ps –eaf | grep YourProgram| grep YourName|grep -v grep |
awk '{print $2}' > /home/ sujith/.tmp.ReminderAlert
if [ -s /home/ sujith/.tmp.ReminderAlert ]
then
echo "Already Running"
else
#. /home/ sujith/.bash_profile
cd $PRGHOME
echo 'program_alert_started' | mailx sujith@test1.lk
nohup java YourProgram &
rm /home/ sujith/.tmp.ReminderAlert
fi
Wednesday, September 24, 2014
JAVA Server Socket Server & Client Program
Following code can be used to run a server socket application. This socket application listen requests on "locathost" port "10225" .
Socket Server Program
public class SocketServer {
ServerSocket m_ServerSocket;
Logger logger;
int SERVER_PORT = 10225;
public SocketServer() {
try {
m_ServerSocket = new ServerSocket(SERVER_PORT);
} catch (IOException ioe) {
System.out.println("Could not create server socket " + SERVER_PORT + " Quitting. " + ioe);
System.exit(-1);
}
int id = 0;
while (true) {
try {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
} catch (Exception ioe) {
System.out.println("Constructor : " + ioe);
}
}
}
public String process(String input) {
String response = "";
// your process logic
return response;
}
class ClientServiceThread extends Thread {
Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID) {
m_clientSocket = s;
m_clientID = clientID;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
System.out.println("Accepted Client ID - " + m_clientID + " |Address - " + m_clientSocket.getInetAddress().getHostName());
try {
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
while (m_bRunThread) {
String clientCommand = in.readLine();
System.out.println("Client Says :" + clientCommand);
if (!clientCommand.equalsIgnoreCase("Quit")) {
out.println(process(clientCommand));
}
if (clientCommand.equalsIgnoreCase("quit")) {
m_bRunThread = false;
} else {
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
m_clientSocket.close();
System.out.println("...Stopped");
} catch (IOException ioe) {
System.out.println("ClientServiceThread.run " + ioe);
}
}
}
}
public static void main(String[] args) {
new ChargeSocket();
}
}
Socket Client Program
public String connectToSocket(String ip, int port, String request) {
String response = null;
Socket socket = null;
try {
socket = new Socket(ip, port);
} catch (UnknownHostException unknownhostexception) {
System.out.println("Unknown Host :localhost");
socket = null;
} catch (IOException ioexception) {
System.out.println("Cant connect to server at " + ip + " " + port + " Make sure it is running.");
socket = null;
}
if (socket == null) {
return null;
}
BufferedReader bufferedreader = null;
PrintWriter printwriter = null;
try {
bufferedreader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
printwriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
printwriter.println(request);
printwriter.flush();
response = bufferedreader.readLine();
System.out.println("Server Says : " + response);
printwriter.println("Quit");
printwriter.flush();
} catch (IOException ioexception) {
System.out.println("Exception during communication. Server probably closed connection.");
ioexception.printStackTrace();
} finally {
try {
printwriter.close();
bufferedreader.close();
socket.close();
} catch (Exception exception1) {
exception1.printStackTrace();
}
}
return response;
}
Socket Server Program
public class SocketServer {
ServerSocket m_ServerSocket;
Logger logger;
int SERVER_PORT = 10225;
public SocketServer() {
try {
m_ServerSocket = new ServerSocket(SERVER_PORT);
} catch (IOException ioe) {
System.out.println("Could not create server socket " + SERVER_PORT + " Quitting. " + ioe);
System.exit(-1);
}
int id = 0;
while (true) {
try {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
} catch (Exception ioe) {
System.out.println("Constructor : " + ioe);
}
}
}
public String process(String input) {
String response = "";
// your process logic
return response;
}
class ClientServiceThread extends Thread {
Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID) {
m_clientSocket = s;
m_clientID = clientID;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
System.out.println("Accepted Client ID - " + m_clientID + " |Address - " + m_clientSocket.getInetAddress().getHostName());
try {
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
while (m_bRunThread) {
String clientCommand = in.readLine();
System.out.println("Client Says :" + clientCommand);
if (!clientCommand.equalsIgnoreCase("Quit")) {
out.println(process(clientCommand));
}
if (clientCommand.equalsIgnoreCase("quit")) {
m_bRunThread = false;
} else {
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
m_clientSocket.close();
System.out.println("...Stopped");
} catch (IOException ioe) {
System.out.println("ClientServiceThread.run " + ioe);
}
}
}
}
public static void main(String[] args) {
new ChargeSocket();
}
}
Socket Client Program
public String connectToSocket(String ip, int port, String request) {
String response = null;
Socket socket = null;
try {
socket = new Socket(ip, port);
} catch (UnknownHostException unknownhostexception) {
System.out.println("Unknown Host :localhost");
socket = null;
} catch (IOException ioexception) {
System.out.println("Cant connect to server at " + ip + " " + port + " Make sure it is running.");
socket = null;
}
if (socket == null) {
return null;
}
BufferedReader bufferedreader = null;
PrintWriter printwriter = null;
try {
bufferedreader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
printwriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
printwriter.println(request);
printwriter.flush();
response = bufferedreader.readLine();
System.out.println("Server Says : " + response);
printwriter.println("Quit");
printwriter.flush();
} catch (IOException ioexception) {
System.out.println("Exception during communication. Server probably closed connection.");
ioexception.printStackTrace();
} finally {
try {
printwriter.close();
bufferedreader.close();
socket.close();
} catch (Exception exception1) {
exception1.printStackTrace();
}
}
return response;
}
Tuesday, August 19, 2014
WSO2 IS enable logging
In WSO2 Identity Server settings, it is deactivated logging for performance gaining. You can activate WSO2 IS logging using following command for debugging issues.
log4j.logger.org.wso2.carbon.identity.sso.saml=DEBUG
Add above log4j property in below file.
./repository/conf/log4j.prorperties
log4j.logger.org.wso2.carbon.identity.sso.saml=DEBUG
Add above log4j property in below file.
./repository/conf/log4j.prorperties
Tuesday, July 22, 2014
Set Password in PDF in Jasper Report
Following code snip can be used to create protected PDF. This authentication is enabled in jasper report 5.6 onward.
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setEncrypted(true);
configuration.set128BitKey(true);
configuration.setUserPassword("jasper");
configuration.setOwnerPassword("reports");
configuration.setPermissions(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
exporter.setConfiguration(configuration);
exporter.exportReport();
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setEncrypted(true);
configuration.set128BitKey(true);
configuration.setUserPassword("jasper");
configuration.setOwnerPassword("reports");
configuration.setPermissions(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
exporter.setConfiguration(configuration);
exporter.exportReport();
Friday, July 11, 2014
Change default JAVA path
Once you installed JAVA 1.7 or 1.8, your default JAVA path is changed. But, it is not listed in Environment Variable Properties. To change to another JAVA path, you can simply do the following.
In Windows,
In Windows,
- go to "C:\Windows\System32"
- delete "java.exe"
- add JAVA path to environment variable
- open command prompt
- check JAVA version (java -version)
Subscribe to:
Posts (Atom)