This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ralf-ueberfuhr-ars/features/config
Introduce config for CORS and refactor in-memory implementation of blogpost service
- Loading branch information
Showing
9 changed files
with
178 additions
and
16 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/de/sample/schulung/spring/blog/boundary/config/CorsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.sample.schulung.spring.blog.boundary.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import static java.util.Arrays.stream; | ||
|
||
@SuppressWarnings("NullableProblems") | ||
@Configuration | ||
public class CorsConfiguration { | ||
|
||
@Bean | ||
public WebMvcConfigurer corsConfigurer(final CorsConfigurationData allowed) { | ||
return new WebMvcConfigurer() { | ||
|
||
@Override | ||
public void addCorsMappings(final CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.exposedHeaders( | ||
HttpHeaders.LOCATION, | ||
HttpHeaders.LINK | ||
) | ||
// allow all HTTP request methods | ||
.allowedMethods( | ||
stream(RequestMethod.values()) | ||
.map(Enum::name) | ||
.toArray(String[]::new) | ||
) | ||
// allow the commonly used headers | ||
.allowedHeaders( | ||
HttpHeaders.ORIGIN, | ||
HttpHeaders.CONTENT_TYPE, | ||
HttpHeaders.CONTENT_LANGUAGE, | ||
HttpHeaders.ACCEPT, | ||
HttpHeaders.ACCEPT_LANGUAGE, | ||
HttpHeaders.IF_MATCH, | ||
HttpHeaders.IF_NONE_MATCH | ||
) | ||
// this is stage specific | ||
.allowedOrigins(allowed.getOrigins()) | ||
.allowCredentials(allowed.isCredentials()); | ||
} | ||
}; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/de/sample/schulung/spring/blog/boundary/config/CorsConfigurationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.sample.schulung.spring.blog.boundary.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* The properties from application.yml. You can specify them by the following snippet: | ||
* | ||
* <pre> | ||
* server: | ||
* endpoints: | ||
* api: | ||
* v1: /api/v1 | ||
* </pre> | ||
*/ | ||
@Configuration | ||
@ConfigurationProperties(prefix = "cors.allow") | ||
@Data | ||
public class CorsConfigurationData { | ||
|
||
private String[] origins = { "*" }; | ||
private boolean credentials = false; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/de/sample/schulung/spring/blog/boundary/config/IndexPageConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package de.sample.schulung.spring.blog.boundary.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@SuppressWarnings("NullableProblems") | ||
@Configuration | ||
public class IndexPageConfiguration { | ||
|
||
@Bean | ||
WebMvcConfigurer indexPageConfig() { | ||
return new WebMvcConfigurer() { | ||
@Override | ||
public void addViewControllers(ViewControllerRegistry registry) { | ||
registry | ||
.addViewController("/") | ||
.setViewName("redirect:/index.html"); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/de/sample/schulung/spring/blog/domain/BlogPostSink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
public interface BlogPostSink { | ||
|
||
long count(); | ||
void create(BlogPost post); | ||
Stream<BlogPost> findAll(); | ||
Optional<BlogPost> findById(UUID id); | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/de/sample/schulung/spring/blog/domain/InMemoryBlogPostSink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
public class InMemoryBlogPostSink implements BlogPostSink { | ||
|
||
private final Map<UUID, BlogPost> blogPosts = new HashMap<>(); | ||
|
||
@Override | ||
public long count() { | ||
return this.blogPosts.size(); | ||
} | ||
|
||
@Override | ||
public void create(BlogPost post) { | ||
final var id = UUID.randomUUID(); | ||
post.setId(id); | ||
post.setTimestamp(LocalDateTime.now()); | ||
this.blogPosts.put(id, post); | ||
|
||
} | ||
|
||
@Override | ||
public Stream<BlogPost> findAll() { | ||
return blogPosts.values().stream(); | ||
} | ||
|
||
@Override | ||
public Optional<BlogPost> findById(UUID id) { | ||
return Optional.ofNullable( | ||
this.blogPosts.get(id) | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/sample/schulung/spring/blog/domain/InMemoryBlogPostSinkConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class InMemoryBlogPostSinkConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(BlogPostSink.class) | ||
BlogPostSink inMemorySink() { | ||
return new InMemoryBlogPostSink(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters