Search in sources :

Example 1 with JSONException

use of com.oracle.truffle.tools.utils.json.JSONException in project graal by oracle.

the class GraalChannel method loadReleasesIndex.

/**
 * Loads the release index. Must be loaded from a local file.
 *
 * @param releasesIndexPath path to the downloaded releases index.
 * @return list of entries in the index
 * @throws IOException in case of I/O error.
 */
List<ReleaseEntry> loadReleasesIndex(Path releasesIndexPath) throws IOException {
    if (edition == null) {
        edition = localRegistry.getGraalCapabilities().get(CommonConstants.CAP_EDITION);
    }
    List<ReleaseEntry> result = new ArrayList<>();
    try (Reader urlReader = new InputStreamReader(Files.newInputStream(releasesIndexPath))) {
        JSONTokener tokener = new JSONTokener(urlReader);
        JSONObject obj = new JSONObject(tokener);
        JSONObject releases = obj.getJSONObject(KEY_RELEASES);
        if (releases == null) {
            // malformed releases file;
            throw new IncompatibleException(fb.l10n("OLDS_InvalidReleasesFile"));
        }
        Version v = localRegistry.getGraalVersion();
        for (String k : releases.keySet()) {
            JSONObject jo = releases.getJSONObject(k);
            ReleaseEntry e = null;
            try {
                e = jsonToRelease(k, jo);
            } catch (JSONException | IllegalArgumentException ex) {
                fb.error("OLDS_ErrorReadingRelease", ex, k, ex.getLocalizedMessage());
            }
            if (e == null) {
                invalidReleases.add(k);
            } else if (!localRegistry.getJavaVersion().equals(e.getJavaVersion())) {
                LOG.log(Level.FINER, "Invalid Java: {0}", k);
            } else if (e.getBasePackages().isEmpty()) {
                LOG.log(Level.FINER, "No distribution packages: {0}", k);
            } else if (edition != null && !edition.equals(e.getEdition())) {
                LOG.log(Level.FINER, "Incorrect edition: {0}", k);
            } else if (!acceptsVersion(v, e.getVersion())) {
                LOG.log(Level.FINER, "Old version: {0}", k);
            } else {
                result.add(e);
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JSONException(com.oracle.truffle.tools.utils.json.JSONException) JSONTokener(com.oracle.truffle.tools.utils.json.JSONTokener) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Version(org.graalvm.component.installer.Version) IncompatibleException(org.graalvm.component.installer.IncompatibleException)

Example 2 with JSONException

use of com.oracle.truffle.tools.utils.json.JSONException in project graal by oracle.

the class GDSChannel method loadArtifacts.

/**
 * Loads the release index. Must be loaded from a local file.
 *
 * @param releasesIndexPath path to the downloaded releases index.
 * @return list of entries in the index
 * @throws IOException in case of I/O error.
 */
List<ComponentInfo> loadArtifacts(Path releasesIndexPath) throws IOException {
    if (edition == null) {
        edition = localRegistry.getGraalCapabilities().get(CommonConstants.CAP_EDITION);
    }
    List<ComponentInfo> result = new ArrayList<>();
    try (InputStreamReader urlReader = new InputStreamReader(new GZIPInputStream(Files.newInputStream(releasesIndexPath)))) {
        JSONTokener tokener = new JSONTokener(urlReader);
        JSONObject obj = new JSONObject(tokener);
        JSONArray releases = obj.getJSONArray(JSON_ITEMS);
        if (releases == null) {
            // malformed releases file;
            throw new IncompatibleException(fb.l10n("OLDS_InvalidReleasesFile"));
        }
        Version v = localRegistry.getGraalVersion();
        for (Object k : releases) {
            JSONObject jo = (JSONObject) k;
            ArtifactParser e;
            try {
                e = new ArtifactParser(jo);
            } catch (JSONException | IllegalArgumentException ex) {
                fb.error("OLDS_ErrorReadingRelease", ex, k, ex.getLocalizedMessage());
                continue;
            }
            if (!OS.get().equals(OS.fromName(e.getOs()))) {
                LOG.log(Level.FINER, "Incorrect OS: {0}", k);
            } else if (!ARCH.get().equals(ARCH.fromName(e.getArch()))) {
                LOG.log(Level.FINER, "Incorrect Arch: {0}", k);
            } else if (!localRegistry.getJavaVersion().equals(e.getJava())) {
                LOG.log(Level.FINER, "Incorrect Java: {0}", k);
            } else if (edition != null && !edition.equals(e.getEdition())) {
                LOG.log(Level.FINER, "Incorrect edition: {0}", k);
            } else if (!acceptsVersion(v, Version.fromString(e.getVersion()))) {
                LOG.log(Level.FINER, "Old version: {0} != {1}", new Object[] { v, Version.fromString(e.getVersion()), e.getVersion() });
            } else {
                result.add(e.asComponentInfo(gdsConnector, fb));
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) JSONException(com.oracle.truffle.tools.utils.json.JSONException) GZIPInputStream(java.util.zip.GZIPInputStream) JSONTokener(com.oracle.truffle.tools.utils.json.JSONTokener) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Version(org.graalvm.component.installer.Version) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) ComponentInfo(org.graalvm.component.installer.model.ComponentInfo) IncompatibleException(org.graalvm.component.installer.IncompatibleException)

Example 3 with JSONException

use of com.oracle.truffle.tools.utils.json.JSONException in project graal by oracle.

the class GDSChannel method interceptDownloadException.

public IOException interceptDownloadException(IOException downloadException, FileDownloader fileDownloader) {
    if (downloadException instanceof HttpConnectionException) {
        HttpConnectionException hex = (HttpConnectionException) downloadException;
        if (hex.getRetCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            return (IOException) hex.getCause();
        }
        String downloadURL = hex.getConnectionUrl().toString();
        if (!downloadURL.endsWith(EXC_URL_END)) {
            return (IOException) hex.getCause();
        }
        String licensePath = findLicensePath(downloadURL);
        if (!nonBlankString(licensePath)) {
            return (IOException) hex.getCause();
        }
        String code;
        try {
            code = new JSONObject(hex.getResponse()).getString(JSON_EXC_CODE);
        } catch (JSONException ex) {
            return (IOException) hex.getCause();
        }
        String token = getToken(licensePath);
        switch(code) {
            case EXC_CODE_INVALID_TOKEN:
                fb.output("ERR_InvalidToken", hex, token);
                token = getToken(licensePath, true);
                break;
            case EXC_CODE_UNACCEPTED:
                Map.Entry<String, String> downloadToken = initTokenStorage().getToken();
                if (fileDownloader.getAttemptNr() == 1) {
                    token = gdsConnector.sendVerificationEmail(downloadToken.getKey(), licensePath, downloadToken.getValue());
                } else {
                    token = downloadToken.getValue();
                }
                if (nonBlankString(token)) {
                    fb.output("PROMPT_VerifyEmailAddressEntry", downloadToken.getKey());
                    fb.acceptLine(false);
                }
                break;
            default:
                return (IOException) hex.getCause();
        }
        fileDownloader.addRequestHeader(HEADER_DOWNLOAD_TOKEN, token);
    } else {
        return downloadException;
    }
    return null;
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONException(com.oracle.truffle.tools.utils.json.JSONException) IOException(java.io.IOException) Map(java.util.Map) HttpConnectionException(org.graalvm.component.installer.remote.ProxyConnectionFactory.HttpConnectionException)

Example 4 with JSONException

use of com.oracle.truffle.tools.utils.json.JSONException in project graal by oracle.

the class InspectServerSession method sendText.

@Override
public void sendText(String message) throws IOException {
    if (processThread == null) {
        throw createClosedException();
    }
    Command cmd;
    try {
        cmd = new Command(message);
    } catch (JSONException ex) {
        PrintWriter err = context.getErr();
        if (err != null) {
            err.println("Illegal message: '" + message + "' " + ex.getLocalizedMessage());
        }
        return;
    }
    CommandProcessThread pt = processThread;
    if (pt != null) {
        pt.push(cmd);
    }
}
Also used : Command(com.oracle.truffle.tools.chromeinspector.commands.Command) JSONException(com.oracle.truffle.tools.utils.json.JSONException) PrintWriter(java.io.PrintWriter)

Example 5 with JSONException

use of com.oracle.truffle.tools.utils.json.JSONException in project graal by oracle.

the class GraalChannel method jsonToBase.

/**
 * Deserializes JSON object to {@link ReleaseEntry.BasePackage}.
 *
 * @param s package id
 * @param jo json input
 * @return Package object or {@code null} if invalid arch/os
 */
ReleaseEntry.BasePackage jsonToBase(String s, JSONObject jo) {
    String os;
    String arch;
    String urlString;
    try {
        os = jo.getString(KEY_BASE_OS);
        arch = jo.getString(KEY_BASE_ARCH);
        urlString = jo.getString(KEY_BASE_URL);
    } catch (JSONException ex) {
        invalidOsArchEntries.add(s);
        return null;
    }
    try {
        URL u = resolveURL(urlString);
        return new ReleaseEntry.BasePackage(SystemUtils.normalizeOSName(os, arch), SystemUtils.normalizeArchitecture(os, arch), u);
    } catch (MalformedURLException ex) {
        invalidURLs.add(urlString);
        return null;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONException(com.oracle.truffle.tools.utils.json.JSONException) URL(java.net.URL)

Aggregations

JSONException (com.oracle.truffle.tools.utils.json.JSONException)6 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)4 JSONTokener (com.oracle.truffle.tools.utils.json.JSONTokener)3 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 IncompatibleException (org.graalvm.component.installer.IncompatibleException)2 Version (org.graalvm.component.installer.Version)2 Command (com.oracle.truffle.tools.chromeinspector.commands.Command)1 JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ComponentInfo (org.graalvm.component.installer.model.ComponentInfo)1 HttpConnectionException (org.graalvm.component.installer.remote.ProxyConnectionFactory.HttpConnectionException)1