1 package nl.mineleni.cbsviewer.servlet.gazetteer;
2
3 import static nl.mineleni.cbsviewer.util.StringConstants.OPENLS_REQ_PARAM_REQUEST;
4 import static nl.mineleni.cbsviewer.util.StringConstants.OPENLS_REQ_PARAM_SEARCH;
5 import static nl.mineleni.cbsviewer.util.StringConstants.OPENLS_REQ_VALUE_GEOCODE;
6 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_GEVONDEN;
7 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_STRAAL;
8 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_XCOORD;
9 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_YCOORD;
10
11 import java.io.IOException;
12 import java.text.DecimalFormat;
13 import java.text.ParseException;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 import javax.servlet.ServletConfig;
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import nl.mineleni.cbsviewer.servlet.AbstractWxSServlet;
24 import nl.mineleni.cbsviewer.servlet.gazetteer.lusclient.OpenLSClient;
25 import nl.mineleni.cbsviewer.servlet.gazetteer.lusclient.OpenLSClientAddress;
26 import nl.mineleni.cbsviewer.servlet.gazetteer.lusclient.OpenLSClientUtil;
27 import nl.mineleni.cbsviewer.util.LabelsBundle;
28 import nl.mineleni.openls.databinding.openls.GeocodeResponse;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import flexjson.JSONSerializer;
34 import flexjson.transformer.NumberTransformer;
35
36
37
38
39
40
41 public class AdresZoekServlet extends AbstractWxSServlet {
42
43 private static final Logger LOGGER = LoggerFactory
44 .getLogger(AbstractWxSServlet.class);
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53 private static final String SERVLETCONFIG_OPENLS_MAX_RESULTS = "openlsmaxresults";
54
55
56 private static final String SERVLETCONFIG_OPENLS_SERVER_URL = "openlsserverurl";
57
58
59 private static final LabelsBundle LABELS = new LabelsBundle();
60
61
62 private transient OpenLSClient openLSClient;
63
64
65 private transient int openLSmaxResults;
66
67
68 private transient String openLSServerUrl;
69
70
71
72
73
74
75
76
77
78 private String formatCoord(final String coord) {
79
80 final DecimalFormat fmt = new DecimalFormat("###");
81 try {
82
83 return fmt.parse(coord).toString();
84 } catch (final ParseException e) {
85 LOGGER.warn("Fout tijden parsen van coordinaat: " + coord, e);
86 return coord;
87 } catch (final NullPointerException e) {
88 LOGGER.warn("Fout tijden parsen van coordinaat: " + coord, e);
89 return coord;
90 }
91 }
92
93
94
95
96 @Override
97 public void init(final ServletConfig config) throws ServletException {
98 super.init(config);
99 this.openLSClient = new OpenLSClient();
100
101 this.openLSServerUrl = config
102 .getInitParameter(SERVLETCONFIG_OPENLS_SERVER_URL);
103 if (this.openLSServerUrl == null) {
104 LOGGER.error("config param " + SERVLETCONFIG_OPENLS_SERVER_URL
105 + " is null.");
106 throw new ServletException("config param "
107 + SERVLETCONFIG_OPENLS_SERVER_URL + " is null.");
108 }
109 this.openLSmaxResults = config
110 .getInitParameter(SERVLETCONFIG_OPENLS_MAX_RESULTS) == null ? null
111 : Integer.parseInt(config
112 .getInitParameter(SERVLETCONFIG_OPENLS_MAX_RESULTS));
113 }
114
115
116
117
118
119
120
121
122
123
124 private String returnJson(final List<OpenLSClientAddress> addrl) {
125 if (addrl.size() < 1) {
126 return "[]";
127 } else {
128 final String json = new JSONSerializer()
129 .exclude("class", "validClientAddress", "municipality")
130 .exclude("countrySubdivision", "municipalitySubdivision")
131 .exclude("streetName", "streetNumber", "postalCode")
132 .transform(new NumberTransformer(), "xCoord", "yCoord")
133 .prettyPrint(LOGGER.isDebugEnabled()).serialize(addrl);
134 if (LOGGER.isDebugEnabled()) {
135 LOGGER.debug("json: " + json);
136 }
137 return json;
138 }
139 }
140
141
142
143
144
145
146
147
148 @Override
149 protected void service(final HttpServletRequest request,
150 final HttpServletResponse response) throws ServletException,
151 IOException {
152
153 final boolean forwardResponse = this.parseForward(request);
154
155 final String zoekTerm = request.getParameter("adres");
156 if (LOGGER.isDebugEnabled()) {
157 LOGGER.debug("Zoeken naar: " + zoekTerm);
158 }
159 if (zoekTerm.length() < 1) {
160 request.setAttribute(REQ_PARAM_GEVONDEN.code,
161 LABELS.getString("KEY_ZOEKEN_NIETS_INGEVULD"));
162 if (forwardResponse) {
163 request.getRequestDispatcher("/index.jsp").forward(request,
164 response);
165 }
166 } else {
167 final Map<String, String> openLSParams = new TreeMap<>();
168 openLSParams.put(OPENLS_REQ_PARAM_REQUEST.code,
169 OPENLS_REQ_VALUE_GEOCODE.code);
170 openLSParams.put(OPENLS_REQ_PARAM_SEARCH.code, zoekTerm);
171 final GeocodeResponse gcr = this.openLSClient.doGetOpenLSRequest(
172 this.openLSServerUrl, openLSParams);
173 final List<OpenLSClientAddress> addrl = OpenLSClientUtil
174 .getOpenLSClientAddressList(gcr, this.openLSmaxResults);
175
176 if (forwardResponse) {
177 if (addrl.isEmpty()) {
178
179 if (LOGGER.isDebugEnabled()) {
180 LOGGER.debug(LABELS.getString("KEY_ZOEKEN_GEEN_ADRES")
181 + zoekTerm);
182 }
183 request.setAttribute(REQ_PARAM_GEVONDEN.code,
184 LABELS.getString("KEY_ZOEKEN_GEEN_ADRES"));
185 } else if (addrl.size() == 1) {
186
187 if (LOGGER.isDebugEnabled()) {
188 LOGGER.debug("Er is 1 match gevonden voor: " + zoekTerm);
189 }
190 final OpenLSClientAddress addr = addrl.get(0);
191
192 request.setAttribute(REQ_PARAM_XCOORD.code,
193 this.formatCoord(addr.getxCoord()));
194 request.setAttribute(REQ_PARAM_YCOORD.code,
195 this.formatCoord(addr.getyCoord()));
196 request.setAttribute(REQ_PARAM_STRAAL.code,
197 addr.getRadius());
198 request.setAttribute(REQ_PARAM_GEVONDEN.code,
199 LABELS.getString("KEY_ZOEKEN_EEN_ADRES") + " "
200 + addr.getAddressString());
201 } else {
202
203 if (LOGGER.isDebugEnabled()) {
204 LOGGER.debug("Er is meer dan 1 match gevonden voor: "
205 + zoekTerm);
206 }
207 request.setAttribute("adreslijst", addrl);
208 request.setAttribute(REQ_PARAM_GEVONDEN.code,
209 LABELS.getString("KEY_ZOEKEN_MEER_ADRES"));
210 }
211 request.getRequestDispatcher("/index.jsp").forward(request,
212 response);
213 } else {
214
215 final String format = request.getParameter("format");
216 if ((format != null) && format.equalsIgnoreCase("json")) {
217
218 final String json = this.returnJson(addrl);
219 response.setCharacterEncoding("UTF-8");
220 response.setContentType("application/json");
221 response.setContentLength(json.length());
222 response.getWriter().print(json);
223 response.flushBuffer();
224 }
225 }
226 }
227 }
228 }