Skip to content

Commit

Permalink
Exception Mapping (#1390)
Browse files Browse the repository at this point in the history
* Exception Mapping

* Don't even need to create the Writer myself, even better.

* Put "activation" back in. fixes #1388

* Rename filter so it resembles the actual query param

* ...and the code comment

* License headers
  • Loading branch information
michaz authored and karussell committed Jun 20, 2018
1 parent 8ca3ccd commit e954f00
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 106 deletions.
40 changes: 40 additions & 0 deletions web-api/src/main/java/com/graphhopper/MultiException.java
@@ -0,0 +1,40 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper;

import java.util.Collections;
import java.util.List;

public class MultiException extends RuntimeException {

private final List<Throwable> errors;

public MultiException(List<Throwable> errors) {
this.errors = errors;
}

public MultiException(Throwable e) {
this(Collections.singletonList(e));
}

public List<Throwable> getErrors() {
return errors;
}

}
@@ -1,6 +1,7 @@
package com.graphhopper.jackson; package com.graphhopper.jackson;


import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.graphhopper.MultiException;
import com.graphhopper.util.CmdArgs; import com.graphhopper.util.CmdArgs;
import com.graphhopper.util.InstructionList; import com.graphhopper.util.InstructionList;
import com.graphhopper.util.details.PathDetail; import com.graphhopper.util.details.PathDetail;
Expand All @@ -18,6 +19,7 @@ public GraphHopperModule() {
addSerializer(PathDetail.class, new PathDetailSerializer()); addSerializer(PathDetail.class, new PathDetailSerializer());
addSerializer(InstructionList.class, new InstructionListSerializer()); addSerializer(InstructionList.class, new InstructionListSerializer());
addDeserializer(CmdArgs.class, new CmdArgsDeserializer()); addDeserializer(CmdArgs.class, new CmdArgsDeserializer());
addSerializer(MultiException.class, new MultiExceptionSerializer());
} }


} }
@@ -0,0 +1,59 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.graphhopper.MultiException;
import com.graphhopper.util.exceptions.GHException;

import java.io.IOException;
import java.util.List;

public class MultiExceptionSerializer extends JsonSerializer<MultiException> {

@Override
public void serialize(MultiException e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
List<Throwable> errors = e.getErrors();
ObjectNode json = JsonNodeFactory.instance.objectNode();
json.put("message", getMessage(errors.get(0)));
ArrayNode errorHintList = json.putArray("hints");
for (Throwable t : errors) {
ObjectNode error = errorHintList.addObject();
error.put("message", getMessage(t));
error.put("details", t.getClass().getName());
if (t instanceof GHException) {
((GHException) t).getDetails().forEach(error::putPOJO);
}
}
jsonGenerator.writeObject(json);
}

private static String getMessage(Throwable t) {
if (t.getMessage() == null)
return t.getClass().getSimpleName();
else
return t.getMessage();
}

}
Expand Up @@ -183,6 +183,14 @@ public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider
public void run(GraphHopperBundleConfiguration configuration, Environment environment) { public void run(GraphHopperBundleConfiguration configuration, Environment environment) {
configuration.getGraphHopperConfiguration().merge(CmdArgs.readFromSystemProperties()); configuration.getGraphHopperConfiguration().merge(CmdArgs.readFromSystemProperties());


// If the "?type=gpx" parameter is present, sets a corresponding media type header
environment.jersey().register(new TypeGPXFilter());

// Together, these two take care that MultiExceptions thrown from RouteResource
// come out as JSON or GPX, depending on the media type
environment.jersey().register(new MultiExceptionMapper());
environment.jersey().register(new MultiExceptionGPXMessageBodyWriter());

if (configuration.getGraphHopperConfiguration().has("gtfs.file")) { if (configuration.getGraphHopperConfiguration().has("gtfs.file")) {
// switch to different API implementation when using Pt // switch to different API implementation when using Pt
runPtGraphHopper(configuration.getGraphHopperConfiguration(), environment); runPtGraphHopper(configuration.getGraphHopperConfiguration(), environment);
Expand Down Expand Up @@ -263,7 +271,6 @@ protected void configure() {
environment.jersey().register(IsochroneResource.class); environment.jersey().register(IsochroneResource.class);
environment.jersey().register(I18NResource.class); environment.jersey().register(I18NResource.class);
environment.jersey().register(InfoResource.class); environment.jersey().register(InfoResource.class);

environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopperManaged.getGraphHopper())); environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopperManaged.getGraphHopper()));
} }


Expand Down
@@ -0,0 +1,99 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper.http;

import com.graphhopper.MultiException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

@Provider
@Produces("application/gpx+xml")
public class MultiExceptionGPXMessageBodyWriter implements MessageBodyWriter<MultiException> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
}

@Override
public long getSize(MultiException e, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}

@Override
public void writeTo(MultiException e, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
if (e.getErrors().isEmpty())
throw new RuntimeException("errorsToXML should not be called with an empty list");

try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element gpxElement = doc.createElement("gpx");
gpxElement.setAttribute("creator", "GraphHopper");
gpxElement.setAttribute("version", "1.1");
doc.appendChild(gpxElement);

Element mdElement = doc.createElement("metadata");
gpxElement.appendChild(mdElement);

Element extensionsElement = doc.createElement("extensions");
mdElement.appendChild(extensionsElement);

Element messageElement = doc.createElement("message");
extensionsElement.appendChild(messageElement);
messageElement.setTextContent(e.getErrors().iterator().next().getMessage());

Element hintsElement = doc.createElement("hints");
extensionsElement.appendChild(hintsElement);

for (Throwable t : e.getErrors()) {
Element error = doc.createElement("error");
hintsElement.appendChild(error);
error.setAttribute("message", t.getMessage());
error.setAttribute("details", t.getClass().getName());
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(entityStream));
} catch (ParserConfigurationException | TransformerException e2) {
throw new RuntimeException(e2);
}

}
}
@@ -0,0 +1,33 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper.http;

import com.graphhopper.MultiException;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class MultiExceptionMapper implements ExceptionMapper<MultiException> {
@Override
public Response toResponse(MultiException exception) {
return Response.status(400).entity(exception).build();
}
}
40 changes: 40 additions & 0 deletions web-bundle/src/main/java/com/graphhopper/http/TypeGPXFilter.java
@@ -0,0 +1,40 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphhopper.http;

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.HttpHeaders;

@PreMatching
@Priority(Priorities.HEADER_DECORATOR)
public class TypeGPXFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext rc) {
String maybeType = rc.getUriInfo().getQueryParameters().getFirst("type");
if (maybeType != null && maybeType.equals("gpx")) {
rc.getHeaders().putSingle(HttpHeaders.ACCEPT, "application/gpx+xml");
}
}

}

0 comments on commit e954f00

Please sign in to comment.