diff --git a/web-bundle/src/main/java/com/graphhopper/http/GpxMessageBodyReader.java b/web-bundle/src/main/java/com/graphhopper/http/GpxMessageBodyReader.java new file mode 100644 index 0000000000..25812b793a --- /dev/null +++ b/web-bundle/src/main/java/com/graphhopper/http/GpxMessageBodyReader.java @@ -0,0 +1,30 @@ +package com.graphhopper.http; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.graphhopper.jackson.Gpx; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.ext.Provider; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.ext.MessageBodyReader; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.io.IOException; + +@Provider +@Consumes({"application/gpx+xml", "application/xml"}) +public class GpxMessageBodyReader implements MessageBodyReader { + final private XmlMapper xmlMapper = new XmlMapper(); + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type == Gpx.class; + } + + @Override + public Gpx readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, InputStream entityStream) throws IOException + { + return xmlMapper.readValue(entityStream, Gpx.class); + } +} diff --git a/web/src/main/java/com/graphhopper/application/GraphHopperApplication.java b/web/src/main/java/com/graphhopper/application/GraphHopperApplication.java index 791351dc01..0c98596aa7 100644 --- a/web/src/main/java/com/graphhopper/application/GraphHopperApplication.java +++ b/web/src/main/java/com/graphhopper/application/GraphHopperApplication.java @@ -21,6 +21,7 @@ import com.graphhopper.application.cli.MatchCommand; import com.graphhopper.application.resources.RootResource; import com.graphhopper.http.CORSFilter; +import com.graphhopper.http.GpxMessageBodyReader; import com.graphhopper.http.GraphHopperBundle; import com.graphhopper.http.RealtimeBundle; import com.graphhopper.navigation.NavigateResource; @@ -53,6 +54,7 @@ public void initialize(Bootstrap bootstrap) { public void run(GraphHopperServerConfiguration configuration, Environment environment) { environment.jersey().register(new RootResource()); environment.jersey().register(NavigateResource.class); + environment.jersey().register(GpxMessageBodyReader.class); environment.servlets().addFilter("cors", CORSFilter.class).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "*"); } }