forked from graphhopper/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphHopperBundle.java
More file actions
312 lines (252 loc) · 11.8 KB
/
GraphHopperBundle.java
File metadata and controls
312 lines (252 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* 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.databind.MapperFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.graphhopper.GraphHopper;
import com.graphhopper.GraphHopperConfig;
import com.graphhopper.gtfs.*;
import com.graphhopper.http.health.GraphHopperHealthCheck;
import com.graphhopper.isochrone.algorithm.JTSTriangulator;
import com.graphhopper.isochrone.algorithm.Triangulator;
import com.graphhopper.jackson.Jackson;
import com.graphhopper.matching.MapMatching;
import com.graphhopper.resources.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.BaseGraph;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.util.PMap;
import com.graphhopper.util.TranslationMap;
import com.graphhopper.util.details.PathDetailsBuilderFactory;
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;
public class GraphHopperBundle implements ConfiguredBundle<GraphHopperBundleConfiguration> {
static class TranslationMapFactory implements Factory<TranslationMap> {
@Inject
GraphHopper graphHopper;
@Override
public TranslationMap provide() {
return graphHopper.getTranslationMap();
}
@Override
public void dispose(TranslationMap instance) {
}
}
static class BaseGraphFactory implements Factory<BaseGraph> {
@Inject
GraphHopper graphHopper;
@Override
public BaseGraph provide() {
return graphHopper.getBaseGraph();
}
@Override
public void dispose(BaseGraph instance) {
}
}
static class GtfsStorageFactory implements Factory<GtfsStorage> {
@Inject
GraphHopperGtfs graphHopper;
@Override
public GtfsStorage provide() {
return graphHopper.getGtfsStorage();
}
@Override
public void dispose(GtfsStorage instance) {
}
}
static class EncodingManagerFactory implements Factory<EncodingManager> {
@Inject
GraphHopper graphHopper;
@Override
public EncodingManager provide() {
return graphHopper.getEncodingManager();
}
@Override
public void dispose(EncodingManager instance) {
}
}
static class LocationIndexFactory implements Factory<LocationIndex> {
@Inject
GraphHopper graphHopper;
@Override
public LocationIndex provide() {
return graphHopper.getLocationIndex();
}
@Override
public void dispose(LocationIndex instance) {
}
}
static class ProfileResolverFactory implements Factory<ProfileResolver> {
@Inject
GraphHopper graphHopper;
@Override
public ProfileResolver provide() {
return new ProfileResolver(graphHopper.getProfiles());
}
@Override
public void dispose(ProfileResolver instance) {
}
}
static class GHRequestTransformerFactory implements Factory<GHRequestTransformer> {
@Override
public GHRequestTransformer provide() {
return req -> req;
}
@Override
public void dispose(GHRequestTransformer instance) {
}
}
static class PathDetailsBuilderFactoryFactory implements Factory<PathDetailsBuilderFactory> {
@Inject
GraphHopper graphHopper;
@Override
public PathDetailsBuilderFactory provide() {
return graphHopper.getPathDetailsBuilderFactory();
}
@Override
public void dispose(PathDetailsBuilderFactory profileResolver) {
}
}
static class MapMatchingRouterFactoryFactory implements Factory<MapMatchingResource.MapMatchingRouterFactory> {
@Inject
GraphHopper graphHopper;
@Override
public MapMatchingResource.MapMatchingRouterFactory provide() {
return new MapMatchingResource.MapMatchingRouterFactory() {
@Override
public MapMatching.Router createMapMatchingRouter(PMap hints) {
return MapMatching.routerFromGraphHopper(graphHopper, hints);
}
};
}
@Override
public void dispose(MapMatchingResource.MapMatchingRouterFactory mapMatchingRouterFactory) {
}
}
static class HasElevation implements Factory<Boolean> {
@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);
}
@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");
}
// When Dropwizard's Hibernate Validation misvalidates a query parameter,
// a JerseyViolationException is thrown.
// With this mapper, we use our custom format for that (backwards compatibility),
// and also coerce the media type of the response to JSON, so we can return JSON error
// messages from methods that normally have a different return type.
// That's questionable, but on the other hand, Dropwizard itself does the same thing,
// not here, but in a different place (the custom parameter parsers).
// So for the moment we have to assume that both mechanisms
// a) always return JSON error messages, and
// b) there's no need to annotate the method with media type JSON for that.
//
// However, for places that throw IllegalArgumentException or MultiException,
// we DO need to use the media type JSON annotation, because
// those are agnostic to the media type (could be GPX!), so the server needs to know
// that a JSON error response is supported. (See below.)
environment.jersey().register(new GHJerseyViolationExceptionMapper());
// 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());
// This makes an IllegalArgumentException come out as a MultiException with
// a single entry.
environment.jersey().register(new IllegalArgumentExceptionMapper());
final GraphHopperManaged graphHopperManaged = new GraphHopperManaged(configuration.getGraphHopperConfiguration());
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(new JTSTriangulator(graphHopper.getRouterConfig())).to(Triangulator.class);
bindFactory(MapMatchingRouterFactoryFactory.class).to(MapMatchingResource.MapMatchingRouterFactory.class);
bindFactory(PathDetailsBuilderFactoryFactory.class).to(PathDetailsBuilderFactory.class);
bindFactory(ProfileResolverFactory.class).to(ProfileResolver.class);
bindFactory(GHRequestTransformerFactory.class).to(GHRequestTransformer.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(BaseGraphFactory.class).to(BaseGraph.class);
bindFactory(GtfsStorageFactory.class).to(GtfsStorage.class);
}
});
environment.jersey().register(MVTResource.class);
environment.jersey().register(NearestResource.class);
environment.jersey().register(RouteResource.class);
environment.jersey().register(MatrixResource.class);
environment.jersey().register(IsochroneResource.class);
environment.jersey().register(MapMatchingResource.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(new AbstractBinder() {
@Override
protected void configure() {
if (configuration.getGraphHopperConfiguration().getBool("gtfs.free_walk", false)) {
bind(PtRouterFreeWalkImpl.class).to(PtRouter.class);
} else {
bind(PtRouterImpl.class).to(PtRouter.class);
}
}
});
environment.jersey().register(PtRouteResource.class);
environment.jersey().register(PtIsochroneResource.class);
environment.jersey().register(PtMVTResource.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));
environment.jersey().register(environment.healthChecks());
environment.jersey().register(HealthCheckResource.class);
}
}