Skip to content

Commit 89b5043

Browse files
Egima profilepivovarit
authored andcommitted
Java low level networking project (eugenp#770)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made
1 parent 4bae453 commit 89b5043

9 files changed

Lines changed: 485 additions & 8 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.java.networking.cookies;
2+
3+
import java.net.*;
4+
import java.util.List;
5+
6+
public class PersistentCookieStore implements CookieStore, Runnable {
7+
CookieStore store;
8+
9+
public PersistentCookieStore() {
10+
store = new CookieManager().getCookieStore();
11+
// deserialize cookies into store
12+
Runtime.getRuntime().addShutdownHook(new Thread(this));
13+
}
14+
15+
@Override
16+
public void run() {
17+
// serialize cookies to persistent storage
18+
}
19+
20+
@Override
21+
public void add(URI uri, HttpCookie cookie) {
22+
store.add(uri, cookie);
23+
24+
}
25+
26+
@Override
27+
public List<HttpCookie> get(URI uri) {
28+
// TODO Auto-generated method stub
29+
return null;
30+
}
31+
32+
@Override
33+
public List<HttpCookie> getCookies() {
34+
// TODO Auto-generated method stub
35+
return null;
36+
}
37+
38+
@Override
39+
public List<URI> getURIs() {
40+
// TODO Auto-generated method stub
41+
return null;
42+
}
43+
44+
@Override
45+
public boolean remove(URI uri, HttpCookie cookie) {
46+
// TODO Auto-generated method stub
47+
return false;
48+
}
49+
50+
@Override
51+
public boolean removeAll() {
52+
// TODO Auto-generated method stub
53+
return false;
54+
}
55+
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.java.networking.cookies;
2+
3+
import java.net.*;
4+
5+
public class ProxyAcceptCookiePolicy implements CookiePolicy {
6+
String acceptedProxy;
7+
8+
public ProxyAcceptCookiePolicy(String acceptedProxy) {
9+
this.acceptedProxy = acceptedProxy;
10+
}
11+
12+
public boolean shouldAccept(URI uri, HttpCookie cookie) {
13+
String host;
14+
try {
15+
host = InetAddress.getByName(uri.getHost()).getCanonicalHostName();
16+
} catch (UnknownHostException e) {
17+
host = uri.getHost();
18+
}
19+
20+
if (!HttpCookie.domainMatches(acceptedProxy, host)) {
21+
return false;
22+
}
23+
24+
return CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
25+
}
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.java.networking.udp;
2+
3+
import java.io.IOException;
4+
import java.net.DatagramPacket;
5+
import java.net.DatagramSocket;
6+
import java.net.InetAddress;
7+
8+
public class EchoClient {
9+
private DatagramSocket socket;
10+
private InetAddress address;
11+
12+
private byte[] buf;
13+
14+
public EchoClient() {
15+
try {
16+
socket = new DatagramSocket();
17+
address = InetAddress.getByName("localhost");
18+
} catch (IOException e) {
19+
e.printStackTrace();
20+
}
21+
22+
}
23+
24+
public String sendEcho(String msg) {
25+
DatagramPacket packet = null;
26+
try {
27+
buf=msg.getBytes();
28+
packet = new DatagramPacket(buf, buf.length, address, 4445);
29+
socket.send(packet);
30+
packet = new DatagramPacket(buf, buf.length);
31+
socket.receive(packet);
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
String received = new String(packet.getData(), 0, packet.getLength());
36+
return received;
37+
}
38+
39+
public void close() {
40+
socket.close();
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.java.networking.udp;
2+
3+
import java.io.IOException;
4+
import java.net.DatagramPacket;
5+
import java.net.DatagramSocket;
6+
import java.net.InetAddress;
7+
8+
public class EchoServer extends Thread {
9+
10+
protected DatagramSocket socket = null;
11+
protected boolean running;
12+
protected byte[] buf = new byte[256];
13+
14+
public EchoServer() throws IOException {
15+
socket = new DatagramSocket(4445);
16+
}
17+
18+
public void run() {
19+
running = true;
20+
21+
while (running) {
22+
try {
23+
24+
DatagramPacket packet = new DatagramPacket(buf, buf.length);
25+
socket.receive(packet);
26+
InetAddress address = packet.getAddress();
27+
int port = packet.getPort();
28+
packet = new DatagramPacket(buf, buf.length, address, port);
29+
String received = new String(packet.getData(), 0, packet.getLength());
30+
if (received.equals("end")) {
31+
running = false;
32+
continue;
33+
}
34+
socket.send(packet);
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
running = false;
38+
}
39+
}
40+
socket.close();
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.java.networking.udp;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class UDPTest {
12+
EchoClient client = null;
13+
14+
@Before
15+
public void setup() throws IOException {
16+
new EchoServer().start();
17+
client = new EchoClient();
18+
}
19+
20+
@Test
21+
public void whenCanSendAndReceivePacket_thenCorrect1() {
22+
String echo = client.sendEcho("hello server");
23+
assertEquals("hello server", echo);
24+
echo = client.sendEcho("server is working");
25+
assertFalse(echo.equals("hello server"));
26+
}
27+
28+
@After
29+
public void tearDown() {
30+
client.sendEcho("end");
31+
client.close();
32+
}
33+
}

‎core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.baeldung.java.nio.selector;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.net.InetSocketAddress;
6-
import java.nio.ByteBuffer;
7-
import java.nio.channels.SelectionKey;
8-
import java.nio.channels.Selector;
93
import java.nio.channels.ServerSocketChannel;
104
import java.nio.channels.SocketChannel;
11-
import java.util.Iterator;
5+
import java.nio.channels.Selector;
6+
import java.nio.channels.SelectionKey;
7+
import java.nio.ByteBuffer;
8+
import java.io.IOException;
129
import java.util.Set;
10+
import java.util.Iterator;
11+
import java.net.InetSocketAddress;
12+
import java.io.File;
1313

1414
public class EchoServer {
1515

@@ -49,6 +49,7 @@ public static void main(String[] args)
4949
}
5050
}
5151

52+
5253
public static Process start() throws IOException, InterruptedException {
5354
String javaHome = System.getProperty("java.home");
5455
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
@@ -59,4 +60,4 @@ public static Process start() throws IOException, InterruptedException {
5960

6061
return builder.start();
6162
}
62-
}
63+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.java.networking.url;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
8+
import org.junit.Test;
9+
10+
public class UrlTest {
11+
12+
@Test
13+
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException {
14+
URL url = new URL("http://baeldung.com");
15+
assertEquals("http", url.getProtocol());
16+
}
17+
18+
@Test
19+
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException {
20+
URL url = new URL("http://baeldung.com");
21+
assertEquals("baeldung.com", url.getHost());
22+
}
23+
24+
@Test
25+
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException {
26+
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
27+
assertEquals("/articles?topic=java&version=8", url.getFile());
28+
}
29+
30+
@Test
31+
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException {
32+
URL url = new URL("http://baeldung.com/guidelines.txt");
33+
assertEquals("/guidelines.txt", url.getFile());
34+
}
35+
36+
@Test
37+
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException {
38+
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
39+
assertEquals("/articles", url.getPath());
40+
}
41+
42+
@Test
43+
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException {
44+
URL url = new URL("http://baeldung.com/articles?topic=java");
45+
assertEquals("topic=java", url.getQuery());
46+
}
47+
48+
@Test
49+
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException {
50+
URL url = new URL("http://baeldung.com");
51+
assertEquals(-1, url.getPort());
52+
assertEquals(80, url.getDefaultPort());
53+
}
54+
55+
@Test
56+
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException {
57+
URL url = new URL("http://baeldung.com:8090");
58+
assertEquals(8090, url.getPort());
59+
assertEquals(80, url.getDefaultPort());
60+
}
61+
62+
@Test
63+
public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException {
64+
URL baseUrl = new URL("http://baeldung.com");
65+
URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets");
66+
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
67+
}
68+
69+
@Test
70+
public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException {
71+
URL baseUrl = new URL("http://baeldung.com");
72+
URL relativeUrl = new URL(baseUrl, "http://baeldung.com/a-guide-to-java-sockets");
73+
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
74+
}
75+
76+
@Test
77+
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
78+
String protocol = "http";
79+
String host = "baeldung.com";
80+
String file = "/guidelines.txt";
81+
URL url = new URL(protocol, host, file);
82+
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
83+
}
84+
85+
@Test
86+
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException {
87+
String protocol = "http";
88+
String host = "baeldung.com";
89+
String file = "/articles?topic=java&version=8";
90+
URL url = new URL(protocol, host, file);
91+
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
92+
}
93+
94+
@Test
95+
public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
96+
String protocol = "http";
97+
String host = "baeldung.com";
98+
int port = 9000;
99+
String file = "/guidelines.txt";
100+
URL url = new URL(protocol, host, port, file);
101+
assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString());
102+
}
103+
104+
}

‎core-java/src/test/java/com/baeldung/java/nio/selector/EchoTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException,
1616
String resp2 = client.sendMessage("world");
1717
assertEquals("hello", resp1);
1818
assertEquals("world", resp2);
19+
1920
process.destroy();
2021
}
2122
}

0 commit comments

Comments
 (0)