Example usage for org.json JSONObject optString

List of usage examples for org.json JSONObject optString

Introduction

In this page you can find the example usage for org.json JSONObject optString.

Prototype

public String optString(String key, String defaultValue) 

Source Link

Document

Get an optional string associated with a key.

Usage

From source file:ja.ohac.wallet.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> requestExchangeRates(final URL url, final String userAgent,
        final String source, final String... fields) {
    final long start = System.currentTimeMillis();

    HttpURLConnection connection = null;
    Reader reader = null;//ww  w . j a v a  2 s .  co  m

    try {
        connection = (HttpURLConnection) url.openConnection();

        connection.setInstanceFollowRedirects(false);
        connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.addRequestProperty("User-Agent", userAgent);
        connection.addRequestProperty("Accept-Encoding", "gzip");
        connection.connect();

        final int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            final String contentEncoding = connection.getContentEncoding();

            InputStream is = new BufferedInputStream(connection.getInputStream(), 1024);
            if ("gzip".equalsIgnoreCase(contentEncoding))
                is = new GZIPInputStream(is);

            reader = new InputStreamReader(is, Charsets.UTF_8);
            final StringBuilder content = new StringBuilder();
            final long length = Io.copy(reader, content);

            final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

            final JSONObject head = new JSONObject(content.toString());
            for (final Iterator<String> i = head.keys(); i.hasNext();) {
                final String currencyCode = i.next();
                if (!"timestamp".equals(currencyCode)) {
                    final JSONObject o = head.getJSONObject(currencyCode);

                    for (final String field : fields) {
                        final String rateStr = o.optString(field, null);

                        if (rateStr != null) {
                            try {
                                final BigInteger rate = GenericUtils.parseCoin(rateStr, 0);

                                if (rate.signum() > 0) {
                                    rates.put(currencyCode, new ExchangeRate(currencyCode, rate, source));
                                    break;
                                }
                            } catch (final ArithmeticException x) {
                                log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode,
                                        url, contentEncoding, x.getMessage());
                            }
                        }
                    }
                }
            }

            log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length,
                    System.currentTimeMillis() - start);

            return rates;
        } else {
            log.warn("http status {} when fetching exchange rates from {}", responseCode, url);
        }
    } catch (final Exception x) {
        log.warn("problem fetching exchange rates from " + url, x);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException x) {
                // swallow
            }
        }

        if (connection != null)
            connection.disconnect();
    }

    return null;
}

From source file:com.example.wcl.test_weiboshare.StatusList.java

public static StatusList parse(String jsonString) {
    if (TextUtils.isEmpty(jsonString)) {
        return null;
    }/*from   www  .j  ava  2s  .co m*/

    StatusList statuses = new StatusList();
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        statuses.hasvisible = jsonObject.optBoolean("hasvisible", false);
        statuses.previous_cursor = jsonObject.optString("previous_cursor", "0");
        statuses.next_cursor = jsonObject.optString("next_cursor", "0");
        statuses.total_number = jsonObject.optInt("total_number", 0);

        JSONArray jsonArray = jsonObject.optJSONArray("statuses");
        if (jsonArray != null && jsonArray.length() > 0) {
            int length = jsonArray.length();
            statuses.statusList = new ArrayList<Status>(length);
            for (int ix = 0; ix < length; ix++) {
                statuses.statusList.add(Status.parse(jsonArray.getJSONObject(ix)));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return statuses;
}

From source file:com.phelps.liteweibo.model.weibo.Tag.java

public static Tag parse(JSONObject jsonObject) {
    if (null == jsonObject) {
        return null;
    }//from  w  ww  .  j  av  a2s . c om

    Tag tag = new Tag();
    tag.id = jsonObject.optInt("id", 0);
    tag.tag = jsonObject.optString("tag", "");

    return tag;
}

From source file:com.external.androidquery.service.MarketService.java

protected void callback(String url, JSONObject jo, AjaxStatus status) {

    if (jo == null)
        return;/*from w w w  . jav  a 2 s .c  o  m*/

    String latestVer = jo.optString("version", "0");
    int latestCode = jo.optInt("code", 0);

    AQUtility.debug("version", getVersion() + "->" + latestVer + ":" + getVersionCode() + "->" + latestCode);
    AQUtility.debug("outdated", outdated(latestVer, latestCode));

    if (force || outdated(latestVer, latestCode)) {
        showUpdateDialog(jo);
    }

}

From source file:com.external.androidquery.service.MarketService.java

protected void showUpdateDialog(JSONObject jo) {

    if (jo == null || version != null)
        return;/* www. j  a  v  a 2 s .  c  om*/

    if (!isActive())
        return;

    JSONObject dia = jo.optJSONObject("dialog");

    String update = dia.optString("update", "Update");
    String skip = dia.optString("skip", "Skip");
    String rate = dia.optString("rate", "Rate");
    //String message = dia.optString("body", "");
    String body = dia.optString("wbody", "");
    String title = dia.optString("title", "Update Available");

    AQUtility.debug("wbody", body);

    version = jo.optString("version", null);

    Drawable icon = getAppIcon();

    Context context = act;

    final AlertDialog dialog = new AlertDialog.Builder(context).setIcon(icon).setTitle(title)
            .setPositiveButton(rate, handler).setNeutralButton(skip, handler).setNegativeButton(update, handler)
            .create();

    dialog.setMessage(Html.fromHtml(patchBody(body), null, handler));

    aq.show(dialog);

    return;

}

From source file:com.rapid.actions.Logic.java

@Override
public String getJavaScript(RapidRequest rapidRequest, Application application, Page page, Control control,
        JSONObject jsonDetails) throws Exception {

    String js = "";

    // assume we couldn't make a condition
    String conditionsJavaScript = "false";

    // check conditions is set
    if (_conditions != null) {
        // check we have some
        if (_conditions.size() > 0) {
            // reset conditionsJavaScript
            conditionsJavaScript = "";
            // loop them
            for (int i = 0; i < _conditions.size(); i++) {
                // add the condition
                conditionsJavaScript += _conditions.get(i)
                        .getJavaScript(rapidRequest.getRapidServlet().getServletContext(), application, page);
                // if there is going to be another condition
                if (i < _conditions.size() - 1) {
                    // add the separator
                    if ("or".equals(_conditionsType)) {
                        conditionsJavaScript += " || ";
                    } else {
                        conditionsJavaScript += " && ";
                    }/*from w  w w . j a v a 2s .  c  om*/
                }
            }
        }
    }

    // create the if statement   
    js += "if (" + conditionsJavaScript + ") {\n";

    // add any try actions
    if (_trueActions != null) {
        for (Action action : _trueActions)
            js += "  " + action.getJavaScript(rapidRequest, application, page, control, jsonDetails).trim()
                    .replace("\n", "\n  ") + "\n";
    }

    // close the if
    js += "}";

    // assume no false actions
    boolean gotFalseActions = false;
    // if there is a collection
    if (_falseActions != null) {
        // if there are some in the collection we have false actions
        if (_falseActions.size() > 0)
            gotFalseActions = true;
    }

    // check for false actions
    if (gotFalseActions) {
        // add any false actions as an else
        js += " else {\n";
        for (Action action : _falseActions)
            js += "  " + action.getJavaScript(rapidRequest, application, page, control, jsonDetails).trim()
                    .replace("\n", "\n  ") + "\n";
        js += "}";
    } else {
        // if we got some details
        if (jsonDetails != null) {
            // check the details for a defaultErrorHandler
            String defaultErrorHandler = jsonDetails.optString("defaultErrorHandler", null);
            // if we got one
            if (defaultErrorHandler != null) {
                // print it
                js += " else {\n  " + defaultErrorHandler + "\n}";
                // remove it from the jsonObject to stop it re-appearing elsewhere
                jsonDetails.remove("defaultErrorHandler");
            }
        }
    }

    // final line break
    js += "\n";

    // return what we built         
    return js;
}

From source file:org.eclipse.orion.server.cf.handlers.v1.AppsHandlerV1.java

@Override
protected CFJob handlePut(App resource, HttpServletRequest request, HttpServletResponse response,
        final String pathString) {
    final JSONObject targetJSON2 = extractJSONData(
            IOUtilities.getQueryParameter(request, CFProtocolConstants.KEY_TARGET));

    IPath path = pathString != null ? new Path(pathString) : new Path("");
    final String appGuid = path.segment(0);
    boolean addRoute = "routes".equals(path.segment(1));
    final String routeGuid = addRoute ? path.segment(2) : null;

    if (addRoute)
        return new CFJob(request, false) {
            @Override//from w ww  .  j av  a  2 s .  c o  m
            protected IStatus performJob() {
                try {
                    ComputeTargetCommand computeTarget = new ComputeTargetCommand(this.userId, targetJSON2);
                    IStatus status = computeTarget.doIt();
                    if (!status.isOK())
                        return status;
                    Target target = computeTarget.getTarget();

                    GetAppByGuidCommand getAppByGuid = new GetAppByGuidCommand(target.getCloud(), appGuid);
                    IStatus getAppByGuidStatus = getAppByGuid.doIt();
                    if (!getAppByGuidStatus.isOK())
                        return getAppByGuidStatus;
                    App app = getAppByGuid.getApp();

                    GetRouteByGuidCommand getRouteByGuid = new GetRouteByGuidCommand(target.getCloud(),
                            routeGuid);
                    IStatus getRouteByGuidStatus = getRouteByGuid.doIt();
                    if (!getRouteByGuidStatus.isOK())
                        return getRouteByGuidStatus;
                    Route route = getRouteByGuid.getRoute();

                    MapRouteCommand unmapRoute = new MapRouteCommand(target, app, route.getGuid());
                    return unmapRoute.doIt();
                } catch (Exception e) {
                    String msg = NLS.bind("Failed to handle request for {0}", pathString); //$NON-NLS-1$
                    ServerStatus status = new ServerStatus(IStatus.ERROR,
                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
                    logger.error(msg, e);
                    return status;
                }
            }
        };

    final JSONObject jsonData = extractJSONData(request);
    final JSONObject targetJSON = jsonData.optJSONObject(CFProtocolConstants.KEY_TARGET);

    final String state = jsonData.optString(CFProtocolConstants.KEY_STATE, null);
    final String appName = jsonData.optString(CFProtocolConstants.KEY_NAME, null);
    final String contentLocation = ServletResourceHandler.toOrionLocation(request,
            jsonData.optString(CFProtocolConstants.KEY_CONTENT_LOCATION, null));

    /* default application startup is one minute */
    int userTimeout = jsonData.optInt(CFProtocolConstants.KEY_TIMEOUT, 60);
    final int timeout = (userTimeout > 0) ? userTimeout : 0;

    /* TODO: The force shouldn't be always with us */
    final boolean force = jsonData.optBoolean(CFProtocolConstants.KEY_FORCE, true);

    return new CFJob(request, false) {
        @Override
        protected IStatus performJob() {
            try {
                ComputeTargetCommand computeTarget = new ComputeTargetCommand(this.userId, targetJSON);
                IStatus status = computeTarget.doIt();
                if (!status.isOK())
                    return status;
                Target target = computeTarget.getTarget();

                /* parse the application manifest */
                String manifestAppName = null;
                ParseManifestCommand parseManifestCommand = null;
                if (contentLocation != null) {
                    parseManifestCommand = new ParseManifestCommand(target, this.userId, contentLocation);
                    status = parseManifestCommand.doIt();
                    if (!status.isOK())
                        return status;

                    /* get the manifest name */
                    ManifestParseTree manifest = parseManifestCommand.getManifest();
                    if (manifest != null) {
                        ManifestParseTree applications = manifest.get(CFProtocolConstants.V2_KEY_APPLICATIONS);
                        if (applications.getChildren().size() > 0)
                            manifestAppName = applications.get(0).get(CFProtocolConstants.V2_KEY_NAME)
                                    .getValue();
                    }
                }

                GetAppCommand getAppCommand = new GetAppCommand(target,
                        appName != null ? appName : manifestAppName);
                status = getAppCommand.doIt();
                App app = getAppCommand.getApp();

                if (CFProtocolConstants.KEY_STARTED.equals(state)) {
                    if (!status.isOK())
                        return status;
                    return new StartAppCommand(target, app, timeout).doIt();
                } else if (CFProtocolConstants.KEY_STOPPED.equals(state)) {
                    if (!status.isOK())
                        return status;
                    return new StopAppCommand(target, app).doIt();
                } else {
                    if (parseManifestCommand == null) {
                        String msg = NLS.bind("Failed to handle request for {0}", pathString); //$NON-NLS-1$
                        status = new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                msg, null);
                        logger.error(msg);
                        return status;
                    }
                }

                // push new application
                if (app == null)
                    app = new App();

                app.setName(appName != null ? appName : manifestAppName);
                app.setManifest(parseManifestCommand.getManifest());

                status = new PushAppCommand(target, app, parseManifestCommand.getAppStore(), force).doIt();
                if (!status.isOK())
                    return status;

                // get the app again
                getAppCommand = new GetAppCommand(target, app.getName());
                getAppCommand.doIt();
                app = getAppCommand.getApp();
                app.setManifest(parseManifestCommand.getManifest());

                new StartAppCommand(target, app).doIt();

                return status;
            } catch (Exception e) {
                String msg = NLS.bind("Failed to handle request for {0}", pathString); //$NON-NLS-1$
                ServerStatus status = new ServerStatus(IStatus.ERROR,
                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
                logger.error(msg, e);
                return status;
            }
        }
    };
}

From source file:net.netheos.pcsapi.credentials.AppInfoFileRepository.java

public AppInfoFileRepository(File file) throws IOException {
    this.file = file;

    BufferedReader reader = null;
    try {/*from   w ww  . j  ava2s . c o m*/
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), PcsUtils.UTF8));

        String line;
        while ((line = reader.readLine()) != null) { // while loop begins here
            line = line.trim();

            if (line.startsWith("#") || line.length() == 0) {
                continue;
            }

            String[] appInfoArray = line.split("=", 2);

            // Key
            String[] appInfoKeyArray = appInfoArray[0].trim().split("\\.");
            String providerName = appInfoKeyArray[0];
            String appName = appInfoKeyArray[1];

            String appInfoValue = appInfoArray[1].trim();
            JSONObject jsonObj = (JSONObject) new JSONTokener(appInfoValue).nextValue();
            String appId = jsonObj.optString("appId", null);

            AppInfo appInfo;
            if (appId != null) {
                // OAuth
                JSONArray jsonScope = jsonObj.optJSONArray("scope");
                List<String> scopeList = new ArrayList<String>();

                for (int i = 0; i < jsonScope.length(); i++) {
                    scopeList.add(jsonScope.get(i).toString());
                }
                String appSecret = jsonObj.getString("appSecret");
                String redirectUrl = jsonObj.optString("redirectUrl", null);
                appInfo = new OAuth2AppInfo(providerName, appName, appId, appSecret, scopeList, redirectUrl);

            } else {
                // Login / Password
                appInfo = new PasswordAppInfo(providerName, appName);
            }

            appInfoMap.put(getAppKey(providerName, appName), appInfo);
        }

    } finally {
        PcsUtils.closeQuietly(reader);
    }
}

From source file:com.parking.billing.Security.java

/**
 * Verifies that the data was signed with the given signature, and returns
 * the list of verified purchases. The data is in JSON format and contains
 * a nonce (number used once) that we generated and that was signed
 * (as part of the whole data string) with a private key. The data also
 * contains the {@link PurchaseState} and product ID of the purchase.
 * In the general case, there can be an array of purchase transactions
 * because there may be delays in processing the purchase on the backend
 * and then several purchases can be batched together.
 * @param signedData the signed JSON string (signed, not encrypted)
 * @param signature the signature for the data, signed with the private key
 *//* w w w.jav a 2s .c o  m*/
public static ArrayList<VerifiedPurchase> verifyPurchase(String signedData, String signature) {
    if (signedData == null) {
        Log.e(TAG, "data is null");
        return null;
    }
    if (BillingConstants.DEBUG) {
        Log.i(TAG, "signedData: " + signedData);
    }

    boolean verified = false;

    if (!TextUtils.isEmpty(signature)) {
        /**
         * Compute your public key (that you got from the Android Market publisher site).
         *
         * Instead of just storing the entire literal string here embedded in the
         * program,  construct the key at runtime from pieces or
         * use bit manipulation (for example, XOR with some other string) to hide
         * the actual key.  The key itself is not secret information, but we don't
         * want to make it easy for an adversary to replace the public key with one
         * of their own and then fake messages from the server.
         *
         * Generally, encryption keys / passwords should only be kept in memory
         * long enough to perform the operation they need to perform.
         */
        //Chintan's Key
        String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuRIrk/6nAPzZo5HKe261/ZMfoe63mtY5QUlc0A0/77RTicrS9Nk1VjtVniRpHmjasQOGsQrBpBGJUYp0ixsjJpgfjLv7OvpF8Hp/ucth2T/Bm7kl/odRDT3urAp3snvqZEzfOg1wtDU7DAnDW1zNSqVNCVczXRnNrEmGxEjamKkTTQwz37ui7AhjKXCXAJY4n5ANj1oymnjGN5FHfzcMb07wR/ucz39ZX+Raf6qBsbnYkmuDH6pJ/4ZI9+vjbgWzXCx07DefQW4dtNMQZlVlKgKnJUkafePUYJVIO4sRgeWL1e5b8dbIYMO7gB9oopyfVhZifi+pDGr5+YAxi6D3PwIDAQAB";
        //Mandar's Key:
        //String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj7zgTswMi1ePyAK7rCnCOkpmviHZzoSn2cxtyQ5ZQRFifNGkKq3Gli3VbeIeeJR8GHzlfOPUSgtMrd17WtGJoo29rsw6UuXov+imQutGKZglcM/cIrlIdZCsse3dGYyDcKFhEHrC/nPdwlYxgIGBaZKAcbbhitkdgYaVHQvGFTagCytxDq9NDJAY7exSKKm2HimfjlcBZjhHeImZ+cRCPux+9uoBQ4mTRYjrXfcpi/OPKTKsq2AHXf/y60qsZJlgGl3tBgRQo6lOEr7UbbHKESTKvOQ4t3J1wjNz8Z3+T0PZHb5JkeTsdAE7cG7jmz2HmMxfdXcT5mBTTDei6DPDGwIDAQAB";
        PublicKey key = Security.generatePublicKey(base64EncodedPublicKey);
        verified = Security.verify(key, signedData, signature);
        if (!verified) {
            Log.w(TAG, "signature does not match data.");
            return null;
        }
    }

    JSONObject jObject;
    JSONArray jTransactionsArray = null;
    int numTransactions = 0;
    long nonce = 0L;
    try {
        jObject = new JSONObject(signedData);

        // The nonce might be null if the user backed out of the buy page.
        nonce = jObject.optLong("nonce");
        jTransactionsArray = jObject.optJSONArray("orders");
        if (jTransactionsArray != null) {
            numTransactions = jTransactionsArray.length();
        }
    } catch (JSONException e) {
        return null;
    }

    if (!Security.isNonceKnown(nonce)) {
        Log.w(TAG, "Nonce not found: " + nonce);
        return null;
    }

    ArrayList<VerifiedPurchase> purchases = new ArrayList<VerifiedPurchase>();
    try {
        for (int i = 0; i < numTransactions; i++) {
            JSONObject jElement = jTransactionsArray.getJSONObject(i);
            int response = jElement.getInt("purchaseState");
            PurchaseState purchaseState = PurchaseState.valueOf(response);
            String productId = jElement.getString("productId");
            String packageName = jElement.getString("packageName");
            long purchaseTime = jElement.getLong("purchaseTime");
            String orderId = jElement.optString("orderId", "");
            String notifyId = null;
            if (jElement.has("notificationId")) {
                notifyId = jElement.getString("notificationId");
            }
            String developerPayload = jElement.optString("developerPayload", null);

            // If the purchase state is PURCHASED, then we require a
            // verified nonce.

            /**
             *   mandarm - We are ok with no signature for our test code! 
             */
            //TODO - Take care for signed purchases.

            if (purchaseState == PurchaseState.PURCHASED && !verified) {
                continue;
            }

            purchases.add(new VerifiedPurchase(purchaseState, notifyId, productId, orderId, purchaseTime,
                    developerPayload));
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSON exception: ", e);
        return null;
    }
    removeNonce(nonce);
    return purchases;
}

From source file:org.schedulesdirect.grabber.Grabber.java

private void updateZip(NetworkEpgClient clnt) throws IOException, JSONException, JsonParseException {
    Set<String> completedListings = new HashSet<String>();
    LOG.debug(String.format("Using %d worker threads", globalOpts.getMaxThreads()));
    pool = createThreadPoolExecutor();//  w  w  w .ja v  a2  s . co m
    start = System.currentTimeMillis();
    File dest = grabOpts.getTarget();
    cachedSeriesIds = new HashSet<String>();
    boolean rmDest = false;
    if (dest.exists()) {
        ZipEpgClient zipClnt = null;
        try {
            zipClnt = new ZipEpgClient(dest);
            if (!zipClnt.getUserStatus().getLastServerRefresh()
                    .before(clnt.getUserStatus().getLastServerRefresh())) {
                LOG.info(
                        "Current cache file contains latest data from Schedules Direct server; use --force-download to force a new download from server.");
                boolean force = grabOpts.isForce();
                if (!force)
                    return;
                else
                    LOG.warn("Forcing an update of data with the server due to user request!");
            }
        } catch (Exception e) {
            if (grabOpts.isKeep()) {
                LOG.error("Existing cache is invalid, keeping by user request!", e);
                return;
            } else {
                LOG.warn("Existing cache is invalid, deleting it; use --keep-bad-cache to keep existing cache!",
                        e);
                rmDest = true;
            }
        } finally {
            if (zipClnt != null)
                try {
                    zipClnt.close();
                } catch (IOException e) {
                }
            if (rmDest && !dest.delete())
                throw new IOException("Unable to delete " + dest);
        }
    }

    freshZip = !dest.exists();
    try (FileSystem vfs = FileSystems.newFileSystem(new URI(String.format("jar:%s", dest.toURI())),
            Collections.singletonMap("create", "true"))) {
        if (freshZip) {
            Path target = vfs.getPath(ZipEpgClient.ZIP_VER_FILE);
            Files.write(target, Integer.toString(ZipEpgClient.ZIP_VER).getBytes(ZipEpgClient.ZIP_CHARSET));
        }
        ProgramCache progCache = ProgramCache.get(vfs);
        Path lineups = vfs.getPath("lineups.txt");
        Files.deleteIfExists(lineups);
        Path scheds = vfs.getPath("/schedules/");
        if (!Files.isDirectory(scheds))
            Files.createDirectory(scheds);
        Path maps = vfs.getPath("/maps/");
        PathUtils.removeDirectory(maps);
        Files.createDirectory(maps);
        Path progs = vfs.getPath("/programs/");
        if (!Files.isDirectory(progs))
            Files.createDirectory(progs);
        Path logos = vfs.getPath("/logos/");
        if (!Files.isDirectory(logos))
            Files.createDirectory(logos);
        Path md5s = vfs.getPath("/md5s/");
        if (!Files.isDirectory(md5s))
            Files.createDirectory(md5s);
        Path cache = vfs.getPath(LOGO_CACHE);
        if (Files.exists(cache)) {
            String cacheData = new String(Files.readAllBytes(cache), ZipEpgClient.ZIP_CHARSET);
            logoCache = Config.get().getObjectMapper().readValue(cacheData, JSONObject.class);
        } else
            logoCache = new JSONObject();
        Path seriesInfo = vfs.getPath("/seriesInfo/");
        if (!Files.isDirectory(seriesInfo))
            Files.createDirectories(seriesInfo);
        loadSeriesInfoIds(seriesInfo);
        missingSeriesIds = Collections.synchronizedSet(new HashSet<String>());
        loadRetryIds(vfs.getPath(SERIES_INFO_DATA));

        JSONObject resp = Config.get().getObjectMapper().readValue(
                factory.get(DefaultJsonRequest.Action.GET, RestNouns.LINEUPS, clnt.getHash(),
                        clnt.getUserAgent(), globalOpts.getUrl().toString()).submitForJson(null),
                JSONObject.class);
        if (!JsonResponseUtils.isErrorResponse(resp))
            Files.write(lineups, resp.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET));
        else
            LOG.error("Received error response when requesting lineup data!");

        for (Lineup l : clnt.getLineups()) {
            buildStationList();
            JSONObject o = Config.get().getObjectMapper()
                    .readValue(
                            factory.get(DefaultJsonRequest.Action.GET, l.getUri(), clnt.getHash(),
                                    clnt.getUserAgent(), globalOpts.getUrl().toString()).submitForJson(null),
                            JSONObject.class);
            Files.write(vfs.getPath("/maps", ZipEpgClient.scrubFileName(String.format("%s.txt", l.getId()))),
                    o.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET));
            JSONArray stations = o.getJSONArray("stations");
            JSONArray ids = new JSONArray();
            for (int i = 0; i < stations.length(); ++i) {
                JSONObject obj = stations.getJSONObject(i);
                String sid = obj.getString("stationID");
                if (stationList != null && !stationList.contains(sid))
                    LOG.debug(String.format("Skipped %s; not listed in station file", sid));
                else if (completedListings.add(sid)) {
                    ids.put(sid);
                    if (!grabOpts.isNoLogos()) {
                        if (logoCacheInvalid(obj))
                            pool.execute(new LogoTask(obj, vfs, logoCache));
                        else if (LOG.isDebugEnabled())
                            LOG.debug(String.format("Skipped logo for %s; already cached!",
                                    obj.optString("callsign", null)));
                    } else if (!logosWarned) {
                        logosWarned = true;
                        LOG.warn("Logo downloads disabled by user request!");
                    }
                } else
                    LOG.debug(String.format("Skipped %s; already downloaded.", sid));
                //pool.setMaximumPoolSize(5); // Processing these new schedules takes all kinds of memory!
                if (ids.length() == grabOpts.getMaxSchedChunk()) {
                    pool.execute(new ScheduleTask(ids, vfs, clnt, progCache, factory));
                    ids = new JSONArray();
                }
            }
            if (ids.length() > 0)
                pool.execute(new ScheduleTask(ids, vfs, clnt, progCache, factory));
        }
        pool.shutdown();
        try {
            LOG.debug("Waiting for SchedLogoExecutor to terminate...");
            if (pool.awaitTermination(15, TimeUnit.MINUTES))
                LOG.debug("SchedLogoExecutor: Terminated successfully.");
            else {
                failedTask = true;
                LOG.warn(
                        "SchedLogoExecutor: Termination timed out; some tasks probably didn't finish properly!");
            }
        } catch (InterruptedException e) {
            failedTask = true;
            LOG.warn(
                    "SchedLogoExecutor: Termination interrupted); some tasks probably didn't finish properly!");
        }
        Files.write(cache, logoCache.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET),
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        ScheduleTask.commit(vfs);

        pool = createThreadPoolExecutor();
        //pool.setMaximumPoolSize(5); // Again, we've got memory problems
        String[] dirtyPrograms = progCache.getDirtyIds();
        progCache.markAllClean();
        progCache = null;
        LOG.info(String.format("Identified %d program ids requiring an update!", dirtyPrograms.length));
        Collection<String> progIds = new ArrayList<String>();
        for (String progId : dirtyPrograms) {
            progIds.add(progId);
            if (progIds.size() == grabOpts.getMaxProgChunk()) {
                pool.execute(new ProgramTask(progIds, vfs, clnt, factory, missingSeriesIds, "programs", null,
                        false));
                progIds.clear();
            }
        }
        if (progIds.size() > 0)
            pool.execute(
                    new ProgramTask(progIds, vfs, clnt, factory, missingSeriesIds, "programs", null, false));
        pool.shutdown();
        try {
            LOG.debug("Waiting for ProgramExecutor to terminate...");
            if (pool.awaitTermination(15, TimeUnit.MINUTES)) {
                LOG.debug("ProgramExecutor: Terminated successfully.");
                Iterator<String> itr = missingSeriesIds.iterator();
                while (itr.hasNext()) {
                    String id = itr.next();
                    if (cachedSeriesIds.contains(id))
                        itr.remove();
                }
                if (missingSeriesIds.size() > 0) {
                    LOG.info(String.format("Grabbing %d series info programs!", missingSeriesIds.size()));
                    Set<String> retrySet = new HashSet<>();
                    try {
                        new ProgramTask(missingSeriesIds, vfs, clnt, factory, missingSeriesIds, "seriesInfo",
                                retrySet, true).run();
                    } catch (RuntimeException e) {
                        LOG.error("SeriesInfo task failed!", e);
                        Grabber.failedTask = true;
                    }
                    Path seriesInfoData = vfs.getPath(SERIES_INFO_DATA);
                    if (retrySet.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        for (String id : retrySet)
                            sb.append(id + "\n");
                        Files.write(seriesInfoData, sb.toString().getBytes(ZipEpgClient.ZIP_CHARSET),
                                StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING,
                                StandardOpenOption.CREATE);
                    } else if (Files.exists(seriesInfoData))
                        Files.delete(seriesInfoData);
                }
            } else {
                failedTask = true;
                LOG.warn("ProgramExecutor: Termination timed out; some tasks probably didn't finish properly!");
            }
        } catch (InterruptedException e) {
            failedTask = true;
            LOG.warn("ProgramExecutor: Termination interrupted); some tasks probably didn't finish properly!");
        }

        String userData = clnt.getUserStatus().toJson();
        if (failedTask) {
            LOG.error("One or more tasks failed!  Resetting last data refresh timestamp to zero.");
            SimpleDateFormat fmt = Config.get().getDateTimeFormat();
            String exp = fmt.format(new Date(0L));
            JSONObject o = Config.get().getObjectMapper().readValue(userData, JSONObject.class);
            o.put("lastDataUpdate", exp);
            userData = o.toString(2);
        }
        Path p = vfs.getPath(USER_DATA);
        Files.write(p, userData.getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.WRITE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
        removeIgnoredStations(vfs);
    } catch (URISyntaxException e1) {
        throw new RuntimeException(e1);
    } finally {
        Runtime rt = Runtime.getRuntime();
        LOG.info(String.format("MemStats:%n\tFREE: %s%n\tUSED: %s%n\t MAX: %s",
                FileUtils.byteCountToDisplaySize(rt.freeMemory()),
                FileUtils.byteCountToDisplaySize(rt.totalMemory()),
                FileUtils.byteCountToDisplaySize(rt.maxMemory())));
    }
}