-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into generic-graphql-base-fetcher-main
- Loading branch information
Showing
18 changed files
with
201 additions
and
39 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
shogun-lib/src/main/java/de/terrestris/shogun/lib/annotation/JsonSuperType.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,20 @@ | ||
package de.terrestris.shogun.lib.annotation; | ||
|
||
import java.io.Serializable; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* The `JsonSuperType` annotation helps to identify a type that should be deserialized instead of a parent type. | ||
* The `type` property specifies the parent type. This annotation needs to go along with a `JsonDeserialize` pointing | ||
* to the own type. | ||
* If the super type that should get replaced is not an interface, than `override` needs to be set to `true`. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface JsonSuperType { | ||
Class<? extends Serializable> type(); | ||
boolean override() default false; | ||
} |
95 changes: 90 additions & 5 deletions
95
shogun-lib/src/main/java/de/terrestris/shogun/lib/config/JacksonConfig.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 |
---|---|---|
@@ -1,25 +1,110 @@ | ||
package de.terrestris.shogun.lib.config; | ||
|
||
import com.bedatadriven.jackson.datatype.jts.JtsModule; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.vladmihalcea.hibernate.type.util.ObjectMapperSupplier; | ||
import de.terrestris.shogun.lib.annotation.JsonSuperType; | ||
import org.locationtech.jts.geom.GeometryFactory; | ||
import org.locationtech.jts.geom.PrecisionModel; | ||
import org.reflections.Reflections; | ||
import org.reflections.scanners.SubTypesScanner; | ||
import org.reflections.scanners.TypeAnnotationsScanner; | ||
import org.reflections.util.ClasspathHelper; | ||
import org.reflections.util.ConfigurationBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
public class JacksonConfig { | ||
public class JacksonConfig implements ObjectMapperSupplier { | ||
|
||
private static ObjectMapper mapper; | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
init(); | ||
return mapper; | ||
} | ||
|
||
private static int srid; | ||
|
||
@Value("${shogun.srid:4326}") | ||
protected int srid; | ||
public void setSrid(int srid) { | ||
JacksonConfig.srid = srid; | ||
} | ||
|
||
private static int coordinatePrecisionScale; | ||
|
||
@Value("${shogun.coordinatePrecisionScale:10}") | ||
protected int coordinatePrecisionScale; | ||
public void setCoordinatePrecisionScale(int coordinatePrecisionScale) { | ||
JacksonConfig.coordinatePrecisionScale = coordinatePrecisionScale; | ||
} | ||
|
||
@Bean | ||
public JtsModule jtsModule() { | ||
GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(coordinatePrecisionScale), srid); | ||
public static JtsModule jtsModule() { | ||
GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(JacksonConfig.coordinatePrecisionScale), JacksonConfig.srid); | ||
return new JtsModule(geomFactory); | ||
} | ||
|
||
@Override | ||
public ObjectMapper get() { | ||
return objectMapper(); | ||
} | ||
|
||
@PostConstruct | ||
public static void init() { | ||
if (JacksonConfig.mapper == null) { | ||
JacksonConfig.mapper = new ObjectMapper().registerModule(jtsModule()); | ||
|
||
JacksonConfig.mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); | ||
JacksonConfig.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
JacksonConfig.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
|
||
for (var entry : findAnnotatedClasses().entrySet()) { | ||
JacksonConfig.mapper.addMixIn(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
private static Map<Class<?>, Class<?>> findAnnotatedClasses() { | ||
var reflections = new Reflections(new ConfigurationBuilder() | ||
.setUrls(ClasspathHelper.forJavaClassPath()) | ||
.setScanners(new SubTypesScanner(), | ||
new TypeAnnotationsScanner())); | ||
|
||
Map<Class<?>, Class<?>> implementers = new HashMap<>(); | ||
|
||
// this finds the type furthest down along the implementation chain | ||
for (var cl : reflections.getTypesAnnotatedWith(JsonSuperType.class)) { | ||
var annotation = cl.getAnnotation(JsonSuperType.class); | ||
var superType = annotation.type(); | ||
|
||
if (!annotation.override() && !superType.isInterface()) { | ||
throw new IllegalStateException("The super type " + superType.getName() + " is not an interface. " + | ||
"Set override to true if this is intended."); | ||
} | ||
|
||
if (!implementers.containsKey(superType)) { | ||
implementers.put(superType, cl); | ||
} else { | ||
var previous = implementers.get(superType); | ||
if (previous.isAssignableFrom(cl)) { | ||
implementers.put(superType, cl); | ||
} else if (!cl.isAssignableFrom(previous)) { | ||
throw new IllegalStateException("Found 2 incompatible types that both want to deserialize to the type " | ||
+ superType.getName() + ". Any existing type should get extended."); | ||
} | ||
} | ||
} | ||
|
||
return implementers; | ||
} | ||
|
||
} |
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
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
9 changes: 9 additions & 0 deletions
9
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/ApplicationClientConfig.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,9 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
public interface ApplicationClientConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerClientConfig.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerClientConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerConfig.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerFeature.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerFeature extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerSourceConfig.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerSourceConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerToolConfig.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerToolConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/LayerTree.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface LayerTree extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/UserClientConfig.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface UserClientConfig extends Serializable { | ||
} |
6 changes: 6 additions & 0 deletions
6
shogun-lib/src/main/java/de/terrestris/shogun/lib/model/jsonb/UserDetails.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,6 @@ | ||
package de.terrestris.shogun.lib.model.jsonb; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface UserDetails extends Serializable { | ||
} |
16 changes: 0 additions & 16 deletions
16
...c/main/java/de/terrestris/shogun/lib/model/jsonb/application/ApplicationClientConfig.java
This file was deleted.
Oops, something went wrong.
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 @@ | ||
hibernate.types.jackson.object.mapper=de.terrestris.shoguncore.config.JacksonConfig |