Merge branch 'eugenp:master' into string_to_int_encapsulation
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
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>core-java-10</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-10</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
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>core-java-11-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
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>core-java-11-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@@ -14,7 +13,7 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
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>core-java-11</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-11</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
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>core-java-12</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-12</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-13</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-13</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
<artifactId>core-java-14</artifactId>
|
||||
<name>core-java-14</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
<artifactId>core-java-15</artifactId>
|
||||
<name>core-java-15</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
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>core-java-16</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-16</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
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>core-java-17</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-17</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
- [Record Patterns in Java 19](https://www.baeldung.com/java-19-record-patterns)
|
||||
- [Structured Concurrency in Java 19](https://www.baeldung.com/java-structured-concurrency)
|
||||
- [Possible Root Causes for High CPU Usage in Java](https://www.baeldung.com/java-high-cpu-usage-causes)
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
<?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">
|
||||
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>core-java-19</artifactId>
|
||||
<name>core-java-19</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>core-java-19</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -30,4 +26,10 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.highcpu;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Application {
|
||||
|
||||
static List<Integer> generateList() {
|
||||
return IntStream.range(0, 10000000).parallel().map(IntUnaryOperator.identity()).collect(LinkedList::new, List::add, List::addAll);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> list = generateList();
|
||||
long start = System.nanoTime();
|
||||
int value = list.get(9500000);
|
||||
System.out.printf("Found value %d in %d nanos\n", value, (System.nanoTime() - start));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
## Relevant Articles
|
||||
- [Scoped Values in Java 20](https://www.baeldung.com/java-20-scoped-values)
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>core-java-20</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>20</source>
|
||||
<target>20</target>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
<arg>--add-modules=jdk.incubator.concurrent</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<argLine>--enable-preview --add-modules=jdk.incubator.concurrent</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>6.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.24.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>5.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.scopedvalues.classic;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Controller {
|
||||
|
||||
private final Service service = new Service();
|
||||
|
||||
public void processRequest(HttpServletRequest request, HttpServletResponse response, User loggedInUser) {
|
||||
Optional<Data> data = service.getData(request, loggedInUser);
|
||||
if (data.isPresent()) {
|
||||
try {
|
||||
PrintWriter out = response.getWriter();
|
||||
response.setContentType("application/json");
|
||||
out.print(data.get());
|
||||
out.flush();
|
||||
response.setStatus(200);
|
||||
} catch (IOException e) {
|
||||
response.setStatus(500);
|
||||
}
|
||||
} else {
|
||||
response.setStatus(400);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.scopedvalues.classic;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Repository {
|
||||
|
||||
public Optional<Data> getData(String id, User user) {
|
||||
return user.isAdmin()
|
||||
? Optional.of(new Data(id, "Title 1", "Description 1"))
|
||||
: Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.scopedvalues.classic;
|
||||
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class Server {
|
||||
|
||||
private static final List<User> AUTHENTICATED_USERS = List.of(
|
||||
new User("1", "admin", "123456", true),
|
||||
new User("2", "user", "123456", false)
|
||||
);
|
||||
private final Controller controller = new Controller();
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(5);
|
||||
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, ExecutionException {
|
||||
Optional<User> user = authenticateUser(request);
|
||||
if (user.isPresent()) {
|
||||
Future<?> future = executor.submit(() ->
|
||||
controller.processRequest(request, response, user.get()));
|
||||
future.get();
|
||||
} else {
|
||||
response.setStatus(401);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<User> authenticateUser(HttpServletRequest request) {
|
||||
return AUTHENTICATED_USERS.stream()
|
||||
.filter(user -> checkUserPassword(user, request))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private boolean checkUserPassword(User user, HttpServletRequest request) {
|
||||
return user.name().equals(request.getParameter("user_name"))
|
||||
&& user.password().equals(request.getParameter("user_pw"));
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.scopedvalues.classic;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Service {
|
||||
|
||||
private final Repository repository = new Repository();
|
||||
|
||||
public Optional<Data> getData(HttpServletRequest request, User loggedInUser) {
|
||||
String id = request.getParameter("data_id");
|
||||
return repository.getData(id, loggedInUser);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.baeldung.scopedvalues.data;
|
||||
|
||||
public record Data(String id, String title, String description) {}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.baeldung.scopedvalues.data;
|
||||
|
||||
public record User(String id, String name, String password, boolean isAdmin) {}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.scopedvalues.scoped;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jdk.incubator.concurrent.ScopedValue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Controller {
|
||||
|
||||
private final Service internalService = new Service();
|
||||
|
||||
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
|
||||
Optional<Data> data = internalService.getData(request);
|
||||
|
||||
ScopedValue.where(Server.LOGGED_IN_USER, null)
|
||||
.run(internalService::extractData);
|
||||
|
||||
if (data.isPresent()) {
|
||||
try {
|
||||
PrintWriter out = response.getWriter();
|
||||
response.setContentType("application/json");
|
||||
out.print(data.get());
|
||||
out.flush();
|
||||
response.setStatus(200);
|
||||
} catch (IOException e) {
|
||||
response.setStatus(500);
|
||||
}
|
||||
} else {
|
||||
response.setStatus(400);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.scopedvalues.scoped;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Repository {
|
||||
|
||||
public Optional<Data> getData(String id) {
|
||||
User loggedInUser = Server.LOGGED_IN_USER.get();
|
||||
return loggedInUser.isAdmin()
|
||||
? Optional.of(new Data(id, "Title 1", "Description 1"))
|
||||
: Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.scopedvalues.scoped;
|
||||
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jdk.incubator.concurrent.ScopedValue;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Server {
|
||||
|
||||
private static final List<User> AUTHENTICATED_USERS = List.of(
|
||||
new User("1", "admin", "123456", true),
|
||||
new User("2", "user", "123456", false)
|
||||
);
|
||||
public final static ScopedValue<User> LOGGED_IN_USER = ScopedValue.newInstance();
|
||||
private final Controller controller = new Controller();
|
||||
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response) {
|
||||
Optional<User> user = authenticateUser(request);
|
||||
if (user.isPresent()) {
|
||||
ScopedValue.where(LOGGED_IN_USER, user.get())
|
||||
.run(() -> controller.processRequest(request, response));
|
||||
} else {
|
||||
response.setStatus(401);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<User> authenticateUser(HttpServletRequest request) {
|
||||
return AUTHENTICATED_USERS.stream()
|
||||
.filter(user -> checkUserPassword(user, request))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private boolean checkUserPassword(User user, HttpServletRequest request) {
|
||||
return user.name().equals(request.getParameter("user_name"))
|
||||
&& user.password().equals(request.getParameter("user_pw"));
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.scopedvalues.scoped;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Service {
|
||||
|
||||
private final Repository repository = new Repository();
|
||||
|
||||
public Optional<Data> getData(HttpServletRequest request) {
|
||||
String id = request.getParameter("data_id");
|
||||
return repository.getData(id);
|
||||
}
|
||||
|
||||
public void extractData() {
|
||||
User loggedInUser = Server.LOGGED_IN_USER.get();
|
||||
assert loggedInUser == null;
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.scopedvalues.scoped.inheriting;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jdk.incubator.concurrent.StructuredTaskScope;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class Controller {
|
||||
|
||||
private final InternalService internalService = new InternalService();
|
||||
private final ExternalService externalService = new ExternalService();
|
||||
|
||||
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
|
||||
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
|
||||
Future<Optional<Data>> internalData = scope.fork(() -> internalService.getData(request));
|
||||
Future<String> externalData = scope.fork(externalService::getData);
|
||||
try {
|
||||
scope.join();
|
||||
scope.throwIfFailed();
|
||||
|
||||
Optional<Data> data = internalData.resultNow();
|
||||
if (data.isPresent()) {
|
||||
PrintWriter out = response.getWriter();
|
||||
response.setContentType("application/json");
|
||||
out.println(data.get());
|
||||
out.print(externalData.resultNow());
|
||||
out.flush();
|
||||
response.setStatus(200);
|
||||
} else {
|
||||
response.setStatus(400);
|
||||
}
|
||||
} catch (InterruptedException | ExecutionException | IOException e) {
|
||||
response.setStatus(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.scopedvalues.scoped.inheriting;
|
||||
|
||||
public class ExternalService {
|
||||
|
||||
public String getData() {
|
||||
return "External data";
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.scopedvalues.scoped.inheriting;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class InternalService {
|
||||
|
||||
private final Repository repository = new Repository();
|
||||
|
||||
public Optional<Data> getData(HttpServletRequest request) {
|
||||
String id = request.getParameter("data_id");
|
||||
return repository.getData(id);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.scopedvalues.scoped.inheriting;
|
||||
|
||||
import com.baeldung.scopedvalues.data.Data;
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Repository {
|
||||
|
||||
public Optional<Data> getData(String id) {
|
||||
User loggedInUser = Server.LOGGED_IN_USER.get();
|
||||
return loggedInUser.isAdmin()
|
||||
? Optional.of(new Data(id, "Title 1", "Description 1"))
|
||||
: Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.scopedvalues.scoped.inheriting;
|
||||
|
||||
import com.baeldung.scopedvalues.data.User;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jdk.incubator.concurrent.ScopedValue;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Server {
|
||||
|
||||
private static final List<User> AUTHENTICATED_USERS = List.of(
|
||||
new User("1", "admin", "123456", true),
|
||||
new User("2", "user", "123456", false)
|
||||
);
|
||||
public final static ScopedValue<User> LOGGED_IN_USER = ScopedValue.newInstance();
|
||||
private final Controller controller = new Controller();
|
||||
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response) {
|
||||
Optional<User> user = authenticateUser(request);
|
||||
if (user.isPresent()) {
|
||||
ScopedValue.where(LOGGED_IN_USER, user.get())
|
||||
.run(() -> controller.processRequest(request, response));
|
||||
} else {
|
||||
response.setStatus(401);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<User> authenticateUser(HttpServletRequest request) {
|
||||
return AUTHENTICATED_USERS.stream()
|
||||
.filter(user -> checkUserPassword(user, request))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private boolean checkUserPassword(User user, HttpServletRequest request) {
|
||||
return user.name().equals(request.getParameter("user_name"))
|
||||
&& user.password().equals(request.getParameter("user_pw"));
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user