forked from couchbaselabs/beersample-java
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JCBC-244 - gitignore & fix welcome file that is not working in Jetty
- Loading branch information
Showing
6 changed files
with
172 additions
and
27 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/com/couchbase/beersample/BreweryAndBeerServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.couchbase.beersample; | ||
|
||
|
||
import com.couchbase.client.CouchbaseClient; | ||
import com.couchbase.client.protocol.views.Query; | ||
import com.couchbase.client.protocol.views.View; | ||
import com.couchbase.client.protocol.views.ViewResponse; | ||
import com.couchbase.client.protocol.views.ViewRow; | ||
import com.google.gson.Gson; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* The BreweryServlet handles all Brewery-related HTTP Queries. | ||
* | ||
* The BreweryServlet is used to handle all HTTP queries under the /breweries | ||
* namespace. The "web.xml" defines a wildcard route for every /breweries/* | ||
* route, so the doGet() method needs to determine what should be dispatched. | ||
*/ | ||
public class BreweryAndBeerServlet 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. | ||
* | ||
* @param request the HTTP request object. | ||
* @param response the HTTP response object. | ||
* @throws javax.servlet.ServletException | ||
* @throws java.io.IOException | ||
*/ | ||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
|
||
if(request.getPathInfo() == null) { | ||
handleIndex(request, response); | ||
} | ||
} | ||
|
||
private void handleIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { | ||
View view = client.getView("brewery", "all_with_beers"); | ||
Query query = new Query(); | ||
query.setIncludeDocs(true).setLimit(100); | ||
ViewResponse result = client.query(view, query); | ||
|
||
ArrayList<HashMap<String, String>> items = | ||
new ArrayList<HashMap<String, String>>(); | ||
for(ViewRow row : result) { | ||
HashMap<String, String> parsedDoc = gson.fromJson( | ||
(String)row.getDocument(), HashMap.class); | ||
|
||
HashMap<String, String> item = new HashMap<String, String>(); | ||
item.put("id", row.getId()); | ||
item.put("name", parsedDoc.get("name")); | ||
item.put("type", parsedDoc.get("type")); | ||
|
||
|
||
items.add(item); | ||
} | ||
request.setAttribute("items", items); | ||
|
||
request.getRequestDispatcher("/WEB-INF/breweries/all.jsp") | ||
.forward(request, response); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %> | ||
<%@page contentType="text/html" pageEncoding="UTF-8" %> | ||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
<t:layout> | ||
<jsp:body> | ||
<h3>Browse Breweries and Beers</h3> | ||
|
||
|
||
<table id="brewery-table" class="table table-striped"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th></th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<c:forEach items="${items}" var="item"> | ||
<c:if test="${ item.type == 'brewery' }"> | ||
<tr> | ||
<td colspan="2"><strong><a href="/breweries/show/${item.id}">${item.name}</a></strong></td> | ||
<td><a class="btn btn-small btn-danger" href="/breweries/delete/${item.id}">Delete</a> | ||
</td> | ||
</tr> | ||
</c:if> | ||
<c:if test="${ item.type == 'beer' }"> | ||
<tr> | ||
<td></td> | ||
<td><a href="/beers/show/${item.id}">${item.name}</a></td> | ||
<td> | ||
<a class="btn btn-small btn-danger" href="/beers/delete/${item.id}">Delete</a> | ||
<a class="btn btn-small btn-warning" href="/beers/edit/${item.id}">Edit</a> | ||
</td> | ||
</tr> | ||
</c:if> | ||
</c:forEach> | ||
</tbody> | ||
</table> | ||
</jsp:body> | ||
</t:layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %> | ||
<%@page contentType="text/html" pageEncoding="UTF-8"%> | ||
<%@page contentType="text/html" pageEncoding="UTF-8" %> | ||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
<t:layout> | ||
<jsp:body> | ||
<div class="span6"> | ||
<div class="span12"> | ||
<h4>Browse all Beers</h4> | ||
<a href="/beers" class="btn btn-warning">Show me all beers</a> | ||
<hr /> | ||
</div> | ||
<div class="span12"> | ||
<h4>Browse all Breweries</h4> | ||
<a href="/breweries" class="btn btn-info">Take me to the breweries</a> | ||
</div> | ||
<div class="span12"> | ||
<h4>Browse all Beers</h4> | ||
<a href="/beers" class="btn btn-warning">Show me all beers</a> | ||
<hr/> | ||
</div> | ||
<div class="span12"> | ||
<h4>Browse all Breweries</h4> | ||
<a href="/breweries" class="btn btn-info">Take me to the breweries</a> | ||
<hr/> | ||
</div> | ||
<div class="span12"> | ||
<h4>Browse all Breweries and Beers</h4> | ||
<a href="/all" class="btn btn-primary">Take me to the all records</a> | ||
<hr/> | ||
</div> | ||
</div> | ||
<div class="span6"> | ||
<div class="span12"> | ||
<h4>About this App</h4> | ||
<p>Welcome to Couchbase!</p> | ||
<p>This application helps you to get started on application | ||
development with Couchbase. It shows how to create, update and | ||
delete documents and how to work with JSON documents.</p> | ||
<p>The official tutorial can be found | ||
<a href="http://www.couchbase.com/docs/couchbase-sdk-java-1.1/tutorial.html">here</a>!</p> | ||
</div> | ||
<div class="span12"> | ||
<h4>About this App</h4> | ||
|
||
<p>Welcome to Couchbase!</p> | ||
|
||
<p>This application helps you to get started on application | ||
development with Couchbase. It shows how to create, update and | ||
delete documents and how to work with JSON documents.</p> | ||
|
||
<p>The official tutorial can be found | ||
<a href="http://www.couchbase.com/docs/couchbase-sdk-java-1.1/tutorial.html">here</a>!</p> | ||
</div> | ||
</div> | ||
</jsp:body> | ||
</t:layout> |