Search in sources :

Example 1 with RoutingResponse

use of org.opentripplanner.routing.api.response.RoutingResponse 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)

Example 2 with RoutingResponse

use of org.opentripplanner.routing.api.response.RoutingResponse in project OpenTripPlanner by opentripplanner.

the class TransmodelGraphQLPlanner method plan.

public PlanResponse plan(DataFetchingEnvironment environment) {
    PlanResponse response = new PlanResponse();
    RoutingRequest request = null;
    try {
        TransmodelRequestContext ctx = environment.getContext();
        Router router = ctx.getRouter();
        request = createRequest(environment);
        RoutingResponse res = ctx.getRoutingService().route(request, router);
        response.plan = res.getTripPlan();
        response.metadata = res.getMetadata();
        for (RoutingError routingError : res.getRoutingErrors()) {
            response.messages.add(PlannerErrorMapper.mapMessage(routingError).message);
        }
        response.debugOutput = res.getDebugAggregator().finishedRendering();
    } catch (Exception e) {
        LOG.warn("System error");
        LOG.error("Root cause: " + e.getMessage(), e);
        PlannerError error = new PlannerError();
        error.setMsg(Message.SYSTEM_ERROR);
        response.messages.add(error.message);
    }
    return response;
}
Also used : RoutingError(org.opentripplanner.routing.api.response.RoutingError) PlanResponse(org.opentripplanner.ext.transmodelapi.model.PlanResponse) 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) ParameterException(org.opentripplanner.api.common.ParameterException)

Aggregations

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