-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExchangeRateServlet.java
175 lines (149 loc) · 6.46 KB
/
ExchangeRateServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pet.project.servlet.exchange;
import com.fasterxml.jackson.databind.ObjectMapper;
import pet.project.model.ExchangeRate;
import pet.project.model.response.ErrorResponse;
import pet.project.repository.ExchangeRepository;
import pet.project.repository.JdbcExchangeRepository;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Optional;
import static javax.servlet.http.HttpServletResponse.*;
import static pet.project.utils.Validation.isValidCurrencyCode;
@WebServlet(name = "ExchangeRateServlet", urlPatterns = "/exchangeRate/*")
public class ExchangeRateServlet extends HttpServlet {
private final ExchangeRepository exchangeRepository = new JdbcExchangeRepository();
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getMethod().equalsIgnoreCase("PATCH")) {
doPatch(req, resp);
} else {
super.service(req, resp);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String url = req.getPathInfo().replaceAll("/", "");
if (url.length() != 6) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Currency codes are either not provided or provided in an incorrect format"
));
return;
}
String baseCurrencyCode = url.substring(0, 3);
String targetCurrencyCode = url.substring(3);
if (!isValidCurrencyCode(baseCurrencyCode)) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Base currency code must be in ISO 4217 format"
));
return;
}
if (!isValidCurrencyCode(targetCurrencyCode)) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Target currency code must be in ISO 4217 format"
));
return;
}
try {
Optional<ExchangeRate> exchangeRateOptional = exchangeRepository.findByCodes(baseCurrencyCode, targetCurrencyCode);
if (exchangeRateOptional.isEmpty()) {
resp.setStatus(SC_NOT_FOUND);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_NOT_FOUND,
"There is no exchange rate for this currency pair"
));
return;
}
objectMapper.writeValue(resp.getWriter(), exchangeRateOptional.get());
} catch (SQLException e) {
resp.setStatus(SC_INTERNAL_SERVER_ERROR);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_INTERNAL_SERVER_ERROR,
"Something happened with the database, try again later!"
));
}
}
protected void doPatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url = req.getPathInfo().replaceAll("/", "");
if (url.length() != 6) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Currency codes are either not provided or provided in an incorrect format"
));
return;
}
String parameter = req.getReader().readLine();
if (parameter == null || !parameter.contains("rate")) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Missing required parameter rate"
));
return;
}
String baseCurrencyCode = url.substring(0, 3);
String targetCurrencyCode = url.substring(3);
String paramRateValue = parameter.replace("rate=", "");
if (!isValidCurrencyCode(baseCurrencyCode)) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Base currency code must be in ISO 4217 format"
));
return;
}
if (!isValidCurrencyCode(targetCurrencyCode)) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Target currency code must be in ISO 4217 format"
));
return;
}
BigDecimal rate;
try {
rate = BigDecimal.valueOf(Double.parseDouble(paramRateValue));
} catch (NumberFormatException e) {
resp.setStatus(SC_BAD_REQUEST);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_BAD_REQUEST,
"Incorrect value of rate parameter"
));
return;
}
try {
Optional<ExchangeRate> exchangeRateOptional = exchangeRepository.findByCodes(baseCurrencyCode, targetCurrencyCode);
if (exchangeRateOptional.isEmpty()) {
resp.setStatus(SC_NOT_FOUND);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_NOT_FOUND,
"There is no exchange rate for this currency pair"
));
return;
}
ExchangeRate exchangeRate = exchangeRateOptional.get();
exchangeRate.setRate(rate);
exchangeRepository.update(exchangeRate);
objectMapper.writeValue(resp.getWriter(), exchangeRate);
} catch (SQLException e) {
resp.setStatus(SC_INTERNAL_SERVER_ERROR);
objectMapper.writeValue(resp.getWriter(), new ErrorResponse(
SC_INTERNAL_SERVER_ERROR,
"Something happened with the database, try again later!"
));
}
}
}