Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
10 changed files
with
299 additions
and
106 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} | |||
|
|||
} |
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
59 changes: 59 additions & 0 deletions
59
web-api/src/main/java/com/graphhopper/jackson/MultiExceptionSerializer.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); | |||
} | |||
|
|||
} |
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
99 changes: 99 additions & 0 deletions
99
web-bundle/src/main/java/com/graphhopper/http/MultiExceptionGPXMessageBodyWriter.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
|
|||
} | |||
} |
33 changes: 33 additions & 0 deletions
33
web-bundle/src/main/java/com/graphhopper/http/MultiExceptionMapper.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
40
web-bundle/src/main/java/com/graphhopper/http/TypeGPXFilter.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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"); | |||
} | |||
} | |||
|
|||
} |
Oops, something went wrong.