diff --git a/.gitignore b/.gitignore index 1cf8079..b651bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ logs/* target/ starttime.txt overtime.txt -highlighted \ No newline at end of file +highlighted +proxy.txt diff --git a/src/main/java/rvc/timecalc/HttpProxy.java b/src/main/java/rvc/timecalc/HttpProxy.java new file mode 100644 index 0000000..a3eda2e --- /dev/null +++ b/src/main/java/rvc/timecalc/HttpProxy.java @@ -0,0 +1,52 @@ +package rvc.timecalc; + +import java.io.File; +import java.io.IOException; + +/** + * @author Robert + * @since 16.02.2024 + */ +public class HttpProxy { + private final String url, port, user, password; + + public HttpProxy(String url, String port, String user, + String password) { + this.url = url; + this.port = port; + this.user = user; + this.password = password; + } + + public HttpProxy(File proxyTxt) { + try { + String[] str = Utils.readTextFromFile(proxyTxt).split(":"); + if(str.length < 4) { + proxyTxt.delete(); + throw new IOException("Invalid content of proxy.txt: str.length < 4"); + } + this.url = str[0]; + this.port = str[1]; + this.user = str[2]; + this.password = str[3]; + } catch (IOException e) { + throw new RuntimeException("Sorry, reading file proxy.txt failed. " + e.getMessage()); + } + } + + public String getUrl() { + return url; + } + + public String getPort() { + return port; + } + + public String getUser() { + return user; + } + + public String getPassword() { + return password; + } +} diff --git a/src/main/java/rvc/timecalc/TimeCalcWindow.java b/src/main/java/rvc/timecalc/TimeCalcWindow.java index 5dc348e..7de1887 100644 --- a/src/main/java/rvc/timecalc/TimeCalcWindow.java +++ b/src/main/java/rvc/timecalc/TimeCalcWindow.java @@ -72,10 +72,12 @@ public class TimeCalcWindow { JFrame window = new JFrame(); + JButton weatherButton = new JButton("Weather"); JButton jokeButton = new JButton("Joke"); JButton restartButton = new JButton("Restart"); JButton exitButton = new JButton("Exit"); + //window.add(weatherButton); window.add(jokeButton); window.add(restartButton); window.add(exitButton); @@ -85,7 +87,8 @@ public class TimeCalcWindow { text.setForeground(Color.GRAY); text.setBackground(new Color(238, 238, 238)); window.add(text); - jokeButton.setBounds(120, text.getY() + text.getHeight() + 10, 100, 30); + weatherButton.setBounds(20, text.getY() + text.getHeight() + 10, 100, 30); + jokeButton.setBounds(140, text.getY() + text.getHeight() + 10, 100, 30); restartButton.setBounds(280, text.getY() + text.getHeight() + 10, 100, 30); exitButton.setBounds(390, text.getY() + text.getHeight() + 10, 100, 30); @@ -101,6 +104,7 @@ public class TimeCalcWindow { System.exit(0); } }); + weatherButton.addActionListener(e-> new WeatherWindow().setVisible(true)); jokeButton.addActionListener(e-> {for(int i =1;i<=1;i++) {Vtipy.showRandom();}}); exitButton.addActionListener(e -> System.exit(0)); restartButton.addActionListener(e -> { diff --git a/src/main/java/rvc/timecalc/WeatherWindow.java b/src/main/java/rvc/timecalc/WeatherWindow.java new file mode 100644 index 0000000..d55a993 --- /dev/null +++ b/src/main/java/rvc/timecalc/WeatherWindow.java @@ -0,0 +1,138 @@ +package rvc.timecalc; + +import javax.net.ssl.HttpsURLConnection; +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.net.URL; + +/** + * @author Robert + * @since 16.02.2024 + */ +public class WeatherWindow extends JFrame { + public WeatherWindow() { + this.setSize(800, 600); + + JEditorPane jep = new JEditorPane(); + jep.setEditable(false); + JScrollPane scrollPane = new JScrollPane(jep); + scrollPane.setBounds(10, 10, 750, 550); + getContentPane().add(scrollPane); + + File proxyTxt = new File("proxy.txt"); + if(!proxyTxt.exists()) { + jep.setText("Sorry, file proxy.txt was not found."); + return; + } + final HttpProxy httpProxy; + try { + httpProxy = new HttpProxy(proxyTxt); + } catch(RuntimeException e) { + jep.setContentType("text/html"); + jep.setText(e.getMessage()); + return; + } + + + + try { + jep.setText(downloadFile3("https://pocasi.seznam.cz/praha", + httpProxy)); + }catch (Exception e) { + e.printStackTrace(); + jep.setContentType("text/html"); + jep.setText("Could not load " + e.getMessage() + ""); + } + } + + public String downloadFile2(String s, HttpProxy httpProxy) throws IOException { + + Authenticator.setDefault(new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(httpProxy.getUser(), + httpProxy.getPassword().toCharArray()); + } + }); + + Proxy proxy = new Proxy( + Proxy.Type.HTTP, new InetSocketAddress(httpProxy.getUrl(), Integer.valueOf(httpProxy.getPort()))); + + URL url = new URL(s); + System.getProperties().put( "proxySet", "true" ); + + System.setProperty("http.proxyHost", httpProxy.getUrl()); + System.setProperty("http.proxyPort", httpProxy.getPort()); + + System.setProperty("http.proxyUser", httpProxy.getUser()); + System.setProperty("http.proxyPassword", httpProxy.getPassword()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(connection.getInputStream()))) { + StringBuilder responseBuilder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + responseBuilder.append(line); + } + + return responseBuilder.toString(); + } + } + public String downloadFile3(String s, HttpProxy httpProxy) + throws IOException { + System.getProperties().put( "proxySet", "true" ); + + System.setProperty("http.proxyHost", httpProxy.getUrl()); + System.setProperty("http.proxyPort", httpProxy.getPort()); + + System.setProperty("http.proxyUser", httpProxy.getUser()); + System.setProperty("http.proxyPassword", httpProxy.getPassword()); + + String uri = s; + URL url = new URL(uri); + + Authenticator.setDefault(new Authenticator() { + @Override + public PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(httpProxy.getUser(), + httpProxy.getPassword().toCharArray()); + }}); + + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + + //connection.setDoOutput(true); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Content-Type", "text/html"); + connection.setRequestProperty("Accept", "text/html"); + connection.setRequestProperty("Method", "GET"); + connection.setRequestProperty("Encoding", "UTF-8"); + connection.setReadTimeout(60000); + + InputStream stream = connection.getInputStream(); + + InputStreamReader inputStreamReader = new InputStreamReader(stream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + StringBuilder content = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + content.append(line + "\n"); + } + + bufferedReader.close(); + return content.toString(); + } +}