Skip to content

Commit 092a238

Browse files
authored
Merge branch 'eugenp:master' into spring-web-test1
2 parents 5a1e8c4 + 806cf30 commit 092a238

File tree

71 files changed

+1752
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1752
-67
lines changed

apache-httpclient/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<plugin>
8686
<groupId>org.apache.maven.plugins</groupId>
8787
<artifactId>maven-surefire-plugin</artifactId>
88+
<version>${maven-surefire-plugin.version}</version>
8889
<configuration>
8990
<systemPropertyVariables>
9091
<mockserver.logLevel>ERROR</mockserver.logLevel>
@@ -101,7 +102,7 @@
101102
</build>
102103

103104
<properties>
104-
<mockserver.version>5.6.1</mockserver.version>
105+
<mockserver.version>5.11.2</mockserver.version>
105106
<wiremock.version>3.9.1</wiremock.version>
106107
<!-- http client & core 5 -->
107108
<httpcore5.version>5.2.5</httpcore5.version>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mockserver.logLevel=OFF

core-java-modules/core-java-date-operations/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</dependencies>
3434

3535
<build>
36-
<finalName>core-java-date-operations-1</finalName>
36+
<finalName>core-java-date-operations</finalName>
3737
<resources>
3838
<resource>
3939
<directory>src/main/resources</directory>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.socketchannel;
2+
3+
import java.io.*;
4+
import java.net.InetSocketAddress;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.ServerSocketChannel;
7+
import java.nio.channels.SocketChannel;
8+
9+
public class BlockingServer {
10+
private static final int PORT = 6000;
11+
12+
public static void main(String[] args) {
13+
try (ServerSocketChannel serverSocket = ServerSocketChannel.open()) {
14+
serverSocket.bind(new InetSocketAddress(PORT));
15+
System.out.println("Blocking Server listening on port " + PORT);
16+
17+
while (true) {
18+
try (SocketChannel clientSocket = serverSocket.accept()) {
19+
System.out.println("Client connected");
20+
MyObject obj = receiveObject(clientSocket);
21+
System.out.println("Received: " + obj.getName() + ", " + obj.getAge());
22+
} catch (ClassNotFoundException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
private static MyObject receiveObject(SocketChannel channel)
32+
throws IOException, ClassNotFoundException {
33+
ByteBuffer buffer = ByteBuffer.allocate(1024);
34+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
35+
36+
int bytesRead;
37+
while ((bytesRead = channel.read(buffer)) > 0) {
38+
buffer.flip();
39+
byteStream.write(buffer.array(), 0, buffer.limit());
40+
buffer.clear();
41+
}
42+
43+
byte[] bytes = byteStream.toByteArray();
44+
try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
45+
return (MyObject) objIn.readObject();
46+
}
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.socketchannel;
2+
3+
import java.io.*;
4+
import java.net.InetSocketAddress;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.SocketChannel;
7+
8+
public class Client {
9+
private static final String SERVER_ADDRESS = "localhost";
10+
private static final int SERVER_PORT = 6000;
11+
12+
public static void main(String[] args) {
13+
try (SocketChannel socketChannel = SocketChannel.open()) {
14+
socketChannel.connect(new InetSocketAddress(SERVER_ADDRESS, SERVER_PORT));
15+
System.out.println("Connected to server");
16+
17+
MyObject objectToSend = new MyObject("Alice", 25);
18+
sendObject(socketChannel, objectToSend);
19+
System.out.println("Object sent");
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
private static void sendObject(SocketChannel channel, MyObject obj)
26+
throws IOException {
27+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
28+
try (ObjectOutputStream objOut = new ObjectOutputStream(byteStream)) {
29+
objOut.writeObject(obj);
30+
}
31+
byte[] bytes = byteStream.toByteArray();
32+
33+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
34+
while (buffer.hasRemaining()) {
35+
channel.write(buffer);
36+
}
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.socketchannel;
2+
3+
import java.io.Serializable;
4+
5+
class MyObject implements Serializable {
6+
7+
private String name;
8+
private int age;
9+
10+
public MyObject(String name, int age) {
11+
this.name = name;
12+
this.age = age;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public int getAge() {
20+
return age;
21+
}
22+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.socketchannel;
2+
3+
import java.io.*;
4+
import java.net.InetSocketAddress;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.*;
7+
import java.util.Iterator;
8+
import java.util.Set;
9+
10+
public class NonBlockingServer {
11+
private static final int PORT = 6000;
12+
13+
public static void main(String[] args) throws IOException {
14+
ServerSocketChannel serverChannel = ServerSocketChannel.open();
15+
serverChannel.bind(new InetSocketAddress(PORT));
16+
serverChannel.configureBlocking(false);
17+
18+
Selector selector = Selector.open();
19+
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
20+
21+
System.out.println("Non-blocking Server listening on port " + PORT);
22+
23+
while (true) {
24+
selector.select();
25+
Set<SelectionKey> keys = selector.selectedKeys();
26+
Iterator<SelectionKey> iter = keys.iterator();
27+
28+
while (iter.hasNext()) {
29+
SelectionKey key = iter.next();
30+
iter.remove();
31+
32+
if (key.isAcceptable()) {
33+
SocketChannel client = serverChannel.accept();
34+
client.configureBlocking(false);
35+
client.register(selector, SelectionKey.OP_READ);
36+
System.out.println("New client connected");
37+
}
38+
else if (key.isReadable()) {
39+
SocketChannel client = (SocketChannel) key.channel();
40+
try {
41+
MyObject obj = receiveObject(client);
42+
if (obj != null) {
43+
System.out.println("Received: " + obj.getName() + ", " + obj.getAge());
44+
}
45+
} catch (ClassNotFoundException e) {
46+
e.printStackTrace();
47+
client.close();
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
private static MyObject receiveObject(SocketChannel channel)
55+
throws IOException, ClassNotFoundException {
56+
ByteBuffer buffer = ByteBuffer.allocate(1024);
57+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
58+
59+
int bytesRead;
60+
while ((bytesRead = channel.read(buffer)) > 0) {
61+
buffer.flip();
62+
byteStream.write(buffer.array(), 0, buffer.limit());
63+
buffer.clear();
64+
}
65+
66+
if (bytesRead == -1) {
67+
channel.close();
68+
return null;
69+
}
70+
71+
byte[] bytes = byteStream.toByteArray();
72+
try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
73+
return (MyObject) objIn.readObject();
74+
}
75+
}
76+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.socketchannel;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
import java.net.InetSocketAddress;
9+
import java.nio.channels.Channels;
10+
import java.nio.channels.ServerSocketChannel;
11+
import java.nio.channels.SocketChannel;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
import java.util.concurrent.Future;
15+
import java.util.concurrent.TimeUnit;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
public class SocketChannelUnitTest {
20+
21+
@Test
22+
void givenServerStarted_whenClientConnects_thenConnectionIsSuccessful() throws Exception {
23+
try (ServerSocketChannel server = ServerSocketChannel.open()
24+
.bind(new InetSocketAddress(6000))) {
25+
int port = ((InetSocketAddress) server.getLocalAddress()).getPort();
26+
ExecutorService executor = Executors.newSingleThreadExecutor();
27+
Future<Boolean> future = executor.submit(() -> {
28+
try (SocketChannel client = server.accept()) {
29+
return client.isConnected();
30+
}
31+
});
32+
try (SocketChannel client = SocketChannel.open()) {
33+
client.configureBlocking(true);
34+
client.connect(new InetSocketAddress("localhost", 6000));
35+
while (!client.finishConnect()) {
36+
Thread.sleep(10);
37+
}
38+
}
39+
assertTrue(future.get(2, TimeUnit.SECONDS));
40+
executor.shutdown();
41+
}
42+
}
43+
44+
@Test
45+
void givenClientSendsObject_whenServerReceives_thenDataMatches() throws Exception {
46+
try (ServerSocketChannel server = ServerSocketChannel.open()
47+
.bind(new InetSocketAddress(6000))) {
48+
int port = ((InetSocketAddress) server.getLocalAddress()).getPort();
49+
ExecutorService executor = Executors.newSingleThreadExecutor();
50+
Future<MyObject> future = executor.submit(() -> {
51+
try (SocketChannel client = server.accept(); ObjectInputStream objIn = new ObjectInputStream(Channels.newInputStream(client))) {
52+
return (MyObject) objIn.readObject();
53+
}
54+
});
55+
56+
try (SocketChannel client = SocketChannel.open()) {
57+
client.configureBlocking(true);
58+
client.connect(new InetSocketAddress("localhost", 6000));
59+
60+
// Ensure connection is fully established before writing
61+
while (!client.finishConnect()) {
62+
Thread.sleep(10);
63+
}
64+
65+
try (ObjectOutputStream objOut = new ObjectOutputStream(Channels.newOutputStream(client))) {
66+
objOut.writeObject(new MyObject("Test User", 25));
67+
}
68+
}
69+
70+
MyObject received = future.get(2, TimeUnit.SECONDS);
71+
assertEquals("Test User", received.getName());
72+
assertEquals(25, received.getAge());
73+
executor.shutdown();
74+
}
75+
}
76+
}

core-java-modules/core-java-security-algorithms/src/main/java/com/baeldung/aes/AESUtil.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.crypto.KeyGenerator;
99
import javax.crypto.SecretKeyFactory;
1010
import javax.crypto.SealedObject;
11+
import javax.crypto.spec.GCMParameterSpec;
1112
import javax.crypto.spec.IvParameterSpec;
1213
import javax.crypto.spec.PBEKeySpec;
1314
import javax.crypto.spec.SecretKeySpec;
@@ -26,7 +27,7 @@
2627

2728
public class AESUtil {
2829

29-
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv)
30+
public static String encrypt(String algorithm, String input, SecretKey key, GCMParameterSpec iv)
3031
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
3132
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
3233
Cipher cipher = Cipher.getInstance(algorithm);
@@ -36,7 +37,7 @@ public static String encrypt(String algorithm, String input, SecretKey key, IvPa
3637
.encodeToString(cipherText);
3738
}
3839

39-
public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv)
40+
public static String decrypt(String algorithm, String cipherText, SecretKey key, GCMParameterSpec iv)
4041
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
4142
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
4243
Cipher cipher = Cipher.getInstance(algorithm);
@@ -62,13 +63,13 @@ public static SecretKey getKeyFromPassword(String password, String salt)
6263
return secret;
6364
}
6465

65-
public static IvParameterSpec generateIv() {
66-
byte[] iv = new byte[16];
66+
public static GCMParameterSpec generateIv() {
67+
byte[] iv = new byte[12];
6768
new SecureRandom().nextBytes(iv);
68-
return new IvParameterSpec(iv);
69+
return new GCMParameterSpec(128, iv);
6970
}
7071

71-
public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
72+
public static void encryptFile(String algorithm, SecretKey key, GCMParameterSpec iv,
7273
File inputFile, File outputFile) throws IOException, NoSuchPaddingException,
7374
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
7475
BadPaddingException, IllegalBlockSizeException {
@@ -92,7 +93,7 @@ public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec
9293
outputStream.close();
9394
}
9495

95-
public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
96+
public static void decryptFile(String algorithm, SecretKey key, GCMParameterSpec iv,
9697
File encryptedFile, File decryptedFile) throws IOException, NoSuchPaddingException,
9798
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
9899
BadPaddingException, IllegalBlockSizeException {
@@ -117,7 +118,7 @@ public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec
117118
}
118119

119120
public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key,
120-
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
121+
GCMParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
121122
InvalidAlgorithmParameterException, InvalidKeyException, IOException, IllegalBlockSizeException {
122123
Cipher cipher = Cipher.getInstance(algorithm);
123124
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
@@ -126,7 +127,7 @@ public static SealedObject encryptObject(String algorithm, Serializable object,
126127
}
127128

128129
public static Serializable decryptObject(String algorithm, SealedObject sealedObject, SecretKey key,
129-
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
130+
GCMParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
130131
InvalidAlgorithmParameterException, InvalidKeyException, ClassNotFoundException,
131132
BadPaddingException, IllegalBlockSizeException, IOException {
132133
Cipher cipher = Cipher.getInstance(algorithm);
@@ -135,19 +136,19 @@ public static Serializable decryptObject(String algorithm, SealedObject sealedOb
135136
return unsealObject;
136137
}
137138

138-
public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv)
139+
public static String encryptPasswordBased(String plainText, SecretKey key, GCMParameterSpec iv)
139140
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
140141
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
141-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
142+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
142143
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
143144
return Base64.getEncoder()
144145
.encodeToString(cipher.doFinal(plainText.getBytes()));
145146
}
146147

147-
public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv)
148+
public static String decryptPasswordBased(String cipherText, SecretKey key, GCMParameterSpec iv)
148149
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
149150
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
150-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
151+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
151152
cipher.init(Cipher.DECRYPT_MODE, key, iv);
152153
return new String(cipher.doFinal(Base64.getDecoder()
153154
.decode(cipherText)));

0 commit comments

Comments
 (0)