diff --git a/helidon/helidon-mp/pom.xml b/helidon/helidon-mp/pom.xml new file mode 100644 index 0000000000..1ec1131a67 --- /dev/null +++ b/helidon/helidon-mp/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + helidon-mp + + + com.baeldung.helidon + helidon + 1.0.0-SNAPSHOT + + + + + io.helidon.microprofile.bundles + helidon-microprofile-1.2 + 0.10.4 + + + org.glassfish.jersey.media + jersey-media-json-binding + 2.26 + + + + diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/LibraryApplication.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/LibraryApplication.java new file mode 100644 index 0000000000..58913c8b39 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/LibraryApplication.java @@ -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> getClasses() { + return CollectionsHelper.setOf(BookEndpoint.class); + } + + public static void main(String... args) { + Server server = Server.builder() + .addApplication(LibraryApplication.class) + .port(9080) + .build(); + server.start(); + } +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/model/Book.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/model/Book.java new file mode 100644 index 0000000000..44b7f5428d --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/model/Book.java @@ -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; + } +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java new file mode 100644 index 0000000000..f7d0bfc5f7 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookListMessageBodyWriter.java @@ -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> { + + @Override + public boolean isWriteable(Class clazz, Type genericType, Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public long getSize(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return 0; + } + + @Override + public void writeTo(List books, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonArray jsonArray = BookMapper.map(books); + jsonWriter.writeArray(jsonArray); + jsonWriter.close(); + } +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java new file mode 100644 index 0000000000..26ce4c1b64 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyReader.java @@ -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 { + + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type.equals(Book.class); + } + + @Override + public Book readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { + return BookMapper.map(entityStream); + } +} \ No newline at end of file diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java new file mode 100644 index 0000000000..9bc6e89958 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java @@ -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 { + @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 httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + JsonWriter jsonWriter = Json.createWriter(entityStream); + JsonObject jsonObject = BookMapper.map(book); + jsonWriter.writeObject(jsonObject); + jsonWriter.close(); + } + +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/repo/BookManager.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/repo/BookManager.java new file mode 100644 index 0000000000..924cf0ce71 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/repo/BookManager.java @@ -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 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 getAll() { + List books = new ArrayList<>(); + books.addAll(inMemoryStore.values()); + return books; + } +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/util/BookMapper.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/util/BookMapper.java new file mode 100644 index 0000000000..861b172299 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/util/BookMapper.java @@ -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 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; + } +} diff --git a/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java new file mode 100644 index 0000000000..13143a5644 --- /dev/null +++ b/helidon/helidon-mp/src/main/java/com/baeldung/microprofile/web/BookEndpoint.java @@ -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(); + } +} diff --git a/helidon/helidon-mp/src/main/resources/META-INF/beans.xml b/helidon/helidon-mp/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..faae50dfc2 --- /dev/null +++ b/helidon/helidon-mp/src/main/resources/META-INF/beans.xml @@ -0,0 +1,7 @@ + + diff --git a/helidon/helidon-mp/src/main/resources/logback.xml b/helidon/helidon-mp/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/helidon/helidon-mp/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/helidon/helidon-se/pom.xml b/helidon/helidon-se/pom.xml new file mode 100644 index 0000000000..5e14ecb81c --- /dev/null +++ b/helidon/helidon-se/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + helidon-se + + + com.baeldung.helidon + helidon + 1.0.0-SNAPSHOT + + + + 0.10.4 + + + + + + io.helidon.config + helidon-config-yaml + ${helidon.version} + + + + + io.helidon.webserver + helidon-webserver + ${helidon.version} + + + io.helidon.webserver + helidon-webserver-netty + ${helidon.version} + runtime + + + io.helidon.webserver + helidon-webserver-json + ${helidon.version} + + + + + io.helidon.security + helidon-security + ${helidon.version} + + + io.helidon.security + helidon-security-provider-http-auth + ${helidon.version} + + + io.helidon.security + helidon-security-integration-webserver + ${helidon.version} + + + + + \ No newline at end of file diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/config/ConfigApplication.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/config/ConfigApplication.java new file mode 100644 index 0000000000..acfcdb2373 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/config/ConfigApplication.java @@ -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); + } + +} diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/Book.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/Book.java new file mode 100644 index 0000000000..9a591bcc73 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/Book.java @@ -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; + } +} \ No newline at end of file diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookManager.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookManager.java new file mode 100644 index 0000000000..2e6e694041 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookManager.java @@ -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 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 getAll() { + List books = new ArrayList<>(); + books.addAll(inMemoryStore.values()); + return books; + } +} \ No newline at end of file diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookResource.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookResource.java new file mode 100644 index 0000000000..0648930841 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/BookResource.java @@ -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 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 books) { + JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); + books.forEach(book -> { + jsonArrayBuilder.add(from(book)); + }); + return jsonArrayBuilder.build(); + } +} diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/WebApplicationRouting.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/WebApplicationRouting.java new file mode 100644 index 0000000000..1f32d3c528 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/routing/WebApplicationRouting.java @@ -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()) + ); + } + +} diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/UserApp.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/UserApp.java new file mode 100644 index 0000000000..f776f13457 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/UserApp.java @@ -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 roles; + + public UserApp(String login, char[] password, Collection 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 getRoles() { + return roles; + } +} diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/WebApplicationSecurity.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/WebApplicationSecurity.java new file mode 100644 index 0000000000..0859726946 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/security/WebApplicationSecurity.java @@ -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 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()) + ); + } +} diff --git a/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/webserver/SimpleWebApplication.java b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/webserver/SimpleWebApplication.java new file mode 100644 index 0000000000..0a603a5123 --- /dev/null +++ b/helidon/helidon-se/src/main/java/com/baeldung/helidon/se/webserver/SimpleWebApplication.java @@ -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()) + ); + } + +} diff --git a/helidon/helidon-se/src/main/resources/application.json b/helidon/helidon-se/src/main/resources/application.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/helidon/helidon-se/src/main/resources/application.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/helidon/helidon-se/src/main/resources/application.properties b/helidon/helidon-se/src/main/resources/application.properties new file mode 100644 index 0000000000..062047de4a --- /dev/null +++ b/helidon/helidon-se/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=9080 +web.debug=true +web.page-size=15 +user.home=C:/Users/app \ No newline at end of file diff --git a/helidon/helidon-se/src/main/resources/application.yaml b/helidon/helidon-se/src/main/resources/application.yaml new file mode 100644 index 0000000000..b6dd6cec22 --- /dev/null +++ b/helidon/helidon-se/src/main/resources/application.yaml @@ -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"] \ No newline at end of file diff --git a/helidon/pom.xml b/helidon/pom.xml new file mode 100644 index 0000000000..ea8cc52ee0 --- /dev/null +++ b/helidon/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + com.baeldung.helidon + helidon + 1.0.0-SNAPSHOT + pom + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + helidon-se + helidon-mp + + + diff --git a/pom.xml b/pom.xml index a546f8e925..fa8a1108b5 100644 --- a/pom.xml +++ b/pom.xml @@ -736,6 +736,7 @@ spring-resttemplate + helidon