-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVendorIDtoNamesService.java
More file actions
96 lines (79 loc) · 2.99 KB
/
VendorIDtoNamesService.java
File metadata and controls
96 lines (79 loc) · 2.99 KB
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
package services;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.Scanner;
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 javax.servlet.http.HttpSession;
import model.VendorsEngine;
@WebServlet(
name = "VendorIDtoNamesService",
urlPatterns = {"/idToNameConcat"}
)
public class VendorIDtoNamesService extends HttpServlet {
private static final long serialVersionUID = 1L;
public VendorIDtoNamesService() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// We want to return a comma delimited list of the searched up names
VendorsEngine engine = VendorsEngine.getInstance();
Writer out = response.getWriter();
HttpSession session = request.getSession(true);
// Set the response type
response.setContentType("text/plain");
// If session doesn't have name, set it to empty string
if (session.getAttribute("name") == null) {
session.setAttribute("name", "");
}
String concat_name = (String) session.getAttribute("name");
Map<String, String[]> parameters = request.getParameterMap();
String resp;
String idAddress = getInitParameter("idAddress");
if (idAddress == null) {
System.out.println(getInitParameter("idAddressRegister"));
try (Scanner in = new Scanner(new File(getInitParameter("idAddressRegister")))) {
idAddress = in.nextLine();
}
} else {
if (getInitParameter("idPort") != null) {
idAddress += ":" + getInitParameter("idPort");
}
}
String[] idParts = idAddress.split(":");
String idHost = idParts[0];
int idPort = Integer.parseInt(idParts[1]);
// If given id
if (parameters.containsKey("id")) {
// Get name from id
String id = request.getParameter("id");
String name = engine.runIDtoName(id, idHost, idPort);
// Do not append to comma separated 'list' if id not found
if (name.startsWith("not found") ||
name.startsWith("Do not understand: ") ||
name.startsWith("Failed to complete connection with ID") ||
name.startsWith("Failed to get ID service's host and port addresses")) {
resp = name + "\n" + "List of names: " + concat_name;
} else {
// Add to list of names if found
String new_name;
if (concat_name.equals("")) {
new_name = name;
} else {
new_name = concat_name + ", " + name;
}
session.setAttribute("name", new_name);
resp = "List of names: " + new_name;
out.write(resp);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}