com.graphhopper.http.GraphHopperBundle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphhopper-web-bundle Show documentation
Show all versions of graphhopper-web-bundle Show documentation
Use the GraphHopper routing engine as a web-service
/*
* 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.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.graphhopper.GraphHopper;
import com.graphhopper.GraphHopperAPI;
import com.graphhopper.GraphHopperConfig;
import com.graphhopper.http.health.GraphHopperHealthCheck;
import com.graphhopper.jackson.Jackson;
import com.graphhopper.reader.gtfs.GraphHopperGtfs;
import com.graphhopper.reader.gtfs.GtfsStorage;
import com.graphhopper.reader.gtfs.PtRouteResource;
import com.graphhopper.resources.*;
import com.graphhopper.routing.ProfileResolver;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.util.TranslationMap;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
public class GraphHopperBundle implements ConfiguredBundle {
static class TranslationMapFactory implements Factory {
@Inject
GraphHopper graphHopper;
@Override
public TranslationMap provide() {
return graphHopper.getTranslationMap();
}
@Override
public void dispose(TranslationMap instance) {
}
}
static class GraphHopperStorageFactory implements Factory {
@Inject
GraphHopper graphHopper;
@Override
public GraphHopperStorage provide() {
return graphHopper.getGraphHopperStorage();
}
@Override
public void dispose(GraphHopperStorage instance) {
}
}
static class GtfsStorageFactory implements Factory {
@Inject
GraphHopperGtfs graphHopper;
@Override
public GtfsStorage provide() {
return graphHopper.getGtfsStorage();
}
@Override
public void dispose(GtfsStorage instance) {
}
}
static class EncodingManagerFactory implements Factory {
@Inject
GraphHopper graphHopper;
@Override
public EncodingManager provide() {
return graphHopper.getEncodingManager();
}
@Override
public void dispose(EncodingManager instance) {
}
}
static class LocationIndexFactory implements Factory {
@Inject
GraphHopper graphHopper;
@Override
public LocationIndex provide() {
return graphHopper.getLocationIndex();
}
@Override
public void dispose(LocationIndex instance) {
}
}
static class HasElevation implements Factory {
@Inject
GraphHopper graphHopper;
@Override
public Boolean provide() {
return graphHopper.hasElevation();
}
@Override
public void dispose(Boolean instance) {
}
}
@Override
public void initialize(Bootstrap> bootstrap) {
// See #1440: avoids warning regarding com.fasterxml.jackson.module.afterburner.util.MyClassLoader
bootstrap.setObjectMapper(io.dropwizard.jackson.Jackson.newMinimalObjectMapper());
// avoids warning regarding com.fasterxml.jackson.databind.util.ClassUtil
bootstrap.getObjectMapper().registerModule(new Jdk8Module());
Jackson.initObjectMapper(bootstrap.getObjectMapper());
bootstrap.getObjectMapper().setDateFormat(new StdDateFormat());
// See https://github.com/dropwizard/dropwizard/issues/1558
bootstrap.getObjectMapper().enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING);
// Because VirtualEdgeIteratorState has getters which throw Exceptions.
// http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
bootstrap.getObjectMapper().registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {
@Override
public List changeProperties(SerializationConfig config, BeanDescription beanDesc, List beanProperties) {
return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {
@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
try {
super.serializeAsField(bean, gen, prov);
} catch (Exception e) {
// Ignoring expected exception, see above.
}
}
}).collect(Collectors.toList());
}
}));
}
@Override
public void run(GraphHopperBundleConfiguration configuration, Environment environment) {
for (Object k : System.getProperties().keySet()) {
if (k instanceof String && ((String) k).startsWith("graphhopper."))
throw new IllegalArgumentException("You need to prefix system parameters with '-Ddw.graphhopper.' instead of '-Dgraphhopper.' see #1879 and #1897");
}
// 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());
environment.jersey().register(new IllegalArgumentExceptionMapper());
environment.jersey().register(new GHPointConverterProvider());
final GraphHopperManaged graphHopperManaged = new GraphHopperManaged(configuration.getGraphHopperConfiguration(), environment.getObjectMapper());
environment.lifecycle().manage(graphHopperManaged);
final GraphHopper graphHopper = graphHopperManaged.getGraphHopper();
environment.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
bind(configuration.getGraphHopperConfiguration()).to(GraphHopperConfig.class);
bind(graphHopper).to(GraphHopper.class);
bind(graphHopper).to(GraphHopperAPI.class);
bind(new ProfileResolver(graphHopper.getEncodingManager(), graphHopper.getProfiles(), graphHopper.getCHPreparationHandler().getCHProfileConfigs(), graphHopper.getLMPreparationHandler().getLMProfileConfigs()))
.to(ProfileResolver.class);
bindFactory(HasElevation.class).to(Boolean.class).named("hasElevation");
bindFactory(LocationIndexFactory.class).to(LocationIndex.class);
bindFactory(TranslationMapFactory.class).to(TranslationMap.class);
bindFactory(EncodingManagerFactory.class).to(EncodingManager.class);
bindFactory(GraphHopperStorageFactory.class).to(GraphHopperStorage.class);
bindFactory(GtfsStorageFactory.class).to(GtfsStorage.class);
}
});
if (configuration.getGraphHopperConfiguration().getBool("web.change_graph.enabled", false)) {
environment.jersey().register(ChangeGraphResource.class);
}
environment.jersey().register(MVTResource.class);
environment.jersey().register(NearestResource.class);
environment.jersey().register(RouteResource.class);
environment.jersey().register(IsochroneResource.class);
if (configuration.getGraphHopperConfiguration().has("gtfs.file")) {
// These are pt-specific implementations of /route and /isochrone, but the same API.
// We serve them under different paths (/route-pt and /isochrone-pt), and forward
// requests for ?vehicle=pt there.
environment.jersey().register(PtRouteResource.class);
environment.jersey().register(PtIsochroneResource.class);
environment.jersey().register(PtRedirectFilter.class);
}
environment.jersey().register(SPTResource.class);
environment.jersey().register(I18NResource.class);
environment.jersey().register(InfoResource.class);
environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopper));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy