Skip to content

Commit

Permalink
Merge pull request #5 from turiot/master
Browse files Browse the repository at this point in the history
Save preferences and add an app icon.
  • Loading branch information
dubasdey authored Oct 7, 2023
2 parents 90f7318 + 5d1b921 commit 8b88ad9
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.erc</groupId>
<artifactId>org.erc.pftps</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
<name>PortableFTPServer</name>
<packaging>jar</packaging>
<url>https://github.com/dubasdey/portable-ftp-server</url>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/erc/pftps/FocusTraversalOnArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,39 @@ private Component cycle(Component currentComponent, int delta) {
/* (non-Javadoc)
* @see java.awt.FocusTraversalPolicy#getComponentAfter(java.awt.Container, java.awt.Component)
*/
@Override
public Component getComponentAfter(Container container, Component component) {
return cycle(component, 1);
}

/* (non-Javadoc)
* @see java.awt.FocusTraversalPolicy#getComponentBefore(java.awt.Container, java.awt.Component)
*/
@Override
public Component getComponentBefore(Container container, Component component) {
return cycle(component, -1);
}

/* (non-Javadoc)
* @see java.awt.FocusTraversalPolicy#getFirstComponent(java.awt.Container)
*/
@Override
public Component getFirstComponent(Container container) {
return m_Components[0];
}

/* (non-Javadoc)
* @see java.awt.FocusTraversalPolicy#getLastComponent(java.awt.Container)
*/
@Override
public Component getLastComponent(Container container) {
return m_Components[m_Components.length - 1];
}

/* (non-Javadoc)
* @see java.awt.FocusTraversalPolicy#getDefaultComponent(java.awt.Container)
*/
@Override
public Component getDefaultComponent(Container container) {
return getFirstComponent(container);
}
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/org/erc/pftps/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JPasswordField;

Expand Down Expand Up @@ -96,16 +97,22 @@ public MainWindow() {
setTitle("Portable FTP Server");
setBounds(10, 10, 530, 340);

java.net.URL imgURL = MainWindow.class.getResource("/ftp.png");
if (imgURL != null) {
ImageIcon arrowIcon = new ImageIcon(imgURL);
this.setIconImage(arrowIcon.getImage());
}

Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - getWidth()) / 2);
int y = (int) ((dimension.getHeight() - getHeight()) / 2);
setLocation(x, y);
int x = (int) ((dimension.getWidth() - getWidth()) / 2);
int y = (int) ((dimension.getHeight() - getHeight()) / 2);
setLocation(x, y);

chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Portable FTP Server - Home Folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Portable FTP Server - Home Folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);

getContentPane().setLayout(null);

Expand Down Expand Up @@ -141,6 +148,7 @@ public MainWindow() {

btnFolder = new JButton("...");
btnFolder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (chooser.showOpenDialog(btnFolder) == JFileChooser.APPROVE_OPTION) {
txtFolder.setText(chooser.getSelectedFile().getAbsolutePath());
Expand All @@ -156,6 +164,7 @@ public void actionPerformed(ActionEvent e) {

btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

// Start
Expand All @@ -181,15 +190,18 @@ public void actionPerformed(ActionEvent e) {
txtPassword.setEnabled(false);
btnFolder.setEnabled(false);
btnStart.setText("Stop");
}else{
}
else {
System.err.println("Error starting server");
}
}else{
}
else {
System.err.println("Invalid port,user or folder");
}

// Stop
}else{
}
else {
ftpServer.stop();
isStarted = false;
txtPort.setEnabled(true);
Expand Down Expand Up @@ -239,6 +251,19 @@ public void actionPerformed(ActionEvent e) {
txtPassword.setText(preferences.getString("FTP.PASSWORD", "user"));
txtFolder.setText(preferences.getString("FTP.FOLDER", ""));


this.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
// save preferences
preferences.set("FTP.PORT", txtPort.getText());
preferences.set("FTP.USER", txtUser.getText());
preferences.set("FTP.PASSWORD", String.valueOf(txtPassword.getPassword())); // encrypt it
preferences.set("FTP.FOLDER", txtFolder.getText());
preferences.save();
e.getWindow().dispose();
System.out.println("Preferences saved"); // logger
}
});

}
}
25 changes: 17 additions & 8 deletions src/main/java/org/erc/pftps/MessageConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
public class MessageConsole {

/** The text component. */
private JTextComponent textComponent;
private final JTextComponent textComponent;

/** The document. */
private Document document;
private final Document document;

/** The limit lines listener. */
private DocumentListener limitLinesListener;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void setMessageLines(int lines) {
class LimitLinesDocumentListener implements DocumentListener {

/** The maximum lines. */
private int maximumLines;
private final int maximumLines;

/**
* Instantiates a new limit lines document listener.
Expand All @@ -111,18 +111,26 @@ public LimitLinesDocumentListener(int maximumLines) {
/* (non-Javadoc)
* @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
*/
public void insertUpdate(final DocumentEvent e) {
SwingUtilities.invokeLater( new Runnable() { public void run() { removeLines(e); } });
}
@Override
public void insertUpdate(final DocumentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
removeLines(e);
}
});
}

/* (non-Javadoc)
* @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
*/
@Override
public void removeUpdate(DocumentEvent e) {}

