-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetatitulServlet.java
146 lines (126 loc) · 5.5 KB
/
MetatitulServlet.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
package cz.incad.nkp.inprove;
import org.json.JSONObject;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
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.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@WebServlet(value = "/metatitul/*")
public class MetatitulServlet extends HttpServlet {
public static final Logger LOGGER = Logger.getLogger(SearchServlet.class.getName());
public static final String solrDefaultHost = "localhost:8983/solr";
public static final String titulPath = "titul/query";
/**
* Processes requests for <code>GET</code> method.
* Creates solr query based on requested path and returns solr response.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/json;charset=UTF-8");
response.addHeader("Access-Control-Allow-Methods", "GET");
PrintWriter out = response.getWriter();
String query = null;
UriComponents uri;
//GET /api/metatitul
if (request.getPathInfo() == null) {
//All title data excluding data with id = 1B569F5B32DD652280C63F6DB9C324D15E510C06 which is test data
query = "*:*&q.op=OR&indent=true&sort=meta_nazev_sort%20asc&rows=500&*=&fq=!id:\"1B569F5B32DD652280C63F6DB9C324D15E510C06\"";
}
//GET /api/metatitul/test
else if (request.getPathInfo() != null && request.getPathInfo().equals("/test")) {
//Title information only about TEST data
query = "*:*&q.op=OR&indent=true&sort=meta_nazev_sort%20asc&rows=500&*=&fq=id:\"1B569F5B32DD652280C63F6DB9C324D15E510C06\"";
}
else {
throw new RuntimeException("Path does not exist.");
}
uri = createUri(query);
LOGGER.log(Level.INFO, "requesting url {0}", uri.toString());
Map<String, String> reqProps = new HashMap<>();
reqProps.put("Content-Type", "application/json");
reqProps.put("Accept", "application/json");
try (InputStream inputStream = RESTHelper.inputStream(uri.toString(), reqProps)) {
JSONObject result = removeResponseHeader(inputStream);
String str = result.toString();
InputStream is = new ByteArrayInputStream(str.getBytes());
out.print(org.apache.commons.io.IOUtils.toString(is, StandardCharsets.UTF_8));
is.close();
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
/**
* Creates correct URI for specific environment.
* Passes given `query` to the URI.
* @param query - solr query
* @return correct URI
*/
private UriComponents createUri(String query) {
//TODO: Take into account that solr can run on different than default url
// Options opts = Options.getInstance();
// String solrhost = opts.getString("solrhost", solrDefaultHost);
return UriComponentsBuilder.newInstance()
.scheme("http")
.host(solrDefaultHost)
.path(titulPath)
.query("q={keyword}")
.buildAndExpand(query);
}
/**
* Removes `responseHeader` from given `inputStream` representing solr response
* @param inputStream solr response in form of inputstream
* @return JSONObject without `responseHeader`
*/
private JSONObject removeResponseHeader(InputStream inputStream) {
try (BufferedReader bR = new BufferedReader(new InputStreamReader(inputStream))) {
String line = "";
StringBuilder responseStrBuilder = new StringBuilder();
while((line = bR.readLine()) != null){
responseStrBuilder.append(line);
}
JSONObject result= new JSONObject(responseStrBuilder.toString());
result.remove("responseHeader");
return result;
}
catch (IOException ioException) {
throw new RuntimeException("Could not create buffered reader.");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}