diff --git a/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java b/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java new file mode 100644 index 0000000000..102ceda18f --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java @@ -0,0 +1,58 @@ +package com.baeldung.java8; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +public class Java9OptionalsStreamTest { + + private static List> listOfOptionals = Arrays.asList(Optional.empty(), Optional.of("foo"), Optional.empty(), Optional.of("bar")); + + @Test + public void filterOutPresentOptionalsWithFilter() { + assertEquals(4, listOfOptionals.size()); + + List filteredList = listOfOptionals.stream() + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + + assertEquals(2, filteredList.size()); + assertEquals("foo", filteredList.get(0)); + assertEquals("bar", filteredList.get(1)); + } + + @Test + public void filterOutPresentOptionalsWithFlatMap() { + assertEquals(4, listOfOptionals.size()); + + List filteredList = listOfOptionals.stream() + .flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty()) + .collect(Collectors.toList()); + assertEquals(2, filteredList.size()); + + assertEquals("foo", filteredList.get(0)); + assertEquals("bar", filteredList.get(1)); + } + + @Test + public void filterOutPresentOptionalsWithJava9() { + assertEquals(4, listOfOptionals.size()); + + List filteredList = listOfOptionals.stream() + .flatMap(Optional::stream) + .collect(Collectors.toList()); + + assertEquals(2, filteredList.size()); + assertEquals("foo", filteredList.get(0)); + assertEquals("bar", filteredList.get(1)); + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java b/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java index 9cafa179ab..30b0111555 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/ArrayListTest.java @@ -18,18 +18,18 @@ public class ArrayListTest { @Before public void setUp() { - List xs = LongStream.range(0, 16) + List list = LongStream.range(0, 16) .boxed() .map(Long::toHexString) .collect(toCollection(ArrayList::new)); - stringsToSearch = new ArrayList<>(xs); - stringsToSearch.addAll(xs); + stringsToSearch = new ArrayList<>(list); + stringsToSearch.addAll(list); } @Test public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() { - List xs = new ArrayList<>(); - assertTrue(xs.isEmpty()); + List list = new ArrayList<>(); + assertTrue(list.isEmpty()); } @Test @@ -37,29 +37,29 @@ public class ArrayListTest { Collection numbers = IntStream.range(0, 10).boxed().collect(toSet()); - List xs = new ArrayList<>(numbers); - assertEquals(10, xs.size()); - assertTrue(numbers.containsAll(xs)); + List list = new ArrayList<>(numbers); + assertEquals(10, list.size()); + assertTrue(numbers.containsAll(list)); } @Test public void givenElement_whenAddToArrayList_thenIsAdded() { - List xs = new ArrayList<>(); + List list = new ArrayList<>(); - xs.add(1L); - xs.add(2L); - xs.add(1, 3L); + list.add(1L); + list.add(2L); + list.add(1, 3L); - assertThat(Arrays.asList(1L, 3L, 2L), equalTo(xs)); + assertThat(Arrays.asList(1L, 3L, 2L), equalTo(list)); } @Test public void givenCollection_whenAddToArrayList_thenIsAdded() { - List xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L)); + List list = new ArrayList<>(Arrays.asList(1L, 2L, 3L)); LongStream.range(4, 10).boxed() - .collect(collectingAndThen(toCollection(ArrayList::new), ys -> xs.addAll(0, ys))); + .collect(collectingAndThen(toCollection(ArrayList::new), ys -> list.addAll(0, ys))); - assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs)); + assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(list)); } @Test @@ -106,27 +106,27 @@ public class ArrayListTest { @Test public void givenIndex_whenRemove_thenCorrectElementRemoved() { - List xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); - Collections.reverse(xs); + List list = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); + Collections.reverse(list); - xs.remove(0); - assertThat(xs.get(0), equalTo(8)); + list.remove(0); + assertThat(list.get(0), equalTo(8)); - xs.remove(Integer.valueOf(0)); - assertFalse(xs.contains(0)); + list.remove(Integer.valueOf(0)); + assertFalse(list.contains(0)); } @Test public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() { - List xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); - ListIterator it = xs.listIterator(xs.size()); - List result = new ArrayList<>(xs.size()); + List list = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new)); + ListIterator it = list.listIterator(list.size()); + List result = new ArrayList<>(list.size()); while (it.hasPrevious()) { result.add(it.previous()); } - Collections.reverse(xs); - assertThat(result, equalTo(xs)); + Collections.reverse(list); + assertThat(result, equalTo(list)); } @Test diff --git a/feign-client/README.md b/feign-client/README.md new file mode 100644 index 0000000000..e6ade4d161 --- /dev/null +++ b/feign-client/README.md @@ -0,0 +1,5 @@ +## Feign Hypermedia Client ## + +This is the implementation of a [spring-hypermedia-api][1] client using Feign. + +[1]: https://github.com/eugenp/spring-hypermedia-api diff --git a/feign-client/pom.xml b/feign-client/pom.xml new file mode 100644 index 0000000000..af61883f1b --- /dev/null +++ b/feign-client/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.baeldung.feign + feign-client + 1.0.0-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + + + + + io.github.openfeign + feign-core + 9.3.1 + + + io.github.openfeign + feign-okhttp + 9.3.1 + + + io.github.openfeign + feign-gson + 9.3.1 + + + io.github.openfeign + feign-slf4j + 9.3.1 + + + org.slf4j + slf4j-api + 1.7.21 + + + org.apache.logging.log4j + log4j-core + 2.6.2 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.6.2 + + + org.projectlombok + lombok + 1.16.10 + provided + + + junit + junit + 4.12 + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + true + + + + org.springframework.boot + spring-boot-maven-plugin + 1.4.0.RELEASE + + + + + diff --git a/feign-client/src/main/java/com/baeldung/feign/BookControllerFeignClientBuilder.java b/feign-client/src/main/java/com/baeldung/feign/BookControllerFeignClientBuilder.java new file mode 100644 index 0000000000..9c0c359d88 --- /dev/null +++ b/feign-client/src/main/java/com/baeldung/feign/BookControllerFeignClientBuilder.java @@ -0,0 +1,26 @@ +package com.baeldung.feign; + +import com.baeldung.feign.clients.BookClient; +import feign.Feign; +import feign.Logger; +import feign.gson.GsonDecoder; +import feign.gson.GsonEncoder; +import feign.okhttp.OkHttpClient; +import feign.slf4j.Slf4jLogger; +import lombok.Getter; + +@Getter +public class BookControllerFeignClientBuilder { + private BookClient bookClient = createClient(BookClient.class, + "http://localhost:8081/api/books"); + + private static T createClient(Class type, String uri) { + return Feign.builder() + .client(new OkHttpClient()) + .encoder(new GsonEncoder()) + .decoder(new GsonDecoder()) + .logger(new Slf4jLogger(type)) + .logLevel(Logger.Level.FULL) + .target(type, uri); + } +} diff --git a/feign-client/src/main/java/com/baeldung/feign/clients/BookClient.java b/feign-client/src/main/java/com/baeldung/feign/clients/BookClient.java new file mode 100644 index 0000000000..df20ef8f93 --- /dev/null +++ b/feign-client/src/main/java/com/baeldung/feign/clients/BookClient.java @@ -0,0 +1,21 @@ +package com.baeldung.feign.clients; + +import com.baeldung.feign.models.Book; +import com.baeldung.feign.models.BookResource; +import feign.Headers; +import feign.Param; +import feign.RequestLine; + +import java.util.List; + +public interface BookClient { + @RequestLine("GET /{isbn}") + BookResource findByIsbn(@Param("isbn") String isbn); + + @RequestLine("GET") + List findAll(); + + @RequestLine("POST") + @Headers("Content-Type: application/json") + void create(Book book); +} diff --git a/feign-client/src/main/java/com/baeldung/feign/models/Book.java b/feign-client/src/main/java/com/baeldung/feign/models/Book.java new file mode 100644 index 0000000000..cda4412e27 --- /dev/null +++ b/feign-client/src/main/java/com/baeldung/feign/models/Book.java @@ -0,0 +1,18 @@ +package com.baeldung.feign.models; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class Book { + private String isbn; + private String author; + private String title; + private String synopsis; + private String language; +} diff --git a/feign-client/src/main/java/com/baeldung/feign/models/BookResource.java b/feign-client/src/main/java/com/baeldung/feign/models/BookResource.java new file mode 100644 index 0000000000..7902db9fe8 --- /dev/null +++ b/feign-client/src/main/java/com/baeldung/feign/models/BookResource.java @@ -0,0 +1,14 @@ +package com.baeldung.feign.models; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class BookResource { + private Book book; +} diff --git a/feign-client/src/main/resources/log4j2.xml b/feign-client/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..659c5fda0e --- /dev/null +++ b/feign-client/src/main/resources/log4j2.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/feign-client/src/test/java/com/baeldung/feign/clients/BookClientTest.java b/feign-client/src/test/java/com/baeldung/feign/clients/BookClientTest.java new file mode 100644 index 0000000000..e7e058a336 --- /dev/null +++ b/feign-client/src/test/java/com/baeldung/feign/clients/BookClientTest.java @@ -0,0 +1,58 @@ +package com.baeldung.feign.clients; + +import com.baeldung.feign.BookControllerFeignClientBuilder; +import com.baeldung.feign.models.Book; +import com.baeldung.feign.models.BookResource; +import lombok.extern.slf4j.Slf4j; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@Slf4j +@RunWith(JUnit4.class) +public class BookClientTest { + private BookClient bookClient; + + @Before + public void setup() { + BookControllerFeignClientBuilder feignClientBuilder = new BookControllerFeignClientBuilder(); + bookClient = feignClientBuilder.getBookClient(); + } + + @Test + public void givenBookClient_shouldRunSuccessfully() throws Exception { + List books = bookClient.findAll().stream() + .map(BookResource::getBook) + .collect(Collectors.toList()); + assertTrue(books.size() > 2); + log.info("{}", books); + } + + @Test + public void givenBookClient_shouldFindOneBook() throws Exception { + Book book = bookClient.findByIsbn("0151072558").getBook(); + assertThat(book.getAuthor(), containsString("Orwell")); + log.info("{}", book); + } + + @Test + public void givenBookClient_shouldPostBook() throws Exception { + String isbn = UUID.randomUUID().toString(); + Book book = new Book(isbn, "Me", "It's me!", null, null); + bookClient.create(book); + + book = bookClient.findByIsbn(isbn).getBook(); + assertThat(book.getAuthor(), is("Me")); + log.info("{}", book); + } +} diff --git a/flyway-migration/db/migration/V2_0__create_department_schema.sql b/flyway-migration/db/migration/V2_0__create_department_schema.sql new file mode 100644 index 0000000000..2b9d3364a5 --- /dev/null +++ b/flyway-migration/db/migration/V2_0__create_department_schema.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `department` ( + +`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, +`name` varchar(20) + +)ENGINE=InnoDB DEFAULT CHARSET=UTF8; + +ALTER TABLE `employee` ADD `dept_id` int AFTER `email`; \ No newline at end of file diff --git a/flyway-migration/pom.xml b/flyway-migration/pom.xml index e3e29cd43f..6e9d683a0e 100644 --- a/flyway-migration/pom.xml +++ b/flyway-migration/pom.xml @@ -23,6 +23,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.5.1 1.8 1.8 diff --git a/log4j/pom.xml b/log4j/pom.xml index 76c05b36c1..586769aa71 100644 --- a/log4j/pom.xml +++ b/log4j/pom.xml @@ -15,7 +15,13 @@ log4j 1.2.17 - + @@ -28,22 +34,34 @@ log4j-core 2.6 - - + com.lmax disruptor 3.3.4 - + - ch.qos.logback logback-classic 1.1.7 + + diff --git a/log4j/src/main/java/com/baeldung/slf4j/Slf4jExample.java b/log4j/src/main/java/com/baeldung/slf4j/Slf4jExample.java new file mode 100644 index 0000000000..6ecc7b887a --- /dev/null +++ b/log4j/src/main/java/com/baeldung/slf4j/Slf4jExample.java @@ -0,0 +1,20 @@ +package com.baeldung.slf4j; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * To switch between logging frameworks you need only to uncomment needed framework dependencies in pom.xml + */ +public class Slf4jExample { + private static Logger logger = LoggerFactory.getLogger(Slf4jExample.class); + + public static void main(String[] args) { + logger.debug("Debug log message"); + logger.info("Info log message"); + logger.error("Error log message"); + + String variable = "Hello John"; + logger.debug("Printing variable value {} ", variable); + } +} diff --git a/pom.xml b/pom.xml index d37be9136e..8115c1e902 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ deltaspike enterprise-patterns + feign-client gson @@ -129,6 +130,7 @@ lombok redis + wicket xstream java-cassandra diff --git a/selenium-junit-testng/pom.xml b/selenium-junit-testng/pom.xml new file mode 100644 index 0000000000..c6bd2b042c --- /dev/null +++ b/selenium-junit-testng/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + selenium-junit-testng + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + + + org.seleniumhq.selenium + selenium-java + 2.53.1 + + + junit + junit + 4.8.1 + + + org.testng + testng + 6.9.10 + + + \ No newline at end of file diff --git a/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java new file mode 100644 index 0000000000..6020b6bd2c --- /dev/null +++ b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java @@ -0,0 +1,29 @@ +package main.java.com.baeldung.selenium; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +public class SeleniumExample { + + private WebDriver webDriver; + private final String url = "http://www.baeldung.com/"; + private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + + public SeleniumExample() { + webDriver = new FirefoxDriver(); + webDriver.get(url); + } + + public void closeWindow() { + webDriver.close(); + } + + public String getActualTitle() { + return webDriver.getTitle(); + } + + public String getExpectedTitle() { + return expectedTitle; + } + +} diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java new file mode 100644 index 0000000000..371f730eb9 --- /dev/null +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java @@ -0,0 +1,30 @@ +package test.java.com.baeldung.selenium.junit; + +import static org.testng.Assert.assertEquals; +import main.java.com.baeldung.selenium.SeleniumExample; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestSeleniumWithJUnit { + + private SeleniumExample seleniumExample; + + @Before + public void setUp() { + seleniumExample = new SeleniumExample(); + } + + @After + public void tearDown() { + seleniumExample.closeWindow(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String expectedTitle = seleniumExample.getExpectedTitle(); + String actualTitle = seleniumExample.getActualTitle(); + assertEquals(actualTitle, expectedTitle); + } +} diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java new file mode 100644 index 0000000000..ec10f9ca82 --- /dev/null +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java @@ -0,0 +1,30 @@ +package test.java.com.baeldung.selenium.testng; + +import static org.testng.Assert.assertEquals; +import main.java.com.baeldung.selenium.SeleniumExample; + +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class TestSeleniumWithTestNG { + + private SeleniumExample seleniumExample; + + @BeforeSuite + public void setUp() { + seleniumExample = new SeleniumExample(); + } + + @AfterSuite + public void tearDown() { + seleniumExample.closeWindow(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String expectedTitle = seleniumExample.getExpectedTitle(); + String actualTitle = seleniumExample.getActualTitle(); + assertEquals(actualTitle, expectedTitle); + } +} diff --git a/selenium-junit-testng/test/com/baeldun/selenium/testng/TestSeleniumWithTestNG.java b/selenium-junit-testng/test/com/baeldun/selenium/testng/TestSeleniumWithTestNG.java new file mode 100644 index 0000000000..dcdfafc4f1 --- /dev/null +++ b/selenium-junit-testng/test/com/baeldun/selenium/testng/TestSeleniumWithTestNG.java @@ -0,0 +1,34 @@ +package com.baeldun.selenium.testng; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class TestSeleniumWithTestNG { + + private WebDriver webDriver; + private final String url = "http://www.baeldung.com/"; + private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + + @BeforeSuite + public void setUp() { + webDriver = new FirefoxDriver(); + webDriver.get(url); + } + + @AfterSuite + public void tearDown() { + webDriver.close(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String actualTitleReturned = webDriver.getTitle(); + assertNotNull(actualTitleReturned); + assertEquals(expectedTitle, actualTitleReturned); + } +} diff --git a/selenium-junit-testng/test/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java b/selenium-junit-testng/test/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java new file mode 100644 index 0000000000..a7b36c4e4e --- /dev/null +++ b/selenium-junit-testng/test/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java @@ -0,0 +1,34 @@ +package com.baeldung.selenium.junit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +public class TestSeleniumWithJUnit { + + private WebDriver webDriver; + private final String url = "http://www.baeldung.com/"; + private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + + @Before + public void setUp() { + webDriver = new FirefoxDriver(); + webDriver.get(url); + } + + @After + public void tearDown() { + webDriver.close(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String actualTitleReturned = webDriver.getTitle(); + assertNotNull(actualTitleReturned); + assertEquals(expectedTitle, actualTitleReturned); + } +} diff --git a/spring-cloud-config/client-config/config-client-development.properties b/spring-cloud-config/client-config/config-client-development.properties deleted file mode 100644 index 6401d1be7f..0000000000 --- a/spring-cloud-config/client-config/config-client-development.properties +++ /dev/null @@ -1,2 +0,0 @@ -user.role=Developer -user.password=pass diff --git a/spring-cloud-config/client-config/config-client-production.properties b/spring-cloud-config/client-config/config-client-production.properties deleted file mode 100644 index cd2e14fcc3..0000000000 --- a/spring-cloud-config/client-config/config-client-production.properties +++ /dev/null @@ -1 +0,0 @@ -user.role=User diff --git a/spring-cloud-config/docker/Dockerfile b/spring-cloud-config/docker/Dockerfile deleted file mode 100644 index bdb37abf80..0000000000 --- a/spring-cloud-config/docker/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM alpine:edge -MAINTAINER baeldung.com -RUN apk add --no-cache openjdk8 -COPY files/UnlimitedJCEPolicyJDK8/* /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/ diff --git a/spring-cloud-config/docker/docker-compose.scale.yml b/spring-cloud-config/docker/docker-compose.scale.yml deleted file mode 100644 index f74153bea3..0000000000 --- a/spring-cloud-config/docker/docker-compose.scale.yml +++ /dev/null @@ -1,41 +0,0 @@ -version: '2' -services: - config-server: - build: - context: . - dockerfile: Dockerfile.server - image: config-server:latest - expose: - - 8888 - networks: - - spring-cloud-network - volumes: - - spring-cloud-config-repo:/var/lib/spring-cloud/config-repo - logging: - driver: json-file - config-client: - build: - context: . - dockerfile: Dockerfile.client - image: config-client:latest - entrypoint: /opt/spring-cloud/bin/config-client-entrypoint.sh - environment: - SPRING_APPLICATION_JSON: '{"spring": {"cloud": {"config": {"uri": "http://config-server:8888"}}}}' - expose: - - 8080 - ports: - - 8080 - networks: - - spring-cloud-network - links: - - config-server:config-server - depends_on: - - config-server - logging: - driver: json-file -networks: - spring-cloud-network: - driver: bridge -volumes: - spring-cloud-config-repo: - external: true diff --git a/spring-cloud-config/docker/docker-compose.yml b/spring-cloud-config/docker/docker-compose.yml deleted file mode 100644 index 74c71b651c..0000000000 --- a/spring-cloud-config/docker/docker-compose.yml +++ /dev/null @@ -1,43 +0,0 @@ -version: '2' -services: - config-server: - container_name: config-server - build: - context: . - dockerfile: Dockerfile.server - image: config-server:latest - expose: - - 8888 - networks: - - spring-cloud-network - volumes: - - spring-cloud-config-repo:/var/lib/spring-cloud/config-repo - logging: - driver: json-file - config-client: - container_name: config-client - build: - context: . - dockerfile: Dockerfile.client - image: config-client:latest - entrypoint: /opt/spring-cloud/bin/config-client-entrypoint.sh - environment: - SPRING_APPLICATION_JSON: '{"spring": {"cloud": {"config": {"uri": "http://config-server:8888"}}}}' - expose: - - 8080 - ports: - - 8080:8080 - networks: - - spring-cloud-network - links: - - config-server:config-server - depends_on: - - config-server - logging: - driver: json-file -networks: - spring-cloud-network: - driver: bridge -volumes: - spring-cloud-config-repo: - external: true diff --git a/spring-cloud-config/spring-cloud-config-client/pom.xml b/spring-cloud-config/spring-cloud-config-client/pom.xml deleted file mode 100644 index 968489bce2..0000000000 --- a/spring-cloud-config/spring-cloud-config-client/pom.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - 4.0.0 - - - com.baeldung.spring.cloud - spring-cloud-config - 1.0.0-SNAPSHOT - - spring-cloud-config-client - jar - - spring-cloud-config-client - - - - org.springframework.cloud - spring-cloud-starter-config - 1.1.3.RELEASE - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - ../docker/files - - - - - diff --git a/spring-cloud-config/spring-cloud-config-client/src/main/java/com/baeldung/spring/cloud/config/client/ConfigClient.java b/spring-cloud-config/spring-cloud-config-client/src/main/java/com/baeldung/spring/cloud/config/client/ConfigClient.java deleted file mode 100644 index 1dd3bbdab0..0000000000 --- a/spring-cloud-config/spring-cloud-config-client/src/main/java/com/baeldung/spring/cloud/config/client/ConfigClient.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.spring.cloud.config.client; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@RestController -public class ConfigClient { - @Value("${user.role}") - private String role; - - @Value("${user.password}") - private String password; - - public static void main(String[] args) { - SpringApplication.run(ConfigClient.class, args); - } - - @RequestMapping(value = "/whoami/{username}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) - public String whoami(@PathVariable("username") String username) { - return String.format("Hello %s! You are a(n) %s and your password is '%s'.\n", username, role, password); - } -} diff --git a/spring-cloud-config/spring-cloud-config-client/src/main/resources/bootstrap.properties b/spring-cloud-config/spring-cloud-config-client/src/main/resources/bootstrap.properties deleted file mode 100644 index 6c350bffcd..0000000000 --- a/spring-cloud-config/spring-cloud-config-client/src/main/resources/bootstrap.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.application.name=config-client -spring.profiles.active=development -spring.cloud.config.uri=http://localhost:${PORT:8888} -spring.cloud.config.username=root -spring.cloud.config.password=s3cr3t diff --git a/spring-cloud-config/spring-cloud-config-server/pom.xml b/spring-cloud-config/spring-cloud-config-server/pom.xml deleted file mode 100644 index f2b8b69a6a..0000000000 --- a/spring-cloud-config/spring-cloud-config-server/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - 4.0.0 - - - com.baeldung.spring.cloud - spring-cloud-config - 1.0.0-SNAPSHOT - - spring-cloud-config-server - - spring-cloud-config-server - - - - org.springframework.cloud - spring-cloud-config-server - 1.1.3.RELEASE - - - org.springframework.boot - spring-boot-starter-security - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - ../docker/files - - - - - diff --git a/spring-cloud-config/spring-cloud-config-server/src/main/resources/application.properties b/spring-cloud-config/spring-cloud-config-server/src/main/resources/application.properties deleted file mode 100644 index c4f57f0a82..0000000000 --- a/spring-cloud-config/spring-cloud-config-server/src/main/resources/application.properties +++ /dev/null @@ -1,10 +0,0 @@ -server.port=${PORT:8888} -spring.cloud.config.server.git.uri=${CONFIG_REPO} -spring.cloud.config.server.git.clone-on-start=false -spring.cloud.config.fail-fast=true -security.user.name=root -security.user.password=s3cr3t -encrypt.key-store.location=classpath:/config-server.jks -encrypt.key-store.password=my-s70r3-s3cr3t -encrypt.key-store.alias=config-server-key -encrypt.key-store.secret=my-k34-s3cr3t diff --git a/spring-cloud-config/spring-cloud-config-server/src/main/resources/config-server.jks b/spring-cloud-config/spring-cloud-config-server/src/main/resources/config-server.jks deleted file mode 100644 index f3dddb4a8f..0000000000 Binary files a/spring-cloud-config/spring-cloud-config-server/src/main/resources/config-server.jks and /dev/null differ diff --git a/spring-cloud-eureka/pom.xml b/spring-cloud-eureka/pom.xml deleted file mode 100644 index 86e0354070..0000000000 --- a/spring-cloud-eureka/pom.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - 4.0.0 - - com.baeldung.spring.cloud - spring-cloud-eureka - 1.0.0-SNAPSHOT - - spring-cloud-eureka-server - spring-cloud-eureka-client - spring-cloud-eureka-feign-client - - pom - - Spring Cloud Eureka - Spring Cloud Eureka Server and Sample Clients - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - .. - - - - UTF-8 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.springframework.boot - spring-boot-maven-plugin - 1.4.0.RELEASE - - - - - diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java deleted file mode 100644 index 48099eeaa2..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.cloud.eureka.client; - -import com.netflix.discovery.EurekaClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.context.annotation.Lazy; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@EnableEurekaClient -@RestController -public class EurekaClientApplication implements GreetingController { - @Autowired - @Lazy - private EurekaClient eurekaClient; - - @Value("${spring.application.name}") - private String appName; - - public static void main(String[] args) { - SpringApplication.run(EurekaClientApplication.class, args); - } - - @Override - public String greeting() { - return String.format("Hello from '%s'!", eurekaClient.getApplication(appName).getName()); - } -} diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java deleted file mode 100644 index 33ee2574b7..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.spring.cloud.eureka.client; - -import org.springframework.web.bind.annotation.RequestMapping; - -public interface GreetingController { - @RequestMapping("/greeting") - String greeting(); -} diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml deleted file mode 100644 index 08624aa159..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml +++ /dev/null @@ -1,13 +0,0 @@ -spring: - application: - name: spring-cloud-eureka-client - -server: - port: 0 - -eureka: - client: - serviceUrl: - defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} - instance: - preferIpAddress: true \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml b/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml deleted file mode 100644 index 9e639c666a..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - 4.0.0 - - spring-cloud-eureka-feign-client - 1.0.0-SNAPSHOT - jar - - Spring Cloud Eureka Feign Client - Spring Cloud Eureka - Sample Feign Client - - - com.baeldung.spring.cloud - spring-cloud-eureka - 1.0.0-SNAPSHOT - .. - - - - - com.baeldung.spring.cloud - spring-cloud-eureka-client - 1.0.0-SNAPSHOT - - - org.springframework.cloud - spring-cloud-starter-feign - 1.1.5.RELEASE - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-thymeleaf - 1.4.0.RELEASE - - - - - - - org.springframework.cloud - spring-cloud-starter-parent - Brixton.SR4 - pom - import - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java deleted file mode 100644 index 7beb51d1ac..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.spring.cloud.feign.client; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.cloud.netflix.feign.EnableFeignClients; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; - -@SpringBootApplication -@EnableEurekaClient -@EnableFeignClients -@Controller -public class FeignClientApplication { - @Autowired - private GreetingClient greetingClient; - - public static void main(String[] args) { - SpringApplication.run(FeignClientApplication.class, args); - } - - @RequestMapping("/get-greeting") - public String greeting(Model model) { - model.addAttribute("greeting", greetingClient.greeting()); - return "greeting-view"; - } -} diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java deleted file mode 100644 index 6bd444b347..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.spring.cloud.feign.client; - -import com.baeldung.spring.cloud.eureka.client.GreetingController; -import org.springframework.cloud.netflix.feign.FeignClient; - -@FeignClient("spring-cloud-eureka-client") -public interface GreetingClient extends GreetingController { -} diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml deleted file mode 100644 index d053ef7a7e..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml +++ /dev/null @@ -1,11 +0,0 @@ -spring: - application: - name: spring-cloud-eureka-feign-client - -server: - port: 8080 - -eureka: - client: - serviceUrl: - defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/greeting-view.html b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/greeting-view.html deleted file mode 100644 index 42cdadb487..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/greeting-view.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - Greeting Page - - -

- - \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml deleted file mode 100644 index 49c3179bb5..0000000000 --- a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml +++ /dev/null @@ -1,7 +0,0 @@ -server: - port: 8761 - -eureka: - client: - registerWithEureka: false - fetchRegistry: false \ No newline at end of file diff --git a/spring-cloud-hystrix/pom.xml b/spring-cloud-hystrix/pom.xml deleted file mode 100644 index 2768a4f05b..0000000000 --- a/spring-cloud-hystrix/pom.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - 4.0.0 - - com.baeldung.spring.cloud - spring-cloud-hystrix - 1.0.0-SNAPSHOT - - spring-cloud-hystrix-rest-producer - spring-cloud-hystrix-rest-consumer - spring-cloud-hystrix-feign-rest-consumer - - pom - - Spring Cloud Hystrix - Spring Cloud Hystrix Demo - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - .. - - - - UTF-8 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.springframework.boot - spring-boot-maven-plugin - 1.4.0.RELEASE - - - - - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml deleted file mode 100644 index d2716e897e..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml +++ /dev/null @@ -1,82 +0,0 @@ - - - 4.0.0 - - spring-cloud-hystrix-feign-rest-consumer - 1.0.0-SNAPSHOT - jar - - Spring Cloud Hystrix Feign REST Consumer - Spring Cloud Hystrix Feign Sample Implementation - - - com.baeldung.spring.cloud - spring-cloud-hystrix - 1.0.0-SNAPSHOT - .. - - - - - com.baeldung.spring.cloud - spring-cloud-hystrix-rest-producer - 1.0.0-SNAPSHOT - - - org.springframework.cloud - spring-cloud-starter-hystrix - 1.1.5.RELEASE - - - org.springframework.cloud - spring-cloud-starter-hystrix-dashboard - 1.1.5.RELEASE - - - org.springframework.cloud - spring-cloud-starter-feign - 1.1.5.RELEASE - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-thymeleaf - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-actuator - 1.4.0.RELEASE - - - - - - - org.springframework.cloud - spring-cloud-starter-parent - Brixton.SR4 - pom - import - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java deleted file mode 100644 index b715e8c052..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.consumer; - -import com.baeldung.spring.cloud.hystrix.rest.producer.GreetingController; -import org.springframework.cloud.netflix.feign.FeignClient; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.PathVariable; - -@FeignClient( - name = "rest-producer", - url = "http://localhost:9090", - fallback = GreetingClient.GreetingClientFallback.class -) -public interface GreetingClient extends GreetingController { - @Component - public static class GreetingClientFallback implements GreetingClient { - @Override - public String greeting(@PathVariable("username") String username) { - return "Hello User!"; - } - } -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java deleted file mode 100644 index b97d84eaf2..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.consumer; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; -import org.springframework.cloud.netflix.feign.EnableFeignClients; -import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; - -@SpringBootApplication -@EnableCircuitBreaker -@EnableHystrixDashboard -@EnableFeignClients -@Controller -public class RestConsumerFeignApplication { - @Autowired - private GreetingClient greetingClient; - - public static void main(String[] args) { - SpringApplication.run(RestConsumerFeignApplication.class, args); - } - - @RequestMapping("/get-greeting/{username}") - public String getGreeting(Model model, @PathVariable("username") String username) { - model.addAttribute("greeting", greetingClient.greeting(username)); - return "greeting-view"; - } -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties deleted file mode 100644 index 3cf12afeb9..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8082 diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html deleted file mode 100644 index 302390fde0..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - Greetings from Hystrix - - -

- - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml deleted file mode 100644 index c9be67c302..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - 4.0.0 - - spring-cloud-hystrix-rest-consumer - 1.0.0-SNAPSHOT - jar - - Spring Cloud Hystrix REST Consumer - Spring Cloud Hystrix Sample Implementation - - - com.baeldung.spring.cloud - spring-cloud-hystrix - 1.0.0-SNAPSHOT - .. - - - - - org.springframework.cloud - spring-cloud-starter-hystrix - 1.1.5.RELEASE - - - org.springframework.cloud - spring-cloud-starter-hystrix-dashboard - 1.1.5.RELEASE - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-thymeleaf - 1.4.0.RELEASE - - - org.springframework.boot - spring-boot-starter-actuator - 1.4.0.RELEASE - - - - - - - org.springframework.cloud - spring-cloud-starter-parent - Brixton.SR4 - pom - import - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java deleted file mode 100644 index d3d5e6e047..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.consumer; - -import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -@Service -public class GreetingService { - @HystrixCommand(fallbackMethod = "defaultGreeting") - public String getGreeting(String username) { - return new RestTemplate().getForObject("http://localhost:9090/greeting/{username}", String.class, username); - } - - private String defaultGreeting(String username) { - return "Hello User!"; - } -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java deleted file mode 100644 index 9df745b1c6..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.consumer; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; -import org.springframework.cloud.netflix.hystrix.EnableHystrix; -import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; - -@SpringBootApplication -@EnableCircuitBreaker -@EnableHystrixDashboard -@Controller -public class RestConsumerApplication { - @Autowired - private GreetingService greetingService; - - public static void main(String[] args) { - SpringApplication.run(RestConsumerApplication.class, args); - } - - @RequestMapping("/get-greeting/{username}") - public String getGreeting(Model model, @PathVariable("username") String username) { - model.addAttribute("greeting", greetingService.getGreeting(username)); - return "greeting-view"; - } -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/application.properties deleted file mode 100644 index 4c00e40deb..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8080 diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html deleted file mode 100644 index 302390fde0..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - Greetings from Hystrix - - -

- - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml deleted file mode 100644 index 44e373c8ac..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - 4.0.0 - - spring-cloud-hystrix-rest-producer - 1.0.0-SNAPSHOT - jar - - Spring Cloud Hystrix REST Producer - Spring Cloud Hystrix Sample REST Producer Implementation - - - com.baeldung.spring.cloud - spring-cloud-hystrix - 1.0.0-SNAPSHOT - .. - - - - - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java deleted file mode 100644 index 81541b4f8f..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.producer; - -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - -public interface GreetingController { - @RequestMapping("/greeting/{username}") - String greeting(@PathVariable("username") String username); -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java deleted file mode 100644 index 9496d4760d..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.spring.cloud.hystrix.rest.producer; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@RestController -public class RestProducerApplication implements GreetingController { - public static void main(String[] args) { - SpringApplication.run(RestProducerApplication.class, args); - } - - @Override - public String greeting(@PathVariable("username") String username) { - return String.format("Hello %s!\n", username); - } -} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties deleted file mode 100644 index 9ce9d88ffb..0000000000 --- a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -spring.application.name=rest-producer -server.port=9090 diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index 4f6b37a76f..340923cbdf 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -10,6 +10,7 @@ spring-cloud-config spring-cloud-eureka spring-cloud-hystrix + spring-cloud-integration pom diff --git a/spring-cloud/spring-cloud-integration/part-1/application-config/discovery.properties b/spring-cloud/spring-cloud-integration/part-1/application-config/discovery.properties new file mode 100644 index 0000000000..7f3df86c7e --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/application-config/discovery.properties @@ -0,0 +1,8 @@ +spring.application.name=discovery +server.port=8082 + +eureka.instance.hostname=localhost + +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/ +eureka.client.register-with-eureka=false +eureka.client.fetch-registry=false diff --git a/spring-cloud/spring-cloud-integration/part-1/application-config/gateway.properties b/spring-cloud/spring-cloud-integration/part-1/application-config/gateway.properties new file mode 100644 index 0000000000..77faec8421 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/application-config/gateway.properties @@ -0,0 +1,10 @@ +spring.application.name=gateway +server.port=8080 + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/ + +zuul.routes.resource.path=/resource/** +hystrix.command.resource.execution.isolation.thread.timeoutInMilliseconds: 5000 + diff --git a/spring-cloud/spring-cloud-integration/part-1/application-config/resource.properties b/spring-cloud/spring-cloud-integration/part-1/application-config/resource.properties new file mode 100644 index 0000000000..4e6cf3817c --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/application-config/resource.properties @@ -0,0 +1,8 @@ +spring.application.name=resource +server.port=8083 + +resource.returnString=hello cloud + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/ diff --git a/spring-cloud/spring-cloud-integration/part-1/config/pom.xml b/spring-cloud/spring-cloud-integration/part-1/config/pom.xml new file mode 100644 index 0000000000..0cb217acfb --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/config/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + config + 1.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + + + org.springframework.cloud + spring-cloud-config-server + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Brixton.RELEASE + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-cloud-config/spring-cloud-config-server/src/main/java/com/baeldung/spring/cloud/config/server/ConfigServer.java b/spring-cloud/spring-cloud-integration/part-1/config/src/main/java/com/baeldung/spring/cloud/integration/config/ConfigApplication.java similarity index 53% rename from spring-cloud-config/spring-cloud-config-server/src/main/java/com/baeldung/spring/cloud/config/server/ConfigServer.java rename to spring-cloud/spring-cloud-integration/part-1/config/src/main/java/com/baeldung/spring/cloud/integration/config/ConfigApplication.java index 4dd34ae3ff..ff6c093b8b 100644 --- a/spring-cloud-config/spring-cloud-config-server/src/main/java/com/baeldung/spring/cloud/config/server/ConfigServer.java +++ b/spring-cloud/spring-cloud-integration/part-1/config/src/main/java/com/baeldung/spring/cloud/integration/config/ConfigApplication.java @@ -1,15 +1,15 @@ -package com.baeldung.spring.cloud.config.server; +package com.baeldung.spring.cloud.integration.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer -@EnableWebSecurity -public class ConfigServer { +@EnableEurekaClient +public class ConfigApplication { public static void main(String[] args) { - SpringApplication.run(ConfigServer.class, args); + SpringApplication.run(ConfigApplication.class, args); } } diff --git a/spring-cloud/spring-cloud-integration/part-1/config/src/main/resources/application.properties b/spring-cloud/spring-cloud-integration/part-1/config/src/main/resources/application.properties new file mode 100644 index 0000000000..6f614d0690 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/config/src/main/resources/application.properties @@ -0,0 +1,8 @@ +server.port=8081 +spring.application.name=config + +spring.cloud.config.server.git.uri=file:///${user.home}/application-config + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/pom.xml b/spring-cloud/spring-cloud-integration/part-1/discovery/pom.xml similarity index 53% rename from spring-cloud-eureka/spring-cloud-eureka-server/pom.xml rename to spring-cloud/spring-cloud-integration/part-1/discovery/pom.xml index f4d655f708..ee7c589549 100644 --- a/spring-cloud-eureka/spring-cloud-eureka-server/pom.xml +++ b/spring-cloud/spring-cloud-integration/part-1/discovery/pom.xml @@ -1,27 +1,32 @@ - 4.0.0 - spring-cloud-eureka-server + discovery 1.0.0-SNAPSHOT - jar - - Spring Cloud Eureka Server - Spring Cloud Eureka Server Demo - com.baeldung.spring.cloud - spring-cloud-eureka - 1.0.0-SNAPSHOT - .. + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + org.springframework.cloud + spring-cloud-starter-config + org.springframework.cloud spring-cloud-starter-eureka-server - 1.1.5.RELEASE + + + org.springframework.boot + spring-boot-starter-test + test @@ -29,8 +34,8 @@ org.springframework.cloud - spring-cloud-starter-parent - Brixton.SR4 + spring-cloud-dependencies + Brixton.RELEASE pom import @@ -39,14 +44,10 @@ - - org.apache.maven.plugins - maven-compiler-plugin - org.springframework.boot spring-boot-maven-plugin - + \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java b/spring-cloud/spring-cloud-integration/part-1/discovery/src/main/java/com/baeldung/spring/cloud/integration/discovery/DiscoveryApplication.java similarity index 64% rename from spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java rename to spring-cloud/spring-cloud-integration/part-1/discovery/src/main/java/com/baeldung/spring/cloud/integration/discovery/DiscoveryApplication.java index d55145448d..a21c65312f 100644 --- a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java +++ b/spring-cloud/spring-cloud-integration/part-1/discovery/src/main/java/com/baeldung/spring/cloud/integration/discovery/DiscoveryApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.cloud.eureka.server; +package com.baeldung.spring.cloud.integration.discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -6,8 +6,8 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer -public class EurekaServerApplication { +public class DiscoveryApplication { public static void main(String[] args) { - SpringApplication.run(EurekaServerApplication.class, args); + SpringApplication.run(DiscoveryApplication.class, args); } } diff --git a/spring-cloud/spring-cloud-integration/part-1/discovery/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-integration/part-1/discovery/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..ca9d59c9ed --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/discovery/src/main/resources/bootstrap.properties @@ -0,0 +1,2 @@ +spring.cloud.config.name=discovery +spring.cloud.config.uri=http://localhost:8081 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-integration/part-1/gateway/pom.xml b/spring-cloud/spring-cloud-integration/part-1/gateway/pom.xml new file mode 100644 index 0000000000..8e56d0fd35 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/gateway/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + gateway + 1.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-zuul + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Brixton.RELEASE + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/java/com/baeldung/spring/cloud/integration/resource/GatewayApplication.java b/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/java/com/baeldung/spring/cloud/integration/resource/GatewayApplication.java new file mode 100644 index 0000000000..66e7c36f2a --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/java/com/baeldung/spring/cloud/integration/resource/GatewayApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.integration.resource; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@SpringBootApplication +@EnableZuulProxy +@EnableEurekaClient +public class GatewayApplication { + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..9610d72675 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/gateway/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.cloud.config.name=gateway +spring.cloud.config.discovery.service-id=config +spring.cloud.config.discovery.enabled=true + +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-integration/part-1/pom.xml b/spring-cloud/spring-cloud-integration/part-1/pom.xml new file mode 100644 index 0000000000..770e26bca2 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + + com.baeldung.spring.cloud + spring-cloud-integration + 1.0.0-SNAPSHOT + + + + config + discovery + gateway + resource + + + part-1 + 1.0.0-SNAPSHOT + pom + + + \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/pom.xml b/spring-cloud/spring-cloud-integration/part-1/resource/pom.xml similarity index 56% rename from spring-cloud-eureka/spring-cloud-eureka-client/pom.xml rename to spring-cloud/spring-cloud-integration/part-1/resource/pom.xml index 720b49ddc2..78112fa3e0 100644 --- a/spring-cloud-eureka/spring-cloud-eureka-client/pom.xml +++ b/spring-cloud/spring-cloud-integration/part-1/resource/pom.xml @@ -1,32 +1,36 @@ - 4.0.0 - spring-cloud-eureka-client + resource 1.0.0-SNAPSHOT - jar - - Spring Cloud Eureka Client - Spring Cloud Eureka Sample Client - com.baeldung.spring.cloud - spring-cloud-eureka - 1.0.0-SNAPSHOT - .. + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + org.springframework.cloud + spring-cloud-starter-config + org.springframework.cloud spring-cloud-starter-eureka - 1.1.5.RELEASE org.springframework.boot spring-boot-starter-web - 1.4.0.RELEASE + + + org.springframework.boot + spring-boot-starter-test + test @@ -34,8 +38,8 @@ org.springframework.cloud - spring-cloud-starter-parent - Brixton.SR4 + spring-cloud-dependencies + Brixton.RELEASE pom import @@ -44,14 +48,10 @@ - - org.apache.maven.plugins - maven-compiler-plugin - org.springframework.boot spring-boot-maven-plugin - + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-integration/part-1/resource/src/main/java/com/baeldung/spring/cloud/integration/resource/ResourceApplication.java b/spring-cloud/spring-cloud-integration/part-1/resource/src/main/java/com/baeldung/spring/cloud/integration/resource/ResourceApplication.java new file mode 100644 index 0000000000..107a9d199f --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/resource/src/main/java/com/baeldung/spring/cloud/integration/resource/ResourceApplication.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.cloud.integration.resource; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@EnableEurekaClient +@RestController +public class ResourceApplication { + public static void main(String[] args) { + SpringApplication.run(ResourceApplication.class, args); + } + + @Value("${resource.returnString}") + private String returnString; + + @RequestMapping("/hello/cloud") + public String getString() { + return returnString; + } +} diff --git a/spring-cloud/spring-cloud-integration/part-1/resource/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-integration/part-1/resource/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..3c88a0b520 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/part-1/resource/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.cloud.config.name=resource +spring.cloud.config.discovery.service-id=config +spring.cloud.config.discovery.enabled=true + +eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-integration/pom.xml b/spring-cloud/spring-cloud-integration/pom.xml new file mode 100644 index 0000000000..1d56995009 --- /dev/null +++ b/spring-cloud/spring-cloud-integration/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-integration + 1.0.0-SNAPSHOT + pom + + + part-1 + + \ No newline at end of file diff --git a/spring-data-elasticsearch/pom.xml b/spring-data-elasticsearch/pom.xml index 3a6e330564..084695c2f3 100644 --- a/spring-data-elasticsearch/pom.xml +++ b/spring-data-elasticsearch/pom.xml @@ -69,6 +69,17 @@ log4j-over-slf4j ${org.slf4j.version} + + + org.elasticsearch + elasticsearch + 2.3.5 + + + com.alibaba + fastjson + 1.2.13 + diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java b/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java new file mode 100644 index 0000000000..b8ad59e2e2 --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java @@ -0,0 +1,52 @@ +package com.baeldung.elasticsearch; + +import java.util.Date; + +public class Person { + + private int age; + + private String fullName; + + private Date dateOfBirth; + + public Person() { + + } + + public Person(int age, String fullName, Date dateOfBirth) { + super(); + this.age = age; + this.fullName = fullName; + this.dateOfBirth = dateOfBirth; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public Date getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + + @Override + public String toString() { + return "Person [age=" + age + ", fullName=" + fullName + ", dateOfBirth=" + dateOfBirth + "]"; + } +} diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchUnitTests.java b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchUnitTests.java new file mode 100644 index 0000000000..db304ee78d --- /dev/null +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchUnitTests.java @@ -0,0 +1,127 @@ +package com.baeldung.elasticsearch; + +import static org.elasticsearch.node.NodeBuilder.*; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.node.Node; +import org.elasticsearch.search.SearchHit; +import org.junit.Before; +import org.junit.Test; + +import com.alibaba.fastjson.JSON; + +public class ElasticSearchUnitTests { + private List listOfPersons = new ArrayList(); + String jsonString = null; + Client client = null; + + @Before + public void setUp() { + Person person1 = new Person(10, "John Doe", new Date()); + Person person2 = new Person(25, "Janette Doe", new Date()); + listOfPersons.add(person1); + listOfPersons.add(person2); + jsonString = JSON.toJSONString(listOfPersons); + Node node = nodeBuilder().clusterName("elasticsearch").client(true).node(); + client = node.client(); + } + + @Test + public void givenJsonString_whenJavaObject_thenIndexDocument() { + String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; + IndexResponse response = client.prepareIndex("people", "Doe") + .setSource(jsonObject).get(); + String index = response.getIndex(); + String type = response.getType(); + assertTrue(response.isCreated()); + assertEquals(index, "people"); + assertEquals(type, "Doe"); + } + + @Test + public void givenDocumentId_whenJavaObject_thenDeleteDocument() { + String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; + IndexResponse response = client.prepareIndex("people", "Doe") + .setSource(jsonObject).get(); + String id = response.getId(); + DeleteResponse deleteResponse = client.prepareDelete("people", "Doe", id).get(); + assertTrue(deleteResponse.isFound()); + } + + @Test + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { + SearchResponse response = client.prepareSearch().execute().actionGet(); + SearchHit[] searchHits = response.getHits().getHits(); + List results = new ArrayList(); + for (SearchHit hit : searchHits) { + String sourceAsString = hit.getSourceAsString(); + Person person = JSON.parseObject(sourceAsString, Person.class); + results.add(person); + } + } + + @Test + public void givenSearchParamters_thenReturnResults() { + boolean isExecutedSuccessfully = true; + SearchResponse response = client.prepareSearch() + .setTypes() + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .setPostFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) + .setFrom(0).setSize(60).setExplain(true) + .execute() + .actionGet(); + + SearchResponse response2 = client.prepareSearch() + .setTypes() + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) + .setFrom(0).setSize(60).setExplain(true) + .execute() + .actionGet(); + + SearchResponse response3 = client.prepareSearch() + .setTypes() + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .setPostFilter(QueryBuilders.matchQuery("John", "Name*")) + .setFrom(0).setSize(60).setExplain(true) + .execute() + .actionGet(); + try { + response2.getHits(); + response3.getHits(); + List searchHits = Arrays.asList(response.getHits().getHits()); + final List results = new ArrayList(); + searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class))); + } catch (Exception e) { + isExecutedSuccessfully = false; + } + assertTrue(isExecutedSuccessfully); + } + + @Test + public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .field("fullName", "Test") + .field("salary", "11500") + .field("age", "10") + .endObject(); + IndexResponse response = client.prepareIndex("people", "Doe") + .setSource(builder).get(); + assertTrue(response.isCreated()); + } +} diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml index f7f28aa9f1..5ae694a04f 100644 --- a/spring-data-rest/pom.xml +++ b/spring-data-rest/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung @@ -15,7 +15,7 @@ org.springframework.boot spring-boot-starter-parent 1.3.3.RELEASE - + diff --git a/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java b/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java index 6e8e62f52c..94eddc5b3e 100644 --- a/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java +++ b/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java @@ -5,7 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringDataRestApplication { - public static void main(String[] args) { - SpringApplication.run(SpringDataRestApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(SpringDataRestApplication.class, args); + } } diff --git a/spring-data-rest/src/main/java/com/baeldung/UserRepository.java b/spring-data-rest/src/main/java/com/baeldung/UserRepository.java deleted file mode 100644 index ebbf0d49ab..0000000000 --- a/spring-data-rest/src/main/java/com/baeldung/UserRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung; - -import org.springframework.data.repository.PagingAndSortingRepository; -import org.springframework.data.repository.query.Param; -import org.springframework.data.rest.core.annotation.RepositoryRestResource; - -import java.util.List; - -@RepositoryRestResource(collectionResourceRel = "users", path = "users") -public interface UserRepository extends PagingAndSortingRepository { - List findByName(@Param("name") String name); -} diff --git a/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java b/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java new file mode 100644 index 0000000000..89ab848e81 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/config/ValidatorEventRegister.java @@ -0,0 +1,30 @@ +package com.baeldung.config; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; +import org.springframework.validation.Validator; + +@Configuration +public class ValidatorEventRegister implements InitializingBean { + + @Autowired + ValidatingRepositoryEventListener validatingRepositoryEventListener; + + @Autowired + private Map validators; + + @Override + public void afterPropertiesSet() throws Exception { + List events = Arrays.asList("beforeCreate", "afterCreate", "beforeSave", "afterSave", "beforeLinkSave", "afterLinkSave", "beforeDelete", "afterDelete"); + + for (Map.Entry entry : validators.entrySet()) { + events.stream().filter(p -> entry.getKey().startsWith(p)).findFirst().ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue())); + } + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java b/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java new file mode 100644 index 0000000000..ee84738e7a --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/exception/handlers/RestResponseEntityExceptionHandler.java @@ -0,0 +1,25 @@ +package com.baeldung.exception.handlers; + +import java.util.stream.Collectors; + +import org.springframework.data.rest.core.RepositoryConstraintViolationException; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { + + @ExceptionHandler({ RepositoryConstraintViolationException.class }) + public ResponseEntity handleAccessDeniedException(Exception ex, WebRequest request) { + RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex; + + String errors = nevEx.getErrors().getAllErrors().stream().map(p -> p.toString()).collect(Collectors.joining("\n")); + return new ResponseEntity(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE); + } + +} \ No newline at end of file diff --git a/spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java b/spring-data-rest/src/main/java/com/baeldung/models/WebsiteUser.java similarity index 96% rename from spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java rename to spring-data-rest/src/main/java/com/baeldung/models/WebsiteUser.java index a7a35a2573..4eb9773e36 100644 --- a/spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java +++ b/spring-data-rest/src/main/java/com/baeldung/models/WebsiteUser.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.models; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java new file mode 100644 index 0000000000..0b55ac89b6 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +import com.baeldung.models.WebsiteUser; + +@RepositoryRestResource(collectionResourceRel = "users", path = "users") +public interface UserRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/validators/WebsiteUserValidator.java b/spring-data-rest/src/main/java/com/baeldung/validators/WebsiteUserValidator.java new file mode 100644 index 0000000000..0380332708 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/validators/WebsiteUserValidator.java @@ -0,0 +1,33 @@ +package com.baeldung.validators; + +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; + +import com.baeldung.models.WebsiteUser; + +@Component("beforeCreateWebsiteUserValidator") +public class WebsiteUserValidator implements Validator { + + @Override + public boolean supports(Class clazz) { + return WebsiteUser.class.equals(clazz); + } + + @Override + public void validate(Object obj, Errors errors) { + + WebsiteUser user = (WebsiteUser) obj; + if (checkInputString(user.getName())) { + errors.rejectValue("name", "name.empty"); + } + + if (checkInputString(user.getEmail())) { + errors.rejectValue("email", "email.empty"); + } + } + + private boolean checkInputString(String input) { + return (input == null || input.trim().length() == 0); + } +} diff --git a/spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java b/spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java new file mode 100644 index 0000000000..b185c6d5ab --- /dev/null +++ b/spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java @@ -0,0 +1,100 @@ +package com.baeldung.validator; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.WebsiteUser; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = SpringDataRestApplication.class) +@WebAppConfiguration +public class SpringDataRestValidatorTest { + public static final String URL = "http://localhost"; + + private MockMvc mockMvc; + + @Autowired + protected WebApplicationContext wac; + + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } + + @Test + public void whenStartingApplication_thenCorrectStatusCode() throws Exception { + mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful()); + }; + + @Test + public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + user.setName("John Doe"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) + .andExpect(status().is2xxSuccessful()) + .andExpect(redirectedUrl("http://localhost/users/1")); + } + + @Test + public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) + .andExpect(status().isNotAcceptable()) + .andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + user.setName(""); + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) + .andExpect(status().isNotAcceptable()) + .andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setName("John Doe"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) + .andExpect(status().isNotAcceptable()) + .andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setName("John Doe"); + user.setEmail(""); + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) + .andExpect(status().isNotAcceptable()) + .andExpect(redirectedUrl(null)); + } + +} diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 0c0d6219dd..18cb1dc72a 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -182,7 +182,6 @@ org.apache.maven.plugins maven-compiler-plugin - ${maven-compiler-plugin.version} 1.8 1.8 @@ -192,16 +191,14 @@ org.apache.maven.plugins maven-war-plugin - ${maven-war-plugin.version} org.apache.maven.plugins maven-surefire-plugin - ${maven-surefire-plugin.version} - + **/*LiveTest.java @@ -216,7 +213,7 @@ true - jetty8x + tomcat8x embedded @@ -234,6 +231,61 @@ + + + live + + + + org.codehaus.cargo + cargo-maven2-plugin + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*LiveTest.java + + + cargo + + + + + + + + + + + @@ -266,7 +318,7 @@ 3.5.1 2.6 2.19.1 - 1.4.18 + 1.6.0 diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java b/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java index 1d4aff2ebf..348ee6d596 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java @@ -5,5 +5,6 @@ import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(value = HttpStatus.FORBIDDEN, reason="To show an example of a custom message") public class ForbiddenException extends RuntimeException { + private static final long serialVersionUID = 6826605655586311552L; } diff --git a/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java b/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java index fd349f1e44..3155b5cda9 100644 --- a/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java @@ -7,7 +7,7 @@ import org.junit.Test; import com.jayway.restassured.RestAssured; public class RequestMappingLiveTest { - private static String BASE_URI = "http://localhost:8080/spring-rest/ex/"; + private static String BASE_URI = "http://localhost:8082/spring-rest/ex/"; @Test public void givenSimplePath_whenGetFoos_thenOk() { diff --git a/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java b/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java similarity index 97% rename from spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java rename to spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java index 1dfe509c09..7f250653ab 100644 --- a/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java +++ b/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java @@ -21,9 +21,9 @@ import org.springframework.web.client.RestTemplate; /** * Integration Test class. Tests methods hits the server's rest services. */ -public class SpringHttpMessageConvertersIntegrationTestsCase { +public class SpringHttpMessageConvertersLiveTest { - private static String BASE_URI = "http://localhost:8080/spring-rest/"; + private static String BASE_URI = "http://localhost:8082/spring-rest/"; /** * Without specifying Accept Header, uses the default response from the diff --git a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java index 1b5f7cd894..13cb92a745 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java @@ -14,6 +14,7 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.core.JsonProcessingException; @@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration +@Transactional public class CsrfAbstractIntegrationTest { @Autowired diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index 51e26fdfdd..35d8c37176 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -8,24 +8,24 @@ 1.7 - 4.1.8.RELEASE + 4.3.3.RELEASE 3.0.1 - 1.7.12 - 1.1.3 + 1.7.12 + 1.1.3 2.1.4.RELEASE 1.1.0.Final 5.1.2.Final - + 3.5.1 2.6 2.19.1 1.4.18 - + @@ -45,6 +45,17 @@ spring-webmvc ${org.springframework-version} + + + org.springframework.security + spring-security-web + 4.1.3.RELEASE + + + org.springframework.security + spring-security-config + 4.1.3.RELEASE + org.thymeleaf @@ -57,29 +68,29 @@ ${org.thymeleaf-version} - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + javax.servlet @@ -98,6 +109,31 @@ hibernate-validator ${org.hibernate-version} + + + + org.springframework + spring-test + 4.1.3.RELEASE + test + + + + + org.springframework.security + spring-security-test + 4.1.3.RELEASE + test + + + + + junit + junit + 4.12 + test + + @@ -129,25 +165,25 @@ - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - 8082 - - - - + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + 8082 + + + + diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java new file mode 100644 index 0000000000..956db4a0e5 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java @@ -0,0 +1,11 @@ +package com.baeldung.thymeleaf.config; + +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; + +public class InitSecurity extends AbstractSecurityWebApplicationInitializer { + + public InitSecurity() { + super(WebMVCSecurity.class); + + } +} diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java index 89ad7e601e..c7d5e33cb8 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java @@ -20,7 +20,7 @@ public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer @Override protected Class[] getServletConfigClasses() { - return new Class[] { WebMVCConfig.class }; + return new Class[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }; } @Override diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java index 51c60247a1..50c9cf06fe 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java @@ -1,6 +1,5 @@ package com.baeldung.thymeleaf.config; -import com.baeldung.thymeleaf.formatter.NameFormatter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -14,6 +13,8 @@ import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; +import com.baeldung.thymeleaf.formatter.NameFormatter; + @Configuration @EnableWebMvc @ComponentScan({ "com.baeldung.thymeleaf" }) diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java new file mode 100644 index 0000000000..00c42831de --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java @@ -0,0 +1,49 @@ +package com.baeldung.thymeleaf.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +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; + +@Configuration +@EnableWebSecurity +@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) +public class WebMVCSecurity extends WebSecurityConfigurerAdapter { + + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + public WebMVCSecurity() { + super(); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER"); + } + + @Override + public void configure(final WebSecurity web) throws Exception { + web.ignoring().antMatchers("/resources/**"); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http + .authorizeRequests() + .anyRequest() + .authenticated() + .and() + .httpBasic() + ; + } + +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html new file mode 100644 index 0000000000..7674caa854 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html @@ -0,0 +1,12 @@ + + + + + + +
+ + +
+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java b/spring-thymeleaf/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java new file mode 100644 index 0000000000..bd70881dd8 --- /dev/null +++ b/spring-thymeleaf/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java @@ -0,0 +1,63 @@ +package org.baeldung.security.csrf; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.servlet.Filter; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.thymeleaf.config.InitSecurity; +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; +import com.baeldung.thymeleaf.config.WebMVCSecurity; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) +public class CsrfEnabledIntegrationTest { + + @Autowired + WebApplicationContext wac; + @Autowired + MockHttpSession session; + + private MockMvc mockMvc; + + @Autowired + private Filter springSecurityFilterChain; + + protected RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } + + @Test + public void addStudentWithoutCSRF() throws Exception { + mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser())).andExpect(status().isForbidden()); + } + + @Test + public void addStudentWithCSRF() throws Exception { + mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser()).with(csrf())).andExpect(status().isOk()); + } + +} diff --git a/wicket/README.md b/wicket/README.md new file mode 100644 index 0000000000..614446e7ba --- /dev/null +++ b/wicket/README.md @@ -0,0 +1,4 @@ + +From the same directory where pom.xml is, execute the following command to run the project: + +mvn jetty:run diff --git a/wicket/pom.xml b/wicket/pom.xml new file mode 100644 index 0000000000..929f723c2c --- /dev/null +++ b/wicket/pom.xml @@ -0,0 +1,106 @@ + + + + 4.0.0 + com.baeldung.wicket.examples + wicket-intro + war + 1.0-SNAPSHOT + WicketIntro + + 7.4.0 + 9.2.13.v20150730 + 2.5 + 4.12 + 3.5.1 + 2.6 + UTF-8 + + + + + org.apache.wicket + wicket-core + ${wicket.version} + + + + + junit + junit + ${junit.version} + test + + + + + org.eclipse.jetty.aggregate + jetty-all + ${jetty9.version} + test + + + + + + false + src/main/resources + + + false + src/main/java + + ** + + + **/*.java + + + + + + false + src/test/resources + + + false + src/test/java + + ** + + + **/*.java + + + + + + true + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + UTF-8 + true + true + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty9.version} + + + + diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/Examples.html b/wicket/src/main/java/com/baeldung/wicket/examples/Examples.html new file mode 100644 index 0000000000..497e98e01a --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/Examples.html @@ -0,0 +1,52 @@ + + + + +Wicket Intro Examples + + + +
+
+
+

Wicket Introduction Examples:

+ + Hello World! +
+
+ Cafes +
+
+
+
+ + diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/Examples.java b/wicket/src/main/java/com/baeldung/wicket/examples/Examples.java new file mode 100644 index 0000000000..358e4f7b19 --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/Examples.java @@ -0,0 +1,9 @@ +package com.baeldung.wicket.examples; + +import org.apache.wicket.markup.html.WebPage; + +public class Examples extends WebPage { + + private static final long serialVersionUID = 1L; + +} diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/ExamplesApplication.java b/wicket/src/main/java/com/baeldung/wicket/examples/ExamplesApplication.java new file mode 100644 index 0000000000..711e8f01fd --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/ExamplesApplication.java @@ -0,0 +1,27 @@ +package com.baeldung.wicket.examples; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebApplication; + +import com.baeldung.wicket.examples.cafeaddress.CafeAddress; +import com.baeldung.wicket.examples.helloworld.HelloWorld; + +public class ExamplesApplication extends WebApplication { + /** + * @see org.apache.wicket.Application#getHomePage() + */ + @Override + public Class getHomePage() { + return Examples.class; + } + + /** + * @see org.apache.wicket.Application#init() + */ + @Override + public void init() { + super.init(); + mountPage("/examples/helloworld", HelloWorld.class); + mountPage("/examples/cafes", CafeAddress.class); + } +} diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html b/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html new file mode 100644 index 0000000000..c5ada2323d --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.html @@ -0,0 +1,15 @@ + + + + +Cafes + + +
+ +

+ Address: address +

+
+ + diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.java b/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.java new file mode 100644 index 0000000000..ce26c5a1ad --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/cafeaddress/CafeAddress.java @@ -0,0 +1,72 @@ +package com.baeldung.wicket.examples.cafeaddress; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.form.DropDownChoice; +import org.apache.wicket.model.PropertyModel; +import org.apache.wicket.request.mapper.parameter.PageParameters; + +public class CafeAddress extends WebPage { + + private static final long serialVersionUID = 1L; + + String selectedCafe; + Address address; + Map cafeNamesAndAddresses = new HashMap<>(); + + public CafeAddress(final PageParameters parameters) { + super(parameters); + initCafes(); + + ArrayList cafeNames = new ArrayList<>(this.cafeNamesAndAddresses.keySet()); + this.selectedCafe = cafeNames.get(0); + this.address = new Address(this.cafeNamesAndAddresses.get(this.selectedCafe).getAddress()); + + final Label addressLabel = new Label("address", new PropertyModel(this.address, "address")); + addressLabel.setOutputMarkupId(true); + + final DropDownChoice cafeDropdown = new DropDownChoice<>("cafes", new PropertyModel(this, "selectedCafe"), cafeNames); + cafeDropdown.add(new AjaxFormComponentUpdatingBehavior("onchange") { + private static final long serialVersionUID = 1L; + + @Override + protected void onUpdate(AjaxRequestTarget target) { + String name = (String) cafeDropdown.getDefaultModel().getObject(); + address.setAddress(cafeNamesAndAddresses.get(name).getAddress()); + target.add(addressLabel); + } + }); + + add(addressLabel); + add(cafeDropdown); + + } + + private void initCafes() { + this.cafeNamesAndAddresses.put("Linda's Cafe", new Address("35 Bower St.")); + this.cafeNamesAndAddresses.put("Old Tree", new Address("2 Edgware Rd.")); + } + + class Address implements Serializable { + private String sAddress = ""; + + public Address(String address) { + this.sAddress = address; + } + + public String getAddress() { + return this.sAddress; + } + + public void setAddress(String address) { + this.sAddress = address; + } + } +} diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.html b/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.html new file mode 100644 index 0000000000..c56d07fc10 --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.html @@ -0,0 +1,5 @@ + + + + + diff --git a/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.java b/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.java new file mode 100644 index 0000000000..f819e05be6 --- /dev/null +++ b/wicket/src/main/java/com/baeldung/wicket/examples/helloworld/HelloWorld.java @@ -0,0 +1,13 @@ +package com.baeldung.wicket.examples.helloworld; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; + +public class HelloWorld extends WebPage { + + private static final long serialVersionUID = 1L; + + public HelloWorld() { + add(new Label("hello", "Hello World!")); + } +} diff --git a/wicket/src/main/test/java/com/baeldung/wicket/examples/TestHomePage.java b/wicket/src/main/test/java/com/baeldung/wicket/examples/TestHomePage.java new file mode 100644 index 0000000000..a393f1d178 --- /dev/null +++ b/wicket/src/main/test/java/com/baeldung/wicket/examples/TestHomePage.java @@ -0,0 +1,23 @@ +package com.baeldung.wicket.examples; + +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Before; +import org.junit.Test; + +public class TestHomePage { + private WicketTester tester; + + @Before + public void setUp() { + tester = new WicketTester(new ExamplesApplication()); + } + + @Test + public void whenPageInvoked_thanRenderedOK() { + //start and render the test page + tester.startPage(Examples.class); + + //assert rendered page class + tester.assertRenderedPage(Examples.class); + } +} diff --git a/wicket/src/main/webapp/WEB-INF/web.xml b/wicket/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..8a4451c80e --- /dev/null +++ b/wicket/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,32 @@ + + + + CafeAddress + + + + + wicket.examples + org.apache.wicket.protocol.http.WicketFilter + + applicationClassName + com.baeldung.wicket.examples.ExamplesApplication + + + + + wicket.examples + /* + + \ No newline at end of file