Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.

Commit b0dcc65

Browse files
authored
Merge pull request #12 from anas-elgarhy/add-docs
Add the java docs and improve the code
2 parents abb9b7b + 35b1f03 commit b0dcc65

File tree

6 files changed

+223
-83
lines changed

6 files changed

+223
-83
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.anas.jdosattacker;
22

3+
/**
4+
* `FieldException` is a custom exception class that is thrown when a field value is wrong.
5+
*/
36
public class FieldException extends Exception {
4-
public FieldException(String message) {
7+
public FieldException(final String message) {
58
super(message);
69
}
710
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.anas.jdosattacker;
22

3+
/**
4+
* Entry point of the program.
5+
*/
36
public class Main {
4-
public static void main(String[] args) {
7+
public static void main(final String[] args) {
58
new MainController(args); // Just it :D
69
}
710
}

src/main/java/com/anas/jdosattacker/MainController.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@
77
import java.io.IOException;
88
import java.util.ArrayList;
99

10+
/**
11+
* It's a controller class that creates threads and starts them
12+
*/
1013
public class MainController {
11-
public static final String version = "1.2.0";
14+
public static final String VERSION = "1.2.0";
1215
private static int threadsNumber = 0;
1316
private final ArrayList<Thread> threads;
1417

15-
public MainController(String[] args) {
18+
/**
19+
* The constructor.
20+
* @param args The arguments passed to the program
21+
*/
22+
public MainController(final String[] args) {
23+
// It's processing arguments from command line.
1624
new ArgumentsProcessor().process(args);
25+
// It's checking if there are important empty fields in the request.
1726
if (this.hasEmptyFields()) {
1827
try {
28+
// If true, start the tui and get the data from the user.
1929
new GetData();
20-
} catch (IOException e) {
30+
} catch (final IOException e) {
2131
System.err.println("Error: " + e.getMessage());
2232
System.exit(1);
2333
}
@@ -27,35 +37,57 @@ public MainController(String[] args) {
2737
startThreads();
2838
}
2939

40+
/**
41+
* Start all the threads in the threads array.
42+
*/
3043
private void startThreads() {
3144
for (Thread thread : threads) {
3245
thread.start();
3346
}
3447
}
3548

36-
public static void setThreadsNum(int threadsNum) throws FieldException {
49+
/**
50+
* Set the number of threads to use.
51+
*
52+
* @param threadsNum number of threads.
53+
* @throws FieldException if the number of threads is less than 1.
54+
*/
55+
public static void setThreadsNum(final int threadsNum) throws FieldException {
3756
if (threadsNum < 1) {
3857
throw new FieldException("Number of threads must be greater than 0");
3958
}
4059
threadsNumber = threadsNum;
4160
}
4261

43-
public static void setThreadsNum(String threadsNumber) throws FieldException {
62+
/**
63+
* Set the threads number from string.
64+
* @param threadsNumber string number of threads.
65+
* @throws FieldException if the number of threads is less than 1, or if it's not an integer number.
66+
*/
67+
public static void setThreadsNum(final String threadsNumber) throws FieldException {
4468
try {
45-
int threadsNum = Integer.parseInt(threadsNumber);
69+
final var threadsNum = Integer.parseInt(threadsNumber);
4670
MainController.setThreadsNum(threadsNum);
4771
} catch (NumberFormatException e) {
48-
throw new FieldException("Number of threads must be a number");
72+
throw new FieldException("Number of threads must be an integer number");
4973
}
5074
}
5175

76+
/**
77+
* It creates a new threads and set there names and add them to the threads array.
78+
*/
5279
public void createThreads() {
53-
for (int i = 0; i < threadsNumber; i++) {
80+
for (var i = 0; i < threadsNumber; i++) {
5481
threads.add(new Thread(new Requester()));
5582
threads.get(i).setName("Requester " + (i + 1));
5683
}
5784
}
5885

86+
/**
87+
* If the requester has empty fields or the number of threads is 0, then return true.
88+
*
89+
* @return A boolean value, representing if the requester has empty fields or the number of threads is 0.
90+
*/
5991
public boolean hasEmptyFields() {
6092
return Requester.hasRequirementEmptyFields() || threadsNumber == 0;
6193
}
Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package com.anas.jdosattacker.args;
22

3+
import com.anas.jdosattacker.FieldException;
34
import com.anas.jdosattacker.MainController;
45
import com.anas.jdosattacker.request.Requester;
56
import org.apache.commons.cli.*;
67

8+
/**
9+
* It parses the command line arguments and sets the corresponding variables, and if there are any errors, it prints the
10+
* help and exits
11+
*/
712
public class ArgumentsProcessor {
8-
private Options options;
9-
private CommandLineParser parser;
13+
private final Options options;
1014

15+
/**
16+
* The constructor.
17+
*/
1118
public ArgumentsProcessor() {
12-
initialize();
19+
options = new Options();
1320
setupOptions();
1421
}
1522

23+
/**
24+
* Set up the command line options.
25+
*/
1626
private void setupOptions() {
1727
options.addOption("h", "help", false, "Print this help");
1828
options.addOption("v", "version", false, "Print version");
@@ -24,45 +34,58 @@ private void setupOptions() {
2434
options.addOption("connectTimeout", true, "Connection timeout (default: 5000)");
2535
}
2636

27-
private void initialize() {
28-
options = new Options();
29-
parser = new DefaultParser();
30-
}
3137

32-
public void process(String[] args) {
38+
/**
39+
* It parses the command line arguments and sets the corresponding variables,
40+
* and if there are any errors, it prints the help and exits.
41+
* and if the arguments have a help or version flag, it prints the info and exits.
42+
*
43+
* @param args the command line arguments
44+
*/
45+
public void process(final String[] args) {
3346
try {
34-
CommandLine commandLine = parser.parse(options, args);
47+
final var commandLine = new DefaultParser().parse(options, args);
3548
if (commandLine.hasOption("help")) {
36-
printHelp();
49+
printHelp(0);
3750
} else if (commandLine.hasOption("version")) {
3851
printVersion();
39-
} else if (commandLine.hasOption("url"))
40-
Requester.setUrl(commandLine.getOptionValue("url"));
52+
}
4153

54+
if (commandLine.hasOption("url"))
55+
Requester.setUrl(commandLine.getOptionValue("url"));
4256
if (commandLine.hasOption("threads"))
43-
MainController.setThreadsNum(Integer.parseInt(commandLine.getOptionValue("threads")));
57+
MainController.setThreadsNum(commandLine.getOptionValue("threads"));
4458
if (commandLine.hasOption("number"))
45-
Requester.setReqNumber(Integer.parseInt(commandLine.getOptionValue("number")));
59+
Requester.setReqNumber(commandLine.getOptionValue("number"));
4660
if (commandLine.hasOption("connectTimeout"))
47-
Requester.setConnectTimeout(Integer.parseInt(commandLine.getOptionValue("connectTimeout")));
61+
Requester.setConnectTimeout(commandLine.getOptionValue("connectTimeout"));
4862
if (commandLine.hasOption("useragent"))
4963
Requester.setUserAgent(commandLine.getOptionValue("useragent"));
5064
if (commandLine.hasOption("requestMethod"))
5165
Requester.setRequestMethod(commandLine.getOptionValue("requestMethod"));
5266

53-
} catch (Exception e) {
67+
} catch (final ParseException | FieldException e) {
68+
// If an error occurs, print the error message and a help message and exit.
5469
System.err.println("Error: " + e.getMessage());
55-
printHelp();
56-
System.exit(1);
70+
printHelp(1);
5771
}
5872
}
5973

74+
/**
75+
* If the user types in the command line argument -v, this method will be called.
76+
*/
6077
private void printVersion() {
61-
System.out.println("Version: " + MainController.version);
78+
System.out.println("Version: " + MainController.VERSION);
6279
System.exit(0);
6380
}
6481

65-
private void printHelp() {
66-
new HelpFormatter().printHelp("java -jar jdosattacker.jar -u <URL>", options);
82+
/**
83+
* Prints the help message and exits the program.
84+
*
85+
* @param exitCode The exit code to use when exiting the application.
86+
*/
87+
private void printHelp(final int exitCode) {
88+
new HelpFormatter().printHelp("java -jar jdosattacker.jar [options]", options);
89+
System.exit(exitCode);
6790
}
6891
}

0 commit comments

Comments
 (0)