use of org.opentripplanner.api.mapping.TripPlanMapper in project OpenTripPlanner by opentripplanner.
the class PlannerResource method plan.
// We inject info about the incoming request so we can include the incoming query
// parameters in the outgoing response. This is a TriMet requirement.
// Jersey uses @Context to inject internal types and @InjectParam or @Resource for DI objects.
@GET
@Produces(MediaType.APPLICATION_JSON)
public TripPlannerResponse plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest) {
/*
* TODO: add Lang / Locale parameter, and thus get localized content (Messages & more...)
* TODO: from/to inputs should be converted / geocoded / etc... here, and maybe send coords
* or vertex ids to planner (or error back to user)
* TODO: org.opentripplanner.routing.module.PathServiceImpl has COOORD parsing. Abstract that
* out so it's used here too...
*/
// Create response object, containing a copy of all request parameters. Maybe they should be in the debug section of the response.
TripPlannerResponse response = new TripPlannerResponse(uriInfo);
RoutingRequest request = null;
Router router = null;
RoutingResponse res = null;
try {
/* Fill in request fields from query parameters via shared superclass method, catching any errors. */
request = super.buildRequest();
router = otpServer.getRouter();
// Route
RoutingService routingService = new RoutingService(router.graph);
res = routingService.route(request, router);
// Map to API
TripPlanMapper tripPlanMapper = new TripPlanMapper(request.locale);
response.setPlan(tripPlanMapper.mapTripPlan(res.getTripPlan()));
response.setMetadata(TripSearchMetadataMapper.mapTripSearchMetadata(res.getMetadata()));
if (!res.getRoutingErrors().isEmpty()) {
// The api can only return one error message, so the first one is mapped
response.setError(PlannerErrorMapper.mapMessage(res.getRoutingErrors().get(0)));
}
/* Populate up the elevation metadata */
response.elevationMetadata = new ElevationMetadata();
response.elevationMetadata.ellipsoidToGeoidDifference = router.graph.ellipsoidToGeoidDifference;
response.elevationMetadata.geoidElevation = request.geoidElevation;
response.debugOutput = res.getDebugAggregator().finishedRendering();
} catch (Exception e) {
LOG.error("System error", e);
PlannerError error = new PlannerError();
error.setMsg(Message.SYSTEM_ERROR);
response.setError(error);
}
/* Log this request if such logging is enabled. */
if (request != null && router != null && router.requestLogger != null) {
StringBuilder sb = new StringBuilder();
String clientIpAddress = grizzlyRequest.getRemoteAddr();
// sb.append(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
sb.append(clientIpAddress);
sb.append(' ');
sb.append(request.arriveBy ? "ARRIVE" : "DEPART");
sb.append(' ');
sb.append(LocalDateTime.ofInstant(Instant.ofEpochSecond(request.dateTime), ZoneId.systemDefault()));
sb.append(' ');
sb.append(request.streetSubRequestModes.getAsStr());
sb.append(' ');
sb.append(request.from.lat);
sb.append(' ');
sb.append(request.from.lng);
sb.append(' ');
sb.append(request.to.lat);
sb.append(' ');
sb.append(request.to.lng);
sb.append(' ');
if (res != null) {
for (Itinerary it : res.getTripPlan().itineraries) {
sb.append(it.durationSeconds);
sb.append(' ');
}
}
router.requestLogger.info(sb.toString());
}
return response;
}
Aggregations