Skip to content

Commit cf62013

Browse files
committed
First release (1.0.0)
1 parent c7829c9 commit cf62013

21 files changed

+600
-299
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/winfoom.iml
22
/.idea/
33
/target/
4+
/logs/

config/user.properties

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ proxy.host=localhost
2222
# Port number of remote proxy (mandatory)
2323
proxy.port=80
2424

25-
# Proxy domain (mandatory)
26-
proxy.domain=DESKTOPQ
27-
2825
# Username (mandatory)
2926
proxy.username=Quasimodo
3027

3128
proxy.test.url=http://example.com
3229

33-
font.size=15

pom.xml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212
<groupId>org.kpax</groupId>
1313
<artifactId>winfoom</artifactId>
14-
<version>1.0-SNAPSHOT</version>
14+
<version>1.0.0</version>
1515

1616
<name>winfoom</name>
1717
<!-- FIXME change it to the project's website -->
@@ -22,7 +22,11 @@
2222
<maven.compiler.source>11</maven.compiler.source>
2323
<maven.compiler.target>11</maven.compiler.target>
2424
<openjfx.version>11</openjfx.version>
25+
<controlsfx.version>11.0.0</controlsfx.version>
2526
<httpclient.version>4.5.10</httpclient.version>
27+
<commons-configuration2.version>2.2</commons-configuration2.version>
28+
<commons-io.version>2.5</commons-io.version>
29+
<commons-beanutils.version>1.9.3</commons-beanutils.version>
2630
</properties>
2731

2832
<dependencies>
@@ -36,11 +40,13 @@
3640
<artifactId>javafx-controls</artifactId>
3741
<version>${openjfx.version}</version>
3842
</dependency>
43+
3944
<dependency>
4045
<groupId>org.openjfx</groupId>
4146
<artifactId>javafx-fxml</artifactId>
4247
<version>${openjfx.version}</version>
4348
</dependency>
49+
4450
<dependency>
4551
<groupId>org.openjfx</groupId>
4652
<artifactId>javafx-graphics</artifactId>
@@ -62,19 +68,19 @@
6268
<dependency>
6369
<groupId>org.apache.commons</groupId>
6470
<artifactId>commons-configuration2</artifactId>
65-
<version>2.2</version>
71+
<version>${commons-configuration2.version}</version>
6672
</dependency>
6773

6874
<dependency>
6975
<groupId>commons-io</groupId>
7076
<artifactId>commons-io</artifactId>
71-
<version>2.5</version>
77+
<version>${commons-io.version}</version>
7278
</dependency>
7379

7480
<dependency>
7581
<groupId>commons-beanutils</groupId>
7682
<artifactId>commons-beanutils</artifactId>
77-
<version>1.9.3</version>
83+
<version>${commons-beanutils.version}</version>
7884
</dependency>
7985

8086
<dependency>
@@ -85,5 +91,13 @@
8591

8692
</dependencies>
8793

88-
94+
<build>
95+
<finalName>winfoom</finalName>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.springframework.boot</groupId>
99+
<artifactId>spring-boot-maven-plugin</artifactId>
100+
</plugin>
101+
</plugins>
102+
</build>
89103
</project>

src/main/java/org/kpax/winfoom/FoomApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class FoomApplication {
1313

1414
public static void main(String[] args) {
15+
System.setProperty("java.awt.headless", "false");
1516
Application.launch(JavafxApplication.class, args);
1617
}
1718

src/main/java/org/kpax/winfoom/JavafxApplication.java

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,41 @@
55
import javafx.fxml.FXMLLoader;
66
import javafx.scene.Parent;
77
import javafx.scene.Scene;
8+
import javafx.scene.control.Alert;
9+
import javafx.scene.control.ButtonType;
810
import javafx.scene.layout.BorderPane;
911
import javafx.stage.Stage;
10-
import org.apache.commons.configuration2.PropertiesConfiguration;
12+
import javafx.stage.StageStyle;
13+
import javafx.stage.WindowEvent;
1114
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
1215
import org.apache.commons.configuration2.builder.fluent.Configurations;
16+
import org.kpax.winfoom.config.UserConfig;
17+
import org.kpax.winfoom.proxy.LocalProxyServer;
1318
import org.kpax.winfoom.util.LocalIOUtils;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
1421
import org.springframework.boot.SpringApplication;
1522
import org.springframework.context.ApplicationContextInitializer;
1623
import org.springframework.context.ConfigurableApplicationContext;
17-
import org.springframework.context.annotation.Bean;
1824
import org.springframework.context.support.GenericApplicationContext;
1925
import org.springframework.core.io.Resource;
2026

27+
import java.awt.*;
28+
import java.awt.event.MouseAdapter;
29+
import java.awt.event.MouseEvent;
30+
import java.io.File;
31+
import java.util.Optional;
32+
2133
public class JavafxApplication extends Application {
2234

35+
private final Logger logger = LoggerFactory.getLogger(JavafxApplication.class);
36+
2337
private ConfigurableApplicationContext applicationContext;
2438

2539
private Stage primaryStage;
2640

2741
@Override
2842
public void init() throws Exception {
29-
3043
ApplicationContextInitializer<GenericApplicationContext> initializer = new ApplicationContextInitializer<GenericApplicationContext>() {
3144
@Override
3245
public void initialize(GenericApplicationContext genericApplicationContext) {
@@ -46,20 +59,86 @@ public void initialize(GenericApplicationContext genericApplicationContext) {
4659
@Override
4760
public void start(Stage primaryStage) throws Exception {
4861
this.primaryStage = primaryStage;
62+
63+
Platform.setImplicitExit(false);
64+
4965
Resource fxml = this.applicationContext.getResource("classpath:/view/main.fxml");
5066
FXMLLoader fxmlLoader = new FXMLLoader(fxml.getURL());
5167
fxmlLoader.setControllerFactory(this.applicationContext::getBean);
5268
Parent root = fxmlLoader.load();
69+
5370
Scene scene = new Scene(root);
54-
primaryStage.setScene(scene);
55-
primaryStage.setTitle("WinFoom");
56-
primaryStage.show();
71+
this.primaryStage.setScene(scene);
72+
this.primaryStage.setTitle("WinFoom");
73+
this.primaryStage.getIcons().add(
74+
new javafx.scene.image.Image(new File("./config/img/icon.png").toURI().toURL().toExternalForm()));
75+
76+
if (SystemTray.isSupported()) {
77+
Image iconImage = Toolkit.getDefaultToolkit().getImage("config/img/icon.png");
78+
final TrayIcon trayIcon = new TrayIcon(iconImage, "Basic Proxy Facade");
79+
trayIcon.setImageAutoSize(true);
80+
trayIcon.addMouseListener(new MouseAdapter() {
81+
@Override
82+
public void mouseClicked(MouseEvent e) {
83+
Platform.runLater(() -> {
84+
primaryStage.setIconified(false);
85+
primaryStage.show();
86+
});
87+
}
88+
});
89+
final SystemTray tray = SystemTray.getSystemTray();
90+
this.primaryStage.iconifiedProperty().addListener((observableValue, oldVal, newVal) -> {
91+
if (newVal != null) {
92+
if (newVal) {
93+
try {
94+
tray.add(trayIcon);
95+
Platform.runLater(() -> {
96+
primaryStage.hide();
97+
});
98+
} catch (AWTException ex) {
99+
logger.error("Cannot add icon to tray", ex);
100+
}
101+
} else {
102+
tray.remove(trayIcon);
103+
}
104+
}
105+
});
106+
} else {
107+
logger.warn("Icon tray not supported!");
108+
}
109+
110+
this.primaryStage.show();
111+
112+
scene.getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, event -> {
113+
if (this.applicationContext.getBean(LocalProxyServer.class).isStarted()) {
114+
Alert alert =
115+
new Alert(Alert.AlertType.NONE,
116+
"The local proxy facade is started. \nDo you like to stop the proxy facade and leave the application?",
117+
ButtonType.OK,
118+
ButtonType.CANCEL);
119+
alert.initStyle(StageStyle.UTILITY);
120+
alert.setTitle("Warning");
121+
Optional<ButtonType> result = alert.showAndWait();
122+
if (result.get() == ButtonType.OK) {
123+
try {
124+
applicationContext.getBean(UserConfig.class).save();
125+
} catch (Exception e) {
126+
logger.error("Error on saving user configuration", e);
127+
}
128+
Platform.exit();
129+
} else {
130+
event.consume();
131+
}
132+
} else {
133+
Platform.exit();
134+
}
135+
});
57136
}
58137

59138
@Override
60139
public void stop() throws Exception {
140+
logger.info("Close the Spring context");
61141
this.applicationContext.close();
62-
Platform.exit();
63142
}
64143

65144

src/main/java/org/kpax/winfoom/auth/Authentication.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/org/kpax/winfoom/config/UserConfig.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
2020
import org.apache.commons.configuration2.ex.ConfigurationException;
2121
import org.apache.commons.lang3.StringUtils;
22+
import org.kpax.winfoom.exception.CommandExecutionException;
23+
import org.kpax.winfoom.util.CommandExecutor;
2224
import org.springframework.beans.factory.annotation.Autowired;
2325
import org.springframework.beans.factory.annotation.Value;
2426
import org.springframework.context.annotation.PropertySource;
2527
import org.springframework.stereotype.Component;
2628

2729
import javax.annotation.PostConstruct;
30+
import java.util.Arrays;
31+
import java.util.Optional;
2832

2933
/**
3034
* @author Eugen Covaci
@@ -41,33 +45,36 @@ public class UserConfig {
4145

4246
private String password;
4347

44-
@Value("${proxy.domain}")
45-
private String domain;
4648

4749
@Value("${local.port:3129}")
4850
private int localPort;
4951

50-
@Value("${proxy.host:localhost}")
52+
@Value("${proxy.host}")
5153
private String proxyHost;
5254

5355
@Value("${proxy.test.url}")
5456
private String proxyTestUrl;
5557

56-
@Value("${proxy.port:80}")
58+
@Value("${proxy.port:0}")
5759
private int proxyPort;
5860

5961
@PostConstruct
6062
public void init() {
61-
if (StringUtils.isEmpty(username)) {
62-
username = System.getProperty("user.name");
63-
}
64-
if (StringUtils.isEmpty(domain)) {
65-
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
66-
if (isWindows) {
67-
domain = System.getenv("USERDOMAIN");
63+
if (StringUtils.isEmpty(proxyHost)) {
64+
try {
65+
CommandExecutor.getSystemProxy().ifPresent((s) -> {
66+
System.out.println("proxyLine: " + s);
67+
String[] split = s.split("\\:");
68+
proxyHost = split[0];
69+
proxyPort = Integer.parseInt(split[1]);
70+
});
71+
} catch (CommandExecutionException e) {
72+
e.printStackTrace();
6873
}
74+
6975
}
7076

77+
7178
}
7279

7380
public String getUsername() {
@@ -78,14 +85,6 @@ public void setUsername(String username) {
7885
this.username = username;
7986
}
8087

81-
public String getDomain() {
82-
return domain;
83-
}
84-
85-
public void setDomain(String domain) {
86-
this.domain = domain;
87-
}
88-
8988
public String getPassword() {
9089
return password;
9190
}
@@ -122,20 +121,22 @@ public String getProxyTestUrl() {
122121
return proxyTestUrl;
123122
}
124123

124+
public void setProxyTestUrl(String proxyTestUrl) {
125+
this.proxyTestUrl = proxyTestUrl;
126+
}
127+
125128
public void save() throws ConfigurationException {
126129
Configuration config = propertiesBuilder.getConfiguration();
127-
config.setProperty("proxy.domain", this.domain);
128130
config.setProperty("local.port", this.localPort);
129131
config.setProperty("proxy.host", this.proxyHost);
130132
config.setProperty("proxy.port", this.proxyPort);
131-
config.setProperty("proxy.username", this.username);
132133
config.setProperty("proxy.test.url", this.proxyTestUrl);
133134
propertiesBuilder.save();
134135
}
135136

136137
@Override
137138
public String toString() {
138-
return "UserConfig [username=" + username + ", domain=" + domain + ", localPort=" + localPort
139+
return "UserConfig [username=" + username + ", localPort=" + localPort
139140
+ ", proxyHost=" + proxyHost + ", proxyPort=" + proxyPort + "]";
140141
}
141142

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.kpax.winfoom.exception;
2+
3+
/**
4+
* Thrown when command line execution fails.
5+
*/
6+
public class CommandExecutionException extends Exception {
7+
8+
private static final long serialVersionUID = -1082218489871862086L;
9+
10+
public CommandExecutionException(String message) {
11+
super(message);
12+
}
13+
14+
public CommandExecutionException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public CommandExecutionException(Throwable cause) {
19+
super(cause);
20+
}
21+
22+
public CommandExecutionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
23+
super(message, cause, enableSuppression, writableStackTrace);
24+
}
25+
}

0 commit comments

Comments
 (0)