Merge pull request #5565 from eelhazati/master
BAEL-2296: A Guide to Oracle Helidon
This commit is contained in:
commit
59c6ad0e71
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>helidon-mp</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
<artifactId>helidon</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.helidon.microprofile.bundles</groupId>
|
||||
<artifactId>helidon-microprofile-1.2</artifactId>
|
||||
<version>0.10.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-json-binding</artifactId>
|
||||
<version>2.26</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.microprofile;
|
||||
|
||||
import com.baeldung.microprofile.web.BookEndpoint;
|
||||
import io.helidon.common.CollectionsHelper;
|
||||
import io.helidon.microprofile.server.Server;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationPath("/library")
|
||||
public class LibraryApplication extends Application {
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getClasses() {
|
||||
return CollectionsHelper.setOf(BookEndpoint.class);
|
||||
}
|
||||
|
||||
public static void main(String... args) {
|
||||
Server server = Server.builder()
|
||||
.addApplication(LibraryApplication.class)
|
||||
.port(9080)
|
||||
.build();
|
||||
server.start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.microprofile.model;
|
||||
|
||||
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 getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.microprofile.providers;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
import com.baeldung.microprofile.util.BookMapper;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonWriter;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyWriter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
@Provider
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class BookListMessageBodyWriter implements MessageBodyWriter<List<Book>> {
|
||||
|
||||
@Override
|
||||
public boolean isWriteable(Class<?> clazz, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize(List<Book> books, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(List<Book> books, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
|
||||
JsonWriter jsonWriter = Json.createWriter(entityStream);
|
||||
JsonArray jsonArray = BookMapper.map(books);
|
||||
jsonWriter.writeArray(jsonArray);
|
||||
jsonWriter.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.microprofile.providers;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
import com.baeldung.microprofile.util.BookMapper;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyReader;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@Provider
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class BookMessageBodyReader implements MessageBodyReader<Book> {
|
||||
|
||||
@Override
|
||||
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return type.equals(Book.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book readFrom(Class<Book> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
|
||||
return BookMapper.map(entityStream);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.microprofile.providers;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
import com.baeldung.microprofile.util.BookMapper;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonWriter;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyWriter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@Provider
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class BookMessageBodyWriter implements MessageBodyWriter<Book> {
|
||||
@Override
|
||||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return type.equals(Book.class);
|
||||
}
|
||||
|
||||
/*
|
||||
Deprecated in JAX RS 2.0
|
||||
*/
|
||||
@Override
|
||||
public long getSize(Book book, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marsahl Book to OutputStream
|
||||
*
|
||||
* @param book
|
||||
* @param type
|
||||
* @param genericType
|
||||
* @param annotations
|
||||
* @param mediaType
|
||||
* @param httpHeaders
|
||||
* @param entityStream
|
||||
* @throws IOException
|
||||
* @throws WebApplicationException
|
||||
*/
|
||||
@Override
|
||||
public void writeTo(Book book, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
|
||||
JsonWriter jsonWriter = Json.createWriter(entityStream);
|
||||
JsonObject jsonObject = BookMapper.map(book);
|
||||
jsonWriter.writeObject(jsonObject);
|
||||
jsonWriter.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.microprofile.repo;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
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;
|
||||
|
||||
@ApplicationScoped
|
||||
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 Eclipse MicroProfile");
|
||||
book.setIsbn("1");
|
||||
book.setAuthor("baeldung");
|
||||
book.setPages(420);
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.microprofile.util;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
|
||||
import javax.json.*;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class BookMapper {
|
||||
|
||||
public static JsonObject map(Book book) {
|
||||
JsonObjectBuilder builder = Json.createObjectBuilder();
|
||||
addValue(builder, "id", book.getId());
|
||||
addValue(builder, "isbn", book.getIsbn());
|
||||
addValue(builder, "name", book.getName());
|
||||
addValue(builder, "author", book.getAuthor());
|
||||
addValue(builder, "pages", book.getPages());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private static void addValue(JsonObjectBuilder builder, String key, Object value) {
|
||||
if (value != null) {
|
||||
builder.add(key, value.toString());
|
||||
} else {
|
||||
builder.addNull(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonArray map(List<Book> books) {
|
||||
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||
books.forEach(book -> {
|
||||
JsonObject jsonObject = map(book);
|
||||
arrayBuilder.add(jsonObject);
|
||||
});
|
||||
return arrayBuilder.build();
|
||||
}
|
||||
|
||||
public static Book map(InputStream is) {
|
||||
try(JsonReader jsonReader = Json.createReader(is)) {
|
||||
JsonObject jsonObject = jsonReader.readObject();
|
||||
Book book = new Book();
|
||||
book.setId(getStringFromJson("id", jsonObject));
|
||||
book.setIsbn(getStringFromJson("isbn", jsonObject));
|
||||
book.setName(getStringFromJson("name", jsonObject));
|
||||
book.setAuthor(getStringFromJson("author", jsonObject));
|
||||
book.setPages(getIntFromJson("pages",jsonObject));
|
||||
return book;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getStringFromJson(String key, JsonObject json) {
|
||||
String returnedString = null;
|
||||
if (json.containsKey(key)) {
|
||||
JsonString value = json.getJsonString(key);
|
||||
if (value != null) {
|
||||
returnedString = value.getString();
|
||||
}
|
||||
}
|
||||
return returnedString;
|
||||
}
|
||||
|
||||
private static Integer getIntFromJson(String key, JsonObject json) {
|
||||
Integer returnedValue = null;
|
||||
if (json.containsKey(key)) {
|
||||
JsonNumber value = json.getJsonNumber(key);
|
||||
if (value != null) {
|
||||
returnedValue = value.intValue();
|
||||
}
|
||||
}
|
||||
return returnedValue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.microprofile.web;
|
||||
|
||||
import com.baeldung.microprofile.model.Book;
|
||||
import com.baeldung.microprofile.repo.BookManager;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
@Path("books")
|
||||
@RequestScoped
|
||||
public class BookEndpoint {
|
||||
|
||||
@Inject
|
||||
private BookManager bookManager;
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getBook(@PathParam("id") String id) {
|
||||
Book book = bookManager.get(id);
|
||||
return Response.ok(book).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getAllBooks() {
|
||||
return Response.ok(bookManager.getAll()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response add(Book book) {
|
||||
String bookId = bookManager.add(book);
|
||||
return Response.created(
|
||||
UriBuilder.fromResource(this.getClass()).path(bookId).build())
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +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>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,64 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>helidon-se</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
<artifactId>helidon</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<helidon.version>0.10.4</helidon.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--Config-->
|
||||
<dependency>
|
||||
<groupId>io.helidon.config</groupId>
|
||||
<artifactId>helidon-config-yaml</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--WebServer-->
|
||||
<dependency>
|
||||
<groupId>io.helidon.webserver</groupId>
|
||||
<artifactId>helidon-webserver</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.webserver</groupId>
|
||||
<artifactId>helidon-webserver-netty</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.webserver</groupId>
|
||||
<artifactId>helidon-webserver-json</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Security-->
|
||||
<dependency>
|
||||
<groupId>io.helidon.security</groupId>
|
||||
<artifactId>helidon-security</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.security</groupId>
|
||||
<artifactId>helidon-security-provider-http-auth</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.security</groupId>
|
||||
<artifactId>helidon-security-integration-webserver</artifactId>
|
||||
<version>${helidon.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +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;
|
||||
}
|
||||
}
|
|
@ -0,0 +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;
|
||||
}
|
||||
}
|
|
@ -0,0 +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();
|
||||
}
|
||||
}
|
|
@ -0,0 +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())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.helidon.se.security;
|
||||
|
||||
import io.helidon.security.provider.httpauth.UserStore;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class UserApp implements UserStore.User {
|
||||
|
||||
private String login;
|
||||
private char[] password;
|
||||
private Collection<String> roles;
|
||||
|
||||
public UserApp(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;
|
||||
}
|
||||
}
|
|
@ -0,0 +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, UserApp> users = new HashMap<>();
|
||||
users.put("user", new UserApp("user", "user".toCharArray(), Arrays.asList("ROLE_USER")));
|
||||
users.put("admin", new UserApp("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())
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +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())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{}
|
|
@ -0,0 +1,4 @@
|
|||
server.port=9080
|
||||
web.debug=true
|
||||
web.page-size=15
|
||||
user.home=C:/Users/app
|
|
@ -0,0 +1,33 @@
|
|||
server:
|
||||
port: 9080
|
||||
web:
|
||||
debug: true
|
||||
page-size: 15
|
||||
user:
|
||||
home: C:/Users/app
|
||||
|
||||
#Config 4 Security ==> Mapped to Security Object
|
||||
security:
|
||||
providers:
|
||||
- http-basic-auth:
|
||||
realm: "myRealm"
|
||||
principal-type: USER # Can be USER or SERVICE, default is USER
|
||||
users:
|
||||
- login: "user"
|
||||
password: "user"
|
||||
roles: ["ROLE_USER"]
|
||||
- login: "admin"
|
||||
password: "admin"
|
||||
roles: ["ROLE_USER", "ROLE_ADMIN"]
|
||||
|
||||
#Config 4 Security Web Server Integration ==> Mapped to WebSecurity Object
|
||||
web-server:
|
||||
securityDefaults:
|
||||
authenticate: true
|
||||
paths:
|
||||
- path: "/user"
|
||||
methods: ["get"]
|
||||
roles-allowed: ["ROLE_USER", "ROLE_ADMIN"]
|
||||
- path: "/admin"
|
||||
methods: ["get"]
|
||||
roles-allowed: ["ROLE_ADMIN"]
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.helidon</groupId>
|
||||
<artifactId>helidon</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>helidon-se</module>
|
||||
<module>helidon-mp</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue