├── .gitignore ├── .travis.yml ├── CONTRIB.md ├── LICENSE ├── README.md ├── eclipse-launch-profiles ├── DevAppServer.launch └── UpdateApplication.launch ├── nbactions.xml ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── guestbook │ │ └── controller │ │ └── GuestbookController.java └── webapp │ ├── WEB-INF │ ├── appengine-web.xml │ ├── datastore-indexes.xml │ ├── logging.properties │ ├── pages │ │ └── guestbook.jsp │ ├── spring-config │ │ └── application-context.xml │ └── web.xml │ └── stylesheets │ └── main.css └── test └── java └── com └── guestbook └── controller └── GuestbookControllerTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | hotspot.log 2 | *.iml 3 | *.ipr 4 | *.iws 5 | target/ 6 | classes/ 7 | /var 8 | pom.xml.versionsBackup 9 | test-output/ 10 | /atlassian-ide-plugin.xml 11 | .idea 12 | .DS_Store 13 | .classpath 14 | .settings 15 | .project 16 | temp-testng-customsuite.xml 17 | test-output 18 | .externalToolBuilders 19 | *~ 20 | /.apt_generated 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk7 3 | - oraclejdk7 4 | language: java 5 | script: "mvn clean test" 6 | -------------------------------------------------------------------------------- /CONTRIB.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we have to jump a couple of legal hurdles. 6 | 7 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 8 | 9 | * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). 10 | * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). 11 | 12 | Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. 13 | 14 | ## Contributing A Patch 15 | 16 | 1. Submit an issue describing your proposed change to the repo in question. 17 | 1. The repo owner will respond to your issue promptly. 18 | 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 19 | 1. Fork the desired repo, develop and test your code changes. 20 | 1. Ensure that your code adheres to the existing style in the sample to which you are contributing. Refer to the [Google Cloud Platform Samples Style Guide](https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the recommended coding standards for this organization. 21 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 22 | 1. Submit a pull request. 23 | 24 | ## Contributing A New Sample App 25 | 26 | 1. Submit an issue to the GoogleCloudPlatform/Template repo describing your proposed sample app. 27 | 1. The Template repo owner will respond to your enhancement issue promptly. Instructional value is the top priority when evaluating new app proposals for this collection of repos. 28 | 1. If your proposal is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 29 | 1. Create your own repo for your app following this naming convention: 30 | * {product}-{app-name}-{language} 31 | * products: appengine, compute, storage, bigquery, prediction, cloudsql 32 | * example: appengine-guestbook-python 33 | * For multi-product apps, concatenate the primary products, like this: compute-appengine-demo-suite-python. 34 | * For multi-language apps, concatenate the primary languages like this: appengine-sockets-python-java-go. 35 | 1. Clone the README.md, CONTRIB.md and LICENSE files from the GoogleCloudPlatform/Template repo. 36 | 1. Ensure that your code adheres to the existing style in the sample to which you are contributing. Refer to the [Google Cloud Platform Samples Style Guide](https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the recommended coding standards for this organization. 37 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 38 | 1. Submit a request to fork your repo in GoogleCloudPlatform organizationt via your proposal issue. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is an unofficial version of Google's Guestbook sample made with Spring MVC. My intention here is to provide a starting point for someone who wants to develop a new project on App Engine using Spring MVC. 2 | 3 | App Engine Java Guestbook with Spring MVC 4 | 5 | * Uses AppEngine SDK 1.8.9 6 | * Spring 4.0 (but works with Spring 3.X, just update the pom.xml) 7 | 8 | 9 | ## Sample guestbook for use with App Engine Java. 10 | 11 | Requires [Apache Maven](http://maven.apache.org) 3.1 or greater, and JDK 7+ in order to run. 12 | 13 | To build, run 14 | 15 | mvn package 16 | 17 | Building will run the tests, but to explicitly run tests you can use the test target 18 | 19 | mvn test 20 | 21 | To start the app, use the [App Engine Maven Plugin](http://code.google.com/p/appengine-maven-plugin/) that is already included in this demo. Just run the command. 22 | 23 | mvn appengine:devserver 24 | 25 | For further information, consult the [Java App Engine](https://developers.google.com/appengine/docs/java/overview) documentation. 26 | 27 | To see all the available goals for the App Engine plugin, run 28 | 29 | mvn help:describe -Dplugin=appengine -------------------------------------------------------------------------------- /eclipse-launch-profiles/DevAppServer.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /eclipse-launch-profiles/UpdateApplication.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /nbactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | CUSTOM-appengine:devserver 5 | appengine:devserver 6 | 7 | appengine:devserver 8 | 9 | 10 | 11 | CUSTOM-appengine:update 12 | appengine:update 13 | 14 | appengine:update 15 | 16 | 17 | 18 | CUSTOM-appengine:rollback 19 | appengine:rollback 20 | 21 | appengine:rollback 22 | 23 | 24 | 25 | CUSTOM-appengine:update_cron 26 | appengine:update_cron 27 | 28 | appengine:update_cron 29 | 30 | 31 | 32 | CUSTOM-appengine:update_dos 33 | appengine:update_dos 34 | 35 | appengine:update_dos 36 | 37 | 38 | 39 | CUSTOM-appengine:update_indexes 40 | appengine:update_indexes 41 | 42 | appengine:update_indexes 43 | 44 | 45 | 46 | CUSTOM-appengine:update_queues 47 | appengine:update_queues 48 | 49 | appengine:update_queues 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | war 7 | 0.1-SNAPSHOT 8 | 9 | com.guestbook 10 | guestbook-webapp 11 | 12 | 13 | 14 | google-staging 15 | Google Staging 16 | https://oss.sonatype.org/content/repositories/comgoogleappengine-1004/ 17 | 18 | 19 | 20 | 21 | 1 22 | 1.9.0 23 | UTF-8 24 | 4.0.0.RELEASE 25 | 26 | 27 | 28 | 29 | 30 | com.google.appengine 31 | appengine-api-1.0-sdk 32 | ${appengine.target.version} 33 | 34 | 35 | javax.servlet 36 | javax.servlet-api 37 | 3.1.0 38 | provided 39 | 40 | 41 | jstl 42 | jstl 43 | 1.2 44 | 45 | 46 | 47 | 48 | com.google.appengine 49 | appengine-testing 50 | ${appengine.target.version} 51 | test 52 | 53 | 54 | com.google.appengine 55 | appengine-api-stubs 56 | ${appengine.target.version} 57 | test 58 | 59 | 60 | 61 | 62 | org.springframework 63 | spring-core 64 | ${spring.framework.version} 65 | 66 | 67 | org.springframework 68 | spring-web 69 | ${spring.framework.version} 70 | 71 | 72 | org.springframework 73 | spring-webmvc 74 | ${spring.framework.version} 75 | 76 | 77 | 78 | com.fasterxml.jackson.core 79 | jackson-core 80 | 2.2.1 81 | test 82 | 83 | 84 | com.fasterxml.jackson.core 85 | jackson-databind 86 | 2.2.1 87 | test 88 | 89 | 90 | org.hamcrest 91 | hamcrest-all 92 | 1.3 93 | test 94 | 95 | 96 | junit 97 | junit 98 | 4.11 99 | test 100 | 101 | 102 | hamcrest-core 103 | org.hamcrest 104 | 105 | 106 | 107 | 108 | org.mockito 109 | mockito-core 110 | 1.9.5 111 | test 112 | 113 | 114 | org.springframework 115 | spring-test 116 | ${spring.framework.version} 117 | test 118 | 119 | 120 | 121 | 122 | 123 | 124 | org.apache.maven.plugins 125 | 2.5.1 126 | maven-compiler-plugin 127 | 128 | 1.7 129 | 1.7 130 | 131 | 132 | 133 | 134 | org.apache.maven.plugins 135 | maven-war-plugin 136 | 2.3 137 | 138 | true 139 | 140 | 141 | 142 | ${basedir}/src/main/webapp/WEB-INF 143 | true 144 | WEB-INF 145 | 146 | 147 | 148 | 149 | 150 | 151 | com.google.appengine 152 | appengine-maven-plugin 153 | ${appengine.target.version} 154 | 155 | 156 | 157 | 158 | 159 | ${basedir}/src/main/webapp 160 | 161 | 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /src/main/java/com/guestbook/controller/GuestbookController.java: -------------------------------------------------------------------------------- 1 | package com.guestbook.controller; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | import org.springframework.web.servlet.ModelAndView; 10 | 11 | import com.google.appengine.api.datastore.DatastoreService; 12 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 13 | import com.google.appengine.api.datastore.Entity; 14 | import com.google.appengine.api.datastore.Key; 15 | import com.google.appengine.api.datastore.KeyFactory; 16 | import com.google.appengine.api.users.User; 17 | import com.google.appengine.api.users.UserService; 18 | import com.google.appengine.api.users.UserServiceFactory; 19 | 20 | @Controller 21 | public class GuestbookController { 22 | @RequestMapping("/") 23 | public String home() { 24 | return "guestbook"; 25 | } 26 | 27 | @RequestMapping("/loggedIn") 28 | public ModelAndView listGuestbook() { 29 | UserService userService = UserServiceFactory.getUserService(); 30 | User currentUser = userService.getCurrentUser(); 31 | 32 | if (currentUser == null) { 33 | return new ModelAndView("redirect:" 34 | + userService.createLoginURL("/")); 35 | } else { 36 | return new ModelAndView("guestbook", "welcomeMsg", "You are authenticated, " 37 | + currentUser.getNickname()); 38 | } 39 | } 40 | 41 | @RequestMapping("/sign") 42 | public String signGuestbook( 43 | @RequestParam(required = true, value = "guestbookName") String guestbookName, 44 | @RequestParam(required = true, value = "content") String content, 45 | Model model) { 46 | UserService userService = UserServiceFactory.getUserService(); 47 | User user = userService.getCurrentUser(); 48 | 49 | Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 50 | Date date = new Date(); 51 | Entity greeting = new Entity("Greeting", guestbookKey); 52 | greeting.setProperty("user", user); 53 | greeting.setProperty("date", date); 54 | greeting.setProperty("content", content); 55 | 56 | DatastoreService datastore = DatastoreServiceFactory 57 | .getDatastoreService(); 58 | datastore.put(greeting); 59 | 60 | model.addAttribute("guestbookName", guestbookName); 61 | return "guestbook"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/appengine-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | guestbook-springmvc 4 | ${appengine.app.version} 5 | true 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/datastore-indexes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/logging.properties: -------------------------------------------------------------------------------- 1 | # A default java.util.logging configuration. 2 | # (All App Engine logging is through java.util.logging by default). 3 | # 4 | # To use this configuration, copy it into your application's WEB-INF 5 | # folder and add the following to your appengine-web.xml: 6 | # 7 | # 8 | # 9 | # 10 | # 11 | 12 | # Set the default logging level for all loggers to WARNING 13 | .level = WARNING 14 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/guestbook.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <%@ page import="com.google.appengine.api.datastore.DatastoreService" %> 3 | <%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %> 4 | <%@ page import="com.google.appengine.api.datastore.Entity" %> 5 | <%@ page import="com.google.appengine.api.datastore.FetchOptions" %> 6 | <%@ page import="com.google.appengine.api.datastore.Key" %> 7 | <%@ page import="com.google.appengine.api.datastore.KeyFactory" %> 8 | <%@ page import="com.google.appengine.api.datastore.Query" %> 9 | <%@ page import="com.google.appengine.api.users.User" %> 10 | <%@ page import="com.google.appengine.api.users.UserService" %> 11 | <%@ page import="com.google.appengine.api.users.UserServiceFactory" %> 12 | <%@ page import="java.util.List" %> 13 | <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |

