undertow (#2426)
* moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities * adding concurrent module * concurrent package description * concurrent package description * Update EitherUnitTest.java * introducing lambda expression * jooby project * jooby project * reducing employee bean content * bootique module * bootique * bootique * undertow module * undertow module * refactoring * using lambda * as per baeldung formatter
This commit is contained in:
parent
dc105bc6f2
commit
c8c0c1e51d
1
pom.xml
1
pom.xml
|
@ -237,6 +237,7 @@
|
|||
<module>liquibase</module>
|
||||
<module>spring-boot-property-exp</module>
|
||||
<module>mockserver</module>
|
||||
<module>undertow</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.undertow</groupId>
|
||||
<artifactId>undertow</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>undertow</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.undertow</groupId>
|
||||
<artifactId>undertow-core</artifactId>
|
||||
<version>1.4.18.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.undertow</groupId>
|
||||
<artifactId>undertow-servlet</artifactId>
|
||||
<version>1.4.18.Final</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.undertow.SimpleServer</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.undertow;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.util.Headers;
|
||||
|
||||
public class SimpleServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(exchange -> {
|
||||
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
|
||||
exchange.getResponseSender().send("Hello Baeldung");
|
||||
}).build();
|
||||
server.start();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.undertow.ftp;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.server.handlers.resource.PathResourceManager;
|
||||
|
||||
import static io.undertow.Handlers.resource;
|
||||
|
||||
public class FileServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
|
||||
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
|
||||
.setDirectoryListingEnabled(true))
|
||||
.build();
|
||||
server.start();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.undertow.secure;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import io.undertow.security.idm.Account;
|
||||
import io.undertow.security.idm.Credential;
|
||||
import io.undertow.security.idm.PasswordCredential;
|
||||
|
||||
public class CustomIdentityManager implements io.undertow.security.idm.IdentityManager {
|
||||
|
||||
private final Map<String, char[]> users;
|
||||
|
||||
CustomIdentityManager(final Map<String, char[]> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account verify(Account account) {
|
||||
return account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account verify(Credential credential) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account verify(String id, Credential credential) {
|
||||
Account account = getAccount(id);
|
||||
if (account != null && verifyCredential(account, credential)) {
|
||||
return account;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean verifyCredential(Account account, Credential credential) {
|
||||
if (credential instanceof PasswordCredential) {
|
||||
char[] password = ((PasswordCredential) credential).getPassword();
|
||||
char[] expectedPassword = users.get(account.getPrincipal().getName());
|
||||
|
||||
return Arrays.equals(password, expectedPassword);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Account getAccount(final String id) {
|
||||
if (users.containsKey(id)) {
|
||||
return new Account() {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Principal principal = new Principal() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public Principal getPrincipal() {
|
||||
return principal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRoles() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.undertow.secure;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.io.IoCallback;
|
||||
import io.undertow.security.api.AuthenticationMechanism;
|
||||
import io.undertow.security.api.AuthenticationMode;
|
||||
import io.undertow.security.api.SecurityContext;
|
||||
import io.undertow.security.handlers.AuthenticationCallHandler;
|
||||
import io.undertow.security.handlers.AuthenticationConstraintHandler;
|
||||
import io.undertow.security.handlers.AuthenticationMechanismsHandler;
|
||||
import io.undertow.security.handlers.SecurityInitialHandler;
|
||||
import io.undertow.security.idm.IdentityManager;
|
||||
import io.undertow.security.impl.BasicAuthenticationMechanism;
|
||||
import io.undertow.server.HttpHandler;
|
||||
import io.undertow.server.HttpServerExchange;
|
||||
|
||||
public class SecureServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Map<String, char[]> users = new HashMap<>(2);
|
||||
users.put("root", "password".toCharArray());
|
||||
users.put("admin", "password".toCharArray());
|
||||
|
||||
final IdentityManager idm = new CustomIdentityManager(users);
|
||||
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity((exchange) -> {
|
||||
final SecurityContext context = exchange.getSecurityContext();
|
||||
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(),
|
||||
IoCallback.END_EXCHANGE);
|
||||
}, idm)).build();
|
||||
|
||||
server.start();
|
||||
|
||||
}
|
||||
|
||||
private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) {
|
||||
HttpHandler handler = toWrap;
|
||||
handler = new AuthenticationCallHandler(handler);
|
||||
handler = new AuthenticationConstraintHandler(handler);
|
||||
final List<AuthenticationMechanism> mechanisms = Collections
|
||||
.<AuthenticationMechanism> singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
|
||||
handler = new AuthenticationMechanismsHandler(handler, mechanisms);
|
||||
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
|
||||
return handler;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.undertow.socket;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.server.handlers.resource.ClassPathResourceManager;
|
||||
import io.undertow.websockets.core.AbstractReceiveListener;
|
||||
import io.undertow.websockets.core.BufferedTextMessage;
|
||||
import io.undertow.websockets.core.WebSocketChannel;
|
||||
import io.undertow.websockets.core.WebSockets;
|
||||
|
||||
import static io.undertow.Handlers.path;
|
||||
import static io.undertow.Handlers.resource;
|
||||
import static io.undertow.Handlers.websocket;
|
||||
|
||||
public class SocketServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
|
||||
.setHandler(path().addPrefixPath("/baeldungApp", websocket((exchange, channel) -> {
|
||||
channel.getReceiveSetter().set(new AbstractReceiveListener() {
|
||||
@Override
|
||||
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
|
||||
final String messageData = message.getData();
|
||||
for (WebSocketChannel session : channel.getPeerConnections()) {
|
||||
WebSockets.sendText(messageData, session, null);
|
||||
}
|
||||
}
|
||||
});
|
||||
channel.resumeReceives();
|
||||
})).addPrefixPath("/", resource(new ClassPathResourceManager(SocketServer.class.getClassLoader(),
|
||||
SocketServer.class.getPackage())).addWelcomeFiles("index.html")))
|
||||
.build();
|
||||
|
||||
server.start();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue