Merge pull request #46 from eugenp/master

update
This commit is contained in:
Maiklins 2020-04-19 22:47:53 +02:00 committed by GitHub
commit a8fe0af8ac
196 changed files with 2648 additions and 251 deletions

View File

@ -1,3 +1,7 @@
### Relevant Articles:
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [How an In-Place Sorting Algorithm Works](https://www.baeldung.com/java-in-place-sorting)
- [Partitioning and Sorting Arrays with Many Repeated Entries](https://www.baeldung.com/java-sorting-arrays-with-repeated-entries)
- More articles: [[<-- prev]](/algorithms-sorting)

View File

@ -1,4 +1,4 @@
package com.baeldung.algorithms.sort.bynumber;
package com.baeldung.algorithms.bynumber;
import java.util.Comparator;

View File

@ -1,6 +1,5 @@
package com.baeldung.algorithms.sort.bynumber;
package com.baeldung.algorithms.bynumber;
import com.baeldung.algorithms.sort.bynumber.NaturalOrderComparators;
import org.junit.Test;
import java.util.ArrayList;

View File

@ -11,10 +11,7 @@ This module contains articles about sorting algorithms.
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [How an In-Place Sorting Algorithm Works](https://www.baeldung.com/java-in-place-sorting)
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- [Bucket Sort in Java](https://www.baeldung.com/java-bucket-sort)
- More articles: [[next -->]](/algorithms-sorintg-2)

View File

@ -1,7 +1,18 @@
## CAS
This module contains articles about the Central Authentication Service (CAS)
This module contains articles about the Central Authentication Service (CAS).
The module consists of 2 submodules:
1. `cas-server` - it requires JDK11 and uses the Gradle War Overlay style to ease setup and deployment. To start the server, simply run:
`./gradlew run
-Dorg.gradle.java.home=$JAVA11_HOME
-Pargs="-Dcas.standalone.configurationDirectory=/cas-server/src/main/resources/etc/cas/config"`
The server starts at https://localhost:8443. `casuser`/`Mellon` are the username and password for logging in.
2. `cas-secured-app` - A Maven based Springboot Application
### Relevant Articles:
- [CAS SSO With Spring Security](baeldung.com/spring-security-cas-sso)
- [CAS SSO With Spring Security](https://www.baeldung.com/spring-security-cas-sso)

View File

View File

View File

@ -0,0 +1,5 @@
### Relevant Articles:
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
- [Creating a LocalDate with Values in Java](https://www.baeldung.com/java-creating-localdate-with-values)
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)

View File

@ -4,9 +4,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>
<artifactId>core-java-datetime-java8</artifactId>
<artifactId>core-java-8-datetime</artifactId>
<version>${project.parent.version}</version>
<name>core-java-datetime-java8</name>
<name>core-java-8-datetime</name>
<packaging>jar</packaging>
<parent>
@ -64,8 +64,8 @@
</build>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<joda-time.version>2.10</joda-time.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>

View File

@ -11,31 +11,31 @@ public class LocalDateExampleUnitTest {
@Test
public void givenValues_whenUsingOfMethod_thenLocalDate() {
assertEquals("2020-01-08", date.getCustomDateOne(2020, 1, 8));
assertEquals("2020-01-08", date.getCustomDateOne(2020, 1, 8).toString());
}
@Test
public void givenValuesWithMonthEnum_whenUsingOfMethod_thenLocalDate() {
assertEquals("2020-01-08", date.getCustomDateTwo(2020, Month.JANUARY, 8));
assertEquals("2020-01-08", date.getCustomDateTwo(2020, Month.JANUARY, 8).toString());
}
@Test
public void givenValues_whenUsingEpochDay_thenLocalDate() {
assertEquals("2020-01-08", date.getDateFromEpochDay(18269));
assertEquals("2020-01-08", date.getDateFromEpochDay(18269).toString());
}
@Test
public void givenValues_whenUsingYearDay_thenLocalDate() {
assertEquals("2020-01-08", date.getDateFromYearAndDayOfYear(2020, 8));
assertEquals("2020-01-08", date.getDateFromYearAndDayOfYear(2020, 8).toString());
}
@Test
public void givenValues_whenUsingParse_thenLocalDate() {
assertEquals("2020-01-08", date.getDateFromString("2020-01-08"));
assertEquals("2020-01-08", date.getDateFromString("2020-01-08").toString());
}
@Test
public void givenValuesWithFormatter_whenUsingParse_thenLocalDate() {
assertEquals("2020-01-08", date.getDateFromStringAndFormatter("8-Jan-2020", "d-MMM-yyyy"));
assertEquals("2020-01-08", date.getDateFromStringAndFormatter("8-Jan-2020", "d-MMM-yyyy").toString());
}
}

View File

@ -13,4 +13,4 @@ This module contains articles about the Date and Time API introduced with Java 8
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
- [[Next -->]](/core-java-modules/core-java-datetime-java8-2)

View File

@ -4,9 +4,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>
<artifactId>core-java-datetime-java8</artifactId>
<artifactId>core-java-8-datetime</artifactId>
<version>${project.parent.version}</version>
<name>core-java-datetime-java8</name>
<name>core-java-8-datetime</name>
<packaging>jar</packaging>
<parent>
@ -64,8 +64,8 @@
</build>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<joda-time.version>2.10</joda-time.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>

View File

@ -0,0 +1,39 @@
package com.baeldung.map.computeifabsent;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ComputeIfAbsentUnitTest {
@Test
public void whenKeyIsPresent_thenFetchTheValue() {
Map<String, Integer> stringLength = new HashMap<>();
stringLength.put("John", 5);
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 5);
}
@Test
public void whenKeyIsNotPresent_thenComputeTheValueUsingMappingFunctionAndStore() {
Map<String, Integer> stringLength = new HashMap<>();
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 4);
assertEquals((long)stringLength.get("John"), 4);
}
@Test
public void whenMappingFunctionReturnsNull_thenDoNotRecordMapping() {
Map<String, Integer> stringLength = new HashMap<>();
assertEquals(stringLength.computeIfAbsent("John", s -> null), null);
assertNull(stringLength.get("John"));
}
@Test(expected = RuntimeException.class)
public void whenMappingFunctionThrowsException_thenExceptionIsRethrown() {
Map<String, Integer> stringLength = new HashMap<>();
stringLength.computeIfAbsent("John", s -> {throw new RuntimeException();});
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.atomicstampedreference;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
public class StampedAccount {
private AtomicInteger stamp = new AtomicInteger(0);
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
public int getBalance() {
return this.account.get(new int[1]);
}
public int getStamp() {
int[] stamps = new int[1];
this.account.get(stamps);
return stamps[0];
}
public boolean deposit(int funds) {
int[] stamps = new int[1];
int current = this.account.get(stamps);
int newStamp = this.stamp.incrementAndGet();
return this.account.compareAndSet(current, current + funds, stamps[0], newStamp);
}
public boolean withdrawal(int funds) {
int[] stamps = new int[1];
int current = this.account.get(stamps);
int newStamp = this.stamp.incrementAndGet();
return this.account.compareAndSet(current, current - funds, stamps[0], newStamp);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.atomicstampedreference;
import org.junit.Assert;
import org.junit.Test;
public class ThreadStampedAccountUnitTest {
@Test
public void givenMultiThread_whenStampedAccount_thenSetBalance() throws InterruptedException {
StampedAccount account = new StampedAccount();
Thread t = new Thread(() -> {
while (!account.withdrawal(100))
Thread.yield();
});
t.start();
Assert.assertTrue(account.deposit(100));
t.join(1_000);
Assert.assertFalse(t.isAlive());
Assert.assertSame(0, account.getBalance());
}
}

View File

@ -9,7 +9,7 @@ import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCount
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.AtomicCounter;
import com.baeldung.concurrent.threadsafety.services.Counter;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import com.baeldung.concurrent.threadsafety.services.MessageService;
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
@ -48,9 +48,9 @@ public class Application {
System.out.println(future3.get());
System.out.println(future4.get());
ExtrinsicLockCounter extrinsicLockCounter = new ExtrinsicLockCounter();
Future<Integer> future5 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
Future<Integer> future6 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
ObjectLockCounter objectLockCounter = new ObjectLockCounter();
Future<Integer> future5 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(objectLockCounter));
Future<Integer> future6 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(objectLockCounter));
System.out.println(future5.get());
System.out.println(future6.get());

View File

@ -1,13 +1,13 @@
package com.baeldung.concurrent.threadsafety.callables;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import java.util.concurrent.Callable;
public class ExtrinsicLockCounterCallable implements Callable<Integer> {
private final ExtrinsicLockCounter counter;
private final ObjectLockCounter counter;
public ExtrinsicLockCounterCallable(ExtrinsicLockCounter counter) {
public ExtrinsicLockCounterCallable(ObjectLockCounter counter) {
this.counter = counter;
}

View File

@ -1,11 +1,11 @@
package com.baeldung.concurrent.threadsafety.services;
public class ExtrinsicLockCounter {
public class ObjectLockCounter {
private int counter;
private final Object lock = new Object();
public ExtrinsicLockCounter() {
public ObjectLockCounter() {
this.counter = 0;
}

View File

@ -1,7 +1,7 @@
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
@ -10,12 +10,12 @@ import java.util.concurrent.Future;
import static org.assertj.core.api.Assertions.assertThat;
public class ExtrinsicLockCounterUnitTest {
public class ObjectLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
ExtrinsicLockCounter counter = new ExtrinsicLockCounter();
ObjectLockCounter counter = new ObjectLockCounter();
Future<Integer> future1 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(counter));
Future<Integer> future2 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(counter));

View File

@ -1,3 +0,0 @@
### Relevant Articles:
- [Creating a LocalDate with Values in Java](https://www.baeldung.com/java-creating-localdate-with-values)

View File

@ -0,0 +1,9 @@
## Core Java IO Conversions (Part 2)
This module contains articles about core Java input/output(IO) conversions.
### Relevant Articles:
- [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
- [Java Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file)
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)

View File

@ -0,0 +1,37 @@
<?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>
<artifactId>core-java-io-conversions-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-io-conversions-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-io-conversions</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1 @@
Hello World

View File

@ -3,10 +3,8 @@
This module contains articles about core Java input/output(IO) conversions.
### Relevant Articles:
- [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string)
- [Java Convert File to InputStream](https://www.baeldung.com/convert-file-to-input-stream)
- [Java Byte Array to Writer](https://www.baeldung.com/java-convert-byte-array-to-writer)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
- [Java String to Reader](https://www.baeldung.com/java-convert-string-to-reader)
- [Java Byte Array to Reader](https://www.baeldung.com/java-convert-byte-array-to-reader)
- [Java File to Reader](https://www.baeldung.com/java-convert-file-to-reader)
@ -15,3 +13,4 @@ This module contains articles about core Java input/output(IO) conversions.
- [Java Write a Reader to File](https://www.baeldung.com/java-write-reader-to-file)
- [Java Reader to Byte Array](https://www.baeldung.com/java-convert-reader-to-byte-array)
- [Java Reader to InputStream](https://www.baeldung.com/java-convert-reader-to-inputstream)
- More articles: [[next -->]](/core-java-modules/core-java-io-conversions-2)

View File

@ -0,0 +1,24 @@
package com.baeldung.connectexception;
import java.net.ConnectException;
import java.net.Socket;
public class ConnectionChecker {
public static void main(String[] args) {
String host = "localhost";
int port = 5000;
try {
Socket clientSocket = new Socket(host, port);
// successfully connected to host, do something with opened socket
clientSocket.close();
} catch (ConnectException e) {
// host and port combination not valid
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -87,6 +87,15 @@ public class OptionalUnitTest {
String name = opt.get();
}
@Test
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
Optional<String> opt = Optional.of("Baeldung");
assertTrue(opt.isPresent());
opt = Optional.ofNullable(null);
assertFalse(opt.isPresent());
}
// Conditional Return With filter()
@Test
public void whenOptionalFilterWorks_thenCorrect() {

View File

@ -0,0 +1,30 @@
package com.baeldung.streams.closure;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
/**
* Contains a couple of simple stream API usages.
*/
public class StreamClosureSnippets {
public static void main(String[] args) throws IOException {
// Collection based streams shouldn't be closed
Arrays.asList("Red", "Blue", "Green")
.stream()
.filter(c -> c.length() > 4)
.map(String::toUpperCase)
.forEach(System.out::print);
String[] colors = {"Red", "Blue", "Green"};
Arrays.stream(colors).map(String::toUpperCase).forEach(System.out::println);
// IO-Based Streams Should be Closed via Try with Resources
try (Stream<String> lines = Files.lines(Paths.get("/path/tp/file"))) {
// lines will be closed after exiting the try block
}
}
}

View File

@ -69,6 +69,7 @@
<module>core-java-io-2</module>
<module>core-java-io-apis</module>
<module>core-java-io-conversions</module>
<module>core-java-io-conversions-2</module>
<module>core-java-jar</module>
<module>core-java-jndi</module>

View File

@ -5,7 +5,7 @@
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.dddmodules</groupId>
<artifactId>dddmodules</artifactId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<name>ddd-modules</name>
<packaging>pom</packaging>

View File

@ -1,6 +1,7 @@
package com.baeldung.jackson.date;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@ -8,6 +9,8 @@ import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;
@ -30,7 +33,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class JacksonDateUnitTest {
@Test
public void whenSerializingDateWithJackson_thenSerializedToNumber() throws JsonProcessingException, ParseException {
public void whenSerializingDateWithJackson_thenSerializedToTimestamp() throws JsonProcessingException, ParseException {
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
@ -62,6 +65,21 @@ public class JacksonDateUnitTest {
assertThat(result, containsString("1970-01-01T02:30:00.000+00:00"));
}
@Test
public void whenDeserialisingZonedDateTimeWithDefaults_thenNotCorrect()
throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
String converted = objectMapper.writeValueAsString(now);
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
System.out.println("serialized: " + now);
System.out.println("restored: " + restored);
assertThat(now, is(restored));
}
@Test
public void whenSettingObjectMapperDateFormat_thenCorrect() throws JsonProcessingException, ParseException {
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm");

View File

@ -2,9 +2,9 @@
<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>
<artifactId>JGit</artifactId>
<artifactId>jgit</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JGit</name>
<name>jgit</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>

View File

@ -14,21 +14,6 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<repositories>
<repository>
<id>OpenNMS Repository</id>
<url>https://repo.opennms.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.samples.jnlp</groupId>
<artifactId>jnlp-servlet</artifactId>
<version>${jnlp-servlet.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
@ -79,7 +64,6 @@
<properties>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<jnlp-servlet.version>1.6.0</jnlp-servlet.version>
</properties>
</project>

View File

@ -14,11 +14,9 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
- [Guide to Java Parallel Collectors Library](https://www.baeldung.com/java-parallel-collectors)
- [Templating with Handlebars](https://www.baeldung.com/handlebars)
- [A Guide to Crawler4j](https://www.baeldung.com/crawler4j)
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
- [Key Value Store with Chronicle Map](https://www.baeldung.com/java-chronicle-map)
- [Guide to MapDB](https://www.baeldung.com/mapdb)
- [A Guide to Apache Mesos](https://www.baeldung.com/apache-mesos)
- [JasperReports with Spring](https://www.baeldung.com/spring-jasper)
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
- More articles [[<-- prev]](/libraries)

View File

@ -75,28 +75,7 @@
</exclusion>
</exclusions>
</dependency>
<!-- Dependencies for response decoder with okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${mockwebserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>edu.uci.ics</groupId>
<artifactId>crawler4j</artifactId>
@ -151,9 +130,6 @@
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
<mesos.library.version>0.28.3</mesos.library.version>
<parallel-collectors.version>1.1.0</parallel-collectors.version>
<okhttp.version>3.14.2</okhttp.version>
<gson.version>2.8.5</gson.version>
<mockwebserver.version>3.14.2</mockwebserver.version>
<handlebars.version>4.1.2</handlebars.version>
<jasperreports.version>6.6.0</jasperreports.version>
<spring.version>5.1.9.RELEASE</spring.version>

View File

@ -0,0 +1,10 @@
## HTTP
This module contains articles about HTTP libraries.
### Relevant Articles:
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
- More articles [[<-- prev]](/libraries-http)

47
libraries-http-2/pom.xml Normal file
View File

@ -0,0 +1,47 @@
<?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>
<artifactId>libraries-http-2</artifactId>
<name>libraries-http-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependencies for response decoder with okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${mockwebserver.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<okhttp.version>3.14.2</okhttp.version>
<gson.version>2.8.5</gson.version>
<mockwebserver.version>3.14.2</mockwebserver.version>
<jackson.version>2.9.8</jackson.version>
</properties>
</project>

View File

@ -14,3 +14,4 @@ This module contains articles about HTTP libraries.
- [Creating REST Microservices with Javalin](https://www.baeldung.com/javalin-rest-microservices)
- [A Quick Guide to Timeouts in OkHttp](https://www.baeldung.com/okhttp-timeouts)
- [A Quick Guide to Post Requests with OkHttp](https://www.baeldung.com/okhttp-post)
- More articles [[next -->]](/libraries-http-2)

View File

@ -13,3 +13,4 @@ This module contains articles about server libraries.
- [MQTT Client in Java](https://www.baeldung.com/java-mqtt-client)
- [Guide to XMPP Smack Client](https://www.baeldung.com/xmpp-smack-chat-client)
- [A Guide to NanoHTTPD](https://www.baeldung.com/nanohttpd)
- [HTTP/2 in Jetty](https://www.baeldung.com/jetty-http-2)

View File

@ -5,6 +5,7 @@
<artifactId>libraries-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>libraries-server</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
@ -106,11 +107,50 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<stopPort>8888</stopPort>
<stopKey>quit</stopKey>
<jvmArgs>
-Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/alpn/alpn-boot/${alpn.version}/alpn-boot-${alpn.version}.jar
</jvmArgs>
<jettyXml>${basedir}/src/main/config/jetty.xml</jettyXml>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-alpn-openjdk8-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<assertj.version>3.6.2</assertj.version>
<httpclient.version>4.5.3</httpclient.version>
<jetty.version>9.4.8.v20171121</jetty.version>
<jetty.version>9.4.27.v20200227</jetty.version>
<netty.version>4.1.20.Final</netty.version>
<alpn.version>8.1.11.v20170118</alpn.version>
<tomcat.version>8.5.24</tomcat.version>
<smack.version>4.3.1</smack.version>
<eclipse.paho.client.mqttv3.version>1.2.0</eclipse.paho.client.mqttv3.version>

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="keyStorePath">src/main/resources/keystore.jks</Set>
<Set name="keyStorePassword">storepwd</Set>
<Set name="trustStorePath">src/main/resources/truststore.jks</Set>
<Set name="trustStorePassword">storepwd</Set>
<Set name="protocol">TLSv1.2</Set>
</New>
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration"/>
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server">
<Ref id="Server"/>
</Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.SslConnectionFactory">
<Arg name="sslContextFactory">
<Ref id="sslContextFactory"/>
</Arg>
<Arg name="next">http/1.1</Arg>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config">
<Ref id="httpConfig"/>
</Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="port">8443</Set>
</New>
</Arg>
</Call>
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server">
<Ref id="Server"/>
</Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.SslConnectionFactory">
<Arg name="sslContextFactory">
<Ref id="sslContextFactory"/>
</Arg>
<Arg name="next">alpn</Arg>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory">
<Arg>h2,h2-17</Arg>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory">
<Arg name="config">
<Ref id="httpConfig"/>
</Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="Port">8444</Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,29 @@
package com.baeldung.jetty.http2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Http2JettyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.addHeader("Cache-control", "no-store, no-cache, must-revalidate");
response.addDateHeader("Last-Modified", 0);
response.addDateHeader("Expires", 0);
String requestPath = request.getRequestURI();
InputStream input = getServletContext().getResourceAsStream(requestPath);
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) >= 0) {
output.write(buffer, 0, read);
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>http2Jetty</servlet-name>
<servlet-class>com.baeldung.jetty.http2.Http2JettyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>http2Jetty</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>push</filter-name>
<filter-class>org.eclipse.jetty.servlets.PushCacheFilter</filter-class>
<init-param>
<param-name>ports</param-name>
<param-value>8444</param-value>
</init-param>
<init-param>
<param-name>maxAssociations</param-name>
<param-value>32</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>push</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Baeldung HTTP/2 Client in Jetty</title>
</head>
<body>
<h2>HTTP/2 Demo</h2>
<div>
<img src="images/homepage-latest_articles.jpg" alt="latest articles" />
<img src="images/homepage-rest_with_spring.jpg" alt="rest with spring" />
<img src="images/homepage-weekly_reviews.jpg" alt="weekly reviews" />
</div>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Baeldung: HTTP2 Client in Jetty</title>
</head>
<body style="margin:100px 200px">
<a href="https://localhost:8443/http2.html"><h1>HTTP/1.1</h1></a>
<br />
<a href="https://localhost:8444/http2.html"><h1>HTTP/2 Push</h1></a>
<br />
</body>
</html>

View File

@ -6,7 +6,7 @@
<groupId>com.baeldung.examples.r2dbc</groupId>
<artifactId>r2dbc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>r2dbc-example</name>
<name>r2dbc</name>
<description>Sample R2DBC Project</description>
<parent>

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.sirix</groupId>
<artifactId>core-api-tutorial</artifactId>
<artifactId>sirix</artifactId>
<version>1.0-SNAPSHOT</version>
<name>core-api-tutorial</name>
<packaging>jar</packaging>

Some files were not shown because too many files have changed in this diff Show More