├── .gitignore ├── LICENSE ├── README.asciidoc ├── build.gradle ├── common ├── build.gradle └── src │ └── main │ └── java │ └── io │ └── github │ └── cdelmas │ └── spike │ └── common │ ├── auth │ ├── AccessTokenVerificationCommand.java │ ├── AccessTokenVerificationCommandFactory.java │ ├── FacebookAccessTokenVerificationCommand.java │ └── User.java │ ├── domain │ ├── Car.java │ └── CarRepository.java │ ├── hateoas │ ├── Link.java │ └── Representation.java │ └── persistence │ └── InMemoryCarRepository.java ├── dropwizard ├── build.gradle └── src │ └── main │ ├── java │ └── io │ │ └── github │ │ └── cdelmas │ │ └── spike │ │ └── dropwizard │ │ ├── Main.java │ │ ├── car │ │ ├── CarModule.java │ │ ├── CarRepresentation.java │ │ ├── CarResource.java │ │ └── CarsResource.java │ │ ├── hello │ │ ├── CarService.java │ │ ├── HelloModule.java │ │ ├── HelloWorldResource.java │ │ ├── RemoteCarService.java │ │ └── Saying.java │ │ └── infrastructure │ │ ├── DropwizardApplication.java │ │ ├── DropwizardServerConfiguration.java │ │ ├── TemplateHealthCheck.java │ │ └── auth │ │ └── FacebookTokenAuthenticator.java │ └── resources │ └── dropwizard-server.yml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── restlet-server ├── build.gradle └── src │ └── main │ └── java │ └── io │ └── github │ └── cdelmas │ └── spike │ └── restlet │ ├── Main.java │ ├── RestComponent.java │ ├── car │ ├── Car.java │ ├── CarApplication.java │ ├── CarModule.java │ ├── CarRepresentation.java │ ├── CarResource.java │ └── CarsResource.java │ ├── hello │ ├── CarService.java │ ├── CarServiceInterface.java │ ├── Hello.java │ ├── HelloApplication.java │ ├── HelloModule.java │ ├── HelloResource.java │ └── RemoteCarService.java │ └── infrastructure │ ├── JacksonCustomConverter.java │ ├── auth │ └── AuthTokenVerifier.java │ └── di │ └── RestletInfraModule.java ├── settings.gradle ├── sparkjava ├── build.gradle └── src │ └── main │ └── java │ └── io │ └── github │ └── cdelmas │ └── spike │ └── sparkjava │ ├── AuthenticationFilter.java │ ├── CarRepresentation.java │ ├── CarsResource.java │ ├── HelloResource.java │ └── Main.java ├── spring-boot ├── build.gradle └── src │ └── main │ ├── java │ └── io │ │ └── github │ │ └── cdelmas │ │ └── spike │ │ └── springboot │ │ ├── Main.java │ │ ├── car │ │ ├── CarRepositoryAdapter.java │ │ ├── CarRepresentation.java │ │ └── CarsController.java │ │ └── hello │ │ └── SampleController.java │ └── resources │ └── application.yml └── vertx ├── build.gradle └── src └── main └── java └── io └── github └── cdelmas └── spike └── vertx ├── Main.java ├── car ├── CarRepresentation.java ├── CarResource.java └── CarsResource.java ├── hello └── HelloResource.java └── infrastructure └── auth ├── BearerAuthHandler.java ├── FacebookOauthTokenVerifier.java └── MyUser.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea 3 | .gradle 4 | common/build 5 | restlet-server/build 6 | dropwizard/build 7 | vertx/build 8 | springboot/build 9 | sparkjava/build 10 | build 11 | -------------------------------------------------------------------------------- /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.asciidoc: -------------------------------------------------------------------------------- 1 | = Microservices libraries comparison 2 | 3 | == Purpose 4 | 5 | This project is the companion of this Blog article: https://cdelmas.github.io/2015/11/01/A-comparison-of-Microservices-Frameworks.html. 6 | 7 | == Setup 8 | 9 | You will need a keystore. You can follow a tutorial http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/https[here] to set up a keystore with a self-signed certificate. 10 | 11 | One of the client libraries requires you to import the Facebook's certificate into the truststore. To do that, you can follow these http://stackoverflow.com/questions/373295/digital-certificate-how-to-import-cer-file-in-to-truststore-file-using[instructions]. 12 | 13 | == Build 14 | 15 | To build the servers, just run `gradlew shadowJar` in the command line. By the way, you need a JDK 8, but I guess you're up-to-date :) 16 | 17 | == Run 18 | 19 | Then you can run each server using `java -Djavax.net.ssl.trustStore=/path/to/trustStore.jks -Djavax.net.ssl.trustStorePassword=thepass -Djavax.net.ssl.keyStorePassword=thePass -Djavax.net.ssl.keyStorePath=/path/to/keyStore.jks -jar .jar`. Don't forget to replace `` with the correct jar name. 20 | 21 | The Dropwizard server needs a special command line: `java ... -jar dropwizard.jar server /path/to/dropwizard-server.yml`. 22 | 23 | == Notes 24 | 25 | There are no unit tests, and it is fully assumed. 26 | 27 | == The missing guys 28 | 29 | In a future version, I will add Restx, Payara and Swarm to the comparison. It is a long exercise though, but stay in touch ;) 30 | 31 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | allprojects { 2 | apply plugin: 'java' 3 | group = 'io.github.cdelmas' 4 | version = '1.0' 5 | sourceCompatibility = 1.8 6 | } 7 | 8 | subprojects { 9 | repositories { 10 | mavenCentral() 11 | jcenter() 12 | } 13 | 14 | dependencies { 15 | compile 'com.javaslang:javaslang:1.2.2' 16 | 17 | compile 'javax.inject:javax.inject:1' 18 | 19 | runtime 'ch.qos.logback:logback-classic:1.1.3' 20 | 21 | testCompile 'org.mockito:mockito-all:1.9.5' 22 | testCompile 'junit:junit:4.12' 23 | testCompile 'org.hamcrest:hamcrest-core:1.3' 24 | } 25 | } 26 | 27 | project(':restlet-server') { 28 | dependencies { 29 | compile project(':common') 30 | } 31 | } 32 | 33 | project(':dropwizard') { 34 | dependencies { 35 | compile project(':common') 36 | } 37 | } 38 | 39 | project(':vertx') { 40 | dependencies { 41 | compile project(':common') 42 | } 43 | } 44 | 45 | project(':spring-boot') { 46 | dependencies { 47 | compile project(':common') 48 | } 49 | } 50 | 51 | project(':sparkjava') { 52 | dependencies { 53 | compile project(':common') 54 | } 55 | } -------------------------------------------------------------------------------- /common/build.gradle: -------------------------------------------------------------------------------- 1 | 2 | dependencies { 3 | compile 'com.restfb:restfb:1.14.0' 4 | compile 'com.netflix.hystrix:hystrix-core:1.4.14' 5 | } -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/auth/AccessTokenVerificationCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.auth; 17 | 18 | import javaslang.control.Try; 19 | 20 | public interface AccessTokenVerificationCommand { 21 | Try executeCommand(); 22 | } 23 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/auth/AccessTokenVerificationCommandFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.auth; 17 | 18 | public class AccessTokenVerificationCommandFactory { 19 | 20 | public AccessTokenVerificationCommand createVerificationCommand(String accessToken) { 21 | return new FacebookAccessTokenVerificationCommand(accessToken); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/auth/FacebookAccessTokenVerificationCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.auth; 17 | 18 | import com.netflix.hystrix.HystrixCommand; 19 | import com.netflix.hystrix.HystrixCommandGroupKey; 20 | import com.netflix.hystrix.HystrixCommandKey; 21 | import com.restfb.DefaultFacebookClient; 22 | import com.restfb.Version; 23 | import com.restfb.json.JsonObject; 24 | import javaslang.control.Try; 25 | 26 | public class FacebookAccessTokenVerificationCommand extends HystrixCommand> implements AccessTokenVerificationCommand { 27 | 28 | private final String accessToken; 29 | 30 | public FacebookAccessTokenVerificationCommand(String accessToken) { 31 | super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("AccessTokenVerification")) 32 | .andCommandKey(HystrixCommandKey.Factory.asKey("FacebookAccessTokenVerification"))); 33 | this.accessToken = accessToken; 34 | } 35 | 36 | @Override 37 | public Try executeCommand() { 38 | return super.execute(); 39 | } 40 | 41 | 42 | @Override 43 | protected Try run() throws Exception { 44 | DefaultFacebookClient fb = new DefaultFacebookClient(accessToken, Version.VERSION_2_4); 45 | 46 | return Try.of(() -> fb.fetchObject("me", JsonObject.class)) 47 | .map(this::createUser); 48 | } 49 | 50 | private User createUser(JsonObject fbu) { 51 | User user = new User(fbu.getString("id"), fbu.getString("name")); 52 | user.setToken(accessToken); 53 | return user; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/auth/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.auth; 17 | 18 | public class User { 19 | 20 | private final String id; 21 | 22 | private final String name; 23 | private String token; 24 | 25 | public User(String id, String name) { 26 | this.id = id; 27 | this.name = name; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public String getToken() { 39 | return token; 40 | } 41 | 42 | public void setToken(String token) { 43 | this.token = token; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/domain/Car.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.domain; 17 | 18 | public class Car { 19 | 20 | private Integer id; 21 | private String name; 22 | 23 | public Car(String name) { 24 | this.name = name; 25 | } 26 | 27 | Car() { 28 | 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public Integer getId() { 40 | return id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/domain/CarRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.domain; 17 | 18 | import java.util.List; 19 | import java.util.Optional; 20 | 21 | public interface CarRepository { 22 | 23 | Optional byId(int id); 24 | 25 | List all(); 26 | 27 | void save(Car car); 28 | 29 | void update(Car car); 30 | 31 | void delete(Car car); 32 | } 33 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/hateoas/Link.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.hateoas; 17 | 18 | public class Link { 19 | private String rel; 20 | private String href; 21 | private String method; 22 | 23 | public static Link self(String href) { 24 | return new Link("self", href); 25 | } 26 | 27 | public static Link remove(String href) { 28 | return new Link("remove", href,"DELETE"); 29 | } 30 | 31 | public Link(String rel, String href, String method) { 32 | this.rel = rel; 33 | this.href = href; 34 | this.method = method; 35 | } 36 | 37 | public Link(String rel, String href) { 38 | this(rel, href, "GET"); 39 | } 40 | 41 | Link() { 42 | } 43 | 44 | public String getRel() { 45 | return rel; 46 | } 47 | 48 | public void setRel(String rel) { 49 | this.rel = rel; 50 | } 51 | 52 | public String getHref() { 53 | return href; 54 | } 55 | 56 | public void setHref(String href) { 57 | this.href = href; 58 | } 59 | 60 | public String getMethod() { 61 | return method; 62 | } 63 | 64 | public void setMethod(String method) { 65 | this.method = method; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/hateoas/Representation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.hateoas; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import static java.util.Collections.unmodifiableList; 22 | 23 | public class Representation { 24 | 25 | private final List links = new ArrayList<>(1); // most of the time, there will only have 1 link (self) 26 | 27 | public void addLink(Link link) { 28 | links.add(link); 29 | } 30 | 31 | public List getLinks() { 32 | return unmodifiableList(links); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /common/src/main/java/io/github/cdelmas/spike/common/persistence/InMemoryCarRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.common.persistence; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | 21 | import javax.inject.Singleton; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | import java.util.Map; 25 | import java.util.Optional; 26 | import java.util.concurrent.ConcurrentHashMap; 27 | import java.util.concurrent.atomic.AtomicInteger; 28 | 29 | @Singleton 30 | public class InMemoryCarRepository implements CarRepository { 31 | 32 | private final AtomicInteger idGenerator = new AtomicInteger(1); 33 | private final Map repository = new ConcurrentHashMap<>(); 34 | 35 | @Override 36 | public Optional byId(int id) { 37 | return Optional.ofNullable(repository.get(id)); 38 | } 39 | 40 | @Override 41 | public List all() { 42 | return new ArrayList<>(repository.values()); 43 | } 44 | 45 | @Override 46 | public void save(Car car) { 47 | if (car.getId() == null) { 48 | car.setId(idGenerator.getAndIncrement()); 49 | } 50 | repository.put(car.getId(), car); 51 | } 52 | 53 | @Override 54 | public void update(Car car) { 55 | save(car); 56 | } 57 | 58 | @Override 59 | public void delete(Car car) { 60 | repository.remove(car.getId()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /dropwizard/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '1.2.2' 3 | } 4 | 5 | dependencies { 6 | compile 'io.dropwizard:dropwizard-core:0.8.2' 7 | compile 'io.dropwizard:dropwizard-client:0.8.2' 8 | compile 'io.dropwizard:dropwizard-auth:0.8.2' 9 | compile 'io.dropwizard.modules:dropwizard-java8:0.8.0-2' 10 | compile 'com.hubspot.dropwizard:dropwizard-guice:0.8.1.2' 11 | } 12 | 13 | jar { 14 | manifest { 15 | attributes 'Main-Class': 'io.github.cdelmas.spike.dropwizard.Main' 16 | } 17 | } 18 | 19 | shadowJar { 20 | mergeServiceFiles() 21 | } -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard; 17 | 18 | import io.github.cdelmas.spike.dropwizard.infrastructure.DropwizardApplication; 19 | 20 | public class Main { 21 | 22 | public static void main(String[] args) throws Exception { 23 | new DropwizardApplication().run(args); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/car/CarModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.car; 17 | 18 | import com.google.inject.AbstractModule; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository; 21 | 22 | public class CarModule extends AbstractModule { 23 | 24 | @Override 25 | protected void configure() { 26 | bind(CarRepository.class).to(InMemoryCarRepository.class); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/car/CarRepresentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.hateoas.Representation; 20 | 21 | public class CarRepresentation extends Representation { 22 | 23 | private int id; 24 | private String name; 25 | 26 | public CarRepresentation(Car car) { 27 | this.id = car.getId(); 28 | this.name = car.getName(); 29 | } 30 | 31 | public int getId() { 32 | return id; 33 | } 34 | 35 | public void setId(int id) { 36 | this.id = id; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/car/CarResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.car; 17 | 18 | import io.dropwizard.auth.Auth; 19 | import io.github.cdelmas.spike.common.auth.User; 20 | import io.github.cdelmas.spike.common.domain.Car; 21 | import io.github.cdelmas.spike.common.domain.CarRepository; 22 | import io.github.cdelmas.spike.common.hateoas.Link; 23 | 24 | import javax.inject.Inject; 25 | import javax.ws.rs.GET; 26 | import javax.ws.rs.Path; 27 | import javax.ws.rs.PathParam; 28 | import javax.ws.rs.Produces; 29 | import javax.ws.rs.core.Context; 30 | import javax.ws.rs.core.MediaType; 31 | import javax.ws.rs.core.Response; 32 | import javax.ws.rs.core.UriInfo; 33 | import java.util.Optional; 34 | 35 | @Path("/cars/{id}") 36 | @Produces(MediaType.APPLICATION_JSON) 37 | public class CarResource { 38 | 39 | @Context 40 | UriInfo uriInfo; 41 | 42 | @Inject 43 | private CarRepository carRepository; 44 | 45 | @GET 46 | public Response byId(@Auth User user, @PathParam("id") int carId) { 47 | Optional car = carRepository.byId(carId); 48 | return car.map(c -> { 49 | CarRepresentation carRepresentation = new CarRepresentation(c); 50 | carRepresentation.addLink(Link.self(uriInfo.getAbsolutePathBuilder().build(c.getId()).toString())); 51 | return Response.ok(carRepresentation).build(); 52 | }).orElse(Response.status(Response.Status.NOT_FOUND).build()); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/car/CarsResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.car; 17 | 18 | import io.dropwizard.auth.Auth; 19 | import io.github.cdelmas.spike.common.auth.User; 20 | import io.github.cdelmas.spike.common.domain.Car; 21 | import io.github.cdelmas.spike.common.domain.CarRepository; 22 | 23 | import javax.inject.Inject; 24 | import javax.ws.rs.Consumes; 25 | import javax.ws.rs.GET; 26 | import javax.ws.rs.POST; 27 | import javax.ws.rs.Path; 28 | import javax.ws.rs.Produces; 29 | import javax.ws.rs.core.Context; 30 | import javax.ws.rs.core.MediaType; 31 | import javax.ws.rs.core.Response; 32 | import javax.ws.rs.core.UriBuilder; 33 | import javax.ws.rs.core.UriInfo; 34 | import java.util.List; 35 | 36 | import static java.util.stream.Collectors.toList; 37 | 38 | @Path("/cars") 39 | public class CarsResource { 40 | 41 | @Context 42 | UriInfo uriInfo; 43 | 44 | @Inject 45 | private CarRepository carRepository; 46 | 47 | @GET 48 | @Produces(MediaType.APPLICATION_JSON) 49 | public Response all(@Auth User user) { 50 | List cars = carRepository.all(); 51 | List representations = cars.stream() 52 | .map(c -> { 53 | CarRepresentation representation = new CarRepresentation(c); 54 | representation.addLink(io.github.cdelmas.spike.common.hateoas.Link.self(uriInfo.getAbsolutePathBuilder().build(c.getId()).toString())); 55 | return representation; 56 | }).collect(toList()); 57 | return Response.ok(representations) 58 | .header("total-count", String.valueOf(cars.size())) 59 | .build(); 60 | } 61 | 62 | @POST 63 | @Consumes(MediaType.APPLICATION_JSON) 64 | public Response createCar(@Auth User user, Car car) { 65 | carRepository.save(car); 66 | return Response.created(UriBuilder.fromResource(CarsResource.class).path("/{id}").build(String.valueOf(car.getId()))) 67 | .build(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/hello/CarService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | 20 | import java.util.List; 21 | 22 | public interface CarService { 23 | 24 | List getAllCars(String auth); 25 | } 26 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/hello/HelloModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.hello; 17 | 18 | import com.google.inject.AbstractModule; 19 | import com.google.inject.Provides; 20 | import io.dropwizard.client.JerseyClientBuilder; 21 | import io.dropwizard.setup.Environment; 22 | import io.github.cdelmas.spike.dropwizard.infrastructure.DropwizardServerConfiguration; 23 | 24 | import javax.inject.Named; 25 | import javax.inject.Singleton; 26 | import javax.ws.rs.client.Client; 27 | 28 | public class HelloModule extends AbstractModule { 29 | 30 | private Client client; 31 | 32 | @Override 33 | protected void configure() { 34 | bind(CarService.class).to(RemoteCarService.class); 35 | } 36 | 37 | @Provides 38 | @Named("template") 39 | public String provideTemplate(DropwizardServerConfiguration configuration) { 40 | return configuration.getTemplate(); 41 | } 42 | 43 | @Provides 44 | @Named("defaultName") 45 | public String provideDefaultName(DropwizardServerConfiguration configuration) { 46 | return configuration.getDefaultName(); 47 | } 48 | 49 | @Provides 50 | public Client createHttpClient(Environment environment, DropwizardServerConfiguration configuration) { 51 | if (client == null) { 52 | client = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()).build("internal client"); 53 | } 54 | return client; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/hello/HelloWorldResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.hello; 17 | 18 | import com.codahale.metrics.annotation.Timed; 19 | import io.dropwizard.auth.Auth; 20 | import io.github.cdelmas.spike.common.auth.User; 21 | import io.github.cdelmas.spike.common.domain.Car; 22 | 23 | import javax.inject.Inject; 24 | import javax.inject.Named; 25 | import javax.ws.rs.GET; 26 | import javax.ws.rs.Path; 27 | import javax.ws.rs.Produces; 28 | import javax.ws.rs.QueryParam; 29 | import javax.ws.rs.core.MediaType; 30 | import java.util.List; 31 | import java.util.Optional; 32 | import java.util.concurrent.atomic.AtomicLong; 33 | 34 | import static java.util.stream.Collectors.toList; 35 | 36 | @Path("/hello-world") 37 | @Produces(MediaType.APPLICATION_JSON) 38 | public class HelloWorldResource { 39 | private final String template; 40 | private final String defaultName; 41 | private final AtomicLong counter; 42 | 43 | @Inject 44 | private CarService carService; 45 | 46 | @Inject 47 | public HelloWorldResource(@Named("template") String template, @Named("defaultName") String defaultName) { 48 | this.template = template; 49 | this.defaultName = defaultName; 50 | this.counter = new AtomicLong(); 51 | } 52 | 53 | @GET 54 | @Timed 55 | public Saying sayHello(@Auth User user, @QueryParam("name") Optional name) { 56 | final String value = String.format(template, name.orElse(defaultName)); 57 | List allCars = carService.getAllCars(user.getToken()); 58 | return new Saying(counter.incrementAndGet(), value + " " + '\n' + 59 | allCars.stream().map(Car::getName).collect(toList()).toString()); 60 | } 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/hello/RemoteCarService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | 20 | import javax.inject.Inject; 21 | import javax.servlet.http.HttpServletRequest; 22 | import javax.ws.rs.HttpMethod; 23 | import javax.ws.rs.client.Client; 24 | import javax.ws.rs.client.Invocation; 25 | import javax.ws.rs.client.WebTarget; 26 | import javax.ws.rs.core.Context; 27 | import javax.ws.rs.core.MediaType; 28 | import javax.ws.rs.core.Request; 29 | import java.util.List; 30 | 31 | import static java.util.Arrays.asList; 32 | 33 | public class RemoteCarService implements CarService { 34 | 35 | @Inject 36 | private Client client; 37 | 38 | @Context 39 | private Request request; 40 | 41 | @Override 42 | public List getAllCars(String auth) { 43 | WebTarget target = client.target("https://localhost:8443/app/cars"); 44 | Invocation invocation = target.request(MediaType.APPLICATION_JSON) 45 | .header("Authorization", "Bearer " + auth) 46 | .build(HttpMethod.GET); 47 | Car[] cars = invocation.invoke(Car[].class); 48 | return asList(cars); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/hello/Saying.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.hello; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import org.hibernate.validator.constraints.Length; 20 | 21 | public class Saying { 22 | private long id; 23 | 24 | @Length(max = 3) 25 | private String content; 26 | 27 | public Saying() { 28 | // Jackson deserialization 29 | } 30 | 31 | public Saying(long id, String content) { 32 | this.id = id; 33 | this.content = content; 34 | } 35 | 36 | @JsonProperty 37 | public long getId() { 38 | return id; 39 | } 40 | 41 | @JsonProperty 42 | public String getContent() { 43 | return content; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/infrastructure/DropwizardApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.infrastructure; 17 | 18 | import com.fasterxml.jackson.databind.DeserializationFeature; 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import com.hubspot.dropwizard.guice.GuiceBundle; 21 | import io.dropwizard.Application; 22 | import io.dropwizard.auth.AuthFactory; 23 | import io.dropwizard.auth.oauth.OAuthFactory; 24 | import io.dropwizard.java8.Java8Bundle; 25 | import io.dropwizard.setup.Bootstrap; 26 | import io.dropwizard.setup.Environment; 27 | import io.github.cdelmas.spike.common.auth.User; 28 | import io.github.cdelmas.spike.dropwizard.car.CarModule; 29 | import io.github.cdelmas.spike.dropwizard.car.CarResource; 30 | import io.github.cdelmas.spike.dropwizard.car.CarsResource; 31 | import io.github.cdelmas.spike.dropwizard.hello.HelloModule; 32 | import io.github.cdelmas.spike.dropwizard.hello.HelloWorldResource; 33 | import io.github.cdelmas.spike.dropwizard.infrastructure.auth.FacebookTokenAuthenticator; 34 | 35 | public class DropwizardApplication extends Application { 36 | 37 | private GuiceBundle guiceBundle; 38 | 39 | @Override 40 | public void run(DropwizardServerConfiguration configuration, Environment environment) throws Exception { 41 | environment.healthChecks().register("template", guiceBundle.getInjector().getInstance(TemplateHealthCheck.class)); 42 | environment.jersey().register(guiceBundle.getInjector().getInstance(HelloWorldResource.class)); 43 | environment.jersey().register(guiceBundle.getInjector().getInstance(CarsResource.class)); 44 | environment.jersey().register(guiceBundle.getInjector().getInstance(CarResource.class)); 45 | environment.jersey().register(AuthFactory.binder( 46 | new OAuthFactory<>(guiceBundle.getInjector().getInstance(FacebookTokenAuthenticator.class), 47 | getName() + "-Realm", 48 | User.class))); 49 | } 50 | 51 | @Override 52 | public String getName() { 53 | return "Dropwizard Spike Server"; 54 | } 55 | 56 | @Override 57 | public void initialize(Bootstrap bootstrap) { 58 | guiceBundle = GuiceBundle.newBuilder() 59 | .addModule(new HelloModule()) 60 | .addModule(new CarModule()) 61 | .setConfigClass(DropwizardServerConfiguration.class) 62 | .build(); 63 | bootstrap.addBundle(guiceBundle); 64 | bootstrap.addBundle(new Java8Bundle()); 65 | 66 | ObjectMapper objectMapper = bootstrap.getObjectMapper(); 67 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/infrastructure/DropwizardServerConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.infrastructure; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import io.dropwizard.Configuration; 20 | import io.dropwizard.client.JerseyClientConfiguration; 21 | import org.hibernate.validator.constraints.NotEmpty; 22 | 23 | import javax.validation.Valid; 24 | import javax.validation.constraints.NotNull; 25 | 26 | public class DropwizardServerConfiguration extends Configuration { 27 | @NotEmpty 28 | private String template; 29 | 30 | @NotEmpty 31 | private String defaultName = "Stranger"; 32 | 33 | @Valid 34 | @NotNull 35 | private JerseyClientConfiguration httpClient = new JerseyClientConfiguration(); 36 | 37 | @JsonProperty("httpClient") 38 | public JerseyClientConfiguration getJerseyClientConfiguration() { 39 | return httpClient; 40 | } 41 | 42 | @JsonProperty 43 | public String getTemplate() { 44 | return template; 45 | } 46 | 47 | @JsonProperty 48 | public void setTemplate(String template) { 49 | this.template = template; 50 | } 51 | 52 | @JsonProperty 53 | public String getDefaultName() { 54 | return defaultName; 55 | } 56 | 57 | @JsonProperty 58 | public void setDefaultName(String name) { 59 | this.defaultName = name; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/infrastructure/TemplateHealthCheck.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.infrastructure; 17 | 18 | import com.codahale.metrics.health.HealthCheck; 19 | 20 | import javax.inject.Inject; 21 | import javax.inject.Named; 22 | 23 | public class TemplateHealthCheck extends HealthCheck { 24 | private final String template; 25 | 26 | @Inject 27 | public TemplateHealthCheck(@Named("template") String template) { 28 | this.template = template; 29 | } 30 | 31 | @Override 32 | protected Result check() throws Exception { 33 | final String saying = String.format(template, "TEST"); 34 | if (!saying.contains("TEST")) { 35 | return Result.unhealthy("template doesn't include a name"); 36 | } 37 | return Result.healthy(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /dropwizard/src/main/java/io/github/cdelmas/spike/dropwizard/infrastructure/auth/FacebookTokenAuthenticator.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.dropwizard.infrastructure.auth; 17 | 18 | import com.google.common.base.Optional; 19 | import io.dropwizard.auth.AuthenticationException; 20 | import io.dropwizard.auth.Authenticator; 21 | import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory; 22 | import io.github.cdelmas.spike.common.auth.User; 23 | import javaslang.control.Try; 24 | 25 | import javax.inject.Inject; 26 | 27 | public class FacebookTokenAuthenticator implements Authenticator { 28 | 29 | @Inject 30 | AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory; 31 | 32 | @Override 33 | public Optional authenticate(String token) throws AuthenticationException { 34 | Try user = accessTokenVerificationCommandFactory.createVerificationCommand(token).executeCommand(); 35 | return user.toJavaOptional() 36 | .map(Optional::of) 37 | .orElse(Optional.absent()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /dropwizard/src/main/resources/dropwizard-server.yml: -------------------------------------------------------------------------------- 1 | template: Hello, %s! 2 | defaultName: Stranger 3 | server: 4 | applicationContextPath: /app 5 | applicationConnectors: 6 | - type: https 7 | port: 8443 8 | keyStorePath: /home/cyril/Work/localhost.jks 9 | keyStorePassword: louise 10 | validateCerts: false -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cdelmas/microservices-comparison/41b9adc0c3cb2fc066445e9c85ee3d14663c39c7/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Aug 14 17:01:14 CEST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /restlet-server/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '1.2.2' 3 | } 4 | 5 | repositories { 6 | maven { 7 | url 'http://maven.restlet.org' 8 | } 9 | } 10 | 11 | dependencies { 12 | compile 'org.restlet.jse:org.restlet:2.3.4' 13 | compile 'org.restlet.jse:org.restlet.ext.guice:2.3.4' 14 | compile 'com.google.inject:guice:4.0' 15 | compile 'javax.inject:javax.inject:1' 16 | compile 'org.restlet.jse:org.restlet.ext.jackson:2.3.4' 17 | compile 'org.restlet.jse:org.restlet.ext.oauth:2.3.4' 18 | compile 'com.fasterxml.jackson.core:jackson-databind:2.4.5' 19 | compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.4.5' 20 | } 21 | 22 | jar { 23 | manifest { 24 | attributes 'Main-Class': 'io.github.cdelmas.spike.restlet.Main' 25 | } 26 | } -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet; 17 | 18 | import com.google.inject.Guice; 19 | import com.google.inject.Injector; 20 | import io.github.cdelmas.spike.restlet.car.CarModule; 21 | import io.github.cdelmas.spike.restlet.hello.HelloModule; 22 | import io.github.cdelmas.spike.restlet.infrastructure.di.RestletInfraModule; 23 | import org.restlet.ext.guice.SelfInjectingServerResourceModule; 24 | 25 | public class Main { 26 | 27 | public static void main(String[] args) throws Exception { 28 | Injector injector = Guice.createInjector(new SelfInjectingServerResourceModule(), 29 | new RestletInfraModule(), 30 | new CarModule(), 31 | new HelloModule()); 32 | RestComponent component = injector.getInstance(RestComponent.class); 33 | component.start(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/RestComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet; 17 | 18 | import io.github.cdelmas.spike.restlet.car.Car; 19 | import io.github.cdelmas.spike.restlet.hello.Hello; 20 | import io.github.cdelmas.spike.restlet.infrastructure.JacksonCustomConverter; 21 | import org.restlet.Application; 22 | import org.restlet.Component; 23 | import org.restlet.Restlet; 24 | import org.restlet.Server; 25 | import org.restlet.data.ChallengeScheme; 26 | import org.restlet.data.Parameter; 27 | import org.restlet.data.Protocol; 28 | import org.restlet.engine.Engine; 29 | import org.restlet.engine.converter.ConverterHelper; 30 | import org.restlet.ext.jackson.JacksonConverter; 31 | import org.restlet.security.ChallengeAuthenticator; 32 | import org.restlet.security.Verifier; 33 | import org.restlet.util.Series; 34 | 35 | import javax.inject.Inject; 36 | import java.util.List; 37 | 38 | public class RestComponent extends Component { 39 | 40 | @Inject 41 | public RestComponent(@Hello Application helloApp, @Car Application carApp, Verifier authTokenVerifier) { 42 | getClients().add(Protocol.HTTPS); 43 | Server secureServer = getServers().add(Protocol.HTTPS, 8043); 44 | Series parameters = secureServer.getContext().getParameters(); 45 | parameters.add("sslContextFactory", "org.restlet.engine.ssl.DefaultSslContextFactory"); 46 | parameters.add("keyStorePath", System.getProperty("javax.net.ssl.keyStorePath")); 47 | getDefaultHost().attach("/api/hello", secure(helloApp, authTokenVerifier, "ame")); 48 | getDefaultHost().attach("/api/cars", secure(carApp, authTokenVerifier, "ame")); 49 | replaceConverter(JacksonConverter.class, new JacksonCustomConverter()); 50 | } 51 | 52 | private Restlet secure(Application app, Verifier verifier, String realm) { 53 | ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext().createChildContext(), 54 | ChallengeScheme.HTTP_OAUTH_BEARER, realm); 55 | guard.setVerifier(verifier); 56 | guard.setNext(app); 57 | return guard; 58 | } 59 | 60 | private static void replaceConverter( 61 | Class converterClass, 62 | ConverterHelper newConverter) { 63 | 64 | // TODO java 8-ify 65 | List converters = Engine.getInstance().getRegisteredConverters(); 66 | for (ConverterHelper converter : converters) { 67 | if (converter.getClass().equals(converterClass)) { 68 | converters.remove(converter); 69 | break; 70 | } 71 | } 72 | 73 | converters.add(newConverter); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/Car.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import javax.inject.Qualifier; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | 22 | @Retention(RetentionPolicy.RUNTIME) 23 | @Qualifier 24 | public @interface Car { 25 | } 26 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/CarApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import org.restlet.Restlet; 19 | import org.restlet.ext.guice.ResourceInjectingApplication; 20 | import org.restlet.routing.Router; 21 | 22 | public class CarApplication extends ResourceInjectingApplication { 23 | 24 | @Override 25 | public Restlet createInboundRoot() { 26 | Router router = newRouter(); 27 | router.attach("/cars", CarsResource.class); 28 | router.attach("/cars/{id}", CarResource.class); 29 | return router; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/CarModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import com.google.inject.AbstractModule; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository; 21 | import org.restlet.Application; 22 | 23 | public class CarModule extends AbstractModule { 24 | 25 | @Override 26 | protected void configure() { 27 | bind(Application.class).annotatedWith(Car.class).to(CarApplication.class); 28 | bind(CarRepository.class).to(InMemoryCarRepository.class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/CarRepresentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import io.github.cdelmas.spike.common.hateoas.Representation; 19 | 20 | public class CarRepresentation extends Representation { 21 | 22 | private int id; 23 | private String name; 24 | 25 | CarRepresentation() { 26 | 27 | } 28 | 29 | public CarRepresentation(io.github.cdelmas.spike.common.domain.Car car) { 30 | this.id = car.getId(); 31 | this.name = car.getName(); 32 | } 33 | 34 | public int getId() { 35 | return id; 36 | } 37 | 38 | public void setId(int id) { 39 | this.id = id; 40 | } 41 | 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | public void setName(String name) { 47 | this.name = name; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/CarResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.CarRepository; 19 | import io.github.cdelmas.spike.common.hateoas.Link; 20 | import org.restlet.data.Status; 21 | import org.restlet.resource.Get; 22 | import org.restlet.resource.ResourceException; 23 | import org.restlet.resource.ServerResource; 24 | 25 | import javax.inject.Inject; 26 | 27 | public class CarResource extends ServerResource { 28 | 29 | @Inject 30 | private CarRepository carRepository; 31 | private int carId; 32 | 33 | @Override 34 | protected void doInit() throws ResourceException { 35 | super.doInit(); 36 | this.carId = Integer.parseInt(getAttribute("id")); 37 | } 38 | 39 | @Get("json") 40 | public CarRepresentation byId() { 41 | io.github.cdelmas.spike.common.domain.Car car = carRepository.byId(carId).orElseThrow(() -> new ResourceException(Status.CLIENT_ERROR_NOT_FOUND)); 42 | CarRepresentation carRepresentation = new CarRepresentation(car); 43 | carRepresentation.addLink(Link.self(getReference().toString())); 44 | carRepresentation.addLink(Link.remove(getReference().toString())); 45 | return carRepresentation; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/car/CarsResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.CarRepository; 19 | import io.github.cdelmas.spike.common.hateoas.Link; 20 | import org.restlet.data.Reference; 21 | import org.restlet.data.Status; 22 | import org.restlet.resource.Get; 23 | import org.restlet.resource.Post; 24 | import org.restlet.resource.ServerResource; 25 | 26 | import javax.inject.Inject; 27 | import java.util.List; 28 | 29 | import static java.util.stream.Collectors.toList; 30 | 31 | public class CarsResource extends ServerResource { 32 | 33 | @Inject 34 | private CarRepository carRepository; 35 | 36 | @Get("json") 37 | public List all() { 38 | List cars = carRepository.all(); 39 | getResponse().getHeaders().add("total-count", String.valueOf(cars.size())); 40 | return cars.stream().map(c -> { 41 | CarRepresentation carRepresentation = new CarRepresentation(c); 42 | carRepresentation.addLink(Link.self(new Reference(getReference()).addSegment(String.valueOf(c.getId())).toString())); 43 | return carRepresentation; 44 | }).collect(toList()); 45 | } 46 | 47 | @Post("json") 48 | public void createCar(io.github.cdelmas.spike.common.domain.Car car) { 49 | carRepository.save(car); 50 | setLocationRef(getReference().addSegment(String.valueOf(car.getId()))); 51 | setStatus(Status.SUCCESS_CREATED); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/CarService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | 20 | import java.util.List; 21 | 22 | public interface CarService { 23 | 24 | List list(); 25 | } 26 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/CarServiceInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import org.restlet.resource.Get; 20 | 21 | public interface CarServiceInterface { 22 | 23 | @Get 24 | public Car[] getAllCars(); 25 | } 26 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/Hello.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import javax.inject.Qualifier; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | 22 | @Retention(RetentionPolicy.RUNTIME) 23 | @Qualifier 24 | public @interface Hello { 25 | } 26 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/HelloApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import org.restlet.Restlet; 19 | import org.restlet.ext.guice.ResourceInjectingApplication; 20 | import org.restlet.routing.Router; 21 | 22 | public class HelloApplication extends ResourceInjectingApplication { 23 | 24 | @Override 25 | public Restlet createInboundRoot() { 26 | Router router = newRouter(); 27 | router.attach("/hello", HelloResource.class); 28 | return router; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/HelloModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import com.google.inject.AbstractModule; 19 | import org.restlet.Application; 20 | 21 | public class HelloModule extends AbstractModule { 22 | 23 | @Override 24 | protected void configure() { 25 | bind(Application.class).annotatedWith(Hello.class).to(HelloApplication.class); 26 | bind(CarService.class).to(RemoteCarService.class); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/HelloResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import org.restlet.resource.Get; 20 | import org.restlet.resource.ServerResource; 21 | 22 | import javax.inject.Inject; 23 | import java.util.List; 24 | 25 | import static java.util.stream.Collectors.toList; 26 | 27 | public class HelloResource extends ServerResource { 28 | 29 | @Inject 30 | private CarService carService; 31 | 32 | @Get("txt") 33 | public String hello() { 34 | List cars = carService.list(); 35 | List carNames = cars.stream().map(Car::getName).collect(toList()); 36 | 37 | return "Resource URI : " + getReference() + '\n' + "Root URI : " 38 | + getRootRef() + '\n' + "Routed part : " 39 | + getReference().getBaseRef() + '\n' + "Remaining part: " 40 | + getReference().getRemainingPart() + '\n' 41 | + carNames.toString(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/hello/RemoteCarService.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import org.restlet.Client; 20 | import org.restlet.Context; 21 | import org.restlet.Request; 22 | import org.restlet.data.ChallengeResponse; 23 | import org.restlet.data.ChallengeScheme; 24 | import org.restlet.data.Parameter; 25 | import org.restlet.data.Protocol; 26 | import org.restlet.resource.ClientResource; 27 | import org.restlet.util.Series; 28 | 29 | import java.util.List; 30 | 31 | import static java.util.Arrays.asList; 32 | 33 | public class RemoteCarService implements CarService { 34 | 35 | @Override 36 | public List list() { 37 | Client client = new Client(new Context(), Protocol.HTTPS); 38 | Series parameters = client.getContext().getParameters(); 39 | parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore")); 40 | 41 | ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars"); 42 | clientResource.setNext(client); 43 | ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER); 44 | challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString()); 45 | clientResource.setChallengeResponse(challenge); 46 | CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class); 47 | Car[] allCars = carServiceInterface.getAllCars(); 48 | try { 49 | client.stop(); 50 | } catch (Exception e) { 51 | throw new RuntimeException(e); 52 | } 53 | return asList(allCars); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/infrastructure/JacksonCustomConverter.java: -------------------------------------------------------------------------------- 1 | 2 | package io.github.cdelmas.spike.restlet.infrastructure; 3 | /* 4 | Copyright 2015 Cyril Delmas 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | */ 18 | import com.fasterxml.jackson.core.JsonFactory; 19 | import com.fasterxml.jackson.core.JsonGenerator; 20 | import com.fasterxml.jackson.databind.DeserializationFeature; 21 | import com.fasterxml.jackson.databind.ObjectMapper; 22 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 23 | import org.restlet.data.MediaType; 24 | import org.restlet.ext.jackson.JacksonConverter; 25 | import org.restlet.ext.jackson.JacksonRepresentation; 26 | import org.restlet.representation.Representation; 27 | 28 | public class JacksonCustomConverter extends JacksonConverter { 29 | 30 | @Override 31 | protected JacksonRepresentation create(MediaType mediaType, T source) { 32 | ObjectMapper mapper = createMapper(); 33 | JacksonRepresentation jr = new JacksonRepresentation<>(mediaType, source); 34 | jr.setObjectMapper(mapper); 35 | return jr; 36 | } 37 | 38 | @Override 39 | protected JacksonRepresentation create(Representation source, Class objectClass) { 40 | ObjectMapper mapper = createMapper(); 41 | JacksonRepresentation jr = new JacksonRepresentation<>(source, objectClass); 42 | jr.setObjectMapper(mapper); 43 | return jr; 44 | } 45 | 46 | 47 | private ObjectMapper createMapper() { 48 | JsonFactory jsonFactory = new JsonFactory(); 49 | jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); 50 | ObjectMapper mapper = new ObjectMapper(jsonFactory); 51 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 52 | mapper.registerModule(new Jdk8Module()); 53 | return mapper; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/infrastructure/auth/AuthTokenVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.infrastructure.auth; 17 | 18 | import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory; 19 | import io.github.cdelmas.spike.common.auth.User; 20 | import javaslang.control.Try; 21 | import org.restlet.Context; 22 | import org.restlet.Request; 23 | import org.restlet.Response; 24 | import org.restlet.data.ChallengeResponse; 25 | import org.restlet.data.ChallengeScheme; 26 | import org.restlet.security.Verifier; 27 | 28 | import javax.inject.Inject; 29 | 30 | public class AuthTokenVerifier implements Verifier { 31 | 32 | @Inject 33 | private AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory; 34 | 35 | @Override 36 | public int verify(Request request, Response response) { 37 | final String token; 38 | 39 | try { 40 | ChallengeResponse cr = request.getChallengeResponse(); 41 | if (cr == null) { 42 | return RESULT_MISSING; 43 | } else if (ChallengeScheme.HTTP_OAUTH_BEARER.equals(cr.getScheme())) { 44 | final String bearer = cr.getRawValue(); 45 | if (bearer == null || bearer.isEmpty()) { 46 | return RESULT_MISSING; 47 | } 48 | token = bearer; 49 | } else { 50 | return RESULT_UNSUPPORTED; 51 | } 52 | } catch (Exception ex) { 53 | return RESULT_INVALID; 54 | } 55 | 56 | Try user = accessTokenVerificationCommandFactory.createVerificationCommand(token).executeCommand(); 57 | return user.map(u -> { 58 | org.restlet.security.User restletUser = createRestletUser(u); 59 | request.getClientInfo().setUser(restletUser); 60 | request.getAttributes().put("token", token); 61 | return RESULT_VALID; 62 | }).orElse(RESULT_INVALID); 63 | } 64 | 65 | private org.restlet.security.User createRestletUser(User user) { 66 | org.restlet.security.User restletUser = new org.restlet.security.User(); 67 | restletUser.setIdentifier(user.getId()); 68 | restletUser.setLastName(user.getName()); 69 | return restletUser; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /restlet-server/src/main/java/io/github/cdelmas/spike/restlet/infrastructure/di/RestletInfraModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.restlet.infrastructure.di; 17 | 18 | import com.google.inject.AbstractModule; 19 | import io.github.cdelmas.spike.restlet.infrastructure.auth.AuthTokenVerifier; 20 | import org.restlet.security.Verifier; 21 | 22 | public class RestletInfraModule extends AbstractModule { 23 | @Override 24 | protected void configure() { 25 | bind(Verifier.class).to(AuthTokenVerifier.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spike-server' 2 | include 'restlet-server' 3 | include 'common' 4 | include 'dropwizard' 5 | include 'vertx' 6 | include 'sparkjava' 7 | include 'spring-boot' 8 | 9 | -------------------------------------------------------------------------------- /sparkjava/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '1.2.2' 3 | } 4 | 5 | dependencies { 6 | compile 'com.sparkjava:spark-core:2.2' 7 | compile 'com.fasterxml.jackson.core:jackson-databind:2.6.2' 8 | compile 'com.mashape.unirest:unirest-java:1.4.7' 9 | } 10 | 11 | jar { 12 | manifest { 13 | attributes 'Main-Class': 'io.github.cdelmas.spike.sparkjava.Main' 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/AuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package io.github.cdelmas.spike.sparkjava; 2 | 3 | import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory; 4 | import io.github.cdelmas.spike.common.auth.User; 5 | import javaslang.control.Try; 6 | import spark.Request; 7 | import spark.Response; 8 | 9 | import static spark.Spark.halt; 10 | 11 | public class AuthenticationFilter { 12 | 13 | private final AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory; 14 | 15 | public AuthenticationFilter(AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory) { 16 | this.accessTokenVerificationCommandFactory = accessTokenVerificationCommandFactory; 17 | } 18 | 19 | public void filter(Request request, Response response) { 20 | String token = readToken(request); 21 | Try user = accessTokenVerificationCommandFactory.createVerificationCommand(token).executeCommand(); 22 | user.peek(u -> 23 | request.attribute("user", u)) 24 | .orElseRun(e -> halt(401, "{\"error\":\"" + e.getMessage() + "\"}")); 25 | } 26 | 27 | private String readToken(Request request) { 28 | String authorization = request.headers("Authorization"); 29 | return authorization.substring(authorization.indexOf(' ') + 1); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/CarRepresentation.java: -------------------------------------------------------------------------------- 1 | package io.github.cdelmas.spike.sparkjava; 2 | 3 | import io.github.cdelmas.spike.common.domain.Car; 4 | import io.github.cdelmas.spike.common.hateoas.Representation; 5 | 6 | public class CarRepresentation extends Representation { 7 | 8 | private final String name; 9 | 10 | public CarRepresentation(Car c) { 11 | this.name = c.getName(); 12 | } 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/CarsResource.java: -------------------------------------------------------------------------------- 1 | package io.github.cdelmas.spike.sparkjava; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import io.github.cdelmas.spike.common.domain.Car; 5 | import io.github.cdelmas.spike.common.domain.CarRepository; 6 | import spark.Request; 7 | import spark.Response; 8 | import spark.Route; 9 | 10 | import java.io.IOException; 11 | import java.util.List; 12 | import java.util.Optional; 13 | 14 | import static io.github.cdelmas.spike.common.hateoas.Link.self; 15 | import static java.util.stream.Collectors.toList; 16 | 17 | public class CarsResource { 18 | 19 | private final CarRepository carRepository; 20 | private final ObjectMapper objectMapper; 21 | 22 | public CarsResource(CarRepository carRepository, ObjectMapper objectMapper) { 23 | this.carRepository = carRepository; 24 | this.objectMapper = objectMapper; 25 | } 26 | 27 | public List all(Request request, Response response) { 28 | List cars = carRepository.all(); 29 | response.header("count", String.valueOf(cars.size())); 30 | response.type("application/json"); 31 | return cars.stream().map(c -> { 32 | CarRepresentation carRepresentation = new CarRepresentation(c); 33 | carRepresentation.addLink(self(request.url() + "/" + c.getId())); 34 | return carRepresentation; 35 | } 36 | ).collect(toList()); 37 | } 38 | 39 | public CarRepresentation byId(Request request, Response response) { 40 | Optional car = carRepository.byId(Integer.parseInt(request.params(":id"))); 41 | return car.map(c -> { 42 | response.type("application/json"); 43 | CarRepresentation carRepresentation = new CarRepresentation(c); 44 | carRepresentation.addLink(self(request.url())); 45 | return carRepresentation; 46 | } 47 | ).orElseGet(() -> { 48 | response.status(404); 49 | return null; 50 | }); 51 | } 52 | 53 | public String createCar(Request request, Response response) { 54 | Car car; 55 | try { 56 | car = objectMapper.readValue(request.body(), Car.class); 57 | carRepository.save(car); 58 | response.header("Location", request.url() + "/" + car.getId()); 59 | response.status(201); 60 | } catch (IOException e) { 61 | response.status(400); 62 | } 63 | return ""; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/HelloResource.java: -------------------------------------------------------------------------------- 1 | package io.github.cdelmas.spike.sparkjava; 2 | 3 | import com.mashape.unirest.http.HttpResponse; 4 | import com.mashape.unirest.http.Unirest; 5 | import com.mashape.unirest.http.exceptions.UnirestException; 6 | import io.github.cdelmas.spike.common.domain.Car; 7 | import spark.Request; 8 | import spark.Response; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | import static java.util.stream.Collectors.toList; 14 | 15 | public class HelloResource { 16 | 17 | public String hello (Request request, Response response) { 18 | String token = readToken(request); 19 | HttpResponse carHttpResponse; 20 | try { 21 | carHttpResponse = Unirest.get("https://localhost:8099/cars") 22 | .header("Accept", "application/json") 23 | .header("Authorization", "Bearer " + token) 24 | .asObject(Car[].class); 25 | } catch (UnirestException e) { 26 | throw new RuntimeException(e); 27 | } 28 | Car[] cars = carHttpResponse.getBody(); 29 | List carNames = Arrays.stream(cars) 30 | .map(Car::getName) 31 | .collect(toList()); 32 | return "We have these cars available: " + carNames; 33 | } 34 | 35 | private String readToken(Request request) { 36 | String authorization = request.headers("Authorization"); 37 | return authorization.substring(authorization.indexOf(' ') + 1); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.sparkjava; 17 | 18 | import com.fasterxml.jackson.core.JsonGenerator; 19 | import com.fasterxml.jackson.core.JsonProcessingException; 20 | import com.fasterxml.jackson.databind.DeserializationFeature; 21 | import com.fasterxml.jackson.databind.JsonSerializer; 22 | import com.fasterxml.jackson.databind.ObjectMapper; 23 | import com.fasterxml.jackson.databind.SerializerProvider; 24 | import com.mashape.unirest.http.HttpResponse; 25 | import com.mashape.unirest.http.Unirest; 26 | import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory; 27 | import io.github.cdelmas.spike.common.domain.Car; 28 | import io.github.cdelmas.spike.common.domain.CarRepository; 29 | import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository; 30 | import spark.Request; 31 | 32 | import java.io.IOException; 33 | import java.io.UncheckedIOException; 34 | import java.util.Arrays; 35 | import java.util.List; 36 | 37 | import static io.github.cdelmas.spike.common.hateoas.Link.self; 38 | import static java.util.stream.Collectors.toList; 39 | import static spark.Spark.before; 40 | import static spark.Spark.get; 41 | import static spark.Spark.halt; 42 | import static spark.Spark.post; 43 | import static spark.SparkBase.port; 44 | import static spark.SparkBase.secure; 45 | 46 | public class Main { 47 | public static void main(String[] args) { 48 | port(8099); // defaults on 4567 49 | ObjectMapper objectMapper = createObjectMapper(); 50 | configureUnirest(objectMapper); 51 | secureServer(); 52 | 53 | AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory = new AccessTokenVerificationCommandFactory(); 54 | AuthenticationFilter authenticationFilter = new AuthenticationFilter(accessTokenVerificationCommandFactory); 55 | before(authenticationFilter::filter); 56 | 57 | HelloResource helloResource = new HelloResource(); 58 | get("/hello", helloResource::hello); 59 | 60 | CarRepository carRepository = new InMemoryCarRepository(); 61 | CarsResource carsResource = new CarsResource(carRepository, objectMapper); 62 | get("/cars", "application/json", carsResource::all, objectMapper::writeValueAsString); 63 | get("/cars/:id", "application/json", carsResource::byId, objectMapper::writeValueAsString); 64 | post("/cars", "application/json", carsResource::createCar); 65 | } 66 | 67 | 68 | 69 | private static void secureServer() { 70 | String keystoreFile = System.getProperty("javax.net.ssl.keyStorePath"); 71 | String keystorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); 72 | String truststoreFile = System.getProperty("javax.net.ssl.trustStore"); 73 | String truststorePassword = System.getProperty("javax.net.ssl.trustStorePassword"); 74 | secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword); 75 | } 76 | 77 | private static void configureUnirest(final ObjectMapper objectMapper) { 78 | Unirest.setObjectMapper(new com.mashape.unirest.http.ObjectMapper() { 79 | @Override 80 | public T readValue(String value, Class valueType) { 81 | try { 82 | return objectMapper.readValue(value, valueType); 83 | } catch (IOException e) { 84 | throw new UncheckedIOException(e); 85 | } 86 | } 87 | 88 | @Override 89 | public String writeValue(Object value) { 90 | try { 91 | return objectMapper.writeValueAsString(value); 92 | } catch (JsonProcessingException e) { 93 | throw new RuntimeException(e); 94 | } 95 | } 96 | }); 97 | } 98 | 99 | private static ObjectMapper createObjectMapper() { 100 | ObjectMapper objectMapper = new ObjectMapper(); 101 | objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() { 102 | @Override 103 | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { 104 | } 105 | }); 106 | objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 107 | return objectMapper; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /spring-boot/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '1.2.2' 3 | } 4 | 5 | dependencies { 6 | 7 | compile ('org.springframework.boot:spring-boot-starter-web:1.2.6.RELEASE') { 8 | exclude module: "spring-boot-starter-tomcat" 9 | } 10 | compile 'org.springframework.boot:spring-boot-starter-jetty:1.2.6.RELEASE' 11 | compile 'org.springframework.boot:spring-boot-starter-actuator:1.2.6.RELEASE' 12 | compile 'org.springframework.hateoas:spring-hateoas:0.19.0.RELEASE' 13 | compile 'org.springframework.security.oauth:spring-security-oauth2:2.0.7.RELEASE' 14 | compile 'org.springframework.cloud:spring-cloud-security:1.0.2.RELEASE' 15 | } 16 | 17 | jar { 18 | manifest { 19 | attributes 'Main-Class': 'io.github.cdelmas.spike.springboot.Main' 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot/src/main/java/io/github/cdelmas/spike/springboot/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.springboot; 17 | 18 | import org.springframework.boot.SpringApplication; 19 | import org.springframework.boot.autoconfigure.SpringBootApplication; 20 | 21 | @SpringBootApplication 22 | public class Main { 23 | public static void main(String[] args) { 24 | 25 | SpringApplication.run(Main.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot/src/main/java/io/github/cdelmas/spike/springboot/car/CarRepositoryAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.springboot.car; 17 | 18 | import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository; 19 | import org.springframework.stereotype.Repository; 20 | 21 | @Repository 22 | public class CarRepositoryAdapter extends InMemoryCarRepository { 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot/src/main/java/io/github/cdelmas/spike/springboot/car/CarRepresentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.springboot.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import org.springframework.hateoas.ResourceSupport; 20 | 21 | public class CarRepresentation extends ResourceSupport { 22 | 23 | private final String name; 24 | 25 | public CarRepresentation(Car car) { 26 | this.name = car.getName(); 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot/src/main/java/io/github/cdelmas/spike/springboot/car/CarsController.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.springboot.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | import org.springframework.cloud.security.oauth2.resource.EnableOAuth2Resource; 21 | import org.springframework.hateoas.Link; 22 | import org.springframework.http.HttpHeaders; 23 | import org.springframework.http.HttpStatus; 24 | import org.springframework.http.ResponseEntity; 25 | import org.springframework.web.bind.annotation.PathVariable; 26 | import org.springframework.web.bind.annotation.RequestBody; 27 | import org.springframework.web.bind.annotation.RequestMapping; 28 | import org.springframework.web.bind.annotation.RequestMethod; 29 | import org.springframework.web.bind.annotation.ResponseBody; 30 | import org.springframework.web.bind.annotation.RestController; 31 | import org.springframework.web.context.request.WebRequest; 32 | 33 | import javax.inject.Inject; 34 | import javax.servlet.ServletRequest; 35 | import javax.servlet.http.HttpServletRequest; 36 | import java.util.List; 37 | import java.util.Optional; 38 | 39 | import static java.util.stream.Collectors.toList; 40 | import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; 41 | import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; 42 | 43 | @EnableOAuth2Resource 44 | @RestController 45 | @RequestMapping(value = "/cars", produces = "application/json") 46 | public class CarsController { 47 | 48 | private final CarRepository carRepository; 49 | 50 | @Inject 51 | public CarsController(CarRepository carRepository) { 52 | this.carRepository = carRepository; 53 | } 54 | 55 | @RequestMapping(method = RequestMethod.GET) 56 | public ResponseEntity> allCars() { 57 | List cars = carRepository.all(); 58 | HttpHeaders responseHeaders = new HttpHeaders(); 59 | responseHeaders.set("total-count", String.valueOf(cars.size())); 60 | return new ResponseEntity<>(cars.stream().map(this::toCarRepresentation).collect(toList()), responseHeaders, HttpStatus.OK); 61 | } 62 | 63 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) 64 | public ResponseEntity byId(@PathVariable("id") String carId) { 65 | Optional car = carRepository.byId(Integer.parseInt(carId)); 66 | return car.map(c -> { 67 | CarRepresentation rep = toCarRepresentation(c); 68 | return new ResponseEntity<>(rep, HttpStatus.OK); 69 | }) 70 | .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND)); 71 | } 72 | 73 | private CarRepresentation toCarRepresentation(Car c) { 74 | String carId = String.valueOf(c.getId()); 75 | CarRepresentation rep = new CarRepresentation(c); 76 | rep.add(linkTo(methodOn(CarsController.class).byId(carId)).withSelfRel()); 77 | return rep; 78 | } 79 | 80 | @RequestMapping(method = RequestMethod.POST, consumes = "application/json") 81 | public ResponseEntity create(@RequestBody Car car) { 82 | carRepository.save(car); 83 | HttpHeaders responseHeaders = new HttpHeaders(); 84 | Link link = linkTo(methodOn(CarsController.class).byId(String.valueOf(car.getId()))).withSelfRel(); 85 | responseHeaders.set("Location", link.getHref()); 86 | return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /spring-boot/src/main/java/io/github/cdelmas/spike/springboot/hello/SampleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.springboot.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import org.springframework.cloud.security.oauth2.resource.EnableOAuth2Resource; 20 | import org.springframework.http.HttpEntity; 21 | import org.springframework.http.HttpMethod; 22 | import org.springframework.http.ResponseEntity; 23 | import org.springframework.util.LinkedMultiValueMap; 24 | import org.springframework.util.MultiValueMap; 25 | import org.springframework.web.bind.annotation.RequestHeader; 26 | import org.springframework.web.bind.annotation.RequestMapping; 27 | import org.springframework.web.bind.annotation.ResponseBody; 28 | import org.springframework.web.bind.annotation.RestController; 29 | import org.springframework.web.client.RestTemplate; 30 | 31 | import java.util.List; 32 | 33 | import static java.util.Arrays.asList; 34 | import static java.util.stream.Collectors.toList; 35 | 36 | @EnableOAuth2Resource 37 | @RestController 38 | public class SampleController { 39 | 40 | @RequestMapping("/") 41 | @ResponseBody 42 | String home(@RequestHeader("Authorization") String authToken) { 43 | 44 | MultiValueMap headers = new LinkedMultiValueMap<>(); 45 | headers.add("Accept", "application/json"); 46 | headers.add("Authorization", authToken); 47 | HttpEntity> requestEntity = new HttpEntity<>(null, headers); 48 | 49 | RestTemplate rest = new RestTemplate(); 50 | ResponseEntity responseEntity = rest.exchange("https://localhost:8443/cars", HttpMethod.GET, requestEntity, Car[].class); 51 | List cars = asList(responseEntity.getBody()); 52 | 53 | return "Hello World! " + cars.stream().map(Car::getName).collect(toList()); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /spring-boot/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8443 3 | ssl: 4 | key-store: /home/cyril/Work/localhost.jks 5 | key-store-password: louise 6 | keyAlias: localhost 7 | 8 | spring: 9 | oauth2: 10 | resource: 11 | userInfoUri: https://graph.facebook.com/v2.4/me 12 | preferTokenInfo: false -------------------------------------------------------------------------------- /vertx/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '1.2.2' 3 | } 4 | 5 | dependencies { 6 | compile 'io.vertx:vertx-core:3.0.0' 7 | compile 'io.vertx:vertx-web:3.0.0' 8 | } 9 | 10 | jar { 11 | manifest { 12 | attributes 'Main-Class': 'io.github.cdelmas.spike.vertx.Main' 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx; 17 | 18 | import com.fasterxml.jackson.databind.DeserializationFeature; 19 | import io.github.cdelmas.spike.common.domain.Car; 20 | import io.github.cdelmas.spike.common.domain.CarRepository; 21 | import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository; 22 | import io.github.cdelmas.spike.vertx.car.CarResource; 23 | import io.github.cdelmas.spike.vertx.car.CarsResource; 24 | import io.github.cdelmas.spike.vertx.hello.HelloResource; 25 | import io.github.cdelmas.spike.vertx.infrastructure.auth.BearerAuthHandler; 26 | import io.github.cdelmas.spike.vertx.infrastructure.auth.FacebookOauthTokenVerifier; 27 | import io.vertx.core.Vertx; 28 | import io.vertx.core.http.HttpClient; 29 | import io.vertx.core.http.HttpClientOptions; 30 | import io.vertx.core.http.HttpServer; 31 | import io.vertx.core.http.HttpServerOptions; 32 | import io.vertx.core.json.Json; 33 | import io.vertx.core.net.JksOptions; 34 | import io.vertx.ext.web.Router; 35 | import io.vertx.ext.web.handler.AuthHandler; 36 | import io.vertx.ext.web.handler.BodyHandler; 37 | 38 | import static java.util.stream.Collectors.toList; 39 | 40 | public class Main { 41 | 42 | public static void main(String[] args) { 43 | // TODO start a vertx instance 44 | // deploy verticles / one per resource in this case 45 | 46 | Json.mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 47 | 48 | Vertx vertx = Vertx.vertx(); 49 | 50 | HttpClientOptions clientOptions = new HttpClientOptions() 51 | .setSsl(true) 52 | .setTrustStoreOptions(new JksOptions() 53 | .setPath(System.getProperty("javax.net.ssl.trustStore")) 54 | .setPassword(System.getProperty("javax.net.ssl.trustStorePassword"))); 55 | HttpClient httpClient = vertx.createHttpClient(clientOptions); 56 | 57 | Router router = Router.router(vertx); 58 | AuthHandler auth = new BearerAuthHandler(new FacebookOauthTokenVerifier(httpClient)); 59 | router.route("/*").handler(auth); 60 | 61 | HelloResource helloResource = new HelloResource(httpClient); 62 | router.get("/hello").produces("text/plain").handler(helloResource::hello); 63 | 64 | CarRepository carRepository = new InMemoryCarRepository(); 65 | CarsResource carsResource = new CarsResource(carRepository); 66 | router.route("/cars*").handler(BodyHandler.create()); 67 | router.get("/cars").produces("application/json").handler(carsResource::all); 68 | router.post("/cars").consumes("application/json").handler(carsResource::create); 69 | 70 | CarResource carResource = new CarResource(carRepository); 71 | router.get("/cars/:id").produces("application/json").handler(carResource::byId); 72 | 73 | HttpServerOptions serverOptions = new HttpServerOptions() 74 | .setSsl(true) 75 | .setKeyStoreOptions(new JksOptions() 76 | .setPath(System.getProperty("javax.net.ssl.keyStorePath")) 77 | .setPassword(System.getProperty("javax.net.ssl.keyStorePassword"))) 78 | .setPort(8090); 79 | HttpServer server = vertx.createHttpServer(serverOptions); 80 | server.requestHandler(router::accept).listen(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/car/CarRepresentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.hateoas.Representation; 20 | 21 | public class CarRepresentation extends Representation { 22 | private final String name; 23 | private final int id; 24 | 25 | public CarRepresentation(Car car) { 26 | this.name = car.getName(); 27 | this.id = car.getId(); 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public int getId() { 35 | return id; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/car/CarResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | import io.vertx.core.http.HttpServerResponse; 21 | import io.vertx.core.json.Json; 22 | import io.vertx.ext.web.RoutingContext; 23 | 24 | import java.util.Optional; 25 | 26 | import static io.github.cdelmas.spike.common.hateoas.Link.self; 27 | 28 | public class CarResource { 29 | 30 | private final CarRepository carRepository; 31 | 32 | public CarResource(CarRepository carRepository) { 33 | this.carRepository = carRepository; 34 | } 35 | 36 | public void byId(RoutingContext routingContext) { 37 | HttpServerResponse response = routingContext.response(); 38 | String idParam = routingContext.request().getParam("id"); 39 | if (idParam == null) { 40 | response.setStatusCode(400).end(); 41 | } else { 42 | Optional car = carRepository.byId(Integer.parseInt(idParam)); 43 | if (car.isPresent()) { 44 | CarRepresentation carRepresentation = new CarRepresentation(car.get()); 45 | carRepresentation.addLink(self(routingContext.request().absoluteURI())); 46 | response.putHeader("content-type", "application/json") 47 | .end(Json.encode(carRepresentation)); 48 | } else { 49 | response.setStatusCode(404).end(); 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/car/CarsResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.car; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.common.domain.CarRepository; 20 | import io.github.cdelmas.spike.common.hateoas.Link; 21 | import io.vertx.core.http.HttpServerResponse; 22 | import io.vertx.core.json.Json; 23 | import io.vertx.ext.web.RoutingContext; 24 | 25 | import java.util.List; 26 | 27 | import static io.github.cdelmas.spike.common.hateoas.Link.self; 28 | import static io.vertx.core.json.Json.encode; 29 | import static java.util.stream.Collectors.toList; 30 | 31 | public class CarsResource { 32 | 33 | private final CarRepository carRepository; 34 | 35 | 36 | public CarsResource(CarRepository carRepository) { 37 | this.carRepository = carRepository; 38 | } 39 | 40 | public void all(RoutingContext routingContext) { 41 | List all = carRepository.all(); 42 | HttpServerResponse response = routingContext.response(); 43 | response.putHeader("content-type", "application/json") 44 | .putHeader("total-count", String.valueOf(all.size())) 45 | .end(encode(all.stream().map(car -> { 46 | CarRepresentation carRepresentation = new CarRepresentation(car); 47 | carRepresentation.addLink(self(routingContext.request().absoluteURI() + "/" + car.getId())); 48 | return carRepresentation; 49 | }).collect(toList()))); 50 | } 51 | 52 | public void create(RoutingContext routingContext) { 53 | Car car = Json.decodeValue(routingContext.getBodyAsString(), Car.class); 54 | carRepository.save(car); 55 | HttpServerResponse response = routingContext.response(); 56 | response.putHeader("Location", routingContext.request().absoluteURI() + "/" + car.getId()) 57 | .setStatusCode(201) 58 | .end(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/hello/HelloResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.hello; 17 | 18 | import io.github.cdelmas.spike.common.domain.Car; 19 | import io.github.cdelmas.spike.vertx.infrastructure.auth.MyUser; 20 | import io.vertx.core.buffer.Buffer; 21 | import io.vertx.core.http.HttpClient; 22 | import io.vertx.core.http.HttpServerResponse; 23 | import io.vertx.core.json.Json; 24 | import io.vertx.ext.auth.User; 25 | import io.vertx.ext.web.RoutingContext; 26 | 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | import java.util.function.Consumer; 30 | import java.util.function.Supplier; 31 | 32 | import static java.util.Arrays.asList; 33 | import static java.util.Arrays.toString; 34 | import static java.util.stream.Collectors.toList; 35 | 36 | public class HelloResource { 37 | 38 | private final HttpClient httpClient; 39 | 40 | public HelloResource(HttpClient httpClient) { 41 | 42 | this.httpClient = httpClient; 43 | } 44 | 45 | public void hello(RoutingContext routingContext) { 46 | httpClient.getAbs("https://localhost:8090/cars") 47 | .putHeader("Accept", "application/json") 48 | .putHeader("Authorization", "Bearer " + routingContext.user().principal().getString("token")) 49 | .handler(response -> 50 | response.bodyHandler(buffer -> { 51 | if (response.statusCode() == 200) { 52 | List cars = new ArrayList<>(asList(Json.decodeValue(buffer.toString(), Car[].class))); 53 | routingContext.response() 54 | .putHeader("content-type", "test/plain") 55 | .setChunked(true) 56 | .write(cars.stream().map(Car::getName).collect(toList()).toString()) 57 | .write(", and then Hello World from Vert.x-Web!") 58 | .end(); 59 | } else { 60 | routingContext.response() 61 | .setStatusCode(response.statusCode()) 62 | .putHeader("content-type", "test/plain") 63 | .setChunked(true) 64 | .write("Oops, something went wrong: " + response.statusMessage()) 65 | .end(); 66 | } 67 | })) 68 | .end(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/infrastructure/auth/BearerAuthHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.infrastructure.auth; 17 | 18 | import io.vertx.core.http.HttpHeaders; 19 | import io.vertx.core.http.HttpServerRequest; 20 | import io.vertx.core.json.JsonObject; 21 | import io.vertx.ext.auth.AuthProvider; 22 | import io.vertx.ext.web.RoutingContext; 23 | import io.vertx.ext.web.handler.impl.AuthHandlerImpl; 24 | 25 | public class BearerAuthHandler extends AuthHandlerImpl { 26 | 27 | public BearerAuthHandler(AuthProvider authProvider) { 28 | super(authProvider); 29 | } 30 | 31 | @Override 32 | public void handle(RoutingContext routingContext) { 33 | HttpServerRequest request = routingContext.request(); 34 | request.pause(); 35 | String authorization = request.headers().get(HttpHeaders.AUTHORIZATION); 36 | if (authorization == null) { 37 | routingContext.fail(401); 38 | } else { 39 | String[] parts = authorization.split(" "); 40 | if (parts.length != 2) { 41 | routingContext.fail(401); 42 | } else { 43 | String scheme = parts[0]; 44 | if (!"bearer".equalsIgnoreCase(scheme)) { 45 | routingContext.fail(401); 46 | } else { 47 | String token = parts[1]; 48 | JsonObject credentials = new JsonObject(); 49 | credentials.put("token", token); 50 | 51 | authProvider.authenticate(credentials, res -> { 52 | if (res.succeeded()) { 53 | routingContext.setUser(res.result()); 54 | request.resume(); 55 | routingContext.next(); 56 | } else { 57 | routingContext.fail(401); 58 | } 59 | }); 60 | } 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/infrastructure/auth/FacebookOauthTokenVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.infrastructure.auth; 17 | 18 | import io.vertx.core.AsyncResult; 19 | import io.vertx.core.Future; 20 | import io.vertx.core.Handler; 21 | import io.vertx.core.http.HttpClient; 22 | import io.vertx.core.http.HttpClientResponse; 23 | import io.vertx.core.json.JsonObject; 24 | import io.vertx.ext.auth.AuthProvider; 25 | import io.vertx.ext.auth.User; 26 | 27 | public class FacebookOauthTokenVerifier implements AuthProvider { 28 | 29 | private final HttpClient httpClient; 30 | 31 | public FacebookOauthTokenVerifier(HttpClient httpClient) { 32 | this.httpClient = httpClient; 33 | } 34 | 35 | @Override 36 | public void authenticate(JsonObject credentials, Handler> resultHandler) { 37 | String token = credentials.getString("token"); 38 | 39 | httpClient.getAbs("https://graph.facebook.com:443/v2.4/me?access_token=" + token) 40 | .handler(response -> 41 | response.bodyHandler(buffer -> { 42 | JsonObject json = new JsonObject(buffer.toString()); 43 | if (response.statusCode() != 200) { 44 | String message = json.getJsonObject("error").getString("message"); 45 | resultHandler.handle(Future.failedFuture(message)); 46 | } else { 47 | resultHandler.handle(Future.succeededFuture( 48 | new MyUser(Long.parseLong(json.getString("id")), 49 | json.getString("name"), 50 | token))); 51 | } 52 | })) 53 | .exceptionHandler(error -> 54 | resultHandler.handle(Future.failedFuture(error.getMessage()))) 55 | .end(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /vertx/src/main/java/io/github/cdelmas/spike/vertx/infrastructure/auth/MyUser.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Cyril Delmas 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package io.github.cdelmas.spike.vertx.infrastructure.auth; 17 | 18 | import io.vertx.core.AsyncResult; 19 | import io.vertx.core.Future; 20 | import io.vertx.core.Handler; 21 | import io.vertx.core.json.JsonObject; 22 | import io.vertx.ext.auth.AbstractUser; 23 | import io.vertx.ext.auth.AuthProvider; 24 | 25 | public class MyUser extends AbstractUser { 26 | 27 | private AuthProvider authProvider; 28 | private final long id; 29 | private final String name; 30 | private final String token; 31 | 32 | public MyUser(long id, String name, String token) { 33 | this.id = id; 34 | this.name = name; 35 | this.token = token; 36 | } 37 | 38 | @Override 39 | protected void doIsPermitted(String permission, Handler> resultHandler) { 40 | resultHandler.handle(Future.failedFuture("Not implemented yet")); 41 | } 42 | 43 | @Override 44 | public JsonObject principal() { 45 | return new JsonObject().put("id", id).put("name", name).put("token", token); 46 | } 47 | 48 | @Override 49 | public void setAuthProvider(AuthProvider authProvider) { 50 | this.authProvider = authProvider; 51 | } 52 | 53 | public String getName() { 54 | return name; 55 | } 56 | 57 | public String getToken() { 58 | return token; 59 | } 60 | } 61 | --------------------------------------------------------------------------------