-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaxService.java
248 lines (222 loc) · 7.96 KB
/
TaxService.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package services;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; // import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import com.google.gson.Gson;
import model.TaxBean;
import model.TaxCollection;
/**
* An example microservice for retrieve the Tax information
* from the Tax table in the database based on the requested
* information.
*
* This service takes 2 request types: 'code_eq' and 'pst_gt'.
* When 'code_eq', the service takes a two letter province code and
* returns a single Tax record. When 'pst_gt', it takes a double, and
* returns a list of Tax records, all of which have PST greater than the
* given number. A third parameter specifies the response format,
* either 'xml' or 'json'.
*
* Usage from telnet, send:
*
* - code_eq <code> xml
* - code_eq <code> json
* - pst_gt <number> xml
* - pst_gt <number> json
*
* Examples:
*
* $ telnet 130.63.96.85 44760
* > code_eq ON xml
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* <tax>
* <code>ON</code>
* <gst>5.0</gst>
* <name>Ontario</name>
* <pst>8.0</pst>
* <type>HST</type>
* </tax>
*
* $ telnet 130.63.96.85 44760
* > code_eq ON json
* {"name":"Ontario","code":"ON","type":"HST","pst":8.0,"gst":5.0}
*
* $ telnet 130.63.96.85 44760
* > pst_gt 9.0 xml
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* <taxes>
* <tax>
* <code>NB</code>
* <gst>5.0</gst>
* <name>New-Brunswick</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>NL</code>
* <gst>5.0</gst>
* <name>Newfoundland and Labrador</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>NS</code>
* <gst>5.0</gst>
* <name>Nova Scotia</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>PE</code>
* <gst>5.0</gst>
* <name>Prince Edward Island</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>QC</code>
* <gst>5.0</gst>
* <name>Québec</name>
* <pst>9.975000000000001</pst>
* <type>QST+GST</type>
* </tax>
* </taxes>
*
* $ telnet 130.63.96.85 44760
* > pst_gt 9.0 json
* {"taxes":[{"name":"New-Brunswick","code":"NB","type":"HST","pst":10.0,"gst":5.0},
* {"name":"Newfoundland and Labrador","code":"NL","type":"HST","pst":10.0,"gst":5.0},
* {"name":"Nova Scotia","code":"NS","type":"HST","pst":10.0,"gst":5.0},
* {"name":"Prince Edward Island","code":"PE","type":"HST","pst":10.0,"gst":5.0},
* {"name":"Québec","code":"QC","type":"QST+GST","pst":9.975000000000001,"gst":5.0}]}
*
*/
public class TaxService extends Thread {
private static PrintStream log = System.out;
private final String Home = System.getProperty("user.home");
private final String URL = "jdbc:sqlite:" + Home + "/4413/pkg/sqlite/Models_R_US.db";
private Socket client;
private TaxService(Socket client) {
this.client = client;
}
private String doRequest(String request) {
String[] token = request.split("\\s+");
String where = token[0];
String search = token[1];
String format = token[2];
try (Connection connection = DriverManager.getConnection(URL)) {
log.printf("Connected to database: %s\n", connection.getMetaData().getURL());
Object responseObject = null;
if (where.equals("code_eq")) {
String query = "SELECT * FROM Tax WHERE code = ?";
if (!search.matches("^[A-Z]{2}$")) {
return "Invalid search value. Expected two letter province code, got: " + search;
}
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, search);
try (ResultSet rs = statement.executeQuery()) {
TaxBean bean = new TaxBean();
while (rs.next()) {
bean.setName(rs.getString("province"));
bean.setCode(rs.getString("code"));
bean.setType(rs.getString("type"));
bean.setGst(rs.getDouble("gst"));
bean.setPst(rs.getDouble("pst"));
}
responseObject = bean;
}
}
} else if (where.equals("pst_gt")) {
String query = "SELECT * FROM Tax WHERE pst > ?";
if (!search.matches("^[0-9]+(\\.[0-9]+)?$")) {
return "Invalid search value. Expected percent value, got: " + search;
}
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setDouble(1, Double.parseDouble(search));
try (ResultSet rs = statement.executeQuery()) {
List<TaxBean> list = new ArrayList<>();
TaxCollection collection = new TaxCollection();
while (rs.next()) {
TaxBean bean = new TaxBean();
bean.setName(rs.getString("province"));
bean.setCode(rs.getString("code"));
bean.setType(rs.getString("type"));
bean.setGst(rs.getDouble("gst"));
bean.setPst(rs.getDouble("pst"));
list.add(bean);
}
collection.setTaxes(list);
responseObject = collection;
}
}
} else {
return "Not implemented: " + where;
}
if (format.equals("xml")) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
JAXBContext context = JAXBContext.newInstance(responseObject.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(responseObject, baos);
return baos.toString();
} catch (Exception e) {
log.println(e);
return "XML Error: " + e.getMessage();
}
} else if (format.equals("json")) {
return (new Gson()).toJson(responseObject);
} else {
return "Unrecognized format: " + format;
}
} catch (SQLException e) {
log.println(e);
return "SQL Error: " + e.getMessage();
} finally {
log.println("Disconnected from database.");
}
}
public void run() {
log.printf("Connected to %s:%d\n", client.getInetAddress(), client.getPort());
try (
Socket client = this.client; // Makes sure that client is closed at end of try-statement.
Scanner req = new Scanner(client.getInputStream());
PrintStream res = new PrintStream(client.getOutputStream(), true);
) {
String response;
String request = req.nextLine().trim();
if (request.matches("^(code_eq|pst_gt)\\s+(\\S+)\\s+(json|xml)$")) {
response = doRequest(request);
} else {
response = "Don't understand: " + request;
}
res.println(response);
} catch (Exception e) {
log.println(e);
} finally {
log.printf("Disconnected from %s:%d\n", client.getInetAddress(), client.getPort());
}
}
public static void main(String[] args) throws Exception {
int port = 0;
InetAddress host = InetAddress.getLocalHost(); // .getLoopbackAddress();
try (ServerSocket server = new ServerSocket(port, 0, host)) {
log.printf("Server listening on %s:%d\n", server.getInetAddress(), server.getLocalPort());
while (true) {
Socket client = server.accept();
(new TaxService(client)).start();
}
}
}
}