public LspFetchResult fetch()

in plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/manager/fetcher/RemoteLspFetcher.java [83:148]


    public LspFetchResult fetch(final PluginPlatform platform, final PluginArchitecture architecture,
            final Path destination, final Instant start) {
        var artifactVersion = resolveVersion(manifest, platform, architecture, start);
        var target = resolveTarget(artifactVersion, platform, architecture);
        if (!target.isPresent()) {
            String failureReason = String.format(
                    "Unable to find a language server that satisfies one or more of these conditions:"
                    + " version in range [%s), matching system's architecture: %s and platform: %s",
                    versionRange.toString(), architecture, platform);
            setErrorReason(LspError.NO_COMPATIBLE_LSP.toString());
            emitGetServer(Result.FAILED, null, LanguageServerLocation.UNKNOWN, start);
            throw new AmazonQPluginException(failureReason);
        }

        var serverVersion = artifactVersion.get().serverVersion();
        var contents = target.get().contents();
        var downloadDirectory = Paths.get(destination.toString(), serverVersion.toString());

        // if the latest version is stored locally already and is valid, return that, else fetch the latest version
        if (hasValidCache(contents, downloadDirectory)) {
            logMessageWithLicense(String.format("Launching Amazon Q language server v%s from local cache %s",
                    serverVersion.toString(), downloadDirectory), artifactVersion.get().thirdPartyLicenses());
            emitGetServer(Result.SUCCEEDED, serverVersion, LanguageServerLocation.CACHE, start);
            return new LspFetchResult(downloadDirectory.toString(), serverVersion, LanguageServerLocation.CACHE);
        }

        // delete invalid local cache
        ArtifactUtils.deleteDirectory(downloadDirectory);

        // if all lsp target contents are successfully downloaded from remote location,
        // return the download location
        if (downloadFromRemote(contents, downloadDirectory)) {
            logMessageWithLicense(String.format("Installing Amazon Q language server v%s to %s",
                    serverVersion.toString(), downloadDirectory.toString()),
                    artifactVersion.get().thirdPartyLicenses());
            emitGetServer(Result.SUCCEEDED, serverVersion, LanguageServerLocation.REMOTE, start);
            return new LspFetchResult(downloadDirectory.toString(), serverVersion, LanguageServerLocation.REMOTE);
        }

        // if unable to retrieve / validate contents from remote location, cleanup
        // download cache
        ArtifactUtils.deleteDirectory(downloadDirectory);
        Activator.getLogger().info(String.format(
                "Unable to download Amazon Q language server version v%s. Attempting to fetch from fallback location",
                serverVersion));
        emitGetServer(Result.FAILED, serverVersion, LanguageServerLocation.REMOTE, start);

        // use the most compatible fallback cached lsp version
        var fallbackDir = getFallback(serverVersion, platform, architecture, destination);
        if (fallbackDir != null && !fallbackDir.toString().isEmpty()) {
            var fallbackVersion = fallbackDir.getFileName().toString();
            var fallBackLspVersion = manifest.versions().stream().filter(x -> x.serverVersion().equals(fallbackVersion))
                    .findFirst();

            logMessageWithLicense(String.format(
                    "Unable to install Amazon Q Language Server v%s. Launching a previous version from: %s",
                    serverVersion, fallbackDir.toString()), fallBackLspVersion.get().thirdPartyLicenses());
            emitGetServer(Result.SUCCEEDED, fallbackVersion, LanguageServerLocation.FALLBACK, start);
            return new LspFetchResult(fallbackDir.toString(), fallbackVersion, LanguageServerLocation.FALLBACK);
        }

        String failureReason = "Unable to find a compatible version of Amazon Q Language Server.";
        setErrorReason(LspError.NO_VALID_SERVER_FALLBACK.toString());
        emitGetServer(Result.FAILED, null, LanguageServerLocation.UNKNOWN, start);
        throw new AmazonQPluginException(failureReason);
    }