├── .gitignore ├── README.MD ├── pom.xml ├── src ├── assembly │ └── pkg.xml └── main │ ├── bin │ ├── start │ └── start.bat │ ├── java │ └── com │ │ └── greenlaw110 │ │ └── bookmark │ │ ├── ActBookmarksApplication.java │ │ ├── endpoint │ │ ├── BookmarksService.java │ │ ├── HelloService.java │ │ ├── MyBookmarkService.java │ │ └── UserService.java │ │ ├── model │ │ ├── Bookmark.java │ │ └── User.java │ │ └── security │ │ └── AppSecurityService.java │ └── resources │ ├── app.version │ ├── application.properties │ └── logback.xml └── start /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | /.act* 4 | /target/ 5 | .classpath 6 | .project 7 | .settings/ 8 | /tmp/ 9 | /*.log 10 | /test_drive 11 | _sys_start.do.not.delete 12 | act_start.id 13 | backup 14 | /upload 15 | .app.id.do-not-delete 16 | .global.id.do-not-delete 17 | **/.~*.xlsx 18 | _sys_metric.do.not.delete 19 | /doc/.* 20 | dump 21 | *.do.not.delete 22 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | This is a simple application to store bookmarks exposing REST API. 4 | This application uses [ActFramework](https://github.com/actframework/actframework) 5 | and [MongoDB](mongodb.org) to store data. 6 | 7 | To use this application, you must have [Java](http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html), [Maven](http://maven.apache.org/download.cgi) and [MongoDB](https://www.mongodb.com/download-center) downloaded and installed. 8 | 9 | You must have MongoDB running before using this application. 10 | 11 | ## Start application 12 | 13 | Start the application in dev mode 14 | 15 | ``` 16 | mvn clean compile exec:exec 17 | ``` 18 | 19 | Start the application in prod mode 20 | 21 | ``` 22 | mvn clean package 23 | cd target/dist 24 | unzip * 25 | ./start 26 | ``` 27 | 28 | Once application has been started, you should see something like: 29 | 30 | ``` 31 | / \ ___ | |_ | __ ) ___ ___ | | __ _ __ ___ __ _ _ __ | | __ 32 | / _ \ / __|| __|| _ \ / _ \ / _ \ | |/ /| '_ ` _ \ / _` || '__|| |/ / 33 | / ___ \ | (__ | |_ | |_) || (_) || (_) || < | | | | | || (_| || | | < 34 | /_/ \_\ \___| \__||____/ \___/ \___/ |_|\_\|_| |_| |_| \__,_||_| |_|\_\ 35 | 36 | powered by ActFramework v0.3.0-38e7 37 | 38 | version: 0.0.1 39 | base dir: /home/luog/p/greenlaw110/ActBookmarks/target/dist 40 | profile: prod 41 | mode: PROD 42 | 43 | Oct 28, 2016 6:49:27 AM org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory 44 | INFO: LoggerImplFactory set to org.mongodb.morphia.logging.jdk.JDKLoggerFactory 45 | 06:49:27.878 [main] INFO a.Act - loading application(s) ... 46 | 06:49:27.882 [main] INFO a.Act - App starting .... 47 | 06:49:27.940 [main] WARN a.c.AppConfig - Application secret key not set! You are in the dangerous zone!!! 48 | 06:49:27.964 [main] WARN a.a.DbServiceManager - DB configuration not found. Will try to init default service with the sole db plugin: act.db.morphia.MorphiaPlugin@6ca367aa 49 | 06:49:28.054 [main] INFO o.m.d.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 50 | 06:49:28.131 [cluster-ClusterId{value='58125a48276a8c4147016ace', description='null'}-localhost:27017] INFO o.m.d.connection - Opened connection [connectionId{localValue:1, serverValue:223}] to localhost:27017 51 | 06:49:28.133 [cluster-ClusterId{value='58125a48276a8c4147016ace', description='null'}-localhost:27017] INFO o.m.d.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 10]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=993392} 52 | 06:49:28.284 [main] WARN a.m.MailerConfig - smtp host configuration not found, will use mock smtp to send email 53 | 06:49:28.284 [main] WARN a.c.AppConfig - host is not configured. Use localhost as hostname 54 | 06:49:28.480 [main] INFO o.m.d.connection - Opened connection [connectionId{localValue:2, serverValue:224}] to localhost:27017 55 | 06:49:28.492 [main] WARN a.Act - acl.yaml file not found... 56 | 06:49:28.494 [main] INFO a.Act - App[ActBookmark] loaded in 612ms 57 | 06:49:28.507 [main] INFO o.xnio - XNIO version 3.3.6.Final 58 | 06:49:28.520 [main] INFO o.x.nio - XNIO NIO Implementation Version 3.3.6.Final 59 | 06:49:28.586 [main] INFO a.Act - network client hooked on port: 5460 60 | 06:49:28.587 [main] INFO a.Act - CLI server started on port: 5461 61 | 06:49:28.587 [main] INFO a.b.a.RunApp - it takes 1693ms to start the app 62 | ``` 63 | 64 | Once application started you can proceed with the next step: 65 | 66 | ## Load Init Data 67 | 68 | Unlike the upstream project [SpringBootBookmarks](https://github.com/javaeeeee/SpringBootBookmarks), 69 | ActBookmarks stores hashed password into the database, thus the init 70 | data must be created through the application. Here is the steps to load 71 | init data: 72 | 73 | Open another console and type: 74 | 75 | ``` 76 | nc localhost 5461 77 | ``` 78 | (Note, if you don't have `nc` try `telnet`) 79 | 80 | **Tips** If you have installed [rlwrap](https://github.com/hanslub42/rlwrap), use it along wit `nc` or `telnet`: 81 | 82 | ``` 83 | rlwrap nc localhost 5461 84 | ``` 85 | 86 | This will bring you into the ACT console and you should see something like: 87 | 88 | ``` 89 | / \ ___ | |_ | __ ) ___ ___ | | __ _ __ ___ __ _ _ __ | | __ 90 | / _ \ / __|| __|| _ \ / _ \ / _ \ | |/ /| '_ ` _ \ / _` || '__|| |/ / 91 | / ___ \ | (__ | |_ | |_) || (_) || (_) || < | | | | | || (_| || | | < 92 | /_/ \_\ \___| \__||____/ \___/ \___/ |_|\_\|_| |_| |_| \__,_||_| |_|\_\ 93 | 94 | powered by ActFramework v0.3.0-38e7 95 | 96 | version: 0.0.1 97 | base dir: /home/luog/p/greenlaw110/ActBookmarks/target/dist 98 | profile: prod 99 | mode: PROD 100 | ActBookmark[2kbNDb01]> 101 | ``` 102 | 103 | Now type in `db.init` and you should see something like: 104 | 105 | ``` 106 | ActBookmark[2kbNDb01]>db.init 107 | db.init 108 | DB initialized with sample data 109 | ActBookmark[2kbNDb01]> 110 | ``` 111 | 112 | Now you have two users created: 113 | 114 | * *Phil* and password *1* 115 | * *Pete* and password *2* 116 | 117 | You can type `user.list` in the ACT Console: 118 | 119 | ``` 120 | ActBookmark[2kbNDb01]>user.list 121 | user.list 122 | +----------+-----------+ 123 | | USERNAME | BOOKMARKS | 124 | +----------+-----------+ 125 | | Phil | 2 | 126 | | Pete | 2 | 127 | +----------+-----------+ 128 | Items found: 2 129 | ActBookmark[2kbNDb01]> 130 | ``` 131 | 132 | ## Play with RESTful API 133 | 134 | The API is secured with Basic Authentication, so the aforementioned credentials 135 | can be used to access the data. 136 | 137 | To get all the bookmarks stored by user *Phil* one should key in the following. 138 | 139 | ~~~~ 140 | curl -w "\n" 2>/dev/null -H "accept: application/json" localhost:5460/Phil/bookmarks -u Phil:1 141 | ~~~~ 142 | 143 | To extract data for a single particular bookmark one should type in a command: 144 | 145 | ~~~~ 146 | curl -w "\n" 2>/dev/null -H "accept: application/json" localhost:5460/Phil/bookmarks/1 -u Phil:1 147 | ~~~~ 148 | 149 | To edit a bookmark the HTTP PUT method is used. 150 | 151 | ~~~~ 152 | curl -X PUT -w "\n" 2>/dev/null localhost:5460/my/bookmarks/1 -u Phil:1 \ 153 | -H "Content-Type: application/json" -H "accept: application/json" -d '{"url":"github.com"}' 154 | ~~~~ 155 | 156 | To add a bookmark use the following. 157 | 158 | ~~~~ 159 | curl -X POST -w "\n" 2>/dev/null localhost:5460/my/bookmarks \ 160 | -u Phil:1 -H "Content-Type: application/json" -H "accept: application/json" \ 161 | -d '{"url":"http://github.com", "description":"A lot of great projects"}' 162 | ~~~~ 163 | 164 | To remove a bookmark the HTTP DELETE method is used. 165 | 166 | ~~~~ 167 | curl -X DELETE -w "\n" 2>/dev/null -H "accept: application/json" \ 168 | localhost:5460/my/bookmarks/1 -u Phil:1 169 | ~~~~ 170 | 171 | The testing case will come up with the following updates to the project 172 | 173 | ## Play with CLI 174 | 175 | CLI is the friend to system administrator. ActFramework makes it super easy 176 | to create CLI command, and you can even share the same code to serve both 177 | HTTP request and CLI command: 178 | 179 | In the `BookmarkService`, we have the following code: 180 | 181 | ```java 182 | @Command("bm.list") 183 | @GetAction 184 | public Collection getAllBookmarks() { 185 | return user.getBookmarks(); 186 | } 187 | ``` 188 | 189 | Which denote the method `getAllBookmarks()` as the HTTP GET request handler 190 | at `/{username}/bookmarks` which is defined in the `@Controller` class 191 | annotation; And it mark the method as the command executor for `bm.list` 192 | command at the same time. We have already seen how to use RESTful 193 | request to check all bookmarks of a user. Now we are going to use CLI command 194 | to do the same job. 195 | 196 | First use telnet or nc or any other similar to to hook into ACT's CLI console 197 | session: 198 | 199 | ``` 200 | GreenLaptop:ActBookmarks luog$ nc localhost 5461 201 | / \ ___ | |_ | __ ) ___ ___ | | __ _ __ ___ __ _ _ __ | | __ 202 | / _ \ / __|| __|| _ \ / _ \ / _ \ | |/ /| '_ ` _ \ / _` || '__|| |/ / 203 | / ___ \ | (__ | |_ | |_) || (_) || (_) || < | | | | | || (_| || | | < 204 | /_/ \_\ \___| \__||____/ \___/ \___/ |_|\_\|_| |_| |_| \__,_||_| |_|\_\ 205 | 206 | powered by ActFramework v0.3.0-38e7 207 | 208 | version: 0.0.1 209 | base dir: /home/luog/p/greenlaw110/ActBookmarks/target/dist 210 | profile: prod 211 | mode: PROD 212 | ActBookmark[2kbNDb01]> 213 | ``` 214 | 215 | Then type in `help -a` to check all application commands: 216 | 217 | ``` 218 | APPLICATION COMMANDS 219 | 220 | bm.list - show all bookmarks of the specified person 221 | bm.show - show specific bookmark of the specified person 222 | db.init - init database (Note, this will clean up all existing data!) 223 | user.add - add an new user 224 | user.list - list all users 225 | user.show - show user 226 | ``` 227 | 228 | To check detail of a certain command type `help ` or ` -h`: 229 | 230 | ``` 231 | bm.list -h 232 | Usage: bm.list 233 | show all bookmarks of the specified person 234 | 235 | Options: 236 | -u,--user specify user by username 237 | 238 | Shortcuts: .bl, b.l, b.list, bm.l, bm.li 239 | ActBookmark[2kbNDb01]> 240 | ``` 241 | 242 | One can type in `bm.list -u Pete` to list all bookmarks of user `Pete` 243 | 244 | ``` 245 | bm.list -u Pete 246 | +----+--------------+-------------------+ 247 | | ID | DESCRIPTION | URL | 248 | +----+--------------+-------------------+ 249 | | 3 | A playground | http://github.com | 250 | | 4 | Be cautious | http://ibm.com | 251 | +----+--------------+-------------------+ 252 | Items found: 2 253 | ``` 254 | 255 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.greenlaw110 5 | actbookmarks 6 | 0.0.1-SNAPSHOT 7 | jar 8 | ActBookmarks 9 | A simple ActFramework project exposing REST API to store bookmarks 10 | 11 | UTF-8 12 | UTF-8 13 | 1.8 14 | 0.3.0-SNAPSHOT 15 | 0.4.0-SNAPSHOT 16 | 0.3.0-SNAPSHOT 17 | com.greenlaw110.bookmark.ActBookmarksApplication 18 | 19 | 20 | 21 | org.sonatype.oss 22 | oss-parent 23 | 7 24 | 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.11 31 | test 32 | 33 | 34 | org.hamcrest 35 | java-hamcrest 36 | 2.0.0.0 37 | test 38 | 39 | 40 | org.mockito 41 | mockito-core 42 | 1.9.5 43 | test 44 | 45 | 46 | com.github.fakemongo 47 | fongo 48 | 2.0.7 49 | test 50 | 51 | 52 | org.actframework 53 | act 54 | ${act.version} 55 | 56 | 57 | org.actframework 58 | act-morphia 59 | ${act.morphia.version} 60 | 61 | 62 | org.actframework 63 | act-aaa 64 | ${act.aaa.version} 65 | 66 | 67 | ch.qos.logback 68 | logback-classic 69 | 1.1.6 70 | 71 | 72 | 73 | ${project.artifactId}-${project.version} 74 | 75 | 76 | src/main/resources 77 | true 78 | 79 | **/*.version 80 | **/*.properties 81 | **/*.xml 82 | **/*.csv 83 | **/*.txt 84 | **/*.gif 85 | **/*.yaml 86 | **/*.json 87 | rythm/** 88 | bin/* 89 | routes 90 | 91 | 92 | 93 | src/main/resources 94 | false 95 | 96 | **/*.xlsx 97 | ios*.p12 98 | 99 | 100 | 101 | 102 | 103 | maven-javadoc-plugin 104 | 2.9 105 | 106 | ch.raffael.doclets.pegdown.PegdownDoclet 107 | 108 | ch.raffael.pegdown-doclet 109 | pegdown-doclet 110 | 1.1 111 | 112 | true 113 | 114 | 115 | 116 | org.apache.maven.plugins 117 | maven-assembly-plugin 118 | 2.5.5 119 | 120 | 121 | create-archive 122 | package 123 | 124 | single 125 | 126 | 127 | false 128 | 129 | src/assembly/pkg.xml 130 | 131 | ${project.build.directory}/dist/ 132 | 133 | 134 | 135 | 136 | 137 | org.codehaus.mojo 138 | exec-maven-plugin 139 | 1.2.1 140 | 141 | java 142 | 143 | -Xms512m 144 | -Xmx512m 145 | -classpath 146 | 147 | ${app.entry} 148 | 149 | 150 | 151 | 152 | org.codehaus.mojo 153 | findbugs-maven-plugin 154 | 3.0.3 155 | 156 | true 157 | true 158 | 159 | 160 | 161 | org.codehaus.mojo 162 | sonar-maven-plugin 163 | 3.0.2 164 | 165 | 166 | org.apache.maven.plugins 167 | maven-resources-plugin 168 | 2.7 169 | 170 | UTF-8 171 | 172 | 173 | 174 | org.apache.maven.plugins 175 | maven-compiler-plugin 176 | 3.3 177 | 178 | ${java.version} 179 | ${java.version} 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /src/assembly/pkg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 21 | 22 | 23 | dist 24 | 25 | 26 | zip 27 | 28 | 29 | false 30 | 31 | 32 | 33 | ${project.basedir} 34 | 35 | 36 | README.md 37 | 38 | 39 | 40 | ${project.build.directory} 41 | 42 | 43 | classes/** 44 | asset/** 45 | 46 | 47 | 48 | ${project.basedir}/src/main/asset 49 | asset 50 | 51 | 52 | ${project.basedir}/src/main/bin 53 | 54 | 55 | start* 56 | 57 | true 58 | 59 | 60 | 61 | 62 | 63 | lib 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/main/bin/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | APP_ENTRY=${app.entry} 3 | 4 | DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 5 | BASE=$DIR/. 6 | CP=$BASE/classes:$BASE/lib/* 7 | CMD=$0 8 | DEBUG=0 9 | DEBUG_PORT=5005 10 | PROFILE= 11 | SYS_PROP= 12 | HELP=0 13 | 14 | # As long as there is at least one more argument, keep looping 15 | while [[ $# -gt 0 ]]; do 16 | key="$1" 17 | case "$key" in 18 | # enable remote debugging with -d or --debug 19 | -d|--debug) 20 | DEBUG=1 21 | ;; 22 | # specify remote debug port with --debug-port= 23 | --debug-port) 24 | shift # past the key and to the value 25 | DEBUG_PORT="$1" 26 | ;; 27 | # specify the profile with -p or --profile 28 | -p|--profile) 29 | shift # past the key and to the value 30 | PROFILE="$1" 31 | ;; 32 | # specify any java system property 33 | -D*) 34 | SYS_PROP="$SYSPROP $1" 35 | ;; 36 | # help ? 37 | -h|--help) 38 | echo $CMD start the app 39 | echo '' 40 | echo ' -d --debug: enable remote debugging' 41 | echo ' --debug-port : specify debug port (if not specified then debug port is 5005)' 42 | echo ' -p --profile: specify the profile to start the app' 43 | echo ' -D*: specify any JVM system properties' 44 | echo ' -h --help: display this help message' 45 | HELP=1 46 | ;; 47 | *) 48 | # Do whatever you want with extra options 49 | echo "Unknown option '$key'" 50 | ;; 51 | esac 52 | # Shift after checking all the cases to get the next option 53 | shift 54 | done 55 | 56 | if [[ HELP -gt 0 ]]; then 57 | # don't run in help mode 58 | echo '' 59 | else 60 | if [[ DEBUG -gt 0 ]]; then 61 | java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$DEBUG_PORT $JAVA_OPTS -Dapp.mode=prod -Dprofile=$PROFILE $SYS_PROP -cp "$CP" $APP_ENTRY 62 | else 63 | java $JAVA_OPTS -Dapp.mode=prod -Dprofile=$PROFILE $SYS_PROP -cp "$CP" $APP_ENTRY 64 | fi 65 | fi -------------------------------------------------------------------------------- /src/main/bin/start.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal & pushd 3 | set APP_ENTRY=${app.entry} 4 | set BASE=%~dp0 5 | set CP=%BASE%\classes;%BASE%\lib\* 6 | java -Dapp.mode=prod -Dprofile=%PROFILE% -cp "%CP%" %APP_ENTRY% 7 | endlocal & popd 8 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/ActBookmarksApplication.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark; 2 | 3 | import act.Version; 4 | import act.boot.app.RunApp; 5 | 6 | public class ActBookmarksApplication { 7 | 8 | public static void main(String[] args) throws Exception { 9 | RunApp.start("ActBookmark", Version.appVersion(), ActBookmarksApplication.class); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/endpoint/BookmarksService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License 3 | * 4 | * Copyright 2016 Dmitry Noranovich . 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | package com.greenlaw110.bookmark.endpoint; 25 | 26 | import act.cli.Command; 27 | import act.cli.JsonView; 28 | import act.cli.Required; 29 | import act.controller.Controller; 30 | import act.db.DbBind; 31 | import com.greenlaw110.bookmark.model.Bookmark; 32 | import com.greenlaw110.bookmark.model.User; 33 | import org.osgl.aaa.NoAuthentication; 34 | import org.osgl.mvc.annotation.Before; 35 | import org.osgl.mvc.annotation.GetAction; 36 | 37 | import javax.enterprise.context.SessionScoped; 38 | import javax.inject.Inject; 39 | import javax.inject.Named; 40 | import java.util.Collection; 41 | 42 | /** 43 | * The controller that exposes resource methods to work with bookmarks of a 44 | * particular user. 45 | * 46 | * Note all logged in user can have access to resources of all users including self 47 | * 48 | * @author Gelin Luo 49 | */ 50 | @Controller("/{username}/bookmarks") 51 | @SuppressWarnings("unused") 52 | public class BookmarksService extends Controller.Util { 53 | 54 | @DbBind(byId = false) 55 | @Named("username") 56 | @Required("specify user by username") 57 | @SessionScoped 58 | private User user; 59 | 60 | @Inject 61 | private User.Dao userDao; 62 | 63 | /** 64 | * If the user does not exist, then respond with 404 65 | */ 66 | @Before 67 | public void validateUser() { 68 | notFoundIfNull(user); 69 | } 70 | 71 | /** 72 | * A method to return bookmarks for a particular user. 73 | * 74 | * @return list of user's bookmarks. 75 | */ 76 | @Command(value = "bm.list", help = "show all bookmarks of the specified person") 77 | @GetAction 78 | public Collection getAllBookmarks() { 79 | return user.getBookmarks(); 80 | } 81 | 82 | /** 83 | * A method to find a bookmark by id. 84 | * 85 | * @param bookmarkId 86 | */ 87 | @Command(value = "bm.show", help = "show specific bookmark of the specified person") 88 | @GetAction("{bookmarkId}") 89 | @JsonView 90 | public Bookmark getBookmark(@Required("specify bookmark ID") Long bookmarkId) { 91 | return user.getBookmark(bookmarkId); 92 | } 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/endpoint/HelloService.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark.endpoint; 2 | 3 | import org.osgl.aaa.NoAuthentication; 4 | import org.osgl.mvc.annotation.GetAction; 5 | import org.osgl.util.S; 6 | 7 | @NoAuthentication 8 | @SuppressWarnings("unused") 9 | public class HelloService { 10 | /** 11 | * A simple greeting. 12 | */ 13 | public static final String GREETING = "Hello Act Framework"; 14 | 15 | /** 16 | * The resource method with a path parameter or query parameter 17 | * to return customized greeting. 18 | * 19 | * When `name` is not found then it will return the 20 | * default {@link #GREETING} 21 | * 22 | * @param name The name of a person to greet. 23 | * @return Customized greeting. 24 | */ 25 | @GetAction("/hello/{name}") 26 | public String getPathParamOrQueryParamGreeting(String name) { 27 | return S.blank(name) ? GREETING : "Hello " + name; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/endpoint/MyBookmarkService.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark.endpoint; 2 | 3 | import act.aaa.LoginUser; 4 | import act.app.ActionContext; 5 | import act.controller.Controller; 6 | import com.greenlaw110.bookmark.model.Bookmark; 7 | import com.greenlaw110.bookmark.model.User; 8 | import org.osgl.http.H; 9 | import org.osgl.mvc.annotation.DeleteAction; 10 | import org.osgl.mvc.annotation.PostAction; 11 | import org.osgl.mvc.annotation.PutAction; 12 | 13 | import javax.inject.Inject; 14 | 15 | /** 16 | * The controller that exposes resource methods to work with bookmarks of 17 | * the logged in user only 18 | * 19 | * @author Gelin Luo 20 | */ 21 | @Controller("/my/bookmarks") 22 | @SuppressWarnings("unused") 23 | public class MyBookmarkService extends Controller.Util { 24 | 25 | @LoginUser 26 | private User me; 27 | 28 | @Inject 29 | private User.Dao userDao; 30 | 31 | 32 | /** 33 | * A method to add a bookmark. 34 | * @param bookmark 35 | * @param context 36 | * @return the bookmark been added 37 | */ 38 | @PostAction 39 | public Bookmark addBookmark(Bookmark bookmark, ActionContext context) { 40 | me.addBookmark(bookmark); 41 | userDao.save(me); 42 | context.resp().status(H.Status.CREATED); 43 | return bookmark; 44 | } 45 | 46 | /** 47 | * A method to edit a bookmark. 48 | * 49 | * @param bookmarkId 50 | * @param bookmark 51 | * @return the patched bookmark 52 | * status code. 53 | */ 54 | @PutAction("{bookmarkId}") 55 | public Bookmark editBookmark(Long bookmarkId, Bookmark bookmark) { 56 | notFoundIfNot(me.hasBookmark(bookmarkId)); 57 | me.updateBookmark(bookmarkId, bookmark); 58 | userDao.save(me); 59 | return bookmark; 60 | } 61 | 62 | /** 63 | * A method to delete a bookmark identified by id. 64 | * 65 | * @param bookmarkId The id of the bookmark to be deleted. 66 | * @return ResponseEntity containing a deleted bookmark, if found, and 67 | * status code. 68 | */ 69 | @DeleteAction("{bookmarkId}") 70 | public Bookmark deleteBookmark(Long bookmarkId) { 71 | Bookmark bookmark = me.deleteBookmark(bookmarkId); 72 | notFoundIfNull(bookmark); 73 | userDao.save(me); 74 | return bookmark; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/endpoint/UserService.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark.endpoint; 2 | 3 | import act.app.DbServiceManager; 4 | import act.cli.CliContext; 5 | import act.cli.Command; 6 | import act.cli.JsonView; 7 | import act.cli.Required; 8 | import act.db.morphia.MorphiaService; 9 | import act.util.PropertySpec; 10 | import com.greenlaw110.bookmark.model.Bookmark; 11 | import com.greenlaw110.bookmark.model.User; 12 | import org.osgl.aaa.NoAuthentication; 13 | import org.osgl.inject.annotation.Provided; 14 | import org.osgl.mvc.annotation.PostAction; 15 | import org.osgl.util.C; 16 | 17 | import javax.inject.Inject; 18 | import java.util.List; 19 | 20 | /** 21 | * The endpoint provides CLI commands for adding/visiting 22 | * users 23 | */ 24 | @SuppressWarnings("unused") 25 | public class UserService { 26 | 27 | @Inject 28 | private User.Dao userDao; 29 | 30 | @Command(name = "user.add", help = "add an new user") 31 | @PropertySpec(User.DETAIL_VIEW) 32 | @JsonView 33 | public User addUser( 34 | @Required("specify username") String username, 35 | @Required("specify password") String password 36 | ) { 37 | User user = new User(username, password); 38 | userDao.save(user); 39 | return user; 40 | } 41 | 42 | @Command(name = "user.list", help = "list all users") 43 | @PropertySpec(User.LIST_VIEW) 44 | public List list() { 45 | return C.list(userDao.findAll()); 46 | } 47 | 48 | @Command(name = "user.show", help = "show user") 49 | @PropertySpec(User.DETAIL_VIEW) 50 | @JsonView 51 | public User show(@Required String username) { 52 | return userDao.findOneBy("username", username); 53 | } 54 | 55 | @Command(name = "db.init", help = "init database (Note, this will clean up all existing data!)") 56 | public void initDb( 57 | @Provided DbServiceManager dbServiceManager, 58 | @Provided CliContext ctx 59 | ) { 60 | resetDatabase(dbServiceManager); 61 | 62 | createSample("Phil", "1", "economist.com::Cool reading", "time.com::Some news"); 63 | createSample("Pete", "2", "github.com::A playground", "ibm.com::Be cautious"); 64 | 65 | ctx.println("DB initialized with sample data"); 66 | } 67 | 68 | private void resetDatabase(DbServiceManager dbServiceManager) { 69 | MorphiaService svc = dbServiceManager.dbService(DbServiceManager.DEFAULT); 70 | svc.ds().getDB().dropDatabase(); 71 | } 72 | 73 | private void createSample(String username, String password, String... bookmarks) { 74 | User user = new User(username, password); 75 | for (String bookmark : bookmarks) { 76 | String[] sa = bookmark.split("::"); 77 | String url = sa[0]; 78 | String desc = sa[1]; 79 | if (!url.startsWith("http")) { 80 | url = "http://" + url; 81 | } 82 | user.addBookmark(new Bookmark(url, desc)); 83 | } 84 | userDao.save(user); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/model/Bookmark.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark.model; 2 | 3 | import act.data.annotation.Data; 4 | import act.db.util.SequenceNumberGenerator; 5 | 6 | @Data 7 | public class Bookmark { 8 | 9 | private Long id; 10 | 11 | /** 12 | * The URL of a bookmark. 13 | */ 14 | private String url; 15 | /** 16 | * The description of a bookmark. 17 | */ 18 | private String description; 19 | 20 | private Bookmark() { 21 | } 22 | 23 | public Bookmark(String url, String description) { 24 | this.url = url; 25 | this.description = description; 26 | this.id = genId(); 27 | } 28 | 29 | public Long ensureId() { 30 | if (null == id) { 31 | id = genId(); 32 | } 33 | return id; 34 | } 35 | 36 | public String getUrl() { 37 | return url; 38 | } 39 | 40 | public void setUrl(String url) { 41 | this.url = url; 42 | } 43 | 44 | public Long getId() { 45 | return id; 46 | } 47 | 48 | public void setId(Long id) { 49 | this.id = id; 50 | } 51 | 52 | public String getDescription() { 53 | return description; 54 | } 55 | 56 | public void setDescription(String description) { 57 | this.description = description; 58 | } 59 | 60 | private static Long genId() { 61 | return SequenceNumberGenerator.next("bookmark"); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/model/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License 3 | * 4 | * Copyright 2016 Dmitry Noranovich . 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | package com.greenlaw110.bookmark.model; 25 | 26 | import act.Act; 27 | import act.db.morphia.MorphiaDao; 28 | import act.db.morphia.MorphiaModel; 29 | import org.mongodb.morphia.annotations.Embedded; 30 | import org.mongodb.morphia.annotations.Entity; 31 | import org.osgl.util.E; 32 | import org.osgl.util.S; 33 | 34 | import java.util.Collection; 35 | import java.util.HashMap; 36 | import java.util.Map; 37 | 38 | /** 39 | * A class to store application user data. 40 | * 41 | * @author Gelin Luo 42 | */ 43 | @Entity("users") 44 | public class User extends MorphiaModel { 45 | 46 | public static final String LIST_VIEW = "username, bookmarkNum as bookmarks"; 47 | public static final String DETAIL_VIEW = "-password"; 48 | 49 | /** 50 | * A username to login to the application. 51 | */ 52 | private String username; 53 | 54 | /** 55 | * A password to login to the application. 56 | */ 57 | private String password; 58 | 59 | /** 60 | * Bookmark list of a user. 61 | */ 62 | @Embedded 63 | private Map bookmarks = new HashMap<>(); 64 | 65 | /** 66 | * A no-argument constructor. 67 | */ 68 | private User() { 69 | } 70 | 71 | /** 72 | * A constructor used to create users. 73 | * 74 | * @param username 75 | * @param password 76 | */ 77 | public User(String username, String password) { 78 | E.illegalArgumentIf(S.blank(username)); 79 | E.illegalArgumentIf(S.blank(password)); 80 | this.username = username; 81 | this.password = Act.crypto().passwordHash(password); 82 | } 83 | 84 | public String getUsername() { 85 | return username; 86 | } 87 | 88 | public void setUsername(String username) { 89 | E.illegalArgumentIf(S.blank(username)); 90 | this.username = username; 91 | } 92 | 93 | public String getPassword() { 94 | return password; 95 | } 96 | 97 | public void setPassword(String password) { 98 | this.password = Act.crypto().passwordHash(password); 99 | } 100 | 101 | public boolean passwordMatches(char[] password) { 102 | return Act.crypto().verifyPassword(password, getPassword()); 103 | } 104 | 105 | public Collection getBookmarks() { 106 | return bookmarks.values(); 107 | } 108 | 109 | public int getBookmarkNum() { 110 | return bookmarks.size(); 111 | } 112 | 113 | public Bookmark getBookmark(Long bookmarkId) { 114 | return bookmarks.get(bookmarkId); 115 | } 116 | 117 | public boolean hasBookmark(Long bookmarkId) { 118 | return bookmarks.containsKey(bookmarkId); 119 | } 120 | 121 | public Bookmark deleteBookmark(Long bookmarkId) { 122 | return bookmarks.remove(bookmarkId); 123 | } 124 | 125 | /** 126 | * A method to add bookmarks to user's list. 127 | * 128 | * @param bookmark a bookmark to add. 129 | * @return the added bookmark. 130 | */ 131 | public Bookmark addBookmark(Bookmark bookmark) { 132 | bookmarks.put(bookmark.ensureId(), bookmark); 133 | return bookmark; 134 | } 135 | 136 | public Bookmark updateBookmark(Long id, Bookmark bookmark) { 137 | bookmark.setId(id); 138 | bookmarks.put(id, bookmark); 139 | return bookmark; 140 | } 141 | 142 | public static class Dao extends MorphiaDao { 143 | public Dao() { 144 | super(User.class); 145 | } 146 | 147 | public User authenticate(String username, char[] password) { 148 | User user = findOneBy("username", username); 149 | if (user.passwordMatches(password)) { 150 | return user; 151 | } 152 | return null; 153 | } 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/main/java/com/greenlaw110/bookmark/security/AppSecurityService.java: -------------------------------------------------------------------------------- 1 | package com.greenlaw110.bookmark.security; 2 | 3 | import act.aaa.ActAAAService; 4 | import com.greenlaw110.bookmark.model.User; 5 | 6 | /** 7 | * This class bridge the application to Act-AAA security framework 8 | */ 9 | public class AppSecurityService extends ActAAAService.Base { 10 | 11 | @Override 12 | protected String nameOf(User user) { 13 | return user.getUsername(); 14 | } 15 | 16 | @Override 17 | protected boolean verifyPassword(User user, char[] chars) { 18 | return user.passwordMatches(chars); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/resources/app.version: -------------------------------------------------------------------------------- 1 | app.version=${project.version} -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # The mongodb database name 2 | db.db=bm 3 | basic_authentication=true 4 | csrf.enabled=false 5 | aaa.user.key=username -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n 11 | 12 | 13 | 14 | 15 | 16 | act.log 17 | 18 | %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | /act.%i.log.zip 23 | 1 24 | 10 25 | 26 | 27 | 28 | 2MB 29 | 30 | 31 | 32 | 33 | act.metric.log 34 | 35 | %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 36 | 37 | 38 | 39 | /act.metric.%i.log.zip 40 | 1 41 | 10 42 | 43 | 44 | 45 | 2MB 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 | -------------------------------------------------------------------------------- /start: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | mvn clean compile exec:exec --------------------------------------------------------------------------------