Skip to content

Commit 17a2680

Browse files
committed
Add API WEB service with endpoint for IBAN validation
1 parent ec6f413 commit 17a2680

File tree

5 files changed

+118
-10
lines changed

5 files changed

+118
-10
lines changed

IBANList.txt.out

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

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<java.version>11</java.version>
1515
<maven.compiler.source>11</maven.compiler.source>
1616
<maven.compiler.target>11</maven.compiler.target>
17+
<jetty.version>9.4.30.v20200611</jetty.version>
1718
</properties>
1819

1920
<dependencies>
@@ -22,6 +23,21 @@
2223
<artifactId>commons-validator</artifactId>
2324
<version>1.6</version>
2425
</dependency>
26+
<dependency>
27+
<groupId>javax.servlet</groupId>
28+
<artifactId>javax.servlet-api</artifactId>
29+
<version>4.0.1</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.eclipse.jetty</groupId>
33+
<artifactId>jetty-server</artifactId>
34+
<version>${jetty.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.eclipse.jetty</groupId>
38+
<artifactId>jetty-servlet</artifactId>
39+
<version>${jetty.version}</version>
40+
</dependency>
2541
</dependencies>
2642

2743
<build>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package edu.task.technical.ibanvalidator.service;
2+
3+
import edu.task.technical.ibanvalidator.servlet.IBANValidationServlet;
4+
import org.eclipse.jetty.server.Connector;
5+
import org.eclipse.jetty.server.Server;
6+
import org.eclipse.jetty.server.ServerConnector;
7+
import org.eclipse.jetty.servlet.ServletHandler;
8+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
9+
10+
public class HttpService {
11+
12+
private Server httpJettyServer;
13+
14+
void start(int WebPort) throws Exception {
15+
16+
int maxThreads = 100;
17+
int minThreads = 10;
18+
int idleTimeout = 120;
19+
20+
QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, minThreads, idleTimeout);
21+
22+
httpJettyServer = new Server(threadPool);
23+
ServerConnector connector = new ServerConnector(httpJettyServer);
24+
connector.setPort(WebPort);
25+
httpJettyServer.setConnectors(new Connector[]{connector});
26+
27+
ServletHandler servletHandler = new ServletHandler();
28+
httpJettyServer.setHandler(servletHandler);
29+
30+
servletHandler.addServletWithMapping(IBANValidationServlet.class, "/api/ibanvalidator");
31+
32+
httpJettyServer.start();
33+
}
34+
35+
void stop() throws Exception {
36+
httpJettyServer.stop();
37+
}
38+
39+
}

src/main/java/edu/task/technical/ibanvalidator/service/IBANValidationService.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class IBANValidationService {
1717
// Get dependencies
1818
private final CLIService cliService = new CLIService();
1919
private final FileService fileService = new FileService();
20+
private final HttpService httpService = new HttpService();
21+
2022

2123
// Prints passed IBAN code and its validation to STDOUT
2224
private void printCodeAndValidation(String ibanCode) {
@@ -28,10 +30,9 @@ private void printCodeAndValidation(String ibanCode) {
2830
public void start(String[] args) {
2931
// Check if arguments from command line passed has an API argument
3032
if (args.length > 0) {
33+
// If so then run as WEB service and expose API endpoint
3134
if (args[0].toLowerCase().equals("api")) {
32-
// if so then run as WEB service and expose API endpoints
33-
// TODO: implement servlet to provide REST API service
34-
System.err.println("Sorry, API functionality has not been yet implemented");
35+
this.initiateAPIMode(args);
3536
} else {
3637
System.err.println("Unknown option, try 'api'");
3738
}
@@ -41,6 +42,30 @@ public void start(String[] args) {
4142
}
4243
}
4344

45+
private void initiateAPIMode(String[] args) {
46+
// Default port to 8080
47+
int port = 8080;
48+
49+
// If port passed as a second argument then parse it as integer and pass as the argument to a httpServer or exit program on error
50+
if (args.length > 1) {
51+
if (args[1] != null) {
52+
try {
53+
port = Integer.parseInt(args[1]);
54+
} catch (NumberFormatException exception) {
55+
System.err.println("Could not parse port number, make sure it is a number");
56+
exitProgram();
57+
}
58+
}
59+
}
60+
// Try to start Http Server, if any problem exit the program
61+
try {
62+
this.httpService.start(port);
63+
} catch (Exception e) {
64+
System.err.println("Could not start Http server");
65+
exitProgram();
66+
}
67+
}
68+
4469
// Overloaded procedure to start IBANValidationService, shortcut to run as CLI version
4570
public void start() {
4671
this.initiateCLIMainMenu();
@@ -50,6 +75,13 @@ public void start() {
5075
private void exitProgram() {
5176
System.out.println("\n\tProgram exiting,\n\t\t\t see you...");
5277

78+
// Wait for http server operations to finish before exiting program
79+
try {
80+
this.httpService.stop();
81+
} catch (Exception e) {
82+
System.err.println("Failed to gracefully stop Http Server");
83+
}
84+
5385
// exit the application
5486
System.exit(0);
5587
}
@@ -70,8 +102,8 @@ private void initiateCLIMainMenu() {
70102
this.readIBANsFileValidateSaveResult(this.ibanCodeList);
71103

72104
} else if (userOption == 99 || userOption == 67) { // if key == 'c' or C
73-
// TODO: invoke servlet API
74-
System.out.println("Sorry, API functionality has not been yet implemented");
105+
// Invoke servlet API
106+
this.initiateAPIMode(new String[]{});
75107

76108
} else if (userOption == 101 || userOption == 69) { // if key == 'e' or E
77109
// Exit the program
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package edu.task.technical.ibanvalidator.servlet;
2+
3+
import org.apache.commons.validator.routines.IBANValidator;
4+
5+
import javax.servlet.ServletException;
6+
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
public class IBANValidationServlet extends HttpServlet {
13+
14+
// Get a singleton instance of the IBAN validator using the default formats
15+
private final IBANValidator ibanValidator = IBANValidator.getInstance();
16+
17+
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
18+
19+
String iban = req.getParameter("iban");
20+
21+
res.setContentType("application/json");
22+
res.setStatus(HttpServletResponse.SC_OK);
23+
res.getWriter().println("{\"" + iban + "\": \"" + ibanValidator.isValid(iban) + "\"}");
24+
}
25+
26+
}

0 commit comments

Comments
 (0)