use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.
the class PredictionsForMultiStopsAction method getModel.
public Body<Predictions> getModel() {
Body<Predictions> body = new Body<Predictions>();
if (isValid(body)) {
String serviceUrl = getServiceUrl() + agencyId + PREDICTIONS_COMMAND + "?";
String routeStopIds = getStopParams();
String uri = serviceUrl + routeStopIds + "format=" + REQUEST_TYPE;
try {
int timeout = _configUtil.getHttpTimeoutSeconds();
JsonArray predictionsJson = _httpUtil.getJsonObject(uri, timeout).getAsJsonArray("predictions");
Type listType = new TypeToken<List<Predictions>>() {
}.getType();
List<Predictions> predictions = new Gson().fromJson(predictionsJson, listType);
modifyJSONObject(predictions);
body.getResponse().addAll(predictions);
} catch (Exception e) {
body.getErrors().add(new BodyError("No valid results found."));
}
}
return body;
}
use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.
the class AgencyListAction method getModel.
@Override
public Body<Agency> getModel() {
Body<Agency> body = new Body<Agency>();
try {
List<AgencyWithCoverageBean> agencies = _service.getAgenciesWithCoverage();
for (AgencyWithCoverageBean agencyBean : agencies) {
Agency agency = new Agency();
agency.setTag(agencyBean.getAgency().getId());
agency.setTitle(agencyBean.getAgency().getName());
agency.setRegionTitle(agencyBean.getAgency().getName());
body.getResponse().add(agency);
}
} catch (ServiceException e) {
body.getErrors().add(new BodyError(ErrorMsg.SERVICE_ERROR.getDescription()));
}
return body;
}
use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.
the class NextBusApiBase method processRouteIds.
protected <E> boolean processRouteIds(String routeVal, List<AgencyAndId> routeIds, List<String> agencyIds, Body<E> body, boolean handleErrors) {
if (routeVal != null) {
try {
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(routeVal);
if (this.isValidRoute(routeId))
routeIds.add(routeId);
} catch (IllegalStateException e) {
for (String agency : agencyIds) {
AgencyAndId routeId = new AgencyAndId(agency, routeVal);
if (this.isValidRoute(routeId)) {
routeIds.add(routeId);
}
}
}
if (handleErrors && routeIds.size() == 0) {
String agencyId = agencyIds.get(0);
body.getErrors().add(new BodyError(ErrorMsg.ROUTE_UNAVAILABLE.getDescription(), agencyId, routeVal));
return false;
}
} else {
if (handleErrors) {
body.getErrors().add(new BodyError(ErrorMsg.ROUTE_NULL.getDescription(), agencyIds.get(0)));
}
return false;
}
return true;
}
use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.
the class PredictionsAction method isValid.
private boolean isValid(Body body, List<AgencyAndId> stopIds, List<AgencyAndId> routeIds) {
if (!isValidAgency(body, agencyId))
return false;
List<String> agencies = new ArrayList<String>();
agencies.add(agencyId);
if (!processStopIds(stopId, stopIds, agencies, body))
return false;
StopBean stopBean = getCachedStopBean(stopIds.get(0).toString());
if (routeTag == null) {
for (RouteBean routeBean : stopBean.getRoutes()) {
routeIds.add(AgencyAndId.convertFromString(routeBean.getId()));
}
} else {
if (!processRouteIds(routeTag, routeIds, agencies, body))
return false;
boolean stopServesRoute = false;
for (RouteBean routeBean : stopBean.getRoutes()) {
if (routeIds.contains(AgencyAndId.convertFromString(routeBean.getId())))
stopServesRoute = true;
}
if (!stopServesRoute) {
body.getErrors().add(new BodyError(ErrorMsg.ROUTE_UNAVAILABLE.getDescription(), agencyId, routeTag));
return false;
}
}
return true;
}
use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.
the class BodySerializer method serialize.
@Override
public void serialize(Body<T> value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
List<T> response = value.getResponse();
List<BodyError> errors = value.getErrors();
String copyright = value.getCopyright();
LastTime lastTime = value.getLastTime();
gen.writeStartObject();
// Copyright
if (StringUtils.isNotBlank(copyright))
gen.writeStringField("copyright", copyright);
// Error
if (errors != null && errors.size() > 0) {
for (BodyError error : errors) error.setContent(error.getContent().replace("\\", ""));
gen.writeObjectField("Error", errors);
}
// Response
if (response != null && response.size() > 0) {
JsonRootName jsonRootName = response.get(0).getClass().getAnnotation(JsonRootName.class);
if (jsonRootName != null)
gen.writeObjectField(jsonRootName.value(), response);
else
gen.writeObjectField("response", response);
}
// LastTime
if (lastTime != null)
gen.writeObjectField("lastTime", lastTime);
gen.writeEndObject();
}
Aggregations