1
2
3
4
5
6
7 package nl.mineleni.cbsviewer.servlet;
8
9 import static nl.mineleni.cbsviewer.util.NumberConstants.DEFAULT_XCOORD;
10 import static nl.mineleni.cbsviewer.util.NumberConstants.DEFAULT_YCOORD;
11 import static nl.mineleni.cbsviewer.util.NumberConstants.OPENLS_ZOOMSCALE_STANDAARD;
12 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_FORWARD;
13 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_STRAAL;
14 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_XCOORD;
15 import static nl.mineleni.cbsviewer.util.StringConstants.REQ_PARAM_YCOORD;
16
17 import java.text.NumberFormat;
18 import java.text.ParseException;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.Cookie;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import nl.mineleni.cbsviewer.util.CookieNamesConstants;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37
38 public abstract class AbstractWxSServlet extends AbstractBaseServlet {
39
40
41 private static final Logger LOGGER = LoggerFactory
42 .getLogger(AbstractWxSServlet.class);
43
44 private static final int AGE = 90 * 24 * 60 * 60;
45
46
47 private static final long serialVersionUID = -5563479037661945586L;
48
49
50
51
52
53
54
55
56
57
58
59 protected boolean isNotNullNotEmptyNotWhiteSpaceOnly(final String toTest) {
60 return ((toTest != null) && !toTest.isEmpty() && !toTest.trim()
61 .isEmpty());
62 }
63
64
65
66
67
68
69
70
71
72
73 protected boolean parseForward(final HttpServletRequest request) {
74 return (null == request.getParameter(REQ_PARAM_FORWARD.code) ? false
75 : Boolean.valueOf(request.getParameter(REQ_PARAM_FORWARD.code)));
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 protected int[] parseLocation(final HttpServletRequest request)
93 throws ServletException {
94 try {
95
96 final NumberFormat nf = NumberFormat.getInstance();
97 final int xcoord = nf
98 .parse((!this.isNotNullNotEmptyNotWhiteSpaceOnly(request
99 .getParameter(REQ_PARAM_XCOORD.code)) ? DEFAULT_XCOORD
100 .toString() : request
101 .getParameter(REQ_PARAM_XCOORD.code))).intValue();
102 final int ycoord = nf
103 .parse((!this.isNotNullNotEmptyNotWhiteSpaceOnly(request
104 .getParameter(REQ_PARAM_YCOORD.code)) ? DEFAULT_YCOORD
105 .toString() : request
106 .getParameter(REQ_PARAM_YCOORD.code))).intValue();
107 final int straal = nf
108 .parse((!this.isNotNullNotEmptyNotWhiteSpaceOnly(request
109 .getParameter(REQ_PARAM_STRAAL.code)) ? OPENLS_ZOOMSCALE_STANDAARD
110 .toString() : request
111 .getParameter(REQ_PARAM_STRAAL.code))).intValue();
112 request.setAttribute(REQ_PARAM_XCOORD.code, xcoord);
113 request.setAttribute(REQ_PARAM_YCOORD.code, ycoord);
114 request.setAttribute(REQ_PARAM_STRAAL.code, straal);
115 if (LOGGER.isDebugEnabled()) {
116 LOGGER.debug("Locatie request params:(" + xcoord + ";" + ycoord
117 + ") straal:" + straal);
118 }
119 return new int[] { xcoord, ycoord, straal };
120 } catch (final NumberFormatException | ParseException e) {
121 LOGGER.error(
122 "Een van de vereiste locatie parameters kon niet geparsed worden als Integer.",
123 e);
124 throw new ServletException(e);
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 protected void setCookie(final HttpServletResponse response,
140 final CookieNamesConstants name, final String value) {
141 Cookie cookie = new Cookie(name.value, value);
142 cookie.setMaxAge(AGE);
143 response.addCookie(cookie);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157 protected void setCookie(final HttpServletResponse response,
158 final CookieNamesConstants name, final Integer value) {
159 Cookie cookie = new Cookie(name.value, Integer.toString(value));
160 cookie.setMaxAge(AGE);
161 response.addCookie(cookie);
162 }
163 }