Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit 678763e

Browse files
committed
Introduce basic authentication, improve ActorServlet
1 parent d7341c4 commit 678763e

25 files changed

+185
-174
lines changed

pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
1212
<module>server</module>
13-
<module>webapp</module>
1413
</modules>
1514

1615

server/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
<artifactId>jetty-servlet</artifactId>
3030
<version>9.4.25.v20191220</version>
3131
</dependency>
32-
<dependency>
33-
<groupId>org.eclipse.jetty</groupId>
34-
<artifactId>jetty-webapp</artifactId>
35-
<version>9.4.25.v20191220</version>
36-
</dependency>
3732
<dependency>
3833
<groupId>com.h2database</groupId>
3934
<artifactId>h2</artifactId>

server/src/main/java/com/balinski/api_project/Main.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
public class Main {
12-
12+
//"name":"jakub","token":"7f078d288631a4de5fc1ce01fa4e0addeebed19ad5c6d6f543fcc160c5c5cdfc"
1313
public static void main(String[] args) {
1414
//testDatabase();
1515
runApplication();

server/src/main/java/com/balinski/api_project/database/DatabaseProxy.java

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static int queryUpdate(String sql, boolean transaction) throws DatabaseEx
6969
connection.commit();
7070
}
7171
} catch (SQLException e) {
72+
e.printStackTrace();
7273
throw new DatabaseException("An error occurred when trying to update the database.", e);
7374
} finally {
7475
closeConnection();

server/src/main/java/com/balinski/api_project/database/dao/ActorDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.*;
66

77
public class ActorDao extends Dao<Actor> {
8-
public ActorDao(boolean transaction) {
9-
super(DaoType.ACTOR, transaction);
8+
public ActorDao() {
9+
super(DaoType.ACTOR);
1010
}
1111

1212
public List<Actor> getByFirstName(String firstName) throws DaoException {

server/src/main/java/com/balinski/api_project/database/dao/Dao.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import java.util.*;
66

77
abstract class Dao<T extends DatabaseModel> {
8-
protected boolean transaction;
98
protected DaoType type;
109

11-
protected Dao(DaoType type, boolean transaction) {
10+
protected Dao(DaoType type) {
1211
this.type = type;
13-
this.transaction = transaction;
1412
}
1513

1614
public int getCount() throws DaoException {
@@ -52,10 +50,19 @@ public int add(T obj) throws DaoException {
5250

5351
String sql = String.format("INSERT INTO %s VALUES (%s);", type.toString(), obj.asCsv());
5452

55-
return DaoManager.modifyData(sql, transaction);
53+
return DaoManager.modifyData(sql, false);
5654
}
5755

58-
public int addAll(List<T> list) throws DaoException {
56+
public int update(T obj) throws DaoException {
57+
if(obj == null)
58+
return 0;
59+
60+
String sql = String.format("INSERT INTO %s VALUES (%s);", type.toString(), obj.asCsv());
61+
62+
return DaoManager.modifyData(sql, false);
63+
}
64+
65+
public int addAll(List<T> list, boolean transaction) throws DaoException {
5966
if(list == null || list.size() == 0)
6067
return 0;
6168

server/src/main/java/com/balinski/api_project/database/dao/DaoManager.java

+4-20
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,19 @@ public class DaoManager {
1010
private DaoManager(){}
1111

1212
public static ActorDao getActorDao() {
13-
return new ActorDao(false);
13+
return new ActorDao();
1414
}
1515

1616
public static FilmDao getFilmDao() {
17-
return new FilmDao(false);
17+
return new FilmDao();
1818
}
1919

2020
public static LanguageDao getLanguageDao() {
21-
return new LanguageDao(false);
21+
return new LanguageDao();
2222
}
2323

2424
public static UserDao getUserDao() {
25-
return new UserDao(false);
26-
}
27-
28-
public static ActorDao getActorDao(boolean transaction) {
29-
return new ActorDao( transaction);
30-
}
31-
32-
public static FilmDao getFilmDao(boolean transaction) {
33-
return new FilmDao(transaction);
34-
}
35-
36-
public static LanguageDao getLanguageDao (boolean transaction) {
37-
return new LanguageDao( transaction);
38-
}
39-
40-
public static UserDao getUserDao (boolean transaction) {
41-
return new UserDao(transaction);
25+
return new UserDao();
4226
}
4327

4428
static List<Map<String, Object>> getData(String sql) throws DaoException {

server/src/main/java/com/balinski/api_project/database/dao/FilmDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
public class FilmDao extends Dao<Film> {
1212
static final DateTimeFormatter toDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
1313

14-
public FilmDao(boolean transaction) {
15-
super(DaoType.FILM, transaction);
14+
public FilmDao() {
15+
super(DaoType.FILM);
1616
}
1717

1818
public List<Film> getByTitle(String title) throws DaoException {

server/src/main/java/com/balinski/api_project/database/dao/LanguageDao.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.*;
66

77
public class LanguageDao extends Dao<Language> {
8-
public LanguageDao(boolean transaction) {
9-
super(DaoType.LANGUAGE, transaction);
8+
public LanguageDao() {
9+
super(DaoType.LANGUAGE);
1010
}
1111

1212
public List<Language> getByName(String name) throws DaoException {

server/src/main/java/com/balinski/api_project/database/dao/UserDao.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.balinski.api_project.database.model.User;
44

5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
57
import java.util.List;
68
import java.util.Map;
79

810
public class UserDao extends Dao<User> {
9-
public UserDao(boolean transaction) {
10-
super(DaoType.USER, transaction);
11+
static final DateTimeFormatter toDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
12+
public UserDao() {
13+
super(DaoType.USER);
1114
}
1215

1316
public List<User> getByName(String name) throws DaoException {
@@ -17,4 +20,21 @@ public List<User> getByName(String name) throws DaoException {
1720

1821
return toListOfObjects(result);
1922
}
23+
24+
public List<User> getByToken(String token) throws DaoException {
25+
List<Map<String, Object>> result = DaoManager.getData(
26+
String.format("SELECT * FROM USER U WHERE lower(U.token) = '%s';", token.toLowerCase())
27+
);
28+
29+
return toListOfObjects(result);
30+
}
31+
32+
public boolean incrementUses(int id) throws DaoException {
33+
return DaoManager.modifyData(
34+
String.format("UPDATE USER SET REQUESTS_SENT=" +
35+
"(SELECT U.REQUESTS_SENT FROM USERS U WHERE U.ID=%d)+1, " +
36+
"LAST_UPDATE='%s' " +
37+
"WHERE U.ID=%d;", id, LocalDateTime.now().format(toDateTime), id),
38+
true) > 0;
39+
}
2040
}

server/src/main/java/com/balinski/api_project/database/model/Actor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public String asJson() {
5252

5353
@Override
5454
public String asCsv() {
55-
return String.format("'%s', '%s', TIMESTAMP '%s'",
56-
firstName, lastName, lastUpdate.format(toDateTime));
55+
return String.format("%d, '%s', '%s', TIMESTAMP '%s'",
56+
id, firstName, lastName, lastUpdate.format(toDateTime));
5757
}
5858
}

server/src/main/java/com/balinski/api_project/database/model/DatabaseModel.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.time.format.DateTimeFormatter;
44

55
public abstract class DatabaseModel {
6-
DateTimeFormatter toDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
7-
DateTimeFormatter toDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
6+
static DateTimeFormatter toDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
7+
static DateTimeFormatter toDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
88

99
public abstract String asCsv();
1010
public abstract String asJson();

server/src/main/java/com/balinski/api_project/database/model/Film.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public String asJson() {
108108

109109
@Override
110110
public String asCsv() {
111-
return String.format("'%s', '%s', TIMESTAMP '%s', %d, %d, %s, %d, TIMESTAMP '%s'",
112-
title, description, releaseYear.format(toDate),
111+
return String.format("%d, '%s', '%s', TIMESTAMP '%s', %d, %d, %s, %d, TIMESTAMP '%s'",
112+
id, title, description, releaseYear.format(toDate),
113113
languageId, rentalDuration, rentalRate.toPlainString(), length,
114114
lastUpdate.format(toDateTime));
115115
}

server/src/main/java/com/balinski/api_project/database/model/Language.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String asJson() {
4141

4242
@Override
4343
public String asCsv() {
44-
return String.format("'%s', TIMESTAMP '%s'",
45-
name, lastUpdate.format(toDateTime));
44+
return String.format("%d, '%s', TIMESTAMP '%s'",
45+
id, name, lastUpdate.format(toDateTime));
4646
}
4747
}

server/src/main/java/com/balinski/api_project/database/model/User.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public String asJson(){
6363
return String.format("{\"type\":\"%s\",\"id\":\"%d\",\"attributes\":" +
6464
"{\"name\":\"%s\",\"token\":\"%s\",\"used\":\"%d\",\"limit\":\"%d\"," +
6565
"\"dateRegistered\":\"%s\",\"lastUpdate\":\"%s\"}}",
66-
"users", id, name, token, used, limit, dateRegistered.format(toDate), lastUpdate.format(toDateTime));
66+
"users", id, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
6767
}
6868

6969
@Override
7070
public String asCsv() {
71-
return String.format("'%s', '%s', %d, %d, TIMESTAMP '%s', TIMESTAMP '%s'",
72-
name, token, used, limit, dateRegistered.format(toDate), lastUpdate.format(toDateTime));
71+
return String.format("%d, '%s', '%s', %d, %d, TIMESTAMP '%s', TIMESTAMP '%s'",
72+
id, name, token, used, limit, dateRegistered.format(toDateTime), lastUpdate.format(toDateTime));
7373
}
7474
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package com.balinski.api_project.server;
22

3-
import com.balinski.api_project.server.handler.WebAppContextWrapper;
3+
import com.balinski.api_project.servlet.*;
44
import org.eclipse.jetty.server.Server;
5+
import org.eclipse.jetty.servlet.ServletContextHandler;
56

67
public class JettyServer {
8+
private static ServletContextHandler context = new ServletContextHandler();
79

810
public static void start(int port) {
911
final Server server = new Server(port);
10-
11-
WebAppContextWrapper contextHandler = new WebAppContextWrapper();
12-
server.setHandler(contextHandler.getWebAppContext());
12+
context.setContextPath("/");
13+
registerServlets();
14+
server.setHandler(context);
1315

1416
try {
1517
server.start();
1618
} catch (Exception e) {
1719
throw new RuntimeException("Could not start the server.", e);
1820
}
1921
}
22+
23+
24+
private static void registerServlets() {
25+
context.addServlet(ActorServlet.class, "/actors");
26+
context.addServlet(FilmServlet.class, "/films/*");
27+
context.addServlet(LanguageServlet.class, "/languages/*");
28+
context.addServlet(UserServlet.class, "/users/*");
29+
context.addServlet(AddUserServlet.class, "/adduser/*");
30+
}
31+
2032
}

server/src/main/java/com/balinski/api_project/server/handler/WebAppContextWrapper.java

-33
This file was deleted.

server/src/main/java/com/balinski/api_project/servlet/ActorServlet.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.balinski.api_project.database.dao.DaoManager;
44
import com.balinski.api_project.database.dao.DaoException;
55
import com.balinski.api_project.database.model.Actor;
6+
import com.balinski.api_project.database.model.User;
67
import com.balinski.api_project.servlet.util.JsonResponseBuilder;
8+
import com.balinski.api_project.servlet.util.UserAuthenticator;
79

810
import javax.servlet.http.HttpServlet;
911
import javax.servlet.http.HttpServletRequest;
@@ -22,17 +24,20 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
2224

2325
var enumeration = req.getParameterNames();
2426

25-
var supportedParameters = Set.of("id", "page", "perPage", "firstName", "lastName", "order");
27+
var supportedParameters = Set.of("id", "page", "perPage", "firstName", "lastName", "order", "user", "token");
2628
var param = Collections.list(enumeration)
2729
.stream()
2830
.distinct()
2931
.filter(supportedParameters::contains)
3032
.collect(Collectors.toMap(x -> x, req::getParameter));
3133

32-
List<Actor> actors;
33-
var dao = DaoManager.getActorDao();
34-
3534
try {
35+
User user = UserAuthenticator.authenticateAndGet(param.get("user"), param.get("token"));
36+
UserAuthenticator.incrementUses(user);
37+
38+
List<Actor> actors;
39+
var dao = DaoManager.getActorDao();
40+
3641
if(param.get("id") != null)
3742
actors = dao.getById(Integer.parseInt(param.get("id")));
3843
else if(param.get("firstName") != null)
@@ -57,9 +62,7 @@ else if(param.get("perPage") != null && param.get("page") != null) {
5762
String response = JsonResponseBuilder.mergeFromList(actors);
5863
writer.print(response);
5964
} catch (DaoException e) {
60-
System.err.println("An error occurred in ActorServlet: " + e.getMessage());
6165
writer.print(JsonResponseBuilder.getErrorJson(e));
62-
e.printStackTrace();
6366
}
6467
}
6568
}

0 commit comments

Comments
 (0)