-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBeerServlet.java
More file actions
289 lines (255 loc) · 9.74 KB
/
BeerServlet.java
File metadata and controls
289 lines (255 loc) · 9.74 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* Copyright (C) 2009-2012 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.beersample;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.*;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.spy.memcached.internal.OperationFuture;
/**
* The BeerServlet handles all Beer-related HTTP Queries.
*
* The BeerServlet is used to handle all HTTP queries under the /beers
* namespace. The "web.xml" defines a wildcard route for every /beers/*
* route, so the doGet() method needs to determine what should be dispatched.
*/
public class BeerServlet extends HttpServlet {
/**
* Obtains the current CouchbaseClient connection.
*/
final CouchbaseClient client = ConnectionManager.getInstance();
/**
* Google GSON is used for JSON encoding/decoding.
*/
final Gson gson = new Gson();
/**
* Dispatch all incoming GET HTTP requests.
*
* Since the /beers/* routes are wildcarded and will all end up here, the
* method needs to check agains the PATH (through getPathInfo()) and
* determine which helper method should be called. The helper method then
* does the actual request and response handling.
*
* @param request the HTTP request object.
* @param response the HTTP response object.
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
if(request.getPathInfo() == null) {
handleIndex(request, response);
} else if(request.getPathInfo().startsWith("/show")) {
handleShow(request, response);
} else if(request.getPathInfo().startsWith("/delete")) {
handleDelete(request, response);
} else if(request.getPathInfo().startsWith("/edit")) {
handleEdit(request, response);
} else if(request.getPathInfo().startsWith("/search")) {
handleSearch(request, response);
}
} catch (InterruptedException ex) {
Logger.getLogger(BeerServlet.class.getName()).log(
Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(BeerServlet.class.getName()).log(
Level.SEVERE, null, ex);
}
}
/**
* Store and validate the beer form.
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String beerId = request.getPathInfo().split("/")[2];
HashMap<String, String> beer = beer = new HashMap<String, String>();
Enumeration<String> params = request.getParameterNames();
while(params.hasMoreElements()) {
String key = params.nextElement();
if(!key.startsWith("beer_")) {
continue;
}
String value = request.getParameter(key);
beer.put(key.substring(5), value);
}
beer.put("type", "beer");
beer.put("updated", new Date().toString());
client.set(beerId, 0, gson.toJson(beer));
response.sendRedirect("/beers/show/" + beerId);
}
/**
* Handle the /beers action.
*
* Based on a defined Couchbase View (beer/brewery_beers), the beers are
* loaded, arranged and passed to the JSP layer. Google GSON is used to
* handle the JSON encoding/decoding.
*
* @param request the HTTP request object.
* @param response the HTTP response object.
* @throws IOException
* @throws ServletException
*/
private void handleIndex(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
try {
View view = client.getView("beer", "by_name");
Query query = new Query();
query.setIncludeDocs(true).setLimit(20);
ViewResponse result = client.query(view, query);
ArrayList<HashMap<String, String>> beers =
new ArrayList<HashMap<String, String>>();
for (ViewRow row : result) {
HashMap<String, String> parsedDoc = gson.fromJson(
(String) row.getDocument(), HashMap.class);
HashMap<String, String> beer = new HashMap<String, String>();
beer.put("id", row.getId());
beer.put("name", parsedDoc.get("name"));
beer.put("brewery", parsedDoc.get("brewery_id"));
beers.add(beer);
}
request.setAttribute("beers", beers);
request.getRequestDispatcher("/WEB-INF/beers/index.jsp")
.forward(request, response);
} catch (InvalidViewException e) {
response.getWriter().print( InstallViewServlet.printNoViewMessage() );
}
}
/**
* Handle the /beers/show/<BEER-ID> action
*
* This method loads up a document based on the given beer id.
*
* @param request the HTTP request object.
* @param response the HTTP response object.
* @throws IOException
* @throws ServletException
*/
private void handleShow(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String beerId = request.getPathInfo().split("/")[2];
String document = (String) client.get(beerId);
if(document != null) {
HashMap<String, String> beer = gson.fromJson(document, HashMap.class);
request.setAttribute("beer", beer);
}
request.getRequestDispatcher("/WEB-INF/beers/show.jsp")
.forward(request, response);
}
/**
* Handle the /beers/delete/<BEER-ID> action
*
* This method deletes a beer based on the given beer id.
*
* @param request the HTTP request object.
* @param response the HTTP response object.
* @throws IOException
* @throws ServletException
* @throws InterruptedException
* @throws ExecutionException
*/
private void handleDelete(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException,
InterruptedException,
ExecutionException {
String beerId = request.getPathInfo().split("/")[2];
OperationFuture<Boolean> delete = client.delete(beerId);
if(delete.get()) {
response.sendRedirect("/beers");
}
}
private void handleEdit(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] beerId = request.getPathInfo().split("/");
if(beerId.length > 2) {
String document = (String) client.get(beerId[2]);
HashMap<String, String> beer = null;
if(document != null) {
beer = gson.fromJson(document, HashMap.class);
beer.put("id", beerId[2]);
request.setAttribute("beer", beer);
}
request.setAttribute("title", "Modify Beer \"" + beer.get("name") + "\"");
} else {
request.setAttribute("title", "Create a new beer");
}
request.getRequestDispatcher("/WEB-INF/beers/edit.jsp")
.forward(request, response);
}
/**
* Handle the /beers/search action.
*
* Based on a defined Couchbase View (beer/by_name), the beers are
* loaded, arranged and passed as JSON to be used by the javascript layer.
*
* @param request the HTTP request object.
* @param response the HTTP response object.
* @throws IOException
* @throws ServletException
*/
private void handleSearch(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String startKey = request.getParameter("value").toLowerCase();
View view = client.getView("beer", "by_name");
Query query = new Query();
query.setIncludeDocs(true)
.setLimit(20)
.setRangeStart(ComplexKey.of(startKey))
.setRangeEnd(ComplexKey.of(startKey + "\uefff"));
System.out.println(query);
ViewResponse result = client.query(view, query);
ArrayList<HashMap<String, String>> beers =
new ArrayList<HashMap<String, String>>();
for(ViewRow row : result) {
HashMap<String, String> parsedDoc = gson.fromJson(
(String)row.getDocument(), HashMap.class);
HashMap<String, String> beer = new HashMap<String, String>();
beer.put("id", row.getId());
beer.put("name", parsedDoc.get("name"));
beer.put("brewery", parsedDoc.get("brewery_id"));
beers.add(beer);
}
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(gson.toJson(beers));
out.flush();
}
}