Skip to content

Commit 3eb57fa

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials.git
Conflicts: pdf/src/main/java/com/baeldung/pdf/PDF2HTMLExample.java pdf/src/main/java/com/baeldung/pdf/PDF2ImageExample.java pdf/src/main/java/com/baeldung/pdf/PDF2TextExample.java pdf/src/main/java/com/baeldung/pdf/PDF2WordExample.java
2 parents c4f9a6e + 8628a63 commit 3eb57fa

46 files changed

Lines changed: 1074 additions & 43 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.hexToAscii;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class HexToAscii {
8+
9+
@Test
10+
public static void whenHexToAscii() {
11+
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
12+
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
13+
14+
assertEquals(asciiString, hexToAscii(hexEquivalent));
15+
}
16+
17+
@Test
18+
public static void whenAsciiToHex() {
19+
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
20+
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
21+
22+
assertEquals(hexEquivalent, asciiToHex(asciiString));
23+
}
24+
25+
//
26+
27+
private static String asciiToHex(String asciiStr) {
28+
char[] chars = asciiStr.toCharArray();
29+
StringBuilder hex = new StringBuilder();
30+
for (char ch : chars) {
31+
hex.append(Integer.toHexString((int) ch));
32+
}
33+
34+
return hex.toString();
35+
}
36+
37+
private static String hexToAscii(String hexStr) {
38+
StringBuilder output = new StringBuilder("");
39+
for (int i = 0; i < hexStr.length(); i += 2) {
40+
String str = hexStr.substring(i, i + 2);
41+
output.append((char) Integer.parseInt(str, 16));
42+
}
43+
return output.toString();
44+
}
45+
46+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static EchoClient start() {
1717
return instance;
1818
}
1919

20+
public static void stop() throws IOException {
21+
client.close();
22+
buffer = null;
23+
}
24+
2025
private EchoClient() {
2126
try {
2227
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
@@ -42,5 +47,4 @@ public String sendMessage(String msg) {
4247
return response;
4348

4449
}
45-
4650
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
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;
39
import java.nio.channels.ServerSocketChannel;
410
import java.nio.channels.SocketChannel;
5-
import java.nio.channels.Selector;
6-
import java.nio.channels.SelectionKey;
7-
import java.nio.ByteBuffer;
8-
import java.io.IOException;
9-
import java.util.Set;
1011
import java.util.Iterator;
11-
import java.net.InetSocketAddress;
12-
import java.io.File;
12+
import java.util.Set;
1313

1414
public class EchoServer {
1515

16-
public static void main(String[] args)
17-
18-
throws IOException {
16+
public static void main(String[] args) throws IOException {
1917
Selector selector = Selector.open();
2018
ServerSocketChannel serverSocket = ServerSocketChannel.open();
2119
serverSocket.bind(new InetSocketAddress("localhost", 5454));
@@ -49,7 +47,6 @@ public static void main(String[] args)
4947
}
5048
}
5149

52-
5350
public static Process start() throws IOException, InterruptedException {
5451
String javaHome = System.getProperty("java.home");
5552
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44

55
import java.io.IOException;
66

7+
import org.junit.After;
8+
import org.junit.Before;
79
import org.junit.Test;
810

911
public class NioEchoIntegrationTest {
1012

13+
Process server;
14+
EchoClient client;
15+
16+
@Before
17+
public void setup() throws IOException, InterruptedException {
18+
server = EchoServer.start();
19+
client = EchoClient.start();
20+
}
21+
1122
@Test
12-
public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException, InterruptedException {
13-
Process process = EchoServer.start();
14-
EchoClient client = EchoClient.start();
23+
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
1524
String resp1 = client.sendMessage("hello");
1625
String resp2 = client.sendMessage("world");
1726
assertEquals("hello", resp1);
1827
assertEquals("world", resp2);
28+
}
1929

20-
process.destroy();
30+
@After
31+
public void teardown() throws IOException {
32+
server.destroy();
33+
EchoClient.stop();
2134
}
2235
}

‎enterprise-patterns/pom.xml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.enterprise.patterns</groupId>
8+
<artifactId>enterprise-patterns-parent</artifactId>
9+
<packaging>pom</packaging>
10+
<modules>
11+
<module>spring-dispatcher-servlet</module>
12+
</modules>
13+
14+
<parent>
15+
<groupId>com.baeldung</groupId>
16+
<artifactId>parent-modules</artifactId>
17+
<version>1.0.0-SNAPSHOT</version>
18+
</parent>
19+
20+
<build>
21+
<pluginManagement>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.5.1</version>
27+
<configuration>
28+
<source>1.8</source>
29+
<target>1.8</target>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</pluginManagement>
34+
</build>
35+
</project>

‎patterns/intercepting-filter/src/main/java/com/baeldung/patterns/intercepting/filter/commands/LoginCommand.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.servlet.ServletException;
44
import javax.servlet.http.HttpSession;
55
import java.io.IOException;
6+
import java.util.Optional;
67

78
public class LoginCommand extends FrontCommand {
89
@Override
@@ -12,10 +13,8 @@ public void process() throws ServletException, IOException {
1213
session.setAttribute("username", request.getParameter("username"));
1314
response.sendRedirect(request.getParameter("redirect"));
1415
} else {
15-
String queryString = request.getQueryString();
16-
if (queryString == null) {
17-
queryString = "command=Home";
18-
}
16+
String queryString = Optional.ofNullable(request.getQueryString())
17+
.orElse("command=Home");
1918
request.setAttribute("redirect", request.getRequestURL()
2019
.append("?").append(queryString).toString());
2120
forward("login");

‎patterns/intercepting-filter/src/main/java/com/baeldung/patterns/intercepting/filter/commands/LogoutCommand.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import javax.servlet.ServletException;
44
import javax.servlet.http.HttpSession;
55
import java.io.IOException;
6+
import java.util.Optional;
67

78
public class LogoutCommand extends FrontCommand {
89
@Override
910
public void process() throws ServletException, IOException {
1011
super.process();
11-
HttpSession session = request.getSession(false);
12-
session.removeAttribute("username");
13-
session.removeAttribute("order");
12+
Optional.ofNullable(request.getSession(false))
13+
.ifPresent(session -> {
14+
session.removeAttribute("username");
15+
session.removeAttribute("order");
16+
});
1417
response.sendRedirect("/?command=Home");
1518
}
1619
}

‎patterns/intercepting-filter/src/main/java/com/baeldung/patterns/intercepting/filter/commands/OrderCommand.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
import javax.servlet.ServletException;
99
import javax.servlet.http.HttpSession;
1010
import java.io.IOException;
11+
import java.util.Optional;
1112

1213
public class OrderCommand extends FrontCommand {
1314
@Override
1415
public void process() throws ServletException, IOException {
1516
super.process();
1617
if (request.getMethod().equals("POST")) {
1718
HttpSession session = request.getSession(false);
18-
Order order = (Order) session.getAttribute("order");
19-
if (order == null) {
20-
String username = (String) session.getAttribute("username");
21-
order = new OrderImpl(username);
22-
}
19+
Order order = Optional
20+
.ofNullable(session.getAttribute("order"))
21+
.map(Order.class::cast)
22+
.orElseGet(() -> new OrderImpl((String) session.getAttribute("username")));
2323
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
2424
.getAttribute("bookshelf");
2525
String isbn = request.getParameter("isbn");

‎patterns/intercepting-filter/src/main/java/com/baeldung/patterns/intercepting/filter/filters/EncodingFilter.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javax.servlet.annotation.WebFilter;
55
import javax.servlet.annotation.WebInitParam;
66
import java.io.IOException;
7+
import java.util.Optional;
78

89
@WebFilter(
910
servletNames = {"intercepting-filter"},
@@ -24,10 +25,9 @@ public void doFilter(
2425
ServletResponse response,
2526
FilterChain chain
2627
) throws IOException, ServletException {
27-
String encoding = request.getParameter("encoding");
28-
if (encoding == null) {
29-
encoding = this.encoding;
30-
}
28+
String encoding = Optional
29+
.ofNullable(request.getParameter("encoding"))
30+
.orElse(this.encoding);
3131
response.setCharacterEncoding(encoding);
3232
chain.doFilter(request, response);
3333
}

‎patterns/intercepting-filter/src/main/java/com/baeldung/patterns/intercepting/filter/filters/LoggingFilter.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.servlet.annotation.WebFilter;
1111
import javax.servlet.http.HttpServletRequest;
1212
import java.io.IOException;
13+
import java.util.Optional;
1314

1415
@WebFilter(servletNames = "intercepting-filter")
1516
public class LoggingFilter extends BaseFilter {
@@ -23,10 +24,10 @@ public void doFilter(
2324
) throws IOException, ServletException {
2425
chain.doFilter(request, response);
2526
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
26-
String username = (String) httpServletRequest.getAttribute("username");
27-
if (username == null) {
28-
username = "guest";
29-
}
27+
String username = Optional
28+
.ofNullable(httpServletRequest.getAttribute("username"))
29+
.map(Object::toString)
30+
.orElse("guest");
3031
log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(),
3132
httpServletRequest.getRequestURI(), request.getParameterMap());
3233
}

0 commit comments

Comments
 (0)