Merge remote-tracking branch 'upstream/master'
This commit is contained in:
		
						commit
						d111a07fc6
					
				| @ -8,4 +8,5 @@ This module contains articles about Java 8 core features | ||||
| - [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit) | ||||
| - [Guide to Java BiFunction Interface](https://www.baeldung.com/java-bifunction-interface) | ||||
| - [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class) | ||||
| - [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid) | ||||
| - [[<-- Prev]](/core-java-modules/core-java-8) | ||||
|  | ||||
| @ -1,17 +1,24 @@ | ||||
| package com.baeldung.game; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Random; | ||||
| import java.util.Scanner; | ||||
| import java.util.*; | ||||
| 
 | ||||
| class RockPaperScissorsGame { | ||||
| 
 | ||||
|     private static Map<Integer, String> movesMap = new HashMap<Integer, String>() {{ | ||||
|         put(0, "rock"); | ||||
|         put(1, "paper"); | ||||
|         put(2, "scissors"); | ||||
|     }}; | ||||
|     enum Move { | ||||
|         ROCK("rock"), | ||||
|         PAPER("paper"), | ||||
|         SCISSORS("scissors"); | ||||
| 
 | ||||
|         private String value; | ||||
| 
 | ||||
|         Move(String value) { | ||||
|             this.value = value; | ||||
|         } | ||||
| 
 | ||||
|         public String getValue() { | ||||
|             return value; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         Scanner scanner = new Scanner(System.in); | ||||
| @ -31,7 +38,7 @@ class RockPaperScissorsGame { | ||||
|                 break; | ||||
|             } | ||||
| 
 | ||||
|             if (!movesMap.containsValue(playerMove)) { | ||||
|             if (Arrays.stream(Move.values()).noneMatch(x -> x.getValue().equals(playerMove))) { | ||||
|                 System.out.println("Your move isn't valid!"); | ||||
|                 continue; | ||||
|             } | ||||
| @ -51,15 +58,15 @@ class RockPaperScissorsGame { | ||||
|     } | ||||
| 
 | ||||
|     private static boolean isPlayerWin(String playerMove, String computerMove) { | ||||
|         return playerMove.equals("rock") && computerMove.equals("scissors") | ||||
|                 || (playerMove.equals("scissors") && computerMove.equals("paper")) | ||||
|                 || (playerMove.equals("paper") && computerMove.equals("rock")); | ||||
|         return playerMove.equals(Move.ROCK.value) && computerMove.equals(Move.SCISSORS.value) | ||||
|                 || (playerMove.equals(Move.SCISSORS.value) && computerMove.equals(Move.PAPER.value)) | ||||
|                 || (playerMove.equals(Move.PAPER.value) && computerMove.equals(Move.ROCK.value)); | ||||
|     } | ||||
| 
 | ||||
|     private static String getComputerMove() { | ||||
|         Random random = new Random(); | ||||
|         int randomNumber = random.nextInt(3); | ||||
|         String computerMove = movesMap.get(randomNumber); | ||||
|         String computerMove = Move.values()[randomNumber].getValue(); | ||||
|         System.out.println("Computer move: " + computerMove); | ||||
|         return computerMove; | ||||
|     } | ||||
|  | ||||
| @ -7,3 +7,4 @@ | ||||
| - [ArrayList vs. LinkedList vs. HashMap in Java](https://www.baeldung.com/java-arraylist-vs-linkedlist-vs-hashmap) | ||||
| - [Java Deque vs. Stack](https://www.baeldung.com/java-deque-vs-stack) | ||||
| - [Collection.toArray(new T[0]) or .toArray(new T[size])](https://www.baeldung.com/java-collection-toarray-methods) | ||||
| - [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map) | ||||
|  | ||||
| @ -0,0 +1,56 @@ | ||||
| package com.baeldung.maps.initialize; | ||||
| 
 | ||||
| import com.google.common.collect.ImmutableMap; | ||||
| import com.google.common.collect.Maps; | ||||
| import java.util.Collections; | ||||
| import java.util.HashMap; | ||||
| import java.util.LinkedHashMap; | ||||
| import java.util.Map; | ||||
| import java.util.NavigableMap; | ||||
| import java.util.SortedMap; | ||||
| import java.util.TreeMap; | ||||
| 
 | ||||
| public class EmptyMapInitializer { | ||||
| 
 | ||||
|   public static Map<String, String> articleMap; | ||||
|   static { | ||||
|     articleMap = new HashMap<>(); | ||||
|   } | ||||
| 
 | ||||
|   public static Map<String, String> createEmptyMap() { | ||||
|     return Collections.emptyMap(); | ||||
|   } | ||||
| 
 | ||||
|   public void createMapUsingConstructors() { | ||||
|     Map hashMap = new HashMap(); | ||||
|     Map linkedHashMap = new LinkedHashMap(); | ||||
|     Map treeMap = new TreeMap(); | ||||
|   } | ||||
| 
 | ||||
|   public Map<String, String> createEmptyMapUsingMapsObject() { | ||||
|     Map<String, String> emptyMap = Maps.newHashMap(); | ||||
|     return emptyMap; | ||||
|   } | ||||
| 
 | ||||
|   public Map createGenericEmptyMapUsingMapsObject() { | ||||
|     Map genericEmptyMap = Maps.<String, Integer>newHashMap(); | ||||
|     return genericEmptyMap; | ||||
|   } | ||||
| 
 | ||||
|   public static Map<String, String> createMapUsingGuava() { | ||||
|     Map<String, String> emptyMapUsingGuava = | ||||
|         Maps.newHashMap(ImmutableMap.of()); | ||||
|     return emptyMapUsingGuava; | ||||
|   } | ||||
| 
 | ||||
|   public SortedMap<String, String> createEmptySortedMap() { | ||||
|     SortedMap<String, String> sortedMap = Collections.emptySortedMap(); | ||||
|     return sortedMap; | ||||
|   } | ||||
| 
 | ||||
|   public NavigableMap<String, String> createEmptyNavigableMap() { | ||||
|     NavigableMap<String, String> navigableMap = Collections.emptyNavigableMap(); | ||||
|     return navigableMap; | ||||
|   } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,31 @@ | ||||
| package com.baeldung.maps.initialize; | ||||
| 
 | ||||
| import java.util.Map; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
| 
 | ||||
| public class EmptyMapInitializerUnitTest { | ||||
| 
 | ||||
|   @Test(expected=UnsupportedOperationException.class) | ||||
|   public void givenEmptyMap_whenAddingEntries_throwsException() { | ||||
|     Map<String, String> map = EmptyMapInitializer.createEmptyMap(); | ||||
|     map.put("key", "value"); | ||||
|   } | ||||
| 
 | ||||
|   @Test | ||||
|   public void givenEmptyMap_whenChecked_returnsTrue() { | ||||
|     assertTrue(EmptyMapInitializer.articleMap.isEmpty()); | ||||
|   } | ||||
| 
 | ||||
|   @Test | ||||
|   public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() { | ||||
|     Map<String, String> emptyMapUsingGuava = | ||||
|         EmptyMapInitializer.createMapUsingGuava(); | ||||
|     assertTrue(emptyMapUsingGuava.isEmpty()); | ||||
|     emptyMapUsingGuava.put("key", "value"); | ||||
|     assertFalse(emptyMapUsingGuava.isEmpty()); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,339 @@ | ||||
| package com.baeldung.bytebuffer; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.lang.reflect.Field; | ||||
| import java.nio.*; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| 
 | ||||
| import static org.junit.Assert.*; | ||||
| 
 | ||||
| public class ByteBufferUnitTest { | ||||
|     @Test | ||||
|     public void givenBufferCreation_whenUsingAllocate_thenSuccess() { | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertNotNull(buffer); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferCreation_whenUsingAllocateDirect_thenSuccess() { | ||||
|         ByteBuffer buffer = ByteBuffer.allocateDirect(10); | ||||
|         assertNotNull(buffer); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferCreation_whenUsingWrap_thenSuccess() { | ||||
|         byte[] bytes = new byte[10]; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||||
|         assertNotNull(buffer); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingAllocate_thenInitialIndices() { | ||||
|         // create instance using allocate | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
| 
 | ||||
|         // get index | ||||
|         int position = buffer.position(); | ||||
|         int limit = buffer.limit(); | ||||
|         int capacity = buffer.capacity(); | ||||
| 
 | ||||
|         // assert | ||||
|         assertEquals(0, position); | ||||
|         assertEquals(10, limit); | ||||
|         assertEquals(10, capacity); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenChangingPositionAndLimit_thenSuccess() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.limit(5); | ||||
| 
 | ||||
|         // assert | ||||
|         assertIndex(buffer, -1, 2, 5, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingWrap_thenInitialIndices() { | ||||
|         // create instance | ||||
|         byte[] bytes = new byte[10]; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||||
| 
 | ||||
|         // assert | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingWrapWithOffsetAndLength_thenInitialIndices() { | ||||
|         // create instance | ||||
|         byte[] bytes = new byte[10]; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes, 2, 6); | ||||
| 
 | ||||
|         // assert | ||||
|         assertIndex(buffer, -1, 2, 8, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingMarkAndReset_thenOK() { | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         buffer.position(2); | ||||
|         assertIndex(buffer, -1, 2, 10, 10); | ||||
| 
 | ||||
|         buffer.mark(); | ||||
|         assertIndex(buffer, 2, 2, 10, 10); | ||||
| 
 | ||||
|         buffer.position(5); | ||||
|         assertIndex(buffer, 2, 5, 10, 10); | ||||
| 
 | ||||
|         buffer.reset(); | ||||
|         assertIndex(buffer, 2, 2, 10, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingClear_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // clear | ||||
|         buffer.clear(); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingFlip_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // flip | ||||
|         buffer.flip(); | ||||
|         assertIndex(buffer, -1, 0, 5, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingRewind_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // rewind | ||||
|         buffer.rewind(); | ||||
|         assertIndex(buffer, -1, 0, 8, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingCompact_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // compact | ||||
|         buffer.compact(); | ||||
|         assertIndex(buffer, -1, 3, 10, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferIndex_whenUsingRemain_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.limit(8); | ||||
| 
 | ||||
|         // remain | ||||
|         boolean flag = buffer.hasRemaining(); | ||||
|         int remaining = buffer.remaining(); | ||||
| 
 | ||||
|         // assert | ||||
|         assertTrue(flag); | ||||
|         assertEquals(6, remaining); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = BufferUnderflowException.class) | ||||
|     public void givenNotEnoughRemaining_WhenCallingGetInt_thenBufferUnderflowException() { | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(2); | ||||
|         buffer.getInt(); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = BufferOverflowException.class) | ||||
|     public void givenNotEnoughRemaining_WhenCallingPutInt_thenBufferOverflowException() { | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(2); | ||||
|         buffer.putInt(10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferView_whenUsingDuplicate_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // view | ||||
|         ByteBuffer view = buffer.duplicate(); | ||||
|         assertIndex(view, 2, 5, 8, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferView_whenUsingSlice_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // view | ||||
|         ByteBuffer view = buffer.slice(); | ||||
|         assertIndex(view, -1, 0, 3, 3); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferView_whenUsingAsReaOnlyBuffer_thenOK() { | ||||
|         // create instance | ||||
|         ByteBuffer buffer = ByteBuffer.allocate(10); | ||||
|         assertIndex(buffer, -1, 0, 10, 10); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer.position(2); | ||||
|         buffer.mark(); | ||||
|         buffer.position(5); | ||||
|         buffer.limit(8); | ||||
|         assertIndex(buffer, 2, 5, 8, 10); | ||||
| 
 | ||||
|         // view | ||||
|         ByteBuffer view = buffer.asReadOnlyBuffer(); | ||||
|         assertIndex(view, 2, 5, 8, 10); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenBufferView_whenUsingAsIntBuffer_thenOK() { | ||||
|         // create instance | ||||
|         byte[] bytes = new byte[]{ | ||||
|                 (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE, // CAFEBABE ---> cafebabe | ||||
|                 (byte) 0xF0, (byte) 0x07, (byte) 0xBA, (byte) 0x11, // F007BA11 ---> football | ||||
|                 (byte) 0x0F, (byte) 0xF1, (byte) 0xCE               // 0FF1CE   ---> office | ||||
|         }; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||||
|         assertIndex(buffer, -1, 0, 11, 11); | ||||
| 
 | ||||
|         // view | ||||
|         IntBuffer intBuffer = buffer.asIntBuffer(); | ||||
|         int capacity = intBuffer.capacity(); | ||||
|         assertEquals(2, capacity); | ||||
|         assertIndex(intBuffer, -1, 0, 2, 2); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenByteOrder_whenUsingBigEndian_thenOK() { | ||||
|         // create instance | ||||
|         byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE}; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||||
| 
 | ||||
|         // change byte order | ||||
|         buffer.order(ByteOrder.BIG_ENDIAN); | ||||
|         int val = buffer.getInt(); | ||||
| 
 | ||||
|         // assert | ||||
|         assertEquals(0xCAFEBABE, val); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenByteOrder_whenUsingLittleEndian_thenOK() { | ||||
|         // create instance | ||||
|         byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE}; | ||||
|         ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||||
| 
 | ||||
|         // change byte order | ||||
|         buffer.order(ByteOrder.LITTLE_ENDIAN); | ||||
|         int val = buffer.getInt(); | ||||
| 
 | ||||
|         // assert | ||||
|         assertEquals(0xBEBAFECA, val); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenComparing_whenUsingEqualsAndCompareTo_thenOK() { | ||||
|         // create instance | ||||
|         byte[] bytes1 = "World".getBytes(StandardCharsets.UTF_8); | ||||
|         byte[] bytes2 = "HelloWorld".getBytes(StandardCharsets.UTF_8); | ||||
|         ByteBuffer buffer1 = ByteBuffer.wrap(bytes1); | ||||
|         ByteBuffer buffer2 = ByteBuffer.wrap(bytes2); | ||||
| 
 | ||||
|         // change index | ||||
|         buffer2.position(5); | ||||
| 
 | ||||
|         // equals and compareTo | ||||
|         boolean equal = buffer1.equals(buffer2); | ||||
|         int result = buffer1.compareTo(buffer2); | ||||
| 
 | ||||
|         // assert | ||||
|         assertTrue(equal); | ||||
|         assertEquals(0, result); | ||||
|     } | ||||
| 
 | ||||
|     private void assertIndex(Buffer buffer, int mark, int position, int limit, int capacity) { | ||||
|         assertEquals(mark, getMark(buffer)); | ||||
|         assertEquals(position, buffer.position()); | ||||
|         assertEquals(limit, buffer.limit()); | ||||
|         assertEquals(capacity, buffer.capacity()); | ||||
|     } | ||||
| 
 | ||||
|     private int getMark(Buffer buffer) { | ||||
|         try { | ||||
|             Class<?> clazz = Buffer.class; | ||||
|             Field f = clazz.getDeclaredField("mark"); | ||||
|             f.setAccessible(true); | ||||
|             Object result = f.get(buffer); | ||||
|             return (int) result; | ||||
|         } catch (NoSuchFieldException | IllegalAccessException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         return -1; | ||||
|     } | ||||
| } | ||||
| @ -10,4 +10,5 @@ This module contains articles about core Java Security | ||||
| - [HMAC in Java](https://www.baeldung.com/java-hmac) | ||||
| - [Generating a Secure AES Key in Java](https://www.baeldung.com/java-secure-aes-key) | ||||
| - [Computing an X509 Certificate’s Thumbprint in Java](https://www.baeldung.com/java-x509-certificate-thumbprint) | ||||
| - [Error: “trustAnchors parameter must be non-empty”](https://www.baeldung.com/java-trustanchors-parameter-must-be-non-empty) | ||||
| - More articles: [[<-- prev]](/core-java-modules/core-java-security-2) | ||||
|  | ||||
| @ -6,3 +6,4 @@ | ||||
| - [How To Configure Java Heap Size Inside a Docker Container](https://www.baeldung.com/ops/docker-jvm-heap-size) | ||||
| - [Dockerfile Strategies for Git](https://www.baeldung.com/ops/dockerfile-git-strategies) | ||||
| - [How to Get Docker-Compose to Always Use the Latest Image](https://www.baeldung.com/ops/docker-compose-latest-image) | ||||
| - [How to Include Files Outside of Docker’s Build Context](https://www.baeldung.com/ops/docker-include-files-outside-build-context) | ||||
|  | ||||
| @ -0,0 +1,2 @@ | ||||
| FROM nginx:latest | ||||
| COPY nginx.conf /etc/nginx/nginx.conf | ||||
| @ -0,0 +1,8 @@ | ||||
| events {}   | ||||
|    | ||||
| http {   | ||||
|  server {   | ||||
|     listen 80; | ||||
|     index index.html; | ||||
|   }   | ||||
| } | ||||
| @ -0,0 +1,3 @@ | ||||
| FROM nginx:latest | ||||
| COPY sample-site/html/* /etc/nginx/html/ | ||||
| COPY config/nginx.conf /etc/nginx/nginx.conf | ||||
| @ -0,0 +1,2 @@ | ||||
| FROM sample-site-base:latest | ||||
| COPY html/* /etc/nginx/html/ | ||||
| @ -0,0 +1,3 @@ | ||||
| FROM nginx:latest | ||||
| COPY html/* /etc/nginx/html/ | ||||
| COPY config/nginx.conf /etc/nginx/nginx.conf | ||||
| @ -0,0 +1,6 @@ | ||||
| mkdir tmp-context | ||||
| cp -R ../html tmp-context/ | ||||
| cp -R ../../config tmp-context/ | ||||
| cp Dockerfile-script tmp-context/Dockerfile | ||||
| docker build -t sample-site:latest tmp-context | ||||
| rm -rf tmp-context | ||||
| @ -0,0 +1,10 @@ | ||||
| <!doctype html> | ||||
| <html lang="en"> | ||||
| <head> | ||||
|   <meta charset="utf-8"> | ||||
|   <title>Sample Site</title> | ||||
| </head> | ||||
| <body> | ||||
|   <h2>Welcome to the first page of the site</h2> | ||||
| </body> | ||||
| </html> | ||||
							
								
								
									
										3
									
								
								gradle/gradle-source-vs-target-compatibility/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								gradle/gradle-source-vs-target-compatibility/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| ### Relevant Articles: | ||||
| 
 | ||||
| - [Gradle: sourceCompatiblity vs targetCompatibility](https://www.baeldung.com/gradle-sourcecompatiblity-vs-targetcompatibility) | ||||
| @ -7,3 +7,4 @@ | ||||
| - [Update the Value Associated With a Key in a HashMap](https://www.baeldung.com/java-hashmap-update-value-by-key) | ||||
| - [Java Map – keySet() vs. entrySet() vs. values() Methods](https://www.baeldung.com/java-map-entries-methods) | ||||
| - [Java IdentityHashMap Class and Its Use Cases](https://www.baeldung.com/java-identityhashmap) | ||||
| - [How to Invert a Map in Java](https://www.baeldung.com/java-invert-map) | ||||
|  | ||||
| @ -0,0 +1,59 @@ | ||||
| package com.baeldung.map.invert; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Map.Entry; | ||||
| import java.util.stream.Collectors; | ||||
| 
 | ||||
| public class InvertHashMapExample { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
| 
 | ||||
|         Map<String, Integer> map = new HashMap<>(); | ||||
|         map.put("first", 1); | ||||
|         map.put("second", 2); | ||||
|         System.out.println(map); | ||||
| 
 | ||||
|         invertMapUsingForLoop(map); | ||||
|         invertMapUsingStreams(map); | ||||
|         invertMapUsingMapper(map); | ||||
| 
 | ||||
|         map.put("two", 2); | ||||
|         invertMapUsingGroupingBy(map); | ||||
|     } | ||||
| 
 | ||||
|     public static <V, K> Map<V, K> invertMapUsingForLoop(Map<K, V> map) { | ||||
|         Map<V, K> inversedMap = new HashMap<V, K>(); | ||||
|         for (Entry<K, V> entry : map.entrySet()) { | ||||
|             inversedMap.put(entry.getValue(), entry.getKey()); | ||||
|         } | ||||
|         System.out.println(inversedMap); | ||||
|         return inversedMap; | ||||
|     } | ||||
| 
 | ||||
|     public static <V, K> Map<V, K> invertMapUsingStreams(Map<K, V> map) { | ||||
|         Map<V, K> inversedMap = map.entrySet() | ||||
|             .stream() | ||||
|             .collect(Collectors.toMap(Entry::getValue, Entry::getKey)); | ||||
|         System.out.println(inversedMap); | ||||
|         return inversedMap; | ||||
|     } | ||||
| 
 | ||||
|     public static <K, V> Map<V, K> invertMapUsingMapper(Map<K, V> sourceMap) { | ||||
|         Map<V, K> inversedMap = sourceMap.entrySet() | ||||
|             .stream() | ||||
|             .collect(Collectors.toMap(Entry::getValue, Entry::getKey, (oldValue, newValue) -> oldValue)); | ||||
|         System.out.println(inversedMap); | ||||
|         return inversedMap; | ||||
|     } | ||||
| 
 | ||||
|     public static <V, K> Map<V, List<K>> invertMapUsingGroupingBy(Map<K, V> map) { | ||||
|         Map<V, List<K>> inversedMap = map.entrySet() | ||||
|             .stream() | ||||
|             .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList()))); | ||||
|         System.out.println(inversedMap); | ||||
|         return inversedMap; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,72 @@ | ||||
| package com.baeldung.map.invert; | ||||
| 
 | ||||
| import static org.junit.Assert.assertNotEquals; | ||||
| import static org.junit.Assert.assertNotNull; | ||||
| import static org.junit.Assert.assertTrue; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.jupiter.api.TestInstance; | ||||
| 
 | ||||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||||
| public class InvertHashMapUnitTest { | ||||
| 
 | ||||
|     Map<String, Integer> sourceMap; | ||||
| 
 | ||||
|     @BeforeEach | ||||
|     void setup() { | ||||
|         sourceMap = new HashMap<>(); | ||||
|         sourceMap.put("Sunday", 0); | ||||
|         sourceMap.put("Monday", 1); | ||||
|         sourceMap.put("Tuesday", 2); | ||||
|         sourceMap.put("Wednesday", 3); | ||||
|         sourceMap.put("Thursday", 4); | ||||
|         sourceMap.put("Friday", 5); | ||||
|         sourceMap.put("Saturday", 6); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenSourceMap_whenUsingForLoop_returnsInvertedMap() { | ||||
|         Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingForLoop(sourceMap); | ||||
|          | ||||
|         assertNotNull(inversedMap); | ||||
|         assertEquals(sourceMap.size(), inversedMap.size()); | ||||
|         assertEquals("Monday", inversedMap.get(1)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenSourceMap_whenUsingStreams_returnsInvertedMap() { | ||||
|         Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingStreams(sourceMap); | ||||
|          | ||||
|         assertNotNull(inversedMap); | ||||
|         assertEquals(sourceMap.size(), inversedMap.size()); | ||||
|         assertEquals("Monday", inversedMap.get(1)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenSourceMap_whenUsingMapper_returnsInvertedMap() { | ||||
|         Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingMapper(sourceMap); | ||||
|          | ||||
|         assertNotNull(inversedMap); | ||||
|         assertEquals(sourceMap.size(), inversedMap.size()); | ||||
|         assertEquals("Monday", inversedMap.get(1)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenSourceMapWithDuplicateValues_whenUsingGroupBy_returnsInvertedMap() { | ||||
|         sourceMap.put("MONDAY", 1); | ||||
|         Map<Integer, List<String>> inversedMap = InvertHashMapExample.invertMapUsingGroupingBy(sourceMap); | ||||
|          | ||||
|         assertNotNull(inversedMap); | ||||
|         assertNotEquals(sourceMap.size(), inversedMap.size()); // duplicate keys are merged now | ||||
|         assertEquals(2, inversedMap.get(1).size()); | ||||
|         assertTrue(inversedMap.get(1).contains("Monday")); | ||||
|         assertTrue(inversedMap.get(1).contains("MONDAY")); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -7,3 +7,4 @@ This module contains articles about the Java Native Interface (JNI). | ||||
| - [Guide to JNI (Java Native Interface)](https://www.baeldung.com/jni) | ||||
| - [Using JNA to Access Native Dynamic Libraries](https://www.baeldung.com/java-jna-dynamic-libraries) | ||||
| - [Check if a Java Program Is Running in 64-Bit or 32-Bit JVM](https://www.baeldung.com/java-detect-jvm-64-or-32-bit) | ||||
| - [How to use JNI’s RegisterNatives() method?](https://www.baeldung.com/jni-registernatives) | ||||
|  | ||||
							
								
								
									
										5
									
								
								javax-servlets-2/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								javax-servlets-2/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| ## Servlets | ||||
| 
 | ||||
| This module contains articles about Servlets. | ||||
| 
 | ||||
| ### Relevant Articles: | ||||
							
								
								
									
										60
									
								
								javax-servlets-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								javax-servlets-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,60 @@ | ||||
| <?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> | ||||
|     <groupId>com.baeldung.javax-servlets</groupId> | ||||
|     <artifactId>javax-servlets-2</artifactId> | ||||
|     <version>1.0-SNAPSHOT</version> | ||||
|     <name>javax-servlets-2</name> | ||||
|     <packaging>war</packaging> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <!-- File Uploading --> | ||||
|         <dependency> | ||||
|             <groupId>commons-fileupload</groupId> | ||||
|             <artifactId>commons-fileupload</artifactId> | ||||
|             <version>${commons-fileupload.version}</version> | ||||
|         </dependency> | ||||
|         <!-- Servlet --> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet</groupId> | ||||
|             <artifactId>javax.servlet-api</artifactId> | ||||
|             <version>${javax.servlet-api.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet.jsp</groupId> | ||||
|             <artifactId>javax.servlet.jsp-api</artifactId> | ||||
|             <version>${javax.servlet.jsp-api.version}</version> | ||||
|             <scope>provided</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet</groupId> | ||||
|             <artifactId>jstl</artifactId> | ||||
|             <version>${jstl.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.httpcomponents</groupId> | ||||
|             <artifactId>httpclient</artifactId> | ||||
|             <version>${org.apache.httpcomponents.version}</version> | ||||
|             <scope>test</scope> | ||||
|             <exclusions> | ||||
|                 <exclusion> | ||||
|                     <artifactId>commons-logging</artifactId> | ||||
|                     <groupId>commons-logging</groupId> | ||||
|                 </exclusion> | ||||
|             </exclusions> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <properties> | ||||
|         <org.apache.httpcomponents.version>4.5.13</org.apache.httpcomponents.version> | ||||
|         <javax.servlet-api.version>4.0.1</javax.servlet-api.version> | ||||
|     </properties> | ||||
| </project> | ||||
| @ -0,0 +1,80 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import java.io.Serializable; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Date; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @since 6 de fev de 2022 | ||||
|  * @author ulisses | ||||
|  */ | ||||
| public class User implements Serializable { | ||||
|     private static final long serialVersionUID = 1L; | ||||
| 
 | ||||
|     protected static final HashMap<String, User> DB = new HashMap<>(); | ||||
|     static { | ||||
|         DB.put("admin", new User("admin", "password")); | ||||
|         DB.put("user", new User("user", "pass")); | ||||
|     } | ||||
| 
 | ||||
|     private String name; | ||||
|     private String password; | ||||
| 
 | ||||
|     private List<Date> logins = new ArrayList<Date>(); | ||||
| 
 | ||||
|     public User(String name, String password) { | ||||
|         this.name = name; | ||||
|         this.password = password; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
|     public List<Date> getLogins() { | ||||
|         return logins; | ||||
|     } | ||||
| 
 | ||||
|     public void setLogins(List<Date> logins) { | ||||
|         this.logins = logins; | ||||
|     } | ||||
| 
 | ||||
|     public String getPassword() { | ||||
|         return password; | ||||
|     } | ||||
| 
 | ||||
|     public void setPassword(String password) { | ||||
|         this.password = password; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int hashCode() { | ||||
|         final int prime = 31; | ||||
|         int result = 1; | ||||
|         result = prime * result + ((name == null) ? 0 : name.hashCode()); | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean equals(Object obj) { | ||||
|         if (this == obj) | ||||
|             return true; | ||||
|         if (obj == null) | ||||
|             return false; | ||||
|         if (getClass() != obj.getClass()) | ||||
|             return false; | ||||
|         User other = (User) obj; | ||||
|         if (name == null) { | ||||
|             if (other.name != null) | ||||
|                 return false; | ||||
|         } else if (!name.equals(other.name)) | ||||
|             return false; | ||||
|         return true; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,46 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import javax.servlet.Filter; | ||||
| import javax.servlet.FilterChain; | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.ServletRequest; | ||||
| import javax.servlet.ServletResponse; | ||||
| import javax.servlet.annotation.WebFilter; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| 
 | ||||
| @WebFilter("/user-check/*") | ||||
| public class UserCheckFilter implements Filter { | ||||
|     public static void forward(HttpServletRequest request, HttpServletResponse response, String page) throws ServletException, IOException { | ||||
|         request.getRequestDispatcher("/WEB-INF/user.check" + page) | ||||
|             .forward(request, response); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||||
|         if (!(req instanceof HttpServletRequest)) { | ||||
|             throw new ServletException("Can only process HttpServletRequest"); | ||||
|         } | ||||
| 
 | ||||
|         if (!(res instanceof HttpServletResponse)) { | ||||
|             throw new ServletException("Can only process HttpServletResponse"); | ||||
|         } | ||||
| 
 | ||||
|         HttpServletRequest request = (HttpServletRequest) req; | ||||
|         HttpServletResponse response = (HttpServletResponse) res; | ||||
| 
 | ||||
|         request.setAttribute("origin", request.getRequestURI()); | ||||
| 
 | ||||
|         if (!request.getRequestURI() | ||||
|             .contains("login") && request.getSession(false) == null) { | ||||
|             response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||||
|             forward(request, response, "/login.jsp"); | ||||
|             // we return here so the original servlet is not processed | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         chain.doFilter(request, response); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,56 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import javax.servlet.http.HttpSession; | ||||
| 
 | ||||
| @WebServlet("/user-check/login") | ||||
| public class UserCheckLoginServlet extends HttpServlet { | ||||
|     private static final long serialVersionUID = 1L; | ||||
| 
 | ||||
|     @Override | ||||
|     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         if (request.getSession(false) != null) { | ||||
|             response.sendRedirect(request.getContextPath() + "/user-check/home"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         String referer = (String) request.getAttribute("origin"); | ||||
|         request.setAttribute("origin", referer); | ||||
|         UserCheckFilter.forward(request, response, "/login.jsp"); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         String key = request.getParameter("name"); | ||||
|         String pass = request.getParameter("password"); | ||||
| 
 | ||||
|         User user = User.DB.get(key); | ||||
|         if (user == null || !user.getPassword() | ||||
|             .equals(pass)) { | ||||
|             response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||||
|             request.setAttribute("origin", request.getParameter("origin")); | ||||
|             request.setAttribute("error", "invalid login"); | ||||
|             UserCheckFilter.forward(request, response, "/login.jsp"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         user.getLogins() | ||||
|             .add(new Date()); | ||||
| 
 | ||||
|         HttpSession session = request.getSession(); | ||||
|         session.setAttribute("user", user); | ||||
| 
 | ||||
|         String origin = request.getParameter("origin"); | ||||
|         if (origin == null || origin.contains("login")) | ||||
|             origin = "./"; | ||||
| 
 | ||||
|         response.sendRedirect(origin); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,26 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import javax.servlet.http.HttpSession; | ||||
| 
 | ||||
| @WebServlet("/user-check/logout") | ||||
| public class UserCheckLogoutServlet extends HttpServlet { | ||||
|     private static final long serialVersionUID = 1L; | ||||
| 
 | ||||
|     @Override | ||||
|     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         HttpSession session = request.getSession(false); | ||||
|         if (session != null) { | ||||
|             session.invalidate(); | ||||
|         } | ||||
| 
 | ||||
|         request.setAttribute("loggedOut", true); | ||||
|         response.sendRedirect("./"); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import javax.servlet.http.HttpSession; | ||||
| 
 | ||||
| @WebServlet(name = "home", urlPatterns = { "/user-check/", "/user-check", "/user-check/home" }) | ||||
| public class UserCheckServlet extends HttpServlet { | ||||
|     private static final long serialVersionUID = 1L; | ||||
| 
 | ||||
|     @Override | ||||
|     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         HttpSession session = request.getSession(false); | ||||
|         if (session == null || session.getAttribute("user") == null) { | ||||
|             throw new IllegalStateException("user not logged in"); | ||||
|         } | ||||
| 
 | ||||
|         User user = (User) session.getAttribute("user"); | ||||
|         request.setAttribute("user", user); | ||||
| 
 | ||||
|         UserCheckFilter.forward(request, response, "/home.jsp"); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										13
									
								
								javax-servlets-2/src/main/resources/logback.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								javax-servlets-2/src/main/resources/logback.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <configuration> | ||||
|     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||
|         <encoder> | ||||
|             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||
|             </pattern> | ||||
|         </encoder> | ||||
|     </appender> | ||||
| 
 | ||||
|     <root level="INFO"> | ||||
|         <appender-ref ref="STDOUT" /> | ||||
|     </root> | ||||
| </configuration> | ||||
							
								
								
									
										31
									
								
								javax-servlets-2/src/main/webapp/WEB-INF/user.check/home.jsp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								javax-servlets-2/src/main/webapp/WEB-INF/user.check/home.jsp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,31 @@ | ||||
| <%@ page contentType="text/html;charset=UTF-8" session="false"%> | ||||
| <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> | ||||
| 
 | ||||
| <html> | ||||
|     <head> | ||||
|         <title>login success - current session info</title> | ||||
|     </head> | ||||
|     <body> | ||||
|         <section> | ||||
|             <h2>user info</h2> | ||||
|             <div> | ||||
|                 <span>name: ${user.name}</span> | ||||
|             </div> | ||||
|      | ||||
|             <div> | ||||
|                 <span>logins:</span> | ||||
|                 <ul> | ||||
|                     <c:forEach var="login" items="${user.logins}"> | ||||
|                         <li>${login}</li> | ||||
|                     </c:forEach> | ||||
|                 </ul> | ||||
|             </div> | ||||
|      | ||||
|             <div> | ||||
|                 <a href="${pageContext.request.contextPath}/user-check/logout"> | ||||
|                     logout | ||||
|                 </a> | ||||
|             </div> | ||||
|         </section> | ||||
|     </body> | ||||
| </html> | ||||
| @ -0,0 +1,33 @@ | ||||
| <%@ page contentType="text/html;charset=UTF-8" session="false"%> | ||||
| <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> | ||||
| 
 | ||||
| <html> | ||||
|     <head> | ||||
|         <title>login</title> | ||||
|     </head> | ||||
|     <body> | ||||
|         <form action="${pageContext.request.contextPath}/user-check/login" | ||||
|           method="POST"> | ||||
|             <input type="hidden" name="origin" value="${origin}"> | ||||
|             <c:if test="${not empty origin}"> | ||||
|                 <div>* redirected to login from: ${origin}</div> | ||||
|             </c:if> | ||||
|      | ||||
|             <c:if test="${not empty error}"> | ||||
|                 <div>* error: ${error}</div> | ||||
|             </c:if> | ||||
|      | ||||
|             <fieldset> | ||||
|                 <legend>credentials</legend> | ||||
|                  | ||||
|                 <label for="name">name</label>  | ||||
|                 <input type="text" name="name"> | ||||
|      | ||||
|                 <label for="password">password</label>  | ||||
|                 <input type="password" name="password">  | ||||
|                  | ||||
|                 <input type="submit"> | ||||
|             </fieldset> | ||||
|         </form> | ||||
|     </body> | ||||
| </html> | ||||
| @ -0,0 +1,98 @@ | ||||
| package com.baeldung.user.check; | ||||
| 
 | ||||
| import static org.junit.Assert.assertTrue; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| 
 | ||||
| import org.apache.http.client.entity.UrlEncodedFormEntity; | ||||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||||
| import org.apache.http.client.methods.HttpGet; | ||||
| import org.apache.http.client.methods.HttpPost; | ||||
| import org.apache.http.impl.client.CloseableHttpClient; | ||||
| import org.apache.http.impl.client.HttpClientBuilder; | ||||
| import org.apache.http.impl.client.LaxRedirectStrategy; | ||||
| import org.apache.http.message.BasicNameValuePair; | ||||
| import org.apache.http.util.EntityUtils; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.junit.MockitoJUnitRunner; | ||||
| 
 | ||||
| @RunWith(MockitoJUnitRunner.class) | ||||
| public class UserCheckServletLiveTest { | ||||
|     private static final String BASE_URL = "http://localhost:8080/javax-servlets-2/user-check"; | ||||
| 
 | ||||
|     @Mock | ||||
|     HttpServletRequest request; | ||||
| 
 | ||||
|     @Mock | ||||
|     HttpServletResponse response; | ||||
| 
 | ||||
|     private CloseableHttpClient buildClient() { | ||||
|         return HttpClientBuilder.create() | ||||
|             .setRedirectStrategy(new LaxRedirectStrategy()) | ||||
|             .build(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCorrectCredentials_thenLoginSucceeds() throws Exception { | ||||
|         try (CloseableHttpClient client = buildClient()) { | ||||
|             HttpPost post = new HttpPost(BASE_URL + "/login"); | ||||
| 
 | ||||
|             List<BasicNameValuePair> form = new ArrayList<>(); | ||||
|             form.add(new BasicNameValuePair("name", "admin")); | ||||
|             form.add(new BasicNameValuePair("password", "password")); | ||||
| 
 | ||||
|             post.setEntity(new UrlEncodedFormEntity(form)); | ||||
|             try (CloseableHttpResponse response = client.execute(post)) { | ||||
|                 String body = EntityUtils.toString(response.getEntity()); | ||||
| 
 | ||||
|                 assertTrue(response.getStatusLine() | ||||
|                     .getStatusCode() == 200); | ||||
| 
 | ||||
|                 assertTrue(body.contains("login success")); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenIncorrectCredentials_thenLoginFails() throws Exception { | ||||
|         try (CloseableHttpClient client = buildClient()) { | ||||
|             HttpPost post = new HttpPost(BASE_URL + "/login"); | ||||
| 
 | ||||
|             List<BasicNameValuePair> form = new ArrayList<>(); | ||||
|             form.add(new BasicNameValuePair("name", "admin")); | ||||
|             form.add(new BasicNameValuePair("password", "invalid")); | ||||
| 
 | ||||
|             post.setEntity(new UrlEncodedFormEntity(form)); | ||||
|             try (CloseableHttpResponse response = client.execute(post)) { | ||||
|                 String body = EntityUtils.toString(response.getEntity()); | ||||
| 
 | ||||
|                 assertTrue(response.getStatusLine() | ||||
|                     .getStatusCode() == 401); | ||||
| 
 | ||||
|                 assertTrue(body.contains("invalid login")); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenNotLoggedIn_thenRedirectedToLoginPage() throws Exception { | ||||
|         try (CloseableHttpClient client = buildClient()) { | ||||
|             HttpGet get = new HttpGet(BASE_URL + "/home"); | ||||
| 
 | ||||
|             try (CloseableHttpResponse response = client.execute(get)) { | ||||
|                 String body = EntityUtils.toString(response.getEntity()); | ||||
| 
 | ||||
|                 assertTrue(response.getStatusLine() | ||||
|                     .getStatusCode() == 401); | ||||
| 
 | ||||
|                 assertTrue(body.contains("redirected to login")); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										13
									
								
								jta/pom.xml
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								jta/pom.xml
									
									
									
									
									
								
							| @ -15,6 +15,18 @@ | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../parent-boot-2</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -37,6 +49,7 @@ | ||||
| 
 | ||||
|     <properties> | ||||
|         <spring-boot.version>2.4.7</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -63,6 +63,7 @@ | ||||
| 
 | ||||
|     <properties> | ||||
|         <java.version>11</java.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -24,7 +24,7 @@ | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <properties> | ||||
|         <spring.version>5.3.13</spring.version> | ||||
|         <spring.version>5.3.16</spring.version> | ||||
|         <spring-security.version>5.6.0</spring-security.version> | ||||
|         <spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version> | ||||
|     </properties> | ||||
|  | ||||
| @ -48,6 +48,13 @@ | ||||
|                 <type>pom</type> | ||||
|                 <scope>import</scope> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
| @ -63,6 +70,7 @@ | ||||
|     <properties> | ||||
|         <camel.version>3.7.4</camel.version> | ||||
|         <spring-boot.version>2.2.2.RELEASE</spring-boot.version> | ||||
|          <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -7,4 +7,5 @@ This module contains articles about use of Queries in Hibernate. | ||||
| - [Criteria Queries Using JPA Metamodel](https://www.baeldung.com/hibernate-criteria-queries-metamodel) | ||||
| - [Get All Data from a Table with Hibernate](https://www.baeldung.com/hibernate-select-all) | ||||
| - [Hibernate Named Query](https://www.baeldung.com/hibernate-named-query) | ||||
| - [Hibernate Query Plan Cache](https://www.baeldung.com/hibernate-query-plan-cache) | ||||
| - [Hibernate Query Plan Cache](https://www.baeldung.com/hibernate-query-plan-cache) | ||||
| - [Hibernate’s addScalar() Method](https://www.baeldung.com/hibernate-addscalar) | ||||
|  | ||||
| @ -24,6 +24,13 @@ | ||||
|                 <type>pom</type> | ||||
|                 <scope>import</scope> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
| @ -184,6 +191,7 @@ | ||||
|         <dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url> | ||||
|         <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version> | ||||
|         <spring-boot.version>2.4.7</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
							
								
								
									
										8
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								pom.xml
									
									
									
									
									
								
							| @ -459,6 +459,7 @@ | ||||
|                 <module>java-vavr-stream</module> | ||||
|                 <module>java-websocket</module> | ||||
|                 <module>javax-servlets</module> | ||||
|                 <module>javax-servlets-2</module> | ||||
|                 <module>javaxval</module> | ||||
|                 <module>jaxb</module> | ||||
|                 <module>jee-7</module> | ||||
| @ -943,6 +944,7 @@ | ||||
|                 <module>java-vavr-stream</module> | ||||
|                 <module>java-websocket</module> | ||||
|                 <module>javax-servlets</module> | ||||
|                 <module>javax-servlets-2</module> | ||||
|                 <module>javaxval</module> | ||||
|                 <module>jaxb</module> | ||||
|                 <module>jee-7</module> | ||||
| @ -1343,7 +1345,8 @@ | ||||
|                  <module>persistence-modules/spring-data-cassandra-2</module> | ||||
|                  <module>quarkus-vs-springboot</module> | ||||
|                  <module>quarkus-jandex</module> | ||||
|                  <module>spring-boot-modules/spring-boot-cassandre</module> | ||||
| 		 <module>spring-boot-modules/spring-boot-cassandre</module> | ||||
| 		 <module>spring-boot-modules/spring-boot-camel</module>	  | ||||
|                  <module>testing-modules/testing-assertions</module> | ||||
|             </modules> | ||||
|         </profile> | ||||
| @ -1400,7 +1403,8 @@ | ||||
|                 <module>persistence-modules/spring-data-cassandra-2</module> | ||||
|                 <module>quarkus-vs-springboot</module> | ||||
|                 <module>quarkus-jandex</module> | ||||
|                 <module>spring-boot-modules/spring-boot-cassandre</module> | ||||
| 		<module>spring-boot-modules/spring-boot-cassandre</module> | ||||
| 		<module>spring-boot-modules/spring-boot-camel</module> | ||||
|                 <module>testing-modules/testing-assertions</module> | ||||
|             </modules> | ||||
|         </profile> | ||||
|  | ||||
| @ -187,6 +187,7 @@ | ||||
|         <java.version>11</java.version> | ||||
|         <repackage.classifier /> | ||||
|         <spring-native.version>0.11.0-RC1</spring-native.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
							
								
								
									
										3
									
								
								spring-5-autowiring-beans/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								spring-5-autowiring-beans/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| ### Relevant Articles: | ||||
| 
 | ||||
| - [Spring @Autowired Field Null – Common Causes and Solutions](https://www.baeldung.com/spring-autowired-field-null) | ||||
| @ -13,6 +13,18 @@ | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../parent-boot-2</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -138,6 +150,7 @@ | ||||
|         <reactor-core.version>3.3.1.RELEASE</reactor-core.version> | ||||
|         <!-- This spring-boot.version is set manually, For upgrading this please refer http://team.baeldung.com/browse/JAVA-2802 --> | ||||
|         <spring-boot.version>2.2.6.RELEASE</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -16,6 +16,18 @@ | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../parent-boot-1</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -59,6 +71,7 @@ | ||||
| 
 | ||||
|     <properties> | ||||
|         <activiti.version>6.0.0</activiti.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -36,7 +36,6 @@ | ||||
|         <module>spring-boot-deployment</module> | ||||
|         <module>spring-boot-di</module> | ||||
|         <module>spring-boot-disable-logging</module> | ||||
|         <module>spring-boot-camel</module> | ||||
|         <module>spring-boot-ci-cd</module> | ||||
|         <!-- <module>spring-boot-cli</module> --> <!-- Not a maven project --> | ||||
|         <module>spring-boot-custom-starter</module> | ||||
| @ -51,6 +50,7 @@ | ||||
|         <!-- <module>spring-boot-keycloak</module> --> <!-- Fixing under JAVA-8271 --> | ||||
|         <module>spring-boot-libraries</module> | ||||
|         <module>spring-boot-libraries-2</module> | ||||
|         <module>spring-boot-libraries-comparison</module> | ||||
|         <module>spring-boot-logging-log4j2</module> | ||||
|         <module>spring-boot-multiple-datasources</module> | ||||
|         <module>spring-boot-mvc</module> | ||||
|  | ||||
| @ -62,7 +62,7 @@ | ||||
|     </build> | ||||
| 
 | ||||
|     <properties> | ||||
|         <log4j2.version>2.14.1</log4j2.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|         <spring-core.version>5.3.15</spring-core.version> | ||||
|         <maven.compiler.target>11</maven.compiler.target> | ||||
|         <maven.compiler.source>11</maven.compiler.source> | ||||
|  | ||||
| @ -16,22 +16,22 @@ | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <groupId>org.apache.camel.springboot</groupId> | ||||
|             <artifactId>camel-servlet-starter</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <groupId>org.apache.camel.springboot</groupId> | ||||
|             <artifactId>camel-jackson-starter</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <groupId>org.apache.camel.springboot</groupId> | ||||
|             <artifactId>camel-swagger-java-starter</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.camel</groupId> | ||||
|             <groupId>org.apache.camel.springboot</groupId> | ||||
|             <artifactId>camel-spring-boot-starter</artifactId> | ||||
|             <version>${camel.version}</version> | ||||
|         </dependency> | ||||
| @ -64,7 +64,8 @@ | ||||
|     </build> | ||||
| 
 | ||||
|     <properties> | ||||
|         <camel.version>3.0.0-M4</camel.version> | ||||
|         <java.version>11</java.version> | ||||
|         <camel.version>3.15.0</camel.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,7 @@ | ||||
| ## Spring Boot Libraries | ||||
| 
 | ||||
| This module contains articles about various Spring Boot libraries Comparison | ||||
| 
 | ||||
| ### Relevant Articles: | ||||
| 
 | ||||
| - [GraphQL vs REST](https://www.baeldung.com/graphql-vs-rest) | ||||
							
								
								
									
										46
									
								
								spring-boot-modules/spring-boot-libraries-comparison/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								spring-boot-modules/spring-boot-libraries-comparison/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,46 @@ | ||||
| <?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>spring-boot-libraries-comparison</artifactId> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung.spring-boot-modules</groupId> | ||||
|         <artifactId>spring-boot-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-web</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.data</groupId> | ||||
|             <artifactId>spring-data-jpa</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.graphql-java</groupId> | ||||
|             <artifactId>graphql-spring-boot-starter</artifactId> | ||||
|             <version>${graphql-spring-boot-starter.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.graphql-java</groupId> | ||||
|             <artifactId>graphql-java-tools</artifactId> | ||||
|             <version>${graphql-java-tools.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.graphql-java</groupId> | ||||
|             <artifactId>graphiql-spring-boot-starter</artifactId> | ||||
|             <version>${graphql-spring-boot-starter.version}</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
| 
 | ||||
|     <properties> | ||||
|         <graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version> | ||||
|         <graphql-java-tools.version>5.2.4</graphql-java-tools.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,19 @@ | ||||
| package com.baeldung.graphqlvsrest; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.configuration.GraphqlConfiguration; | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; | ||||
| import org.springframework.context.annotation.Import; | ||||
| 
 | ||||
| @SpringBootApplication | ||||
| @Import(GraphqlConfiguration.class) | ||||
| @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) | ||||
| public class GraphqlVsRestApplication { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         SpringApplication.run(GraphqlVsRestApplication.class, args); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,35 @@ | ||||
| package com.baeldung.graphqlvsrest.configuration; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.repository.OrderRepository; | ||||
| import com.baeldung.graphqlvsrest.resolver.Mutation; | ||||
| import com.baeldung.graphqlvsrest.resolver.ProductResolver; | ||||
| import com.baeldung.graphqlvsrest.resolver.Query; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| 
 | ||||
| @Configuration | ||||
| public class GraphqlConfiguration { | ||||
| 
 | ||||
|     @Autowired | ||||
|     ProductRepository productRepository; | ||||
| 
 | ||||
|     @Autowired | ||||
|     OrderRepository orderRepository; | ||||
| 
 | ||||
|     @Bean | ||||
|     public Query query() { | ||||
|         return new Query(productRepository); | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public ProductResolver productResolver(){ | ||||
|         return new ProductResolver(orderRepository); | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public Mutation mutation() { | ||||
|         return new Mutation(productRepository); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,25 @@ | ||||
| package com.baeldung.graphqlvsrest.controller; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Order; | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| import com.baeldung.graphqlvsrest.repository.OrderRepository; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.data.domain.Pageable; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| @RestController | ||||
| @RequestMapping("order") | ||||
| public class OrderController { | ||||
| 
 | ||||
|     @Autowired | ||||
|     OrderRepository orderRepository; | ||||
| 
 | ||||
|     @GetMapping() | ||||
|     public List<Order> getOrders(@RequestParam("product-id") Integer productId){ | ||||
|         return orderRepository.getOrdersByProduct(productId); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,38 @@ | ||||
| package com.baeldung.graphqlvsrest.controller; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.data.domain.Pageable; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| @RestController | ||||
| @RequestMapping("product") | ||||
| public class ProductController { | ||||
| 
 | ||||
|     @Autowired | ||||
|     ProductRepository productRepository; | ||||
| 
 | ||||
|     @GetMapping | ||||
|     public List<Product> getProducts(Pageable pageable){ | ||||
|         return productRepository.getProducts(pageable.getPageSize(), pageable.getPageNumber()); | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/{product-id}") | ||||
|     public Product getProducts(@PathVariable("product-id") Integer productId){ | ||||
|         return productRepository.getProduct(productId); | ||||
|     } | ||||
| 
 | ||||
|     @PostMapping | ||||
|     public Product save(@RequestBody ProductModel productModel){ | ||||
|         return productRepository.save(productModel); | ||||
|     } | ||||
| 
 | ||||
|     @PutMapping("/{product-id}") | ||||
|     public Product update(@PathVariable("product-id") Integer productId, @RequestBody ProductModel productModel){ | ||||
|         return productRepository.update(productId, productModel); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,58 @@ | ||||
| package com.baeldung.graphqlvsrest.entity; | ||||
| 
 | ||||
| public class Order { | ||||
|     private Integer id; | ||||
|     private Integer product_id; | ||||
|     private String customer_uuid; | ||||
|     private String status; | ||||
|     private String address; | ||||
|     private String creation_date; | ||||
| 
 | ||||
|     public Integer getId() { | ||||
|         return id; | ||||
|     } | ||||
| 
 | ||||
|     public void setId(Integer id) { | ||||
|         this.id = id; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getProduct_id() { | ||||
|         return product_id; | ||||
|     } | ||||
| 
 | ||||
|     public void setProduct_id(Integer product_id) { | ||||
|         this.product_id = product_id; | ||||
|     } | ||||
| 
 | ||||
|     public String getStatus() { | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     public void setStatus(String status) { | ||||
|         this.status = status; | ||||
|     } | ||||
| 
 | ||||
|     public String getCustomer_uuid() { | ||||
|         return customer_uuid; | ||||
|     } | ||||
| 
 | ||||
|     public void setCustomer_uuid(String customer_uuid) { | ||||
|         this.customer_uuid = customer_uuid; | ||||
|     } | ||||
| 
 | ||||
|     public String getAddress() { | ||||
|         return address; | ||||
|     } | ||||
| 
 | ||||
|     public void setAddress(String address) { | ||||
|         this.address = address; | ||||
|     } | ||||
| 
 | ||||
|     public String getCreation_date() { | ||||
|         return creation_date; | ||||
|     } | ||||
| 
 | ||||
|     public void setCreation_date(String creation_date) { | ||||
|         this.creation_date = creation_date; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,115 @@ | ||||
| package com.baeldung.graphqlvsrest.entity; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Product { | ||||
|     private Integer id; | ||||
|     private String name; | ||||
|     private String description; | ||||
|     private String status; | ||||
|     private String currency; | ||||
|     private Double price; | ||||
|     private List<String> image_url; | ||||
|     private List<String> video_url; | ||||
|     private Integer stock; | ||||
|     private Float average_rating; | ||||
| 
 | ||||
|     public Product(Integer id, ProductModel productModel) { | ||||
|         this.id = id; | ||||
|         this.name = productModel.getName(); | ||||
|         this.description = productModel.getDescription(); | ||||
|         this.currency = productModel.getCurrency(); | ||||
|         this.price = productModel.getPrice(); | ||||
|         this.stock = productModel.getStock(); | ||||
|         this.image_url = productModel.getImage_url(); | ||||
|         this.video_url = productModel.getVideo_url(); | ||||
|         this.average_rating = 0F; | ||||
|         this.status = productModel.getStatus(); | ||||
|     } | ||||
| 
 | ||||
|     public Product(){ | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public Integer getId() { | ||||
|         return id; | ||||
|     } | ||||
| 
 | ||||
|     public void setId(Integer id) { | ||||
|         this.id = id; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
|     public String getDescription() { | ||||
|         return description; | ||||
|     } | ||||
| 
 | ||||
|     public void setDescription(String description) { | ||||
|         this.description = description; | ||||
|     } | ||||
| 
 | ||||
|     public String getStatus() { | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     public void setStatus(String status) { | ||||
|         this.status = status; | ||||
|     } | ||||
| 
 | ||||
|     public String getCurrency() { | ||||
|         return currency; | ||||
|     } | ||||
| 
 | ||||
|     public void setCurrency(String currency) { | ||||
|         this.currency = currency; | ||||
|     } | ||||
| 
 | ||||
|     public Double getPrice() { | ||||
|         return price; | ||||
|     } | ||||
| 
 | ||||
|     public void setPrice(Double price) { | ||||
|         this.price = price; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> getImage_url() { | ||||
|         return image_url; | ||||
|     } | ||||
| 
 | ||||
|     public void setImage_url(List<String> image_url) { | ||||
|         this.image_url = image_url; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> getVideo_url() { | ||||
|         return video_url; | ||||
|     } | ||||
| 
 | ||||
|     public void setVideo_url(List<String> video_url) { | ||||
|         this.video_url = video_url; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getStock() { | ||||
|         return stock; | ||||
|     } | ||||
| 
 | ||||
|     public void setStock(Integer stock) { | ||||
|         this.stock = stock; | ||||
|     } | ||||
| 
 | ||||
|     public Float getAverage_rating() { | ||||
|         return average_rating; | ||||
|     } | ||||
| 
 | ||||
|     public void setAverage_rating(Float average_rating) { | ||||
|         this.average_rating = average_rating; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,92 @@ | ||||
| package com.baeldung.graphqlvsrest.model; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class ProductModel { | ||||
|     private String name; | ||||
|     private String description; | ||||
|     private String status; | ||||
|     private String currency; | ||||
|     private Double price; | ||||
|     private List<String> image_url; | ||||
|     private List<String> video_url; | ||||
|     private Integer stock; | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
|     public String getDescription() { | ||||
|         return description; | ||||
|     } | ||||
| 
 | ||||
|     public void setDescription(String description) { | ||||
|         this.description = description; | ||||
|     } | ||||
| 
 | ||||
|     public String getStatus() { | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     public void setStatus(String status) { | ||||
|         this.status = status; | ||||
|     } | ||||
| 
 | ||||
|     public String getCurrency() { | ||||
|         return currency; | ||||
|     } | ||||
| 
 | ||||
|     public void setCurrency(String currency) { | ||||
|         this.currency = currency; | ||||
|     } | ||||
| 
 | ||||
|     public Double getPrice() { | ||||
|         return price; | ||||
|     } | ||||
| 
 | ||||
|     public void setPrice(Double price) { | ||||
|         this.price = price; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> getImage_url() { | ||||
|         return image_url; | ||||
|     } | ||||
| 
 | ||||
|     public void setImage_url(List<String> image_url) { | ||||
|         this.image_url = image_url; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> getVideo_url() { | ||||
|         return video_url; | ||||
|     } | ||||
| 
 | ||||
|     public void setVideo_url(List<String> video_url) { | ||||
|         this.video_url = video_url; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getStock() { | ||||
|         return stock; | ||||
|     } | ||||
| 
 | ||||
|     public void setStock(Integer stock) { | ||||
|         this.stock = stock; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "ProductModel{" + | ||||
|                 "name='" + name + '\'' + | ||||
|                 ", description='" + description + '\'' + | ||||
|                 ", status='" + status + '\'' + | ||||
|                 ", currency='" + currency + '\'' + | ||||
|                 ", price=" + price + | ||||
|                 ", image_url=" + image_url + | ||||
|                 ", video_url=" + video_url + | ||||
|                 ", stock=" + stock + | ||||
|                 '}'; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| package com.baeldung.graphqlvsrest.repository; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Order; | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public interface OrderRepository { | ||||
|     List<Order> getOrdersByProduct(Integer productId); | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| package com.baeldung.graphqlvsrest.repository; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public interface ProductRepository { | ||||
|     List<Product> getProducts(Integer pageSize, Integer pageNumber); | ||||
|     Product getProduct(Integer id); | ||||
|     Product save(ProductModel productModel); | ||||
|     Product update(Integer productId, ProductModel productModel); | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| package com.baeldung.graphqlvsrest.repository.impl; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Order; | ||||
| import com.baeldung.graphqlvsrest.repository.OrderRepository; | ||||
| import org.springframework.stereotype.Repository; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| import java.util.UUID; | ||||
| import java.util.stream.Collectors; | ||||
| 
 | ||||
| @Repository | ||||
| public class OrderRepositoryImpl implements OrderRepository { | ||||
| 
 | ||||
|     private static List<Order> orderList = new ArrayList<>(); | ||||
| 
 | ||||
|     public OrderRepositoryImpl() { | ||||
|         for (int i = 1; i <= 100; i++){ | ||||
|             Order order = new Order(); | ||||
|             order.setId(i); | ||||
|             order.setProduct_id(i%10); | ||||
|             order.setAddress(UUID.randomUUID().toString()); | ||||
|             order.setCustomer_uuid(UUID.randomUUID().toString()); | ||||
|             order.setCreation_date(new Date(System.currentTimeMillis()).toString()); | ||||
|             order.setStatus("Delivered"); | ||||
|             orderList.add(order); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public List<Order> getOrdersByProduct(Integer productId) { | ||||
|         return orderList.stream().filter(order -> order.getProduct_id().equals(productId)).collect(Collectors.toList()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,74 @@ | ||||
| package com.baeldung.graphqlvsrest.repository.impl; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import org.springframework.stereotype.Repository; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
| import java.util.stream.Collectors; | ||||
| 
 | ||||
| @Repository | ||||
| public class ProductRepositoryImpl implements ProductRepository { | ||||
| 
 | ||||
|     private static List<Product> productList = new ArrayList<>(); | ||||
| 
 | ||||
|     public ProductRepositoryImpl() { | ||||
|         for (int i = 1; i <= 10; i++){ | ||||
|             Product product = new Product(); | ||||
|             product.setId(i); | ||||
|             product.setName(String.format("Product %d", i)); | ||||
|             product.setDescription(String.format("Product %d description", i)); | ||||
|             product.setCurrency(String.format("Product %d currency", i)); | ||||
|             product.setPrice(Double.valueOf(i^2)); | ||||
|             product.setStock(10); | ||||
|             product.setAverage_rating(0F); | ||||
|             product.setImage_url(Arrays.asList(String.format("www.baeldung.com/imageurl/%d", i))); | ||||
|             product.setVideo_url(Arrays.asList(String.format("www.baeldung.com/videourl/%d", i))); | ||||
|             productList.add(product); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<Product> getProducts(Integer pageSize, Integer pageNumber) { | ||||
|         return productList.stream().skip(pageSize*pageNumber).limit(pageSize).collect(Collectors.toList()); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Product getProduct(Integer id) { | ||||
|         return productList.stream().filter(product -> product.getId().equals(id)).findFirst().orElse(null); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Product save(ProductModel productModel) { | ||||
|         Product product = new Product(productList.size()+1, productModel); | ||||
|         productList.add(product); | ||||
|         return product; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Product update(Integer productId, ProductModel productModel) { | ||||
|         Product product =  getProduct(productId); | ||||
|         if (product != null){ | ||||
|             update(product, productModel); | ||||
|         } | ||||
|         return product; | ||||
|     } | ||||
| 
 | ||||
|     private void update(Product product, ProductModel productModel){ | ||||
|         if (productModel != null) { | ||||
|             System.out.println(productModel.toString()); | ||||
|             Optional.ofNullable(productModel.getName()).ifPresent(product::setName); | ||||
|             Optional.ofNullable(productModel.getDescription()).ifPresent(product::setDescription); | ||||
|             Optional.ofNullable(productModel.getCurrency()).ifPresent(product::setCurrency); | ||||
|             Optional.ofNullable(productModel.getImage_url()).ifPresent(product::setImage_url); | ||||
|             Optional.ofNullable(productModel.getStock()).ifPresent(product::setStock); | ||||
|             Optional.ofNullable(productModel.getStatus()).ifPresent(product::setStatus); | ||||
|             Optional.ofNullable(productModel.getVideo_url()).ifPresent(product::setVideo_url); | ||||
|             Optional.ofNullable(productModel.getPrice()).ifPresent(product::setPrice); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| package com.baeldung.graphqlvsrest.resolver; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.model.ProductModel; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import com.coxautodev.graphql.tools.GraphQLMutationResolver; | ||||
| 
 | ||||
| public class Mutation implements GraphQLMutationResolver { | ||||
| 
 | ||||
|     private ProductRepository productRepository; | ||||
|     public Mutation(ProductRepository productRepository){ | ||||
|         this.productRepository = productRepository; | ||||
|     } | ||||
| 
 | ||||
|     public Product saveProduct(ProductModel productModel) { | ||||
|         return productRepository.save(productModel); | ||||
|     } | ||||
| 
 | ||||
|     public Product updateProduct(Integer productId, ProductModel productModel) { | ||||
|         return productRepository.update(productId, productModel); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,18 @@ | ||||
| package com.baeldung.graphqlvsrest.resolver; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Order; | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.repository.OrderRepository; | ||||
| import com.coxautodev.graphql.tools.GraphQLResolver; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class ProductResolver implements GraphQLResolver<Product> { | ||||
|     private OrderRepository orderRepository; | ||||
|     public ProductResolver(OrderRepository orderRepository){ | ||||
|         this.orderRepository = orderRepository; | ||||
|     } | ||||
|     public List<Order> getOrders(Product product){ | ||||
|         return orderRepository.getOrdersByProduct(product.getId()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,27 @@ | ||||
| package com.baeldung.graphqlvsrest.resolver; | ||||
| 
 | ||||
| import com.baeldung.graphqlvsrest.entity.Order; | ||||
| import com.baeldung.graphqlvsrest.entity.Product; | ||||
| import com.baeldung.graphqlvsrest.repository.OrderRepository; | ||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||
| import com.coxautodev.graphql.tools.GraphQLQueryResolver; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Query implements GraphQLQueryResolver { | ||||
| 
 | ||||
|     private ProductRepository productRepository; | ||||
|     public Query(ProductRepository productRepository){ | ||||
|         this.productRepository = productRepository; | ||||
|     } | ||||
| 
 | ||||
|     public List<Product> getProducts(int pageSize, int pageNumber) { | ||||
|         return productRepository.getProducts(pageSize, pageNumber); | ||||
|     } | ||||
| 
 | ||||
|     public Product getProduct(int id) { | ||||
|         return productRepository.getProduct(id); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,57 @@ | ||||
| type Product { | ||||
|     id: ID | ||||
|     name: String! | ||||
|     description: String | ||||
|     status: String | ||||
|     currency: String! | ||||
|     price: Float | ||||
|     image_url: [String] | ||||
|     video_url: [String] | ||||
|     stock: Int | ||||
|     average_rating: Float | ||||
|     orders:[Order] | ||||
| } | ||||
| 
 | ||||
| type Order{ | ||||
|     id:ID | ||||
|     product_id:Int | ||||
|     customer_uuid:String | ||||
|     address:String | ||||
|     status:String | ||||
|     creation_date:String | ||||
| } | ||||
| 
 | ||||
| input ProductModel { | ||||
|     name: String! | ||||
|     description: String | ||||
|     status: String | ||||
|     currency: String! | ||||
|     price: Float | ||||
|     image_url: [String] | ||||
|     video_url: [String] | ||||
|     stock: Int | ||||
| } | ||||
| 
 | ||||
| input ProductUpdateModel { | ||||
|     name: String | ||||
|     description: String | ||||
|     status: String | ||||
|     currency: String | ||||
|     price: Float | ||||
|     image_url: [String] | ||||
|     video_url: [String] | ||||
|     stock: Int | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| # The Root Query for the application | ||||
| type Query { | ||||
|     products(size: Int, page: Int): [Product]! | ||||
|     product(id: Int): Product! | ||||
| } | ||||
| 
 | ||||
| # The Root Mutation for the application | ||||
| type Mutation { | ||||
|     saveProduct(product: ProductModel) : Product! | ||||
|     updateProduct(id: Int, product: ProductUpdateModel) : Product! | ||||
| } | ||||
| @ -102,6 +102,7 @@ | ||||
|         <!-- used only in dependency management to force this version, not included as a direct dependency --> | ||||
|         <junit.version>4.13.2</junit.version> | ||||
|         <junit-jupiter.version>5.8.1</junit-jupiter.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -70,6 +70,7 @@ | ||||
|         <maven.compiler.target>1.8</maven.compiler.target> | ||||
|         <eclipse.birt.runtime.version>4.8.0</eclipse.birt.runtime.version> | ||||
|         <log4j.version>1.2.17</log4j.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -11,6 +11,18 @@ | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
| @ -128,6 +140,7 @@ | ||||
|         <yarn.version>v1.12.1</yarn.version> | ||||
|         <spring-boot.version>2.4.4</spring-boot.version> | ||||
|         <javafaker.version>1.0.2</javafaker.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -10,6 +10,7 @@ This module contains articles about Spring Boot Security | ||||
| - [Guide to @CurrentSecurityContext in Spring Security](https://www.baeldung.com/spring-currentsecuritycontext) | ||||
| - [Disable Security for a Profile in Spring Boot](https://www.baeldung.com/spring-security-disable-profile) | ||||
| - [Spring @EnableWebSecurity vs. @EnableGlobalMethodSecurity](https://www.baeldung.com/spring-enablewebsecurity-vs-enableglobalmethodsecurity) | ||||
| - [Spring Security – Configuring Different URLs](https://www.baeldung.com/spring-security-configuring-urls) | ||||
| 
 | ||||
| 
 | ||||
| ### Spring Boot Security Auto-Configuration | ||||
|  | ||||
| @ -24,6 +24,13 @@ | ||||
|                 <type>pom</type> | ||||
|                 <scope>import</scope> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
| @ -62,6 +69,7 @@ | ||||
|         <spring-boot.version>2.4.5</spring-boot.version> | ||||
|         <springfox.version>3.0.0</springfox.version> | ||||
|         <keycloak.version>15.0.2</keycloak.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
|  | ||||
| @ -5,3 +5,4 @@ | ||||
| - [Generate PDF from Swagger API Documentation](https://www.baeldung.com/swagger-generate-pdf) | ||||
| - [Remove Basic Error Controller In SpringFox Swagger-UI](https://www.baeldung.com/spring-swagger-remove-error-controller) | ||||
| - [Setting Example and Description with Swagger](https://www.baeldung.com/swagger-set-example-description) | ||||
| - [Document Enum in Swagger](https://www.baeldung.com/swagger-enum) | ||||
|  | ||||
| @ -14,6 +14,18 @@ | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
| @ -131,6 +143,7 @@ | ||||
|         <gmavenplus-plugin.version>1.6</gmavenplus-plugin.version> | ||||
|         <redis.version>0.7.2</redis.version> | ||||
|         <spring-boot.version>2.5.0</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -21,6 +21,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.boot</groupId> | ||||
|                 <artifactId>spring-boot-starter-web</artifactId> | ||||
| @ -55,6 +62,7 @@ | ||||
|     <properties> | ||||
|         <spring-cloud.version>2.1.1.RELEASE</spring-cloud.version> | ||||
|         <spring-boot.version>2.1.4.RELEASE</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -14,6 +14,18 @@ | ||||
|         <artifactId>spring-cloud</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <modules> | ||||
|         <module>spring-cloud-eureka-server</module> | ||||
| @ -32,6 +44,7 @@ | ||||
|     <properties> | ||||
|         <spring-boot.version>2.1.3.RELEASE</spring-boot.version> | ||||
|         <spring-cloud-dependencies.version>Greenwich.SR3</spring-cloud-dependencies.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -14,6 +14,18 @@ | ||||
|         <artifactId>spring-cloud</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <modules> | ||||
|         <module>spring-cloud-eureka-server</module> | ||||
| @ -34,6 +46,7 @@ | ||||
|     <properties> | ||||
|         <spring-boot.version>2.1.2.RELEASE</spring-boot.version> | ||||
|         <spring-cloud-dependencies.version>Greenwich.RELEASE</spring-cloud-dependencies.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -19,6 +19,18 @@ | ||||
|         <module>spring-cloud-loadbalancer-server</module> | ||||
|         <module>spring-cloud-loadbalancer-client</module> | ||||
|     </modules> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -32,6 +44,7 @@ | ||||
|     <properties> | ||||
|         <spring-boot.version>2.6.1</spring-boot.version> | ||||
|         <spring-cloud.version>2021.0.0</spring-cloud.version> | ||||
|          <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
|  | ||||
| @ -4,3 +4,4 @@ | ||||
| - [Differences Between Netflix Feign and OpenFeign](https://www.baeldung.com/netflix-feign-vs-openfeign) | ||||
| - [File Upload With Open Feign](https://www.baeldung.com/java-feign-file-upload) | ||||
| - [Feign Logging Configuration](https://www.baeldung.com/java-feign-logging) | ||||
| - [Provide an OAuth2 Token to a Feign Client](https://www.baeldung.com/spring-cloud-feign-oauth-token) | ||||
|  | ||||
| @ -78,6 +78,7 @@ | ||||
|         <!-- used only in dependency management to force this version, not included as a direct dependency --> | ||||
|         <junit.version>4.13.2</junit.version> | ||||
|         <junit-jupiter.version>5.8.1</junit-jupiter.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -17,6 +17,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.cloud</groupId> | ||||
|                 <artifactId>spring-cloud-starter-parent</artifactId> | ||||
| @ -45,5 +52,9 @@ | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
|      | ||||
|     <properties> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -17,6 +17,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.cloud</groupId> | ||||
|                 <artifactId>spring-cloud-starter-parent</artifactId> | ||||
| @ -45,5 +52,9 @@ | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
|      | ||||
|     <properties> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -14,6 +14,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.cloud</groupId> | ||||
|                 <artifactId>spring-cloud-starter-parent</artifactId> | ||||
| @ -53,5 +60,9 @@ | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
|      | ||||
|      <properties> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -13,6 +13,18 @@ | ||||
|         <artifactId>spring-cloud-zuul</artifactId> | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -44,6 +56,7 @@ | ||||
|         <rate.limit.version>2.2.0.RELEASE</rate.limit.version> | ||||
|         <spring-boot.version>2.4.7</spring-boot.version> | ||||
|         <spring-cloud-dependencies.version>2020.0.4</spring-cloud-dependencies.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -16,6 +16,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.boot</groupId> | ||||
|                 <artifactId>spring-boot-dependencies</artifactId> | ||||
| @ -80,6 +87,7 @@ | ||||
|         <spring-boot.version>2.6.1</spring-boot.version> | ||||
|         <aspectj-plugin.version>1.11</aspectj-plugin.version> | ||||
|         <javax.inject.version>1</javax.inject.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -5,4 +5,5 @@ This module contains articles about dependency injection with Spring | ||||
| ### Relevant Articles | ||||
| 
 | ||||
| - [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup) | ||||
| - [Spring @Autowired Field Null – Common Causes and Solutions](https://www.baeldung.com/spring-autowired-field-null) | ||||
| - More articles: [[<-- prev]](../spring-di-2) | ||||
|  | ||||
| @ -16,6 +16,13 @@ | ||||
| 
 | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.boot</groupId> | ||||
|                 <artifactId>spring-boot-dependencies</artifactId> | ||||
| @ -40,6 +47,7 @@ | ||||
| 
 | ||||
|     <properties> | ||||
|         <spring-boot.version>2.6.1</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -75,6 +75,7 @@ | ||||
|         <java.version>1.8</java.version> | ||||
|         <maven.compiler.target>1.8</maven.compiler.target> | ||||
|         <maven.compiler.source>1.8</maven.compiler.source> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -625,6 +625,7 @@ | ||||
|         <bootstrap.ie10-viewport-bug-workaround.version>1.0.3</bootstrap.ie10-viewport-bug-workaround.version> | ||||
|         <querydsl-processor.version>2.0.0.RELEASE</querydsl-processor.version> | ||||
|         <olap4j.version>1.2.0</olap4j.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -15,6 +15,19 @@ | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../../parent-boot-2</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|      <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
|      | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -72,6 +85,7 @@ | ||||
|             is available --> | ||||
|         <spring-boot.version>2.5.2</spring-boot.version> | ||||
|         <start-class>com.baeldung.oauth2.SpringOAuthApplication</start-class> | ||||
|          <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -14,6 +14,18 @@ | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../../parent-boot-2</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|      <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|         </dependencies> | ||||
|     </dependencyManagement> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -50,6 +62,7 @@ | ||||
|         <spring-security-jwt.version>1.0.9.RELEASE</spring-security-jwt.version> | ||||
|         <jwks-rsa.version>0.3.0</jwks-rsa.version> | ||||
|         <spring-boot.version>2.4.7</spring-boot.version> | ||||
|          <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,16 @@ | ||||
| package com.baeldung.logging; | ||||
| 
 | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.stereotype.Controller; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| 
 | ||||
| @Controller | ||||
| public class LoggingController { | ||||
| 
 | ||||
|     @GetMapping("/logging") | ||||
|     public ResponseEntity<String> logging() { | ||||
|         return new ResponseEntity<>("logging/baeldung", HttpStatus.OK); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,26 @@ | ||||
| package com.baeldung.logging; | ||||
| 
 | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||||
| import org.springframework.security.config.annotation.web.builders.WebSecurity; | ||||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||||
| 
 | ||||
| @EnableWebSecurity | ||||
| public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||||
| 
 | ||||
|     @Value("${spring.websecurity.debug:false}") | ||||
|     boolean webSecurityDebug; | ||||
| 
 | ||||
|     @Override | ||||
|     public void configure(WebSecurity web) { | ||||
|         web.debug(webSecurityDebug); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void configure(HttpSecurity http) throws Exception { | ||||
|         http.authorizeRequests() | ||||
|             .antMatchers("/**") | ||||
|             .permitAll(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| package com.baeldung.logging; | ||||
| 
 | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| 
 | ||||
| @SpringBootApplication | ||||
| public class SecurityLoggingApplication { | ||||
| 
 | ||||
|     public static void main(String... args) { | ||||
|         SpringApplication application = new SpringApplication(SecurityLoggingApplication.class); | ||||
|         application.setAdditionalProfiles("logging"); | ||||
|         application.run(args); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,4 @@ | ||||
| 
 | ||||
| logging.level.org.springframework.security=DEBUG | ||||
| 
 | ||||
| spring.websecurity.debug=true | ||||
| @ -100,6 +100,7 @@ | ||||
|     <properties> | ||||
|         <java.version>11</java.version> | ||||
|         <springfox-version>3.0.0</springfox-version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -23,6 +23,13 @@ | ||||
|                 <type>pom</type> | ||||
|                 <scope>import</scope> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.apache.logging.log4j</groupId> | ||||
|                 <artifactId>log4j-bom</artifactId> | ||||
|                 <version>${log4j2.version}</version> | ||||
|                 <scope>import</scope> | ||||
|                 <type>pom</type> | ||||
|             </dependency> | ||||
|             <dependency> | ||||
|                 <groupId>org.springframework.boot</groupId> | ||||
|                 <artifactId>spring-boot-dependencies</artifactId> | ||||
| @ -92,6 +99,7 @@ | ||||
|     <properties> | ||||
|         <jstl.version>1.2</jstl.version> | ||||
|         <spring-boot.version>2.4.4</spring-boot.version> | ||||
|         <log4j2.version>2.17.1</log4j2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user