${welcomeMsg}

22 | 23 | <% 24 | String guestbookName = request.getParameter("guestbookName"); 25 | if (guestbookName == null) { 26 | guestbookName = "default"; 27 | } 28 | pageContext.setAttribute("guestbookName", guestbookName); 29 | UserService userService = UserServiceFactory.getUserService(); 30 | User user = userService.getCurrentUser(); 31 | if (user != null) { 32 | pageContext.setAttribute("user", user); 33 | %> 34 |

Hello, ${fn:escapeXml(user.nickname)}! (You can 35 | ">sign out.)

36 | <% 37 | } else { 38 | %> 39 |

Hello! 40 | ">Sign in 41 | to include your name with greetings you post.

42 | <% 43 | } 44 | %> 45 | 46 | <% 47 | DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 48 | Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 49 | // Run an ancestor query to ensure we see the most up-to-date 50 | // view of the Greetings belonging to the selected Guestbook. 51 | Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING); 52 | List greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5)); 53 | if (greetings.isEmpty()) { 54 | %> 55 |

Guestbook '${fn:escapeXml(guestbookName)}' has no messages.

56 | <% 57 | } else { 58 | %> 59 |

Messages in Guestbook '${fn:escapeXml(guestbookName)}'.

60 | <% 61 | for (Entity greeting : greetings) { 62 | pageContext.setAttribute("greeting_content", 63 | greeting.getProperty("content")); 64 | if (greeting.getProperty("user") == null) { 65 | %> 66 |

An anonymous person wrote:

67 | <% 68 | } else { 69 | pageContext.setAttribute("greeting_user", 70 | greeting.getProperty("user")); 71 | %> 72 |

${fn:escapeXml(greeting_user.nickname)} wrote:

73 | <% 74 | } 75 | %> 76 |
${fn:escapeXml(greeting_content)}
77 | <% 78 | } 79 | } 80 | %> 81 | 82 |
83 |
84 |
85 | 86 |
87 | 88 |
89 |
90 |
91 |
92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/spring-config/application-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 14 | 15 | /WEB-INF/pages/ 16 | 17 | 18 | .jsp 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | mvc-dispatcher 7 | org.springframework.web.servlet.DispatcherServlet 8 | 9 | contextConfigLocation 10 | /WEB-INF/spring-config/application-context.xml 11 | 12 | 1 13 | 14 | 15 | mvc-dispatcher 16 | / 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/webapp/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Verdana, Helvetica, sans-serif; 3 | background-color: #FFFFCC; 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/guestbook/controller/GuestbookControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.guestbook.controller; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.is; 5 | import static org.hamcrest.Matchers.notNullValue; 6 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 8 | 9 | import org.junit.After; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.test.context.ContextConfiguration; 15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 16 | import org.springframework.test.context.web.WebAppConfiguration; 17 | import org.springframework.test.web.ModelAndViewAssert; 18 | import org.springframework.test.web.servlet.MockMvc; 19 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 20 | import org.springframework.web.context.WebApplicationContext; 21 | import org.springframework.web.servlet.ModelAndView; 22 | 23 | import com.google.appengine.api.datastore.DatastoreService; 24 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 25 | import com.google.appengine.api.datastore.Entity; 26 | import com.google.appengine.api.datastore.Key; 27 | import com.google.appengine.api.datastore.KeyFactory; 28 | import com.google.appengine.api.datastore.Query; 29 | import com.google.appengine.api.datastore.Query.FilterOperator; 30 | import com.google.appengine.api.datastore.Query.FilterPredicate; 31 | import com.google.appengine.api.users.User; 32 | import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; 33 | import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 34 | import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; 35 | 36 | @RunWith(SpringJUnit4ClassRunner.class) 37 | @ContextConfiguration(locations = "classpath:**/application-context.xml") 38 | @WebAppConfiguration 39 | public class GuestbookControllerTest { 40 | @Autowired 41 | private WebApplicationContext wac; 42 | 43 | private MockMvc mockMvc; 44 | 45 | private final LocalServiceTestHelper localServiceHelper = new LocalServiceTestHelper( 46 | new LocalUserServiceTestConfig(), 47 | new LocalDatastoreServiceTestConfig()); 48 | 49 | private final String userEmail = "test@guestbook.com"; 50 | private final String userDomain = "guestbook.com"; 51 | private final String userNickname = "test"; 52 | 53 | @Before 54 | public void setup() { 55 | this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 56 | localServiceHelper.setUp(); 57 | } 58 | 59 | @After 60 | public void tearDown() { 61 | localServiceHelper.tearDown(); 62 | } 63 | 64 | @Test 65 | public void listingLoggedIn() throws Exception { 66 | loginUser(); 67 | 68 | ModelAndView mav = mockMvc.perform(get("/loggedIn")) 69 | .andExpect(status().isOk()).andReturn().getModelAndView(); 70 | ModelAndViewAssert.assertModelAttributeValue(mav, "welcomeMsg", 71 | "You are authenticated, " + userNickname); 72 | } 73 | 74 | @Test 75 | public void listingNotLoggedIn() throws Exception { 76 | logoutUser(); 77 | 78 | // Moved to authentication service 79 | mockMvc.perform(get("/loggedIn")).andExpect( 80 | status().isMovedTemporarily()); 81 | } 82 | 83 | @Test 84 | public void signGuestbookLoggedIn() throws Exception { 85 | loginUser(); 86 | signGuestbook(); 87 | 88 | DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); 89 | 90 | // Throws exception if more than one is found 91 | Key guestbookKey = KeyFactory.createKey("Guestbook", "default"); 92 | Entity entity = ds.prepare( 93 | new Query("Greeting", guestbookKey) 94 | .setFilter(new FilterPredicate("user", 95 | FilterOperator.EQUAL, new User(userEmail, 96 | userDomain)))).asSingleEntity(); 97 | 98 | assertThat(entity, is(notNullValue())); 99 | } 100 | 101 | @Test 102 | public void signGuestbookLoggedOut() throws Exception { 103 | logoutUser(); 104 | signGuestbook(); 105 | 106 | DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); 107 | 108 | // Throws exception if more than one is found 109 | Key guestbookKey = KeyFactory.createKey("Guestbook", "default"); 110 | Entity entity = ds.prepare( 111 | new Query("Greeting", guestbookKey) 112 | .setFilter(new FilterPredicate("user", 113 | FilterOperator.EQUAL, null))).asSingleEntity(); 114 | 115 | assertThat(entity, is(notNullValue())); 116 | } 117 | 118 | private void signGuestbook() throws Exception { 119 | ModelAndView mav = mockMvc 120 | .perform( 121 | get("/sign").param("guestbookName", "default").param( 122 | "content", "Inserting new greeting")) 123 | .andExpect(status().isOk()).andReturn().getModelAndView(); 124 | ModelAndViewAssert.assertModelAttributeValue(mav, "guestbookName", 125 | "default"); 126 | } 127 | 128 | private void loginUser() { 129 | localServiceHelper.setEnvIsLoggedIn(true).setEnvEmail(userEmail) 130 | .setEnvAuthDomain(userDomain); 131 | } 132 | 133 | private void logoutUser() { 134 | localServiceHelper.setEnvIsLoggedIn(false); 135 | } 136 | } 137 | --------------------------------------------------------------------------------