Skip to content

Commit c8c0c1e

Browse files
AbhinabKanrarpivovarit
authored andcommitted
undertow (eugenp#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
1 parent dc105bc commit c8c0c1e

7 files changed

Lines changed: 260 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<module>liquibase</module>
238238
<module>spring-boot-property-exp</module>
239239
<module>mockserver</module>
240+
<module>undertow</module>
240241
</modules>
241242

242243
<dependencies>

‎undertow/pom.xml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.undertow</groupId>
5+
<artifactId>undertow</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>undertow</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.undertow</groupId>
19+
<artifactId>undertow-core</artifactId>
20+
<version>1.4.18.Final</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>io.undertow</groupId>
24+
<artifactId>undertow-servlet</artifactId>
25+
<version>1.4.18.Final</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<finalName>${project.artifactId}</finalName>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-shade-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-jar-plugin</artifactId>
47+
<configuration>
48+
<archive>
49+
<manifest>
50+
<mainClass>com.baeldung.undertow.SimpleServer</mainClass>
51+
</manifest>
52+
</archive>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.undertow;
2+
3+
import io.undertow.Undertow;
4+
import io.undertow.util.Headers;
5+
6+
public class SimpleServer {
7+
8+
public static void main(String[] args) {
9+
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(exchange -> {
10+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
11+
exchange.getResponseSender().send("Hello Baeldung");
12+
}).build();
13+
server.start();
14+
}
15+
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.undertow.ftp;
2+
3+
import java.nio.file.Paths;
4+
5+
import io.undertow.Undertow;
6+
import io.undertow.server.handlers.resource.PathResourceManager;
7+
8+
import static io.undertow.Handlers.resource;
9+
10+
public class FileServer {
11+
12+
public static void main(String[] args) {
13+
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
14+
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
15+
.setDirectoryListingEnabled(true))
16+
.build();
17+
server.start();
18+
}
19+
20+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.undertow.secure;
2+
3+
import java.security.Principal;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
import io.undertow.security.idm.Account;
10+
import io.undertow.security.idm.Credential;
11+
import io.undertow.security.idm.PasswordCredential;
12+
13+
public class CustomIdentityManager implements io.undertow.security.idm.IdentityManager {
14+
15+
private final Map<String, char[]> users;
16+
17+
CustomIdentityManager(final Map<String, char[]> users) {
18+
this.users = users;
19+
}
20+
21+
@Override
22+
public Account verify(Account account) {
23+
return account;
24+
}
25+
26+
@Override
27+
public Account verify(Credential credential) {
28+
return null;
29+
}
30+
31+
@Override
32+
public Account verify(String id, Credential credential) {
33+
Account account = getAccount(id);
34+
if (account != null && verifyCredential(account, credential)) {
35+
return account;
36+
}
37+
return null;
38+
}
39+
40+
private boolean verifyCredential(Account account, Credential credential) {
41+
if (credential instanceof PasswordCredential) {
42+
char[] password = ((PasswordCredential) credential).getPassword();
43+
char[] expectedPassword = users.get(account.getPrincipal().getName());
44+
45+
return Arrays.equals(password, expectedPassword);
46+
}
47+
return false;
48+
}
49+
50+
private Account getAccount(final String id) {
51+
if (users.containsKey(id)) {
52+
return new Account() {
53+
54+
private static final long serialVersionUID = 1L;
55+
56+
private final Principal principal = new Principal() {
57+
@Override
58+
public String getName() {
59+
return id;
60+
}
61+
};
62+
63+
@Override
64+
public Principal getPrincipal() {
65+
return principal;
66+
}
67+
68+
@Override
69+
public Set<String> getRoles() {
70+
return Collections.emptySet();
71+
}
72+
};
73+
}
74+
return null;
75+
}
76+
77+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.undertow.secure;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import io.undertow.Undertow;
9+
import io.undertow.io.IoCallback;
10+
import io.undertow.security.api.AuthenticationMechanism;
11+
import io.undertow.security.api.AuthenticationMode;
12+
import io.undertow.security.api.SecurityContext;
13+
import io.undertow.security.handlers.AuthenticationCallHandler;
14+
import io.undertow.security.handlers.AuthenticationConstraintHandler;
15+
import io.undertow.security.handlers.AuthenticationMechanismsHandler;
16+
import io.undertow.security.handlers.SecurityInitialHandler;
17+
import io.undertow.security.idm.IdentityManager;
18+
import io.undertow.security.impl.BasicAuthenticationMechanism;
19+
import io.undertow.server.HttpHandler;
20+
import io.undertow.server.HttpServerExchange;
21+
22+
public class SecureServer {
23+
24+
public static void main(String[] args) {
25+
final Map<String, char[]> users = new HashMap<>(2);
26+
users.put("root", "password".toCharArray());
27+
users.put("admin", "password".toCharArray());
28+
29+
final IdentityManager idm = new CustomIdentityManager(users);
30+
31+
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity((exchange) -> {
32+
final SecurityContext context = exchange.getSecurityContext();
33+
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(),
34+
IoCallback.END_EXCHANGE);
35+
}, idm)).build();
36+
37+
server.start();
38+
39+
}
40+
41+
private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) {
42+
HttpHandler handler = toWrap;
43+
handler = new AuthenticationCallHandler(handler);
44+
handler = new AuthenticationConstraintHandler(handler);
45+
final List<AuthenticationMechanism> mechanisms = Collections
46+
.<AuthenticationMechanism> singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
47+
handler = new AuthenticationMechanismsHandler(handler, mechanisms);
48+
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
49+
return handler;
50+
}
51+
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.undertow.socket;
2+
3+
import io.undertow.Undertow;
4+
import io.undertow.server.handlers.resource.ClassPathResourceManager;
5+
import io.undertow.websockets.core.AbstractReceiveListener;
6+
import io.undertow.websockets.core.BufferedTextMessage;
7+
import io.undertow.websockets.core.WebSocketChannel;
8+
import io.undertow.websockets.core.WebSockets;
9+
10+
import static io.undertow.Handlers.path;
11+
import static io.undertow.Handlers.resource;
12+
import static io.undertow.Handlers.websocket;
13+
14+
public class SocketServer {
15+
16+
public static void main(String[] args) {
17+
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
18+
.setHandler(path().addPrefixPath("/baeldungApp", websocket((exchange, channel) -> {
19+
channel.getReceiveSetter().set(new AbstractReceiveListener() {
20+
@Override
21+
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
22+
final String messageData = message.getData();
23+
for (WebSocketChannel session : channel.getPeerConnections()) {
24+
WebSockets.sendText(messageData, session, null);
25+
}
26+
}
27+
});
28+
channel.resumeReceives();
29+
})).addPrefixPath("/", resource(new ClassPathResourceManager(SocketServer.class.getClassLoader(),
30+
SocketServer.class.getPackage())).addWelcomeFiles("index.html")))
31+
.build();
32+
33+
server.start();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)