-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add updated geojson serializers/deserializers
- Loading branch information
Showing
17 changed files
with
436 additions
and
56 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
47 changes: 47 additions & 0 deletions
47
...api/src/main/java/ca/bc/gov/nrs/wfprev/common/serializers/GeoJsonJacksonDeserializer.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,47 @@ | ||
package ca.bc.gov.nrs.wfprev.common.serializers; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.ObjectCodec; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.vividsolutions.jts.geom.Geometry; | ||
import com.vividsolutions.jts.io.ParseException; | ||
import com.vividsolutions.jts.io.geojson.GeoJsonReader; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
|
||
@Slf4j | ||
public class GeoJsonJacksonDeserializer extends StdDeserializer<Geometry> { | ||
|
||
public GeoJsonJacksonDeserializer() { | ||
super(Geometry.class); | ||
} | ||
|
||
public Geometry deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException { | ||
log.trace("<deserialize"); | ||
Geometry result = null; | ||
ObjectCodec oc = jsonParser.getCodec(); | ||
JsonNode node = (JsonNode)oc.readTree(jsonParser); | ||
String geoJson = node.toString(); | ||
GeoJsonReader geoJsonReader = new GeoJsonReader(); | ||
|
||
try { | ||
Reader reader = new StringReader(geoJson); | ||
result = geoJsonReader.read(reader); | ||
} catch (ParseException var9) { | ||
log.error("Failed to deserialize geojson: " + geoJson, var9); | ||
throw new IOException("Failed to deserialize geojson: " + geoJson, var9); | ||
} catch (RuntimeException var10) { | ||
log.error("Failed to deserialize geojson: " + geoJson, var10); | ||
} | ||
|
||
log.trace(">deserialize " + result); | ||
return result; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...v-api/src/main/java/ca/bc/gov/nrs/wfprev/common/serializers/GeoJsonJacksonSerializer.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,37 @@ | ||
package ca.bc.gov.nrs.wfprev.common.serializers; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.vividsolutions.jts.geom.Geometry; | ||
import com.vividsolutions.jts.io.geojson.GeoJsonWriter; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
|
||
@Slf4j | ||
public class GeoJsonJacksonSerializer extends StdSerializer<Geometry> { | ||
|
||
public GeoJsonJacksonSerializer() { | ||
super(Geometry.class); | ||
} | ||
|
||
public void serialize(Geometry geometry, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { | ||
log.trace("<serialize"); | ||
|
||
try { | ||
GeoJsonWriter geoJsonWriter = new GeoJsonWriter(); | ||
StringWriter writer = new StringWriter(); | ||
geoJsonWriter.write(geometry, writer); | ||
String json = writer.toString(); | ||
generator.writeRawValue(json); | ||
} catch (RuntimeException var7) { | ||
log.error("Failed to serialize Geometry: " + geometry, var7); | ||
} | ||
|
||
log.trace(">serialize"); | ||
} | ||
} |
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
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
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
38 changes: 38 additions & 0 deletions
38
server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/MockMvcRestExceptionConfiguration.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,38 @@ | ||
package ca.bc.gov.nrs.wfprev; | ||
|
||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import jakarta.servlet.RequestDispatcher; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@TestConfiguration | ||
public class MockMvcRestExceptionConfiguration implements WebMvcConfigurer { | ||
private final BasicErrorController errorController; | ||
|
||
public MockMvcRestExceptionConfiguration (final BasicErrorController basicErrorController) { | ||
this.errorController = basicErrorController; | ||
} | ||
|
||
@Override | ||
public void addInterceptors (final InterceptorRegistry registry) { | ||
registry.addInterceptor( | ||
new HandlerInterceptor() { | ||
@Override | ||
public void afterCompletion (final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception { | ||
final int status = response.getStatus(); | ||
|
||
if (status >= 400) { | ||
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, status); | ||
new ObjectMapper().writeValue(response.getOutputStream(), MockMvcRestExceptionConfiguration.this.errorController.error(request).getBody()); | ||
} | ||
} | ||
}); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/MockMvcVExceptionAdvice.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,31 @@ | ||
package ca.bc.gov.nrs.wfprev; | ||
|
||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
@ControllerAdvice | ||
public class MockMvcVExceptionAdvice { | ||
|
||
public static final String DEFAULT_ERROR_VIEW = "error"; | ||
|
||
@ExceptionHandler(value = Exception.class) | ||
public ModelAndView | ||
defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { | ||
// If the exception is annotated with @ResponseStatus rethrow it and let | ||
// the framework handle it - like the OrderNotFoundException example | ||
// at the start of this post. | ||
// AnnotationUtils is a Spring Framework utility class. | ||
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e; | ||
|
||
// Otherwise setup and send the user to a default error-view. | ||
ModelAndView mav = new ModelAndView(); | ||
mav.addObject("exception", e); | ||
mav.addObject("url", req.getRequestURL()); | ||
mav.setViewName(DEFAULT_ERROR_VIEW); | ||
return mav; | ||
} | ||
} |
Oops, something went wrong.