Skip to content

Commit 84cbc58

Browse files
authored
Undertow refactor (eugenp#2462)
1 parent 82c8d88 commit 84cbc58

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

‎undertow/src/main/java/com/baeldung/undertow/ftp/FileServer.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.baeldung.undertow.ftp;
22

3-
import java.nio.file.Paths;
4-
53
import io.undertow.Undertow;
64
import io.undertow.server.handlers.resource.PathResourceManager;
75

6+
import java.nio.file.Paths;
7+
88
import static io.undertow.Handlers.resource;
99

1010
public class FileServer {
1111

1212
public static void main(String[] args) {
1313
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
14-
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
15-
.setDirectoryListingEnabled(true))
16-
.build();
14+
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
15+
.setDirectoryListingEnabled(true))
16+
.build();
1717
server.start();
1818
}
1919

‎undertow/src/main/java/com/baeldung/undertow/secure/CustomIdentityManager.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ private Account getAccount(final String id) {
5454

5555
private static final long serialVersionUID = 1L;
5656

57-
private final Principal principal = new Principal() {
58-
@Override
59-
public String getName() {
60-
return id;
61-
}
62-
};
57+
private final Principal principal = () -> id;
6358

6459
@Override
6560
public Principal getPrincipal() {

‎undertow/src/main/java/com/baeldung/undertow/secure/SecureServer.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.baeldung.undertow.secure;
22

3-
import java.util.Collections;
4-
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
83
import io.undertow.Undertow;
94
import io.undertow.io.IoCallback;
105
import io.undertow.security.api.AuthenticationMechanism;
@@ -19,6 +14,11 @@
1914
import io.undertow.server.HttpHandler;
2015
import io.undertow.server.HttpServerExchange;
2116

17+
import java.util.Collections;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
2222
public class SecureServer {
2323

2424
public static void main(String[] args) {
@@ -28,24 +28,24 @@ public static void main(String[] args) {
2828

2929
final IdentityManager idm = new CustomIdentityManager(users);
3030

31-
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity((exchange) -> {
32-
setExchange(exchange);
33-
}, idm)).build();
31+
Undertow server = Undertow.builder()
32+
.addHttpListener(8080, "localhost")
33+
.setHandler(addSecurity(SecureServer::setExchange, idm)).build();
3434

3535
server.start();
3636
}
3737

3838
private static void setExchange(HttpServerExchange exchange) {
3939
final SecurityContext context = exchange.getSecurityContext();
40-
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(),IoCallback.END_EXCHANGE);
40+
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(), IoCallback.END_EXCHANGE);
4141
}
4242

4343
private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) {
4444
HttpHandler handler = toWrap;
4545
handler = new AuthenticationCallHandler(handler);
4646
handler = new AuthenticationConstraintHandler(handler);
4747
final List<AuthenticationMechanism> mechanisms = Collections
48-
.<AuthenticationMechanism> singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
48+
.singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
4949
handler = new AuthenticationMechanismsHandler(handler, mechanisms);
5050
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
5151
return handler;

0 commit comments

Comments
 (0)