Search in sources :

Example 1 with GetFile

use of edu.usf.cutr.gtfsrtvalidator.helper.GetFile in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsFeed method runStaticGTFSValidation.

private Response runStaticGTFSValidation(String saveFileName, String gtfsFeedUrl, GtfsFeedModel gtfsFeed) {
    FileSystemFeedBackend backend = new FileSystemFeedBackend();
    FeedValidationResultSet results = new FeedValidationResultSet();
    File input = backend.getFeed(saveFileName);
    FeedProcessor processor = new FeedProcessor(input);
    try {
        _log.info("Running static GTFS validation on " + gtfsFeedUrl + "...");
        processor.run();
    } catch (IOException ex) {
        Logger.getLogger(GtfsFeed.class.getName()).log(Level.SEVERE, null, ex);
        return generateError("Unable to access input GTFS " + input.getPath() + ".", "Does the file " + saveFileName + "exist and do I have permission to read it?", Response.Status.NOT_FOUND);
    }
    results.add(processor.getOutput());
    saveGtfsErrorCount(gtfsFeed, processor.getOutput());
    JsonSerializer serializer = new JsonSerializer(results);
    // get the location of the executed jar file
    GetFile jarInfo = new GetFile();
    String saveDir = jarInfo.getJarLocation().getParentFile().getAbsolutePath();
    saveFileName = saveDir + File.separator + jsonFilePath + File.separator + saveFileName + "_out.json";
    try {
        serializer.serializeToFile(new File(saveFileName));
        _log.info("Static GTFS validation data written to " + saveFileName);
    } catch (Exception e) {
        _log.error("Exception running static GTFS validation on " + gtfsFeedUrl + ": " + e.getMessage());
    }
    return Response.ok(gtfsFeed).build();
}
Also used : FileSystemFeedBackend(com.conveyal.gtfs.validator.json.backends.FileSystemFeedBackend) FeedProcessor(com.conveyal.gtfs.validator.json.FeedProcessor) GetFile(edu.usf.cutr.gtfsrtvalidator.helper.GetFile) FeedValidationResultSet(com.conveyal.gtfs.validator.json.FeedValidationResultSet) JsonSerializer(com.conveyal.gtfs.validator.json.serialization.JsonSerializer) GetFile(edu.usf.cutr.gtfsrtvalidator.helper.GetFile) MalformedURLException(java.net.MalformedURLException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with GetFile

use of edu.usf.cutr.gtfsrtvalidator.helper.GetFile 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)

Aggregations

GetFile (edu.usf.cutr.gtfsrtvalidator.helper.GetFile)2 FeedProcessor (com.conveyal.gtfs.validator.json.FeedProcessor)1 FeedValidationResultSet (com.conveyal.gtfs.validator.json.FeedValidationResultSet)1 FileSystemFeedBackend (com.conveyal.gtfs.validator.json.backends.FileSystemFeedBackend)1 JsonSerializer (com.conveyal.gtfs.validator.json.serialization.JsonSerializer)1 GtfsFeedModel (edu.usf.cutr.gtfsrtvalidator.lib.model.GtfsFeedModel)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 Response (javax.ws.rs.core.Response)1 Session (org.hibernate.Session)1 GtfsDaoImpl (org.onebusaway.gtfs.impl.GtfsDaoImpl)1