Search in sources :

Example 6 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class TripUpdatesForAgencyActionTest method test.

@Test
public void test() {
    long now = System.currentTimeMillis();
    List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
    RouteBean.Builder routeBuilder = RouteBean.builder();
    routeBuilder.setId("1_r1");
    RouteBean route = routeBuilder.create();
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(1234 * 1000);
        vehicle.setVehicleId("1_v1");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        tripStatus.setScheduleDeviation(2 * 60);
        TripBean trip = new TripBean();
        trip.setId("1_t0");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        StopBean stop = new StopBean();
        stop.setId("1_s2");
        tripStatus.setNextStop(stop);
        tripStatus.setNextStopTimeOffset(5 * 60);
    }
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(5678 * 1000);
        vehicle.setVehicleId("1_v2");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        tripStatus.setScheduleDeviation(3 * 60);
        TripBean trip = new TripBean();
        trip.setId("1_t1");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        StopBean stop = new StopBean();
        stop.setId("1_s3");
        tripStatus.setNextStop(stop);
        tripStatus.setNextStopTimeOffset(10 * 60);
    }
    ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
    bean.setList(vehicles);
    Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
    _action.setId("1");
    _action.setTime(new Date(now));
    _action.show();
    ResponseBean model = _action.getModel();
    FeedMessage feed = (FeedMessage) model.getData();
    assertEquals(now / 1000, feed.getHeader().getTimestamp());
    assertEquals(2, feed.getEntityCount());
    {
        FeedEntity entity = feed.getEntity(0);
        assertEquals("1", entity.getId());
        TripUpdate tripUpdate = entity.getTripUpdate();
        assertEquals("t0", tripUpdate.getTrip().getTripId());
        assertEquals("r1", tripUpdate.getTrip().getRouteId());
        assertEquals("v1", tripUpdate.getVehicle().getId());
        assertEquals(1234, tripUpdate.getTimestamp());
        assertEquals(120, tripUpdate.getDelay());
        assertEquals(1, tripUpdate.getStopTimeUpdateCount());
        StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
        assertEquals("s2", stopTimeUpdate.getStopId());
        assertEquals(now / 1000 + 5 * 60, stopTimeUpdate.getDeparture().getTime());
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        TripUpdate tripUpdate = entity.getTripUpdate();
        assertEquals("t1", tripUpdate.getTrip().getTripId());
        assertEquals("r1", tripUpdate.getTrip().getRouteId());
        assertEquals("v2", tripUpdate.getVehicle().getId());
        assertEquals(5678, tripUpdate.getTimestamp());
        assertEquals(180, tripUpdate.getDelay());
        assertEquals(1, tripUpdate.getStopTimeUpdateCount());
        StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
        assertEquals("s3", stopTimeUpdate.getStopId());
        assertEquals(now / 1000 + 10 * 60, stopTimeUpdate.getDeparture().getTime());
    }
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) Date(java.util.Date) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) StopBean(org.onebusaway.transit_data.model.StopBean) ResponseBean(org.onebusaway.api.model.ResponseBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Example 7 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class CustomProtocolBufferTextHandler method fromObject.

@Override
public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
    ResponseBean response = (ResponseBean) obj;
    if (response.getData() != null && response.getData() instanceof Message) {
        Message message = (Message) response.getData();
        stream.write(message.toString());
    } else {
        stream.write(response.getText());
    }
    return null;
}
Also used : Message(com.google.protobuf.Message) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 8 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class ApiActionSupport method setValidationErrorsResponse.

protected DefaultHttpHeaders setValidationErrorsResponse() {
    ValidationErrorBean bean = new ValidationErrorBean(new ArrayList<String>(getActionErrors()), getFieldErrors());
    _response = new ResponseBean(getReturnVersion(), ResponseCodes.RESPONSE_INVALID_ARGUMENT, "validation error", bean);
    return new DefaultHttpHeaders().withStatus(_response.getCode());
}
Also used : DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 9 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class ApiKeyInterceptor method unauthorized.

// package private for unit tests
String unauthorized(ActionInvocation invocation, ApiKeyPermissionService.Status reason) throws IOException {
    ActionProxy proxy = invocation.getProxy();
    int httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
    String message = "permission denied";
    switch(reason) {
        case UNAUTHORIZED:
            httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
            break;
        case RATE_EXCEEDED:
            httpCode = ResponseCodes.RESPONSE_TOO_MANY_REQUESTS;
            message = "rate limit exceeded";
            break;
        case AUTHORIZED:
            // this should never happen!
            throw new IllegalStateException("Valid status code " + reason + " in unauthorized response");
        default:
            httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
    }
    ResponseBean response = new ResponseBean(1, httpCode, message, null);
    DefaultHttpHeaders methodResult = new DefaultHttpHeaders().withStatus(response.getCode());
    return _handlerSelector.handleResult(proxy.getConfig(), methodResult, response);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean)

Aggregations

ResponseBean (org.onebusaway.api.model.ResponseBean)9 Date (java.util.Date)4 Test (org.junit.Test)4 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)3 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)3 ArrayList (java.util.ArrayList)3 DefaultHttpHeaders (org.apache.struts2.rest.DefaultHttpHeaders)3 ListBean (org.onebusaway.transit_data.model.ListBean)3 Message (com.google.protobuf.Message)2 EntryWithReferencesBean (org.onebusaway.api.model.transit.EntryWithReferencesBean)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)2 TripBean (org.onebusaway.transit_data.model.trips.TripBean)2 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)2 Alert (com.google.transit.realtime.GtfsRealtime.Alert)1 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)1 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)1 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)1 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)1 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)1