Skip to content

Commit 4fe2f08

Browse files
authored
Merge pull request #5562 from psychsane/master
[BAEL-1995] Add new module for restx-demo
2 parents 034d71d + 9ba7937 commit 4fe2f08

19 files changed

+597
-0
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@
17141714
<module>persistence-modules/spring-data-elasticsearch</module>
17151715
<module>core-java-concurrency</module>
17161716
<module>core-java-concurrency-collections</module>
1717+
<module>restx</module>
17171718
</modules>
17181719

17191720
</profile>

restx/data/credentials.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"//": "lines with // keys are just comments (we don't have real comments in json)",
3+
"//": "this file stores password passed through md5+bcrypt hash",
4+
"//": "you can use `restx hash md5+bcrypt {password}` shell command to get hashed passwords to put here",
5+
6+
"//": "to help startup with restx, there are comments with clear text passwords,",
7+
"//": "which should obviously not be stored here.",
8+
"user1": "$2a$10$iZluFUJShbjb1ue68bLrDuGCeJL9EMLHelVIf8u0SUbCseDOvKnoe",
9+
"//": "user 1 password is 'user1-pwd'",
10+
"user2": "$2a$10$oym3SYMFXf/9gGfDKKHO4eM1vWNqAZMsRZCL.BORCaP4yp5cdiCXu",
11+
"//": "user 2 password is 'user2-pwd'"
12+
}

restx/data/users.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{"name":"user1", "roles": ["hello"]},
3+
{"name":"user2", "roles": []}
4+
]

restx/md.restx.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"module": "restx-demo:restx-demo:0.1-SNAPSHOT",
3+
"packaging": "war",
4+
5+
"properties": {
6+
"java.version": "1.8",
7+
"restx.version": "0.35-rc4"
8+
},
9+
"fragments": {
10+
"maven": [
11+
"classpath:///restx/build/fragments/maven/javadoc-apidoclet.xml" ]
12+
},
13+
"dependencies": {
14+
"compile": [
15+
"io.restx:restx-core:${restx.version}",
16+
"io.restx:restx-security-basic:${restx.version}",
17+
"io.restx:restx-core-annotation-processor:${restx.version}",
18+
"io.restx:restx-factory:${restx.version}",
19+
"io.restx:restx-factory-admin:${restx.version}",
20+
"io.restx:restx-validation:${restx.version}",
21+
"io.restx:restx-monitor-codahale:${restx.version}",
22+
"io.restx:restx-monitor-admin:${restx.version}",
23+
"io.restx:restx-log-admin:${restx.version}",
24+
"io.restx:restx-i18n-admin:${restx.version}",
25+
"io.restx:restx-stats-admin:${restx.version}",
26+
"io.restx:restx-servlet:${restx.version}",
27+
"io.restx:restx-server-jetty8:${restx.version}!optional",
28+
"io.restx:restx-apidocs:${restx.version}",
29+
"io.restx:restx-specs-admin:${restx.version}",
30+
"io.restx:restx-admin:${restx.version}",
31+
"ch.qos.logback:logback-classic:1.0.13"
32+
],
33+
"test": [
34+
"io.restx:restx-specs-tests:${restx.version}",
35+
"junit:junit:4.11"
36+
]
37+
}
38+
}

restx/pom.xml

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>restx</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
<name>restx-demo</name>
11+
12+
<properties>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<restx.version>0.35-rc4</restx.version>
16+
</properties>
17+
18+
<parent>
19+
<groupId>com.baeldung</groupId>
20+
<artifactId>parent-modules</artifactId>
21+
<version>1.0.0-SNAPSHOT</version>
22+
</parent>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.restx</groupId>
27+
<artifactId>restx-core</artifactId>
28+
<version>${restx.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.restx</groupId>
32+
<artifactId>restx-security-basic</artifactId>
33+
<version>${restx.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>io.restx</groupId>
37+
<artifactId>restx-core-annotation-processor</artifactId>
38+
<version>${restx.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.restx</groupId>
42+
<artifactId>restx-factory</artifactId>
43+
<version>${restx.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.restx</groupId>
47+
<artifactId>restx-factory-admin</artifactId>
48+
<version>${restx.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.restx</groupId>
52+
<artifactId>restx-validation</artifactId>
53+
<version>${restx.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.restx</groupId>
57+
<artifactId>restx-monitor-codahale</artifactId>
58+
<version>${restx.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.restx</groupId>
62+
<artifactId>restx-monitor-admin</artifactId>
63+
<version>${restx.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.restx</groupId>
67+
<artifactId>restx-log-admin</artifactId>
68+
<version>${restx.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>io.restx</groupId>
72+
<artifactId>restx-i18n-admin</artifactId>
73+
<version>${restx.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>io.restx</groupId>
77+
<artifactId>restx-stats-admin</artifactId>
78+
<version>${restx.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>io.restx</groupId>
82+
<artifactId>restx-servlet</artifactId>
83+
<version>${restx.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>io.restx</groupId>
87+
<artifactId>restx-server-jetty8</artifactId>
88+
<version>${restx.version}</version>
89+
<optional>true</optional>
90+
</dependency>
91+
<dependency>
92+
<groupId>io.restx</groupId>
93+
<artifactId>restx-apidocs</artifactId>
94+
<version>${restx.version}</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>io.restx</groupId>
98+
<artifactId>restx-specs-admin</artifactId>
99+
<version>${restx.version}</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>io.restx</groupId>
103+
<artifactId>restx-admin</artifactId>
104+
<version>${restx.version}</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>ch.qos.logback</groupId>
108+
<artifactId>logback-classic</artifactId>
109+
<version>1.0.13</version>
110+
</dependency>
111+
<dependency>
112+
<groupId>io.restx</groupId>
113+
<artifactId>restx-specs-tests</artifactId>
114+
<version>${restx.version}</version>
115+
<scope>test</scope>
116+
</dependency>
117+
<dependency>
118+
<groupId>junit</groupId>
119+
<artifactId>junit</artifactId>
120+
<version>4.11</version>
121+
<scope>test</scope>
122+
</dependency>
123+
</dependencies>
124+
<build>
125+
<plugins>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-javadoc-plugin</artifactId>
129+
<executions>
130+
<execution>
131+
<id>attach-docs</id>
132+
<!--
133+
we generate javadoc before packaging the jar to let a chance to apidocs doclet
134+
to generate comments dictionary to be packaged inside the jar as a resource
135+
-->
136+
<phase>prepare-package</phase>
137+
<goals>
138+
<goal>jar</goal>
139+
</goals>
140+
</execution>
141+
</executions>
142+
<configuration>
143+
<source>${maven.compiler.source}</source>
144+
<doclet>restx.apidocs.doclet.ApidocsDoclet</doclet>
145+
<docletArtifact>
146+
<groupId>io.restx</groupId>
147+
<artifactId>restx-apidocs-doclet</artifactId>
148+
<version>${restx.version}</version>
149+
</docletArtifact>
150+
<additionalparam>-restx-target-dir ${project.basedir}/target/classes</additionalparam>
151+
</configuration>
152+
</plugin>
153+
</plugins>
154+
</build>
155+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package restx.demo;
2+
3+
import restx.config.ConfigLoader;
4+
import restx.config.ConfigSupplier;
5+
import restx.factory.Provides;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.google.common.base.Charsets;
9+
import com.google.common.collect.ImmutableSet;
10+
import restx.security.*;
11+
import restx.factory.Module;
12+
import restx.factory.Provides;
13+
import javax.inject.Named;
14+
15+
import java.nio.file.Paths;
16+
17+
@Module
18+
public class AppModule {
19+
@Provides
20+
public SignatureKey signatureKey() {
21+
return new SignatureKey("restx-demo -447494532235718370 restx-demo 801c9eaf-4116-48f2-906b-e979fba72757".getBytes(Charsets.UTF_8));
22+
}
23+
24+
@Provides
25+
@Named("restx.admin.password")
26+
public String restxAdminPassword() {
27+
return "4780";
28+
}
29+
30+
@Provides
31+
public ConfigSupplier appConfigSupplier(ConfigLoader configLoader) {
32+
// Load settings.properties in restx.demo package as a set of config entries
33+
return configLoader.fromResource("restx/demo/settings");
34+
}
35+
36+
@Provides
37+
public CredentialsStrategy credentialsStrategy() {
38+
return new BCryptCredentialsStrategy();
39+
}
40+
41+
@Provides
42+
public BasicPrincipalAuthenticator basicPrincipalAuthenticator(
43+
SecuritySettings securitySettings, CredentialsStrategy credentialsStrategy,
44+
@Named("restx.admin.passwordHash") String defaultAdminPasswordHash, ObjectMapper mapper) {
45+
return new StdBasicPrincipalAuthenticator(new StdUserService<>(
46+
// use file based users repository.
47+
// Developer's note: prefer another storage mechanism for your users if you need real user management
48+
// and better perf
49+
new FileBasedUserRepository<>(
50+
StdUser.class, // this is the class for the User objects, that you can get in your app code
51+
// with RestxSession.current().getPrincipal().get()
52+
// it can be a custom user class, it just need to be json deserializable
53+
mapper,
54+
55+
// this is the default restx admin, useful to access the restx admin console.
56+
// if one user with restx-admin role is defined in the repository, this default user won't be
57+
// available anymore
58+
new StdUser("admin", ImmutableSet.<String>of("*")),
59+
60+
// the path where users are stored
61+
Paths.get("data/users.json"),
62+
63+
// the path where credentials are stored. isolating both is a good practice in terms of security
64+
// it is strongly recommended to follow this approach even if you use your own repository
65+
Paths.get("data/credentials.json"),
66+
67+
// tells that we want to reload the files dynamically if they are touched.
68+
// this has a performance impact, if you know your users / credentials never change without a
69+
// restart you can disable this to get better perfs
70+
true),
71+
credentialsStrategy, defaultAdminPasswordHash),
72+
securitySettings);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package restx.demo;
2+
3+
import com.google.common.base.Optional;
4+
import restx.server.WebServer;
5+
import restx.server.Jetty8WebServer;
6+
7+
/**
8+
* This class can be used to run the app.
9+
*
10+
* Alternatively, you can deploy the app as a war in a regular container like tomcat or jetty.
11+
*
12+
* Reading the port from system env PORT makes it compatible with heroku.
13+
*/
14+
public class AppServer {
15+
public static final String WEB_INF_LOCATION = "src/main/webapp/WEB-INF/web.xml";
16+
public static final String WEB_APP_LOCATION = "src/main/webapp";
17+
18+
public static void main(String[] args) throws Exception {
19+
int port = Integer.valueOf(Optional.fromNullable(System.getenv("PORT")).or("8080"));
20+
WebServer server = new Jetty8WebServer(WEB_INF_LOCATION, WEB_APP_LOCATION, port, "0.0.0.0");
21+
22+
/*
23+
* load mode from system property if defined, or default to dev
24+
* be careful with that setting, if you use this class to launch your server in production, make sure to launch
25+
* it with -Drestx.mode=prod or change the default here
26+
*/
27+
System.setProperty("restx.mode", System.getProperty("restx.mode", "dev"));
28+
System.setProperty("restx.app.package", "restx.demo");
29+
30+
server.startAndAwait();
31+
}
32+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package restx.demo;
2+
3+
/**
4+
* A list of roles for the application.
5+
*
6+
* We don't use an enum here because it must be used inside an annotation.
7+
*/
8+
public final class Roles {
9+
public static final String HELLO_ROLE = "hello";
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package restx.demo.domain;
2+
3+
public class Message {
4+
private String message;
5+
6+
public String getMessage() {
7+
return message;
8+
}
9+
10+
public Message setMessage(final String message) {
11+
this.message = message;
12+
return this;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "Message{" +
18+
"message='" + message + '\'' +
19+
'}';
20+
}
21+
}

0 commit comments

Comments
 (0)