Search in sources :

Example 1 with GtfsFeedModel

use of edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsFeed method getFeedErrorCount.

@GET
@Path("/{id : \\d+}/errorCount")
@Produces(MediaType.APPLICATION_JSON)
public Response getFeedErrorCount(@PathParam("id") int id) {
    Session session = GTFSDB.initSessionBeginTrans();
    GtfsFeedModel gtfsFeed = (GtfsFeedModel) session.createQuery(" FROM GtfsFeedModel WHERE feedId = :id").setParameter("id", id).uniqueResult();
    GTFSDB.commitAndCloseSession(session);
    return Response.ok(gtfsFeed).build();
}
Also used : GtfsFeedModel(edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel) Session(org.hibernate.Session)

Example 2 with GtfsFeedModel

use of edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsFeed method postGtfsFeed.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response postGtfsFeed(@FormParam("gtfsurl") String gtfsFeedUrl, @FormParam("enablevalidation") String enableValidation) {
    // Extract the URL from the provided gtfsFeedUrl
    URL url = getUrlFromString(gtfsFeedUrl);
    if (url == null) {
        return generateError("Malformed URL", "Malformed URL for the GTFS feed.", Response.Status.BAD_REQUEST);
    }
    _log.info(String.format("Downloading GTFS data from %s...", url));
    HttpURLConnection connection = null;
    // Open a connection for the given URL u
    try {
        connection = (HttpURLConnection) url.openConnection();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (connection == null) {
        return generateError("Can't read from URL", "Failed to establish a connection to the GTFS URL.", Response.Status.BAD_REQUEST);
    }
    String saveFileName = null;
    try {
        saveFileName = URLEncoder.encode(gtfsFeedUrl, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    boolean canReturn = false;
    // Read gtfsFeedModel with the same URL in the database
    Session session = GTFSDB.initSessionBeginTrans();
    GtfsFeedModel gtfsFeed = (GtfsFeedModel) session.createQuery("FROM GtfsFeedModel " + "WHERE gtfsUrl = :gtfsFeedUrl").setParameter("gtfsFeedUrl", gtfsFeedUrl).uniqueResult();
    Response.Status response = downloadGtfsFeed(saveFileName, connection);
    if (response == Response.Status.BAD_REQUEST) {
        return generateError("Download Failed", "Downloading static GTFS feed from provided Url failed.", Response.Status.BAD_REQUEST);
    } else if (response == Response.Status.FORBIDDEN) {
        return generateError("SSL Handshake Failed", "SSL handshake failed.  Try installing the JCE Extension - see https://github.com/CUTR-at-USF/gtfs-realtime-validator#prerequisites", Response.Status.FORBIDDEN);
    }
    _log.info("GTFS data downloaded successfully");
    // TODO: Move to one method
    if (gtfsFeed == null) {
        gtfsFeed = createGtfsFeedModel(gtfsFeedUrl, saveFileName);
    } else {
        _log.info("GTFS URL already exists exists in database - checking if data has changed...");
        byte[] newChecksum = calculateMD5checksum(gtfsFeed.getFeedLocation());
        byte[] oldChecksum = gtfsFeed.getChecksum();
        // If file digest are equal, check whether validated json file exists
        if (MessageDigest.isEqual(newChecksum, oldChecksum)) {
            _log.info("GTFS data hasn't changed since last execution");
            String projectPath = new GetFile().getJarLocation().getParentFile().getAbsolutePath();
            if (new File(projectPath + File.separator + jsonFilePath + File.separator + saveFileName + "_out.json").exists())
                canReturn = true;
        } else {
            _log.info("GTFS data has changed, updating database...");
            gtfsFeed.setChecksum(newChecksum);
            updateGtfsFeedModel(gtfsFeed);
        }
    }
    // Saves GTFS data to store and validates GTFS feed
    GtfsDaoImpl store = saveGtfsFeed(gtfsFeed);
    if (store == null) {
        return generateError("Can't read content", "Can't read content from the GTFS URL", Response.Status.NOT_FOUND);
    }
    // Save gtfs agency to the database
    gtfsFeed.setAgency(store.getAllAgencies().iterator().next().getTimezone());
    session.update(gtfsFeed);
    GTFSDB.commitAndCloseSession(session);
    GtfsDaoMap.put(gtfsFeed.getFeedId(), store);
    if (canReturn)
        return Response.ok(gtfsFeed).build();
    if ("checked".equalsIgnoreCase(enableValidation)) {
        return runStaticGTFSValidation(saveFileName, gtfsFeedUrl, gtfsFeed);
    }
    return Response.ok(gtfsFeed).build();
}
Also used : GetFile(edu.usf.cutr.gtfsrtvalidator.helper.GetFile) URL(java.net.URL) Response(javax.ws.rs.core.Response) HttpURLConnection(java.net.HttpURLConnection) GtfsDaoImpl(org.onebusaway.gtfs.impl.GtfsDaoImpl) GtfsFeedModel(edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel) GetFile(edu.usf.cutr.gtfsrtvalidator.helper.GetFile) Session(org.hibernate.Session)

Example 3 with GtfsFeedModel

use of edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsFeed method createGtfsFeedModel.

private GtfsFeedModel createGtfsFeedModel(String gtfsFeedUrl, String saveFilePath) {
    GtfsFeedModel gtfsFeed;
    gtfsFeed = new GtfsFeedModel();
    gtfsFeed.setFeedLocation(saveFilePath);
    gtfsFeed.setGtfsUrl(gtfsFeedUrl);
    gtfsFeed.setStartTime(System.currentTimeMillis());
    byte[] checksum = calculateMD5checksum(saveFilePath);
    gtfsFeed.setChecksum(checksum);
    // Create GTFS feed row in database
    Session session = GTFSDB.initSessionBeginTrans();
    session.save(gtfsFeed);
    GTFSDB.commitAndCloseSession(session);
    return gtfsFeed;
}
Also used : GtfsFeedModel(edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel) Session(org.hibernate.Session)

Example 4 with GtfsFeedModel

use of edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsFeed method getGtfsFeeds.

// GET return list of available gtfs-feeds
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getGtfsFeeds() {
    List<GtfsFeedModel> gtfsFeeds = new ArrayList<>();
    try {
        Session session = GTFSDB.initSessionBeginTrans();
        List<GtfsFeedModel> tempGtfsFeeds = session.createQuery(" FROM GtfsFeedModel").list();
        GTFSDB.commitAndCloseSession(session);
        if (tempGtfsFeeds != null) {
            gtfsFeeds = tempGtfsFeeds;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    GenericEntity<List<GtfsFeedModel>> feedList = new GenericEntity<List<GtfsFeedModel>>(gtfsFeeds) {
    };
    return Response.ok(feedList).build();
}
Also used : GenericEntity(javax.ws.rs.core.GenericEntity) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GtfsFeedModel(edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel) MalformedURLException(java.net.MalformedURLException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(org.hibernate.Session)

Aggregations

GtfsFeedModel (edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel)4 Session (org.hibernate.Session)4 GetFile (edu.usf.cutr.gtfsrtvalidator.helper.GetFile)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 GenericEntity (javax.ws.rs.core.GenericEntity)1 Response (javax.ws.rs.core.Response)1 GtfsDaoImpl (org.onebusaway.gtfs.impl.GtfsDaoImpl)1