Search in sources :

Example 1 with TripPlanMapper

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;
}
Also used : TripPlanMapper(org.opentripplanner.api.mapping.TripPlanMapper) RoutingService(org.opentripplanner.routing.RoutingService) Itinerary(org.opentripplanner.model.plan.Itinerary) Router(org.opentripplanner.standalone.server.Router) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) PlannerError(org.opentripplanner.api.model.error.PlannerError) RoutingResponse(org.opentripplanner.routing.api.response.RoutingResponse) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 TripPlanMapper (org.opentripplanner.api.mapping.TripPlanMapper)1 PlannerError (org.opentripplanner.api.model.error.PlannerError)1 Itinerary (org.opentripplanner.model.plan.Itinerary)1 RoutingService (org.opentripplanner.routing.RoutingService)1 RoutingRequest (org.opentripplanner.routing.api.request.RoutingRequest)1 RoutingResponse (org.opentripplanner.routing.api.response.RoutingResponse)1 Router (org.opentripplanner.standalone.server.Router)1