Search in sources :

Example 1 with BundleFileItem

use of org.onebusaway.transit_data_federation.model.bundle.BundleFileItem in project onebusaway-application-modules by camsys.

the class HttpBundleStoreImpl method getBundles.

@Override
public List<BundleItem> getBundles() throws Exception {
    List<BundleItem> output = new ArrayList<BundleItem>();
    List<BundleItem> bundlesFromHttp = getBundleListFromHttp();
    for (BundleItem bundle : bundlesFromHttp) {
        boolean bundleIsValid = true;
        // ensure bundle path exists locally
        File bundleRoot = new File(_bundleRootPath, bundle.getName());
        if (!bundleRoot.exists()) {
            if (!bundleRoot.mkdirs()) {
                throw new Exception("Creation of bundle root for " + bundle.getName() + " at " + bundleRoot + " failed.");
            }
        }
        for (BundleFileItem file : bundle.getFiles()) {
            File fileInBundlePath = new File(bundleRoot, file.getFilename());
            // see if file already exists locally and matches its MD5
            if (fileInBundlePath.exists()) {
                if (!file.verifyMd5(fileInBundlePath)) {
                    _log.warn("File " + fileInBundlePath + " is corrupted; removing.");
                    if (!fileInBundlePath.delete()) {
                        _log.error("Could not remove corrupted file " + fileInBundlePath);
                        bundleIsValid = false;
                        break;
                    }
                }
            }
            // if the file is not there, or was removed above, try to download it again from the TDM.
            if (!fileInBundlePath.exists()) {
                int tries = _fileDownloadRetries;
                while (tries > 0) {
                    URL fileDownloadUrl = _apiLibrary.buildUrl("bundle", "deploy", bundle.getName(), "file", file.getFilename(), "get");
                    try {
                        downloadUrlToLocalPath(fileDownloadUrl, fileInBundlePath, file.getMd5());
                    } catch (Exception e) {
                        tries--;
                        if (tries == 0) {
                            bundleIsValid = false;
                        }
                        _log.warn("Download of " + fileDownloadUrl + " failed (" + e.getMessage() + ");" + " retrying (retries left=" + tries + ")");
                        continue;
                    }
                    // file was downloaded successfully--break out of retry loop
                    break;
                }
            }
        }
        if (bundleIsValid) {
            _log.info("Bundle " + bundle.getName() + " files pass checksums; added to list of local bundles.");
            output.add(bundle);
        } else {
            _log.warn("Bundle " + bundle.getName() + " files do NOT pass checksums; skipped.");
        }
    }
    return output;
}
Also used : BundleFileItem(org.onebusaway.transit_data_federation.model.bundle.BundleFileItem) BundleItem(org.onebusaway.transit_data_federation.model.bundle.BundleItem) ArrayList(java.util.ArrayList) File(java.io.File) IOException(java.io.IOException) ParseException(java.text.ParseException) URL(java.net.URL)

Example 2 with BundleFileItem

use of org.onebusaway.transit_data_federation.model.bundle.BundleFileItem in project onebusaway-application-modules by camsys.

the class HttpBundleStoreImpl method getBundleListFromHttp.

private List<BundleItem> getBundleListFromHttp() {
    ArrayList<BundleItem> output = new ArrayList<BundleItem>();
    _log.info("Getting current bundle list from Server...");
    List<JsonObject> bundles = null;
    try {
        bundles = _apiLibrary.getItemsForRequest("bundle", "list");
    } catch (Exception e) {
        _log.info("Error executing apiLibrary.getItemsForRequest", e);
    }
    if (bundles != null) {
        for (JsonObject itemToAdd : bundles) {
            try {
                BundleItem item = new BundleItem();
                if (itemToAdd.get("id") == null)
                    item.setId(getJsonObjAsString(itemToAdd, "name"));
                else
                    item.setId(getJsonObjAsString(itemToAdd, "id"));
                item.setName(getJsonObjAsString(itemToAdd, "name"));
                item.setServiceDateFrom(ServiceDate.parseString(getJsonObjAsString(itemToAdd, "service-date-from").replace("-", "")));
                item.setServiceDateTo(ServiceDate.parseString(getJsonObjAsString(itemToAdd, "service-date-to").replace("-", "")));
                if (itemToAdd.get("created") != null)
                    item.setCreated(_updatedDateFormatter.parseDateTime(getJsonObjAsString(itemToAdd, "created")));
                if (itemToAdd.get("updated") != null)
                    item.setUpdated(_updatedDateFormatter.parseDateTime(getJsonObjAsString(itemToAdd, "updated")));
                ArrayList<BundleFileItem> files = new ArrayList<BundleFileItem>();
                JsonElement filesElement = itemToAdd.get("files");
                if (filesElement != null && filesElement.isJsonArray()) {
                    for (JsonElement _subitemToAdd : filesElement.getAsJsonArray()) {
                        if (_subitemToAdd.isJsonObject()) {
                            JsonObject subitemToAdd = _subitemToAdd.getAsJsonObject();
                            BundleFileItem fileItemToAdd = new BundleFileItem();
                            fileItemToAdd.setFilename(getJsonObjAsString(subitemToAdd, "filename"));
                            fileItemToAdd.setMd5(getJsonObjAsString(subitemToAdd, "md5"));
                            files.add(fileItemToAdd);
                        } else {
                            _log.warn("Unable to retreive file name/md5 as Json Object");
                        }
                    }
                    item.setFiles(files);
                    output.add(item);
                } else {
                    _log.warn("Unable to get list of files for Bundle " + item.getName());
                }
            } catch (NullPointerException npe) {
                _log.warn("Error retrieving bundle information");
                continue;
            } catch (ParseException e) {
                _log.warn("Error parsing dates for Bundle Item");
                continue;
            }
        }
        _log.info("Found " + output.size() + " bundle(s) available from the Server.");
    }
    return output;
}
Also used : BundleFileItem(org.onebusaway.transit_data_federation.model.bundle.BundleFileItem) BundleItem(org.onebusaway.transit_data_federation.model.bundle.BundleItem) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ParseException(java.text.ParseException) IOException(java.io.IOException) ParseException(java.text.ParseException)

Aggregations

IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 BundleFileItem (org.onebusaway.transit_data_federation.model.bundle.BundleFileItem)2 BundleItem (org.onebusaway.transit_data_federation.model.bundle.BundleItem)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 URL (java.net.URL)1