Skip to content

Commit

Permalink
JCBC-244 - gitignore & fix welcome file that is not working in Jetty
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrall committed Feb 13, 2013
1 parent c886a13 commit 291f1cf
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 27 deletions.
85 changes: 85 additions & 0 deletions src/main/java/com/couchbase/beersample/BreweryAndBeerServlet.java
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);
}


}
40 changes: 40 additions & 0 deletions src/main/webapp/WEB-INF/breweries/all.jsp
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>
6 changes: 3 additions & 3 deletions src/main/webapp/WEB-INF/breweries/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
</tr>
</thead>
<tbody>
<c:forEach items="${breweries}" var="brewery">
<c:forEach items="${breweries}" var="items">
<tr>
<td><a href="/breweries/show/${brewery.id}">${brewery.name}</a></td>
<td><a href="/breweries/show/${items.id}">${items.name}</a></td>
<td><a class="btn btn-small btn-danger"
href="/breweries/delete/${brewery.id}">Delete</a>
href="/breweries/delete/${items.id}">Delete</a>
</td>
</tr>
</c:forEach>
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/WEB-INF/tags/layout.tag
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<ul class="nav nav-pills pull-right">
<li><a href="/welcome">Home</a></li>
<li><a href="/beers">Beers</a></li>
<li><a href="/breweries">Breweries</a></li>
<li><a href="/breweries">Breweries</a></li>
<li><a href="/all">All</a></li>
</ul>
<h2 class="muted">Couchbase Beer-Sample</h2>
</div>
Expand Down
18 changes: 14 additions & 4 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>com.couchbase.beersample.ConnectionManager</listener-class>
</listener>
Expand All @@ -15,6 +16,10 @@
<servlet-name>BeerServlet</servlet-name>
<servlet-class>com.couchbase.beersample.BeerServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>BreweryBeerServlet</servlet-name>
<servlet-class>com.couchbase.beersample.BreweryAndBeerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
Expand All @@ -29,7 +34,12 @@
<url-pattern>/beers/*</url-pattern>
<url-pattern>/beers</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>BreweryBeerServlet</servlet-name>
<url-pattern>/all/*</url-pattern>
<url-pattern>/all</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome</welcome-file>
</welcome-file-list>
</web-app>
47 changes: 28 additions & 19 deletions src/main/webapp/WEB-INF/welcome/index.jsp
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>

0 comments on commit 291f1cf

Please sign in to comment.