/* (non-Javadoc)
* @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
*/
@Override
public void changedUpdate(DocumentEvent e) {}

/**
Expand Down Expand Up @@ -157,7 +165,7 @@ class ConsoleOutputStream extends ByteArrayOutputStream {
private SimpleAttributeSet attributes;

/** The buffer. */
private StringBuffer buffer = new StringBuffer(80);
private final StringBuffer buffer = new StringBuffer(80);

/** The is first line. */
private boolean isFirstLine;
Expand All @@ -179,6 +187,7 @@ public ConsoleOutputStream(Color textColor) {
/* (non-Javadoc)
* @see java.io.OutputStream#flush()
*/
@Override
public void flush() {
String message = toString();
if (message.length() == 0) return;
Expand Down Expand Up @@ -216,7 +225,7 @@ private void clearBuffer() {
int offset = document.getLength();
document.insertString(offset, line, attributes);
textComponent.setCaretPosition( document.getLength() );
}catch (BadLocationException ble) {}
} catch (BadLocationException ble) {}
buffer.setLength(0);
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/org/erc/pftps/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public class Preferences {
private Properties properties;

/** The Constant userPath. */
public static final String userPath = System.getProperty("user.home");
public static final String USER_HOME = System.getProperty("user.home");

/**
* Instantiates a new preferences.
*/
public Preferences() {
properties = new Properties();
try {
Path path = Paths.get(userPath, ".erc.sftp.server");
Path path = Paths.get(USER_HOME, ".erc.sftp.server");
if(path.toFile().exists() ){
properties.load(Files.newInputStream(path));
}
Expand All @@ -60,16 +60,6 @@ public Preferences() {
*/
public <T> void set(String key,T value){
properties.setProperty(key, value.toString());
Path path = Paths.get(userPath, ".erc.sftp.server");
OutputStream os;
try {
os = Files.newOutputStream(path);
properties.store(os,"");
os.flush();
os.close();
} catch (IOException e) {
/* Ignored */
}
}

/**
Expand All @@ -82,4 +72,18 @@ public <T> void set(String key,T value){
public String getString(String key,String defaultValue){
return properties.getProperty(key, defaultValue);
}

public void save() {
Path path = Paths.get(USER_HOME, ".erc.sftp.server");
OutputStream os;
try {
os = Files.newOutputStream(path);
properties.store(os,"");
os.flush();
os.close();
} catch (IOException e) {
/* Ignored */
}
}

}
37 changes: 20 additions & 17 deletions src/main/java/org/erc/pftps/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.erc.pftps.services.FTPServer;

Expand All @@ -40,22 +41,23 @@ public static void main(String[] args) {

if(args == null || args.length<1){
// Run with GUI
java.awt.EventQueue.invokeLater ( new Runnable() {
public void run() {
try{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
System.setProperty("sun.awt.noerasebackground", "true");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MainWindow window = new MainWindow();
window.setVisible(true);
} catch (Exception e) {
//log.error(e);
}
}
});
}else{

java.awt.EventQueue.invokeLater ( new Runnable() {
@Override
public void run() {
try{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
System.setProperty("sun.awt.noerasebackground", "true");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MainWindow window = new MainWindow();
window.setVisible(true);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {
//log.error(e);
}
}
});
}
else {
// Run command line or help
if (args.length == 4 || args.length == 6){
FTPServer server = new FTPServer();
Expand All @@ -65,7 +67,8 @@ public void run() {
server.setSSL(args[4], args[5]);
}
server.start();
} else{
}
else {
System.out.println("Invalid arguments");
System.out.println("");
System.out.println("Use without arguments for GUI or call with:");
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/org/erc/pftps/services/FTPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,26 @@ public void stop(){

/**
* Start.
* @return true if ok
*/
public boolean start(){

stop();

ConnectionConfigFactory configFactory = new ConnectionConfigFactory();
configFactory.setAnonymousLoginEnabled(false);
configFactory.setMaxAnonymousLogins(0);

configFactory.setMaxLoginFailures(5);
configFactory.setLoginFailureDelay(30);
stop();

configFactory.setMaxThreads(10);
configFactory.setMaxLogins(10);

FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.addListener("default", factory.createListener());
serverFactory.setUserManager(userManager);
serverFactory.setConnectionConfig(configFactory.createConnectionConfig());
ConnectionConfigFactory configFactory = new ConnectionConfigFactory();
configFactory.setAnonymousLoginEnabled(false);
configFactory.setMaxAnonymousLogins(0);

configFactory.setMaxLoginFailures(5);
configFactory.setLoginFailureDelay(30);

configFactory.setMaxThreads(10);
configFactory.setMaxLogins(10);

FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.addListener("default", factory.createListener());
serverFactory.setUserManager(userManager);
serverFactory.setConnectionConfig(configFactory.createConnectionConfig());

server = serverFactory.createServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class InMemoryUserManager implements UserManager {
public void setUser(BaseUser _user){
user = _user;
if(user.getAuthorities() == null || user.getAuthorities().isEmpty()){
List<Authority> authorities = new ArrayList<Authority>();
List<Authority> authorities = new ArrayList<>();
authorities.add(new WritePermission());
authorities.add(new ConcurrentLoginPermission(10, 10));
user.setAuthorities(authorities);
Expand Down
Binary file added src/main/resources/ftp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8b88ad9

Please sign in to comment.