Skip to content

Commit

Permalink
Add updated geojson serializers/deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhlevi committed Nov 23, 2024
1 parent 80c598c commit 3355606
Show file tree
Hide file tree
Showing 17 changed files with 436 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ AuthenticationEntryPoint authenticationEntryPoint() {
BasicAuthenticationEntryPoint result;

result = new BasicAuthenticationEntryPoint();
result.setRealmName("wfim-incidents-api");
result.setRealmName("wfprev-api");

return result;
}
Expand All @@ -60,7 +60,7 @@ public TokenService tokenServiceImpl() {

@Bean
public AuthenticationProvider authenticationProvider() {
return new WebadeOauth2AuthenticationProvider(tokenServiceImpl(), "WFIM.*");
return new WebadeOauth2AuthenticationProvider(tokenServiceImpl(), "WFPREV.*");
}

@Bean
Expand Down
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;
}
}
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ProgramAreaController(ProgramAreaService programAreaService) {
this.programAreaService = programAreaService;
}

@GetMapping("/")
@GetMapping
@Operation(summary = "Fetch all Program Area Resources",
description = "Fetch all Program Area Resources",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down Expand Up @@ -93,7 +93,7 @@ public ResponseEntity<ProgramAreaModel> getById(@PathVariable("id") String id) {
return response;
}

@PostMapping("/")
@PostMapping
@Operation(summary = "Create a Program Area Resource",
description = "Create a Program Area Resource",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.UUID;

import org.springframework.hateoas.CollectionModel;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ProjectBoundaryController(ProjectBoundaryService projectBoundaryService)
this.projectBoundaryService = projectBoundaryService;
}

@GetMapping("/")
@GetMapping
@Operation(summary = "Fetch all Project Boundary Resources",
description = "Fetch all Project Boundary Resources",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down Expand Up @@ -92,7 +93,7 @@ public ResponseEntity<ProjectBoundaryModel> getById(@PathVariable("id") String i
return response;
}

@PostMapping("/")
@PostMapping(consumes = "application/json")
@Operation(summary = "Create a project boundary Resource",
description = "Create a project boundary Resource",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ProjectController(ProjectService projectService) {
this.projectService = projectService;
}

@GetMapping("/")
@GetMapping
@Operation(summary = "Fetch all Project Resources",
description = "Fetch all Project Resources",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down Expand Up @@ -92,7 +92,7 @@ public ResponseEntity<ProjectModel> getById(@PathVariable("id") String id) {
return response;
}

@PostMapping("/")
@PostMapping
@Operation(summary = "Create a Project Resource",
description = "Create a Project Resource",
security = @SecurityRequirement(name = "Webade-OAUTH2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public class ForestAreaCodeEntity implements Serializable {
private Integer displayOrder;

@NotNull
@Column(name = "effective_date", length = 200)
@Column(name = "effective_date")
private Date effectiveDate;

@NotNull
@Column(name = "expiry_date", length = 200)
@Column(name = "expiry_date")
private Date expiryDate;

@Column(name = "revision_count", columnDefinition="Decimal(10) default '0'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public class GeneralScopeCodeEntity implements Serializable {
private Integer displayOrder;

@NotNull
@Column(name = "effective_date", length = 200)
@Column(name = "effective_date")
private Date effectiveDate;

@NotNull
@Column(name = "expiry_date", length = 200)
@Column(name = "expiry_date")
private Date expiryDate;

@Column(name = "revision_count", columnDefinition="Decimal(10) default '0'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

import ca.bc.gov.nrs.common.wfone.rest.resource.transformers.GeoJsonJacksonDeserializer;
import ca.bc.gov.nrs.common.wfone.rest.resource.transformers.GeoJsonJacksonSerializer;
import ca.bc.gov.nrs.wfprev.common.serializers.GeoJsonJacksonDeserializer;
import ca.bc.gov.nrs.wfprev.common.serializers.GeoJsonJacksonSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -83,7 +82,7 @@ public class ProjectBoundaryEntity implements Serializable {
@JsonSerialize(using=GeoJsonJacksonSerializer.class)
@JsonDeserialize(using=GeoJsonJacksonDeserializer.class)
@Column(name="location_geometry", columnDefinition = "geometry(Point,4326)")
public Point locationGeometry;
public Geometry locationGeometry;

@NotNull
@JsonSerialize(using=GeoJsonJacksonSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public class ProjectTypeCodeEntity implements Serializable {
private Integer displayOrder;

@NotNull
@Column(name = "effective_date", length = 200)
@Column(name = "effective_date")
private Date effectiveDate;

@NotNull
@Column(name = "expiry_date", length = 200)
@Column(name = "expiry_date")
private Date expiryDate;

@Column(name = "revision_count", columnDefinition="Decimal(10) default '0'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public class ProjectBoundaryModel extends CommonModel<ProjectBoundaryModel> {
private String collectorName;
private BigDecimal boundarySizeHa;
private String boundaryComment;
private Point locationGeometry;
private Geometry locationGeometry;
private Geometry boundaryGeometry;
}
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());
}
}
});
}
}
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;
}
}
Loading

0 comments on commit 3355606

Please sign in to comment.