JAVA-13855 Create new microservices-modules (#12612)

This commit is contained in:
anuragkumawat
2022-08-20 14:20:24 +05:30
committed by GitHub
parent 14c998c3ac
commit 6bc1d484ca
95 changed files with 661 additions and 639 deletions
Binary file not shown.
@@ -1,7 +1,7 @@
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0"
bean-discovery-mode="annotated">
</beans>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0"
bean-discovery-mode="annotated">
</beans>
@@ -1,29 +1,29 @@
package com.baeldung.helidon.se.config;
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.config.spi.ConfigSource;
public class ConfigApplication {
public static void main(String... args) throws Exception {
ConfigSource configSource = ConfigSources.classpath("application.yaml").build();
Config config = Config.builder()
.disableSystemPropertiesSource()
.disableEnvironmentVariablesSource()
.sources(configSource)
.build();
int port = config.get("server.port").asInt();
int pageSize = config.get("web.page-size").asInt();
boolean debug = config.get("web.debug").asBoolean();
String userHome = config.get("user.home").asString();
System.out.println("port: " + port);
System.out.println("pageSize: " + pageSize);
System.out.println("debug: " + debug);
System.out.println("userHome: " + userHome);
}
}
package com.baeldung.helidon.se.config;
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.config.spi.ConfigSource;
public class ConfigApplication {
public static void main(String... args) throws Exception {
ConfigSource configSource = ConfigSources.classpath("application.yaml").build();
Config config = Config.builder()
.disableSystemPropertiesSource()
.disableEnvironmentVariablesSource()
.sources(configSource)
.build();
int port = config.get("server.port").asInt();
int pageSize = config.get("web.page-size").asInt();
boolean debug = config.get("web.debug").asBoolean();
String userHome = config.get("user.home").asString();
System.out.println("port: " + port);
System.out.println("pageSize: " + pageSize);
System.out.println("debug: " + debug);
System.out.println("userHome: " + userHome);
}
}
@@ -1,49 +1,49 @@
package com.baeldung.helidon.se.routing;
public class Book {
private String id;
private String isbn;
private String name;
private String author;
private Integer pages;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
package com.baeldung.helidon.se.routing;
public class Book {
private String id;
private String isbn;
private String name;
private String author;
private Integer pages;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
}
@@ -1,49 +1,49 @@
package com.baeldung.helidon.se.routing;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
public class BookManager {
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
private AtomicInteger bookIdGenerator = new AtomicInteger(0);
private ConcurrentMap<String, Book> inMemoryStore = new ConcurrentHashMap<>();
public BookManager() {
Book book = new Book();
book.setId(getNextId());
book.setName("Building Microservice With Oracle Helidon");
book.setIsbn("11223344");
book.setAuthor("baeldung");
book.setPages(560);
inMemoryStore.put(book.getId(), book);
}
private String getNextId() {
String date = LocalDate.now().format(formatter);
return String.format("%04d-%s", bookIdGenerator.incrementAndGet(), date);
}
public String add(Book book) {
String id = getNextId();
book.setId(id);
inMemoryStore.put(id, book);
return id;
}
public Book get(String id) {
return inMemoryStore.get(id);
}
public List<Book> getAll() {
List<Book> books = new ArrayList<>();
books.addAll(inMemoryStore.values());
return books;
}
package com.baeldung.helidon.se.routing;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
public class BookManager {
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
private AtomicInteger bookIdGenerator = new AtomicInteger(0);
private ConcurrentMap<String, Book> inMemoryStore = new ConcurrentHashMap<>();
public BookManager() {
Book book = new Book();
book.setId(getNextId());
book.setName("Building Microservice With Oracle Helidon");
book.setIsbn("11223344");
book.setAuthor("baeldung");
book.setPages(560);
inMemoryStore.put(book.getId(), book);
}
private String getNextId() {
String date = LocalDate.now().format(formatter);
return String.format("%04d-%s", bookIdGenerator.incrementAndGet(), date);
}
public String add(Book book) {
String id = getNextId();
book.setId(id);
inMemoryStore.put(id, book);
return id;
}
public Book get(String id) {
return inMemoryStore.get(id);
}
public List<Book> getAll() {
List<Book> books = new ArrayList<>();
books.addAll(inMemoryStore.values());
return books;
}
}
@@ -1,58 +1,58 @@
package com.baeldung.helidon.se.routing;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import java.util.List;
public class BookResource implements Service {
private BookManager bookManager = new BookManager();
@Override
public void update(Routing.Rules rules) {
rules
.get("/", this::books)
.get("/{id}", this::bookById);
}
private void bookById(ServerRequest serverRequest, ServerResponse serverResponse) {
//get the book with the given id
String id = serverRequest.path().param("id");
Book book = bookManager.get(id);
JsonObject jsonObject = from(book);
serverResponse.send(jsonObject);
}
private void books(ServerRequest serverRequest, ServerResponse serverResponse) {
//get all books
List<Book> books = bookManager.getAll();
JsonArray jsonArray = from(books);
serverResponse.send(jsonArray);
}
private JsonObject from(Book book) {
JsonObject jsonObject = Json.createObjectBuilder()
.add("id", book.getId())
.add("isbn", book.getIsbn())
.add("name", book.getName())
.add("author", book.getAuthor())
.add("pages", book.getPages())
.build();
return jsonObject;
}
private JsonArray from(List<Book> books) {
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
books.forEach(book -> {
jsonArrayBuilder.add(from(book));
});
return jsonArrayBuilder.build();
}
}
package com.baeldung.helidon.se.routing;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import java.util.List;
public class BookResource implements Service {
private BookManager bookManager = new BookManager();
@Override
public void update(Routing.Rules rules) {
rules
.get("/", this::books)
.get("/{id}", this::bookById);
}
private void bookById(ServerRequest serverRequest, ServerResponse serverResponse) {
//get the book with the given id
String id = serverRequest.path().param("id");
Book book = bookManager.get(id);
JsonObject jsonObject = from(book);
serverResponse.send(jsonObject);
}
private void books(ServerRequest serverRequest, ServerResponse serverResponse) {
//get all books
List<Book> books = bookManager.getAll();
JsonArray jsonArray = from(books);
serverResponse.send(jsonArray);
}
private JsonObject from(Book book) {
JsonObject jsonObject = Json.createObjectBuilder()
.add("id", book.getId())
.add("isbn", book.getIsbn())
.add("name", book.getName())
.add("author", book.getAuthor())
.add("pages", book.getPages())
.build();
return jsonObject;
}
private JsonArray from(List<Book> books) {
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
books.forEach(book -> {
jsonArrayBuilder.add(from(book));
});
return jsonArrayBuilder.build();
}
}
@@ -1,29 +1,29 @@
package com.baeldung.helidon.se.routing;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.json.JsonSupport;
public class WebApplicationRouting {
public static void main(String... args) throws Exception {
ServerConfiguration serverConfig = ServerConfiguration.builder()
.port(9080)
.build();
Routing routing = Routing.builder()
.register(JsonSupport.get())
.register("/books", new BookResource())
.get("/greet", (request, response) -> response.send("Hello World !"))
.build();
WebServer.create(serverConfig, routing)
.start()
.thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
package com.baeldung.helidon.se.routing;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.json.JsonSupport;
public class WebApplicationRouting {
public static void main(String... args) throws Exception {
ServerConfiguration serverConfig = ServerConfiguration.builder()
.port(9080)
.build();
Routing routing = Routing.builder()
.register(JsonSupport.get())
.register("/books", new BookResource())
.get("/greet", (request, response) -> response.send("Hello World !"))
.build();
WebServer.create(serverConfig, routing)
.start()
.thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
@@ -1,33 +1,33 @@
package com.baeldung.helidon.se.security;
import io.helidon.security.provider.httpauth.UserStore;
import java.util.Collection;
public class MyUser implements UserStore.User {
private String login;
private char[] password;
private Collection<String> roles;
public MyUser(String login, char[] password, Collection<String> roles) {
this.login = login;
this.password = password;
this.roles = roles;
}
@Override
public String getLogin() {
return login;
}
@Override
public char[] getPassword() {
return password;
}
@Override
public Collection<String> getRoles() {
return roles;
}
}
package com.baeldung.helidon.se.security;
import io.helidon.security.provider.httpauth.UserStore;
import java.util.Collection;
public class MyUser implements UserStore.User {
private String login;
private char[] password;
private Collection<String> roles;
public MyUser(String login, char[] password, Collection<String> roles) {
this.login = login;
this.password = password;
this.roles = roles;
}
@Override
public String getLogin() {
return login;
}
@Override
public char[] getPassword() {
return password;
}
@Override
public Collection<String> getRoles() {
return roles;
}
}
@@ -1,61 +1,61 @@
package com.baeldung.helidon.se.security;
import io.helidon.config.Config;
import io.helidon.security.Security;
import io.helidon.security.SubjectType;
import io.helidon.security.provider.httpauth.HttpBasicAuthProvider;
import io.helidon.security.provider.httpauth.UserStore;
import io.helidon.security.webserver.WebSecurity;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class WebApplicationSecurity {
public static void main(String... args) throws Exception {
Config config = Config.create();
ServerConfiguration serverConfig =
ServerConfiguration.fromConfig(config.get("server"));
Map<String, MyUser> users = new HashMap<>();
users.put("user", new MyUser("user", "user".toCharArray(), Arrays.asList("ROLE_USER")));
users.put("admin", new MyUser("admin", "admin".toCharArray(), Arrays.asList("ROLE_USER", "ROLE_ADMIN")));
UserStore store = user -> Optional.ofNullable(users.get(user));
HttpBasicAuthProvider httpBasicAuthProvider = HttpBasicAuthProvider.builder()
.realm("myRealm")
.subjectType(SubjectType.USER)
.userStore(store)
.build();
//1. Using Builder Pattern or Config Pattern
Security security = Security.builder()
.addAuthenticationProvider(httpBasicAuthProvider)
.build();
//Security security = Security.fromConfig(config);
//2. WebSecurity from Security or from Config
// WebSecurity webSecurity = WebSecurity.from(security)
// .securityDefaults(WebSecurity.authenticate());
WebSecurity webSecurity = WebSecurity.from(config);
Routing routing = Routing.builder()
.register(webSecurity)
.get("/user", (request, response) -> response.send("Hello, I'm a Helidon SE user with ROLE_USER"))
.get("/admin", (request, response) -> response.send("Hello, I'm a Helidon SE user with ROLE_ADMIN"))
.build();
WebServer webServer = WebServer.create(serverConfig, routing);
webServer.start().thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
package com.baeldung.helidon.se.security;
import io.helidon.config.Config;
import io.helidon.security.Security;
import io.helidon.security.SubjectType;
import io.helidon.security.provider.httpauth.HttpBasicAuthProvider;
import io.helidon.security.provider.httpauth.UserStore;
import io.helidon.security.webserver.WebSecurity;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class WebApplicationSecurity {
public static void main(String... args) throws Exception {
Config config = Config.create();
ServerConfiguration serverConfig =
ServerConfiguration.fromConfig(config.get("server"));
Map<String, MyUser> users = new HashMap<>();
users.put("user", new MyUser("user", "user".toCharArray(), Arrays.asList("ROLE_USER")));
users.put("admin", new MyUser("admin", "admin".toCharArray(), Arrays.asList("ROLE_USER", "ROLE_ADMIN")));
UserStore store = user -> Optional.ofNullable(users.get(user));
HttpBasicAuthProvider httpBasicAuthProvider = HttpBasicAuthProvider.builder()
.realm("myRealm")
.subjectType(SubjectType.USER)
.userStore(store)
.build();
//1. Using Builder Pattern or Config Pattern
Security security = Security.builder()
.addAuthenticationProvider(httpBasicAuthProvider)
.build();
//Security security = Security.fromConfig(config);
//2. WebSecurity from Security or from Config
// WebSecurity webSecurity = WebSecurity.from(security)
// .securityDefaults(WebSecurity.authenticate());
WebSecurity webSecurity = WebSecurity.from(config);
Routing routing = Routing.builder()
.register(webSecurity)
.get("/user", (request, response) -> response.send("Hello, I'm a Helidon SE user with ROLE_USER"))
.get("/admin", (request, response) -> response.send("Hello, I'm a Helidon SE user with ROLE_ADMIN"))
.build();
WebServer webServer = WebServer.create(serverConfig, routing);
webServer.start().thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
@@ -1,26 +1,26 @@
package com.baeldung.helidon.se.webserver;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
public class SimpleWebApplication {
public static void main(String... args) throws Exception {
ServerConfiguration serverConfig = ServerConfiguration.builder()
.port(9001)
.build();
Routing routing = Routing.builder()
.get("/greet", (request, response) -> response.send("Hello World !"))
.build();
WebServer.create(serverConfig, routing)
.start()
.thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
package com.baeldung.helidon.se.webserver;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
public class SimpleWebApplication {
public static void main(String... args) throws Exception {
ServerConfiguration serverConfig = ServerConfiguration.builder()
.port(9001)
.build();
Routing routing = Routing.builder()
.get("/greet", (request, response) -> response.send("Hello World !"))
.build();
WebServer.create(serverConfig, routing)
.start()
.thenAccept(ws ->
System.out.println("Server started at: http://localhost:" + ws.port())
);
}
}
@@ -1,4 +1,4 @@
server.port=9080
web.debug=true
web.page-size=15
server.port=9080
web.debug=true
web.page-size=15
user.home=C:/Users/app
@@ -10,7 +10,7 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<artifactId>microservices-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

Some files were not shown because too many files have changed in this diff Show More