diff --git a/build.gradle b/build.gradle index 73b138e..cbc77ee 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ plugins { } group 'cn.reddragon' -version '1.0-SNAPSHOT' +version '1.2' repositories { mavenCentral() diff --git a/src/main/java/cn/reddragon/eportal/Authenticator.java b/src/main/java/cn/reddragon/eportal/Authenticator.java index 41e9582..c2aff6b 100644 --- a/src/main/java/cn/reddragon/eportal/Authenticator.java +++ b/src/main/java/cn/reddragon/eportal/Authenticator.java @@ -27,32 +27,36 @@ public static HttpURLConnection login(String username, String password, String s out.flush(); connection.connect(); return connection; - }catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); return null; } } - public static String getUserIndex() throws IOException { - if (!Authenticator.isOnline()) { - return null; + public static void getUserIndex() { + if (!Authenticator.getOnline()) { + userIndex = null; + } + try { + HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET"); + connection.setInstanceFollowRedirects(false); + connection.connect(); + Map> result = connection.getHeaderFields(); + String redirectLocation = result.get("Location").get(0); + userIndex = redirectLocation.substring(redirectLocation.indexOf('=') + 1); + } catch (IOException e) { + e.printStackTrace(); + userIndex = null; } - HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET"); - connection.setInstanceFollowRedirects(false); - connection.connect(); - Map> result = connection.getHeaderFields(); - String redirectLocation = result.get("Location").get(0); - String r = redirectLocation.substring(redirectLocation.indexOf('=') + 1); - userIndex = r; - return r; } - public static HttpURLConnection logout(String userIndex) { - String content = "userIndex=" + userIndex; + //经过测试,登出时不需要userIndex也能成功登出 + public static HttpURLConnection logout() { + String content = "userIndex=" + userIndex(); try { HttpURLConnection connection = HttpUtils.make(ePortalUrl + "logout", "POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - connection.setRequestProperty("Referer", "http://10.96.0.155/eportal/success.jsp?userIndex=" + userIndex); + connection.setRequestProperty("Referer", "http://10.96.0.155/eportal/success.jsp?userIndex=" + userIndex()); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.print(content); out.flush(); @@ -63,7 +67,18 @@ public static HttpURLConnection logout(String userIndex) { } } - public static boolean isOnline() { + public static boolean getOnline() { + return online; + } + + public static String userIndex() { + if (userIndex == null) { + getUserIndex(); + } + return userIndex; + } + + public static void checkOnline() { try { HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET"); connection.setInstanceFollowRedirects(false); @@ -72,14 +87,7 @@ public static boolean isOnline() { String redirectLocation = result.get("Location").get(0); if (Objects.equals(redirectLocation, "Http://123.123.123.123")) { online = false; - return false; - } else if (redirectLocation.contains("http://10.96.0.155/eportal/./success.jsp?")) { - online = true; - return true; - } else { - online = false; - return false; - } + } else online = redirectLocation.contains("http://10.96.0.155/eportal/./success.jsp?"); } catch (SocketTimeoutException e) { if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); @@ -95,7 +103,6 @@ public static boolean isOnline() { tray.remove(icon); System.exit(0); } - return false; } catch (NoRouteToHostException e) { if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); @@ -111,10 +118,8 @@ public static boolean isOnline() { tray.remove(icon); System.exit(0); } - return false; } catch (IOException e) { online = false; - return false; } } } diff --git a/src/main/java/cn/reddragon/eportal/HelloApplication.java b/src/main/java/cn/reddragon/eportal/HelloApplication.java index aea1bc3..55f38ad 100644 --- a/src/main/java/cn/reddragon/eportal/HelloApplication.java +++ b/src/main/java/cn/reddragon/eportal/HelloApplication.java @@ -25,10 +25,10 @@ public class HelloApplication extends Application { } catch (InterruptedException e) { //throw new RuntimeException(e); } - Authenticator.isOnline(); Platform.runLater(() -> { - statusLabel.setText("Current status:" + (Authenticator.online ? "Online" : "Offline")); - if (Authenticator.online) { + Authenticator.checkOnline(); + statusLabel.setText("Current status:" + (Authenticator.getOnline() ? "Online" : "Offline")); + if (Authenticator.getOnline()) { button.setText("Logout"); } else { button.setText("Login"); @@ -37,33 +37,9 @@ public class HelloApplication extends Application { } }); - @Override - public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); - Parent root = fxmlLoader.load(); - Scene scene = new Scene(root, 320, 240); - ChoiceBox box = (ChoiceBox) root.lookup("#selector"); - box.getItems().addAll("LAN", "WAN"); - box.setValue("WAN"); - statusLabel = (Label) root.lookup("#statusLabel"); - button = (Button) root.lookup("#button"); - stage.setTitle("EPortal"); - stage.setScene(scene); - stage.show(); - String[] config = Config.read(); - TextField name = (TextField) root.lookup("#userNameField"); - TextField pass = (TextField) root.lookup("#passwordField"); - name.setText(config[0]); - pass.setText(config[1]); - askThread.start(); - if (!Authenticator.online) { - button.getOnAction().handle(new ActionEvent()); - } - } - public static void main(String[] args) { try { - if (Authenticator.isOnline()) { + if (Authenticator.getOnline()) { System.out.println("Already connected!"); if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); @@ -83,9 +59,33 @@ public static void main(String[] args) { } catch (NullPointerException | AWTException e) { e.printStackTrace(); System.exit(0); - } catch (IOException ignored) { } launch(); System.exit(0); } + + @Override + public void start(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); + Parent root = fxmlLoader.load(); + Scene scene = new Scene(root, 320, 240); + ChoiceBox box = (ChoiceBox) root.lookup("#selector"); + box.getItems().addAll("LAN", "WAN"); + box.setValue("WAN"); + statusLabel = (Label) root.lookup("#statusLabel"); + button = (Button) root.lookup("#button"); + stage.setTitle("EPortal"); + stage.setScene(scene); + stage.show(); + String[] config = Config.read(); + TextField name = (TextField) root.lookup("#userNameField"); + TextField pass = (TextField) root.lookup("#passwordField"); + name.setText(config[0]); + pass.setText(config[1]); + askThread.start(); + Authenticator.checkOnline(); + if (!Authenticator.getOnline()) { + button.getOnAction().handle(new ActionEvent()); + } + } } \ No newline at end of file diff --git a/src/main/java/cn/reddragon/eportal/HelloController.java b/src/main/java/cn/reddragon/eportal/HelloController.java index 6e49584..02a0ff3 100644 --- a/src/main/java/cn/reddragon/eportal/HelloController.java +++ b/src/main/java/cn/reddragon/eportal/HelloController.java @@ -30,28 +30,27 @@ public class HelloController { @FXML protected void onLoginButtonClick() { - if (Authenticator.online) { + if (Authenticator.getOnline()) { if (button.getText().equals("Login")) { resultText.setText("Already logged in!"); button.setText("Logout"); } else if (button.getText().equals("Logout")) { try { - //经过测试,登出时不需要userIndex也能成功登出 - //为了避免不必要的麻烦,仍然传入userIndex - HttpURLConnection connection = Authenticator.logout(Authenticator.userIndex); + HttpURLConnection connection = Authenticator.logout(); if (connection == null) { resultText.setText("Error!"); return; } JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject(); System.out.println(resultMessage.toString()); - String result = resultMessage.get("result").getAsString(); - if (result.equals("success")) { + //String result = resultMessage.get("result").getAsString(); + resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString()); + /* if (result.equals("success")) { resultText.setText("success!" + resultMessage.get("message").getAsString()); button.setText("Login"); } else if (result.equals("fail")) { resultText.setText("fail!" + resultMessage.get("message").getAsString()); - } + }*/ } catch (Exception e) { e.printStackTrace(); } @@ -106,14 +105,19 @@ protected void onLoginButtonClick() { JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(loginConnection.getInputStream())).getAsJsonObject(); System.out.println(resultMessage.toString()); String result = resultMessage.get("result").getAsString(); - if (result.equals("fail")) { + resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString()); + if (result.equals("success")) { + Authenticator.userIndex = resultMessage.get("userIndex").getAsString(); + Config.save(username, password); + } + /*if (result.equals("fail")) { resultText.setText(resultMessage.get("message").getAsString()); } else if (result.equals("success")) { resultText.setText("Success!" + resultMessage.get("message").getAsString()); Authenticator.userIndex = resultMessage.get("userIndex").getAsString(); button.setText("Logout"); Config.save(username, password); - } + }*/ } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/cn/reddragon/eportal/utils/IOUtils.java b/src/main/java/cn/reddragon/eportal/utils/IOUtils.java index 5b0ba0d..6c58f13 100644 --- a/src/main/java/cn/reddragon/eportal/utils/IOUtils.java +++ b/src/main/java/cn/reddragon/eportal/utils/IOUtils.java @@ -1,20 +1,24 @@ package cn.reddragon.eportal.utils; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; public class IOUtils { public static String readText(InputStream stream) { try { - InputStreamReader reader = new InputStreamReader(stream); - StringBuilder sb = new StringBuilder(); + InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); + BufferedReader bufferedReader = new BufferedReader(reader); + return bufferedReader.readLine(); + /*StringBuilder sb = new StringBuilder(); int ch = reader.read(); while (ch != -1) { sb.append((char) ch); ch = reader.read(); } - return sb.toString(); + return sb.toString();*/ } catch (IOException e) { e.printStackTrace(); return "";