requests-simulator

Details

.gitignore 34(+34 -0)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4a482b8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,34 @@
+# ignore eclipse project files
+.project
+.classpath
+
+# ignore Intellij Idea project files
+.idea
+*.iml
+
+# Netbeans
+/nbproject/
+/nbactions.xml
+
+# Netbeans deploy
+/build/
+
+# Maven deploy
+/target/
+
+# Ant deploy
+/dist/
+
+# Class files
+*.class
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.ear
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*

pom.xml 52(+52 -0)

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..515e53c
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>br.ufrgs.inf.prosoft.requestssimulator</groupId>
+    <artifactId>RequestsSimulator</artifactId>
+    <version>1.0</version>
+    <packaging>jar</packaging>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>
+                                ${project.build.directory}/lib
+                            </outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>3.1.0</version>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <classpathPrefix>lib</classpathPrefix> 
+                            <mainClass>br.ufrgs.inf.prosoft.requestssimulator.Main</mainClass>
+                        </manifest>
+                        <manifestEntries>
+                            <Class-Path>lib/</Class-Path>
+                        </manifestEntries>
+                    </archive>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
new file mode 100644
index 0000000..28dc131
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
@@ -0,0 +1,88 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package br.ufrgs.inf.prosoft.requestssimulator;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.function.Consumer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Stream;
+
+/**
+ *
+ * @author romulo
+ */
+public class Main {
+
+    private static final Logger logger = Logger.getLogger(Main.class.getName());
+
+    public static void main(String[] args) {
+        System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT+%1$tL] [%4$-7s] [RequestsSimulator] %5$s %n");
+
+        String path = null;
+        if (args.length < 1) {
+            System.err.println("<RequestsPath>");
+            System.exit(1);
+        } else {
+            path = args[0];
+        }
+
+        try (Stream<String> lines = Files.lines(Paths.get(path))) {
+            long count = Files.lines(Paths.get(path)).count();
+            logger.log(Level.INFO, "requesting {0} URLS", count);
+            lines.forEach(new Consumer<String>() {
+                private int i = 1;
+
+                @Override
+                public void accept(String line) {
+                    try {
+                        if (line.startsWith("POST")) {
+                            String[] split = line.split(" ");
+                            URL url = new URL(split[1]);
+                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+                            connection.setRequestMethod("POST");
+                            connection.setDoOutput(true);
+                            try (OutputStream outputStream = connection.getOutputStream()) {
+                                outputStream.write(split[2].getBytes());
+                            }
+                            int responseCode = connection.getResponseCode();
+                            if (responseCode == 200) {
+                                logger.log(Level.INFO, "{0} {1}", new Object[]{i, line});
+                            } else {
+                                logger.log(Level.SEVERE, "{0} error {1} on {2}", new Object[]{i, responseCode, line});
+                            }
+                            connection.disconnect();
+                        } else {
+                            URL url = new URL(line);
+                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+                            connection.setRequestMethod("GET");
+                            int responseCode = connection.getResponseCode();
+                            if (responseCode == 200) {
+                                logger.log(Level.INFO, "{0} GET {1}", new Object[]{i, line});
+                            } else {
+                                logger.log(Level.SEVERE, "{0} error {1} on {2}", new Object[]{i, responseCode, line});
+                            }
+                            connection.disconnect();
+                        }
+                    } catch (MalformedURLException ex) {
+                        logger.log(Level.SEVERE, "Malormed URL");
+                    } catch (IOException ex) {
+                        logger.log(Level.SEVERE, "IOException");
+                    }
+                    i++;
+                }
+            });
+        } catch (IOException ex) {
+            logger.log(Level.SEVERE, "cannot open file {0}", path);
+        }
+    }
+}