Search in sources :

Example 16 with WithDumpException

use of com.github.mob41.osumer.debug.WithDumpException in project osumer by mob41.

the class Installer method install.

public void install() throws WithDumpException {
    if (!Osumer.isWindows()) {
        throw new WithDumpException(null, "Validate OS is Windows", "Throw debuggable exception", "Validate is osumer elevated", "Installer does not support non-Windows environment", false);
    }
    if (!Osumer.isWindowsElevated()) {
        throw new WithDumpException(null, "Validate OS is Windows", "Validate is osumer elevated", "Create File instance of \"osumer.exe\"", "osumer is not elevated. Restart osumer with administrative privileges.", false);
    }
    String runningFilePath = null;
    try {
        runningFilePath = URLDecoder.decode(Installer.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        throw new WithDumpException(null, "Validate is osumer elevated", "Get current running file path", "Validate is running file name is ending with .exe", "Unsupported encoding UTF-8", false, e1);
    }
    if (!runningFilePath.endsWith(".exe")) {
        throw new WithDumpException(runningFilePath, "Get current running file path", "Validate is running file name is ending with .exe", "Create File instance of \"osumer.exe\"", "A Windows executable (.exe) version of osumer is required for installation. It can be downloaded from the releases. If you have it, rename it to \"osumer.exe\" to continue.", false);
    }
    File file = new File(runningFilePath);
    if (!file.exists()) {
        throw new WithDumpException(runningFilePath, "Validate is running file name is ending with .exe", "Create File instance of \"osumer.exe\"", "Create File instance of constant field winPath", "Unexpected? The running file-name does not exist!", false);
    }
    File destFolder = new File(winPath);
    if (!destFolder.exists()) {
        destFolder.mkdirs();
    }
    File verInfo = new File(winPath + "\\" + verInfoFile);
    try {
        if (verInfo.exists()) {
            verInfo.delete();
        }
        JSONObject verInfoJson = new JSONObject();
        verInfoJson.put("version", Osumer.OSUMER_VERSION);
        verInfoJson.put("branch", Osumer.OSUMER_BRANCH);
        verInfoJson.put("build", Osumer.OSUMER_BUILD_NUM);
        verInfo.createNewFile();
        FileOutputStream out = new FileOutputStream(verInfo);
        out.write(Base64.encodeBase64(verInfoJson.toString().getBytes(StandardCharsets.UTF_8)));
        out.flush();
        out.close();
    } catch (IOException e) {
        throw new WithDumpException(null, "Create new File instance with \"" + winPath + "\\" + verInfoFile + "\"", "Create application version info", "Create new File instance with \"" + winPath + "\\" + winFile + "\"", "Error creating version info", false, e);
    }
    File dest = new File(winPath + "\\" + winFile);
    try {
        if (dest.exists()) {
            dest.delete();
        }
        dest.createNewFile();
        FileOutputStream out = new FileOutputStream(dest);
        Files.copy(file.toPath(), out);
        out.flush();
        out.close();
    } catch (IOException e) {
        throw new WithDumpException(null, "Create new File instance with \"" + winPath + "\\" + winFile + "\"", "(Try scope) Copying file", "Creating Main osumer link in Start Menu", "Error copying file", false, e);
    }
    File startPathFolder = new File(START_MENU_PATH);
    if (!startPathFolder.exists() || !startPathFolder.isDirectory()) {
        startPathFolder.mkdir();
    }
    ShellLink slMain = ShellLink.createLink(Installer.winPath + "\\" + Installer.winFile);
    slMain.setWorkingDir(Installer.winPath);
    slMain.setIconLocation(Installer.winPath + "\\" + Installer.winFile);
    slMain.getHeader().setIconIndex(0);
    try {
        slMain.saveTo(START_MENU_PATH + "\\osumer.lnk");
    } catch (IOException e1) {
        throw new WithDumpException(null, "(Try scope) Copying file", "Creating Main osumer link in Start Menu", "Creating Daemon osumer link in Start Menu", "Error copying file", false, e1);
    }
    ShellLink slDaemon = ShellLink.createLink(Installer.winPath + "\\" + Installer.winFile);
    slDaemon.setWorkingDir(Installer.winPath);
    slDaemon.setCMDArgs("-daemon");
    slDaemon.setIconLocation(Installer.winPath + "\\" + Installer.winFile);
    slDaemon.getHeader().setIconIndex(0);
    try {
        slDaemon.saveTo(START_MENU_PATH + "\\osumer (Start as Daemon).lnk");
    } catch (IOException e1) {
        throw new WithDumpException(null, "Creating Main osumer link in Start Menu", "Creating Daemon osumer link in Start Menu", "(Next Try scope) Writing to registry", "Error copying file", false, e1);
    }
    try {
        // Create root key
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress
        boolean success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLIENTS_PATH, WIN_REG_INTERNET_CLIENT_KEY);
        final String clientRegPath = WIN_REG_CLIENTS_PATH + "\\" + WIN_REG_INTERNET_CLIENT_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/@
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, "", WIN_REG_INTERNET_CLIENT_DEFAULT_VALUE);
        // Capabilities
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_CAP_KEY);
        final String capRegPath = clientRegPath + "\\" + WIN_REG_CAP_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/@ApplicationName,@ApplicationDescription,@ApplicationIcon
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_APPNAME_PARA, WIN_REG_CAP_APPNAME_VALUE);
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_APPDESC_PARA, WIN_REG_CAP_APPDESC_VALUE);
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_APPICON_PARA, WIN_REG_CAP_APPICON_VALUE);
        // Capabilities: Startmenu
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/Startmenu
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_STARTMENU_KEY);
        // Legacy use
        final String capStartMenuRegPath = capRegPath + "\\" + WIN_REG_CAP_STARTMENU_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/Startmenu/@StartMenuInternet
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, capStartMenuRegPath, WIN_REG_CAP_STARTMENU_STARTMENUINTERNET_PARA, WIN_REG_CAP_STARTMENU_STARTMENUINTERNET_VALUE);
        // Capabilities: File associations
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/FileAssociations
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_FILEASSOC_KEY);
        // No file associations currently
        // Capabilities: URL Associations
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/URLAssociations
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_URLASSOC_KEY);
        final String urlAssocRegPath = capRegPath + "\\" + WIN_REG_CAP_URLASSOC_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/Capabilities/URLAssociations/@http,@https
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, urlAssocRegPath, WIN_REG_CAP_URLASSOC_HTTP_PARA, WIN_REG_CAP_URLASSOC_HTTP_VALUE);
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, urlAssocRegPath, WIN_REG_CAP_URLASSOC_HTTPS_PARA, WIN_REG_CAP_URLASSOC_HTTPS_VALUE);
        // Default icon
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/DefaultIcon
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_DEFAULTICON_KEY);
        final String defaultIconRegPath = clientRegPath + "\\" + WIN_REG_DEFAULTICON_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/DefaultIcon/@
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, defaultIconRegPath, "", WIN_REG_DEFAULTICON_DEFAULT_VALUE);
        // Install info
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/InstallInfo
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_INSTALLINFO_KEY);
        final String installInfoRegPath = clientRegPath + "\\" + WIN_REG_INSTALLINFO_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/InstallInfo/@HideIconsCommand
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, installInfoRegPath, WIN_REG_INSTALLINFO_HIDEICON_PARA, WIN_REG_INSTALLINFO_HIDEICON_VALUE);
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/InstallInfo/@ShowIconsCommand
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, installInfoRegPath, WIN_REG_INSTALLINFO_SHOWICON_PARA, WIN_REG_INSTALLINFO_SHOWICON_VALUE);
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/InstallInfo/@ReinstallCommand
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, installInfoRegPath, WIN_REG_INSTALLINFO_REINSTALL_PARA, WIN_REG_INSTALLINFO_REINSTALL_VALUE);
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/InstallInfo/@IconsVisible
        Advapi32Util.registrySetIntValue(WinReg.HKEY_LOCAL_MACHINE, installInfoRegPath, WIN_REG_INSTALLINFO_ICONSVISIBLE_PARA, WIN_REG_INSTALLINFO_ICONSVISIBLE_VALUE);
        // Shell: Open: Command
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/shell
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_SHELL_KEY);
        final String shellRegPath = clientRegPath + "\\" + WIN_REG_SHELL_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/shell/open
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, shellRegPath, WIN_REG_SHELL_OPEN_KEY);
        final String shellOpenRegPath = shellRegPath + "\\" + WIN_REG_SHELL_OPEN_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/shell/open/command
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, shellOpenRegPath, WIN_REG_SHELL_OPEN_COMMAND_KEY);
        final String shellOpenCmdRegPath = shellOpenRegPath + "\\" + WIN_REG_SHELL_OPEN_COMMAND_KEY;
        // HKLM/SOFTWARE/Clients/StartMenuInternet/osumerExpress/shell/open/command/@
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, shellOpenCmdRegPath, "", WIN_REG_SHELL_OPEN_COMMAND_DEFAULT_VALUE);
        // Registered Applications
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_REGISTEREDAPPS_PATH, WIN_REG_REGISTEREDAPPS_OSUMEREXPRESS_PARA, WIN_REG_REGISTEREDAPPS_OSUMEREXPRESS_VALUE);
        // Classes
        // HKLM/SOFTWARE/Classes/osumer
        success = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLASSES_PATH, WIN_REG_CLASSES_OSUMER_KEY);
        final String osumerClassRegPath = WIN_REG_CLASSES_PATH + "\\" + WIN_REG_CLASSES_OSUMER_KEY;
        // HKLM/SOFTWARE/Classes/osumer/@
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath, "", WIN_REG_CLASSES_OSUMER_DEFAULT_VALUE);
        // HKLM/SOFTWARE/Classes/osumer/@FriendlyTypeName
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath, WIN_REG_CLASSES_OSUMER_FRIENDLYTYPENAME_PARA, WIN_REG_CLASSES_OSUMER_FRIENDLYTYPENAME_VALUE);
        Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath, "shell");
        Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath + "\\shell", "open");
        Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath + "\\shell\\open", "command");
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, osumerClassRegPath + "\\shell\\open\\command", "", WIN_REG_SHELL_OPEN_COMMAND_DEFAULT_VALUE + " \"%1\"");
        // Run
        // HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Run/@osumerDaemon
        Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_RUN_PATH, WIN_REG_RUN_OSUMERDAEMON_PARA, WIN_REG_RUN_OSUMERDAEMON_VALUE);
    } catch (Win32Exception e) {
        throw new WithDumpException(null, "(Try&catch try) Writing to registry", "Throw debuggable exception", "(End of function)", "Error writing registry", false, e);
    }
}
Also used : WithDumpException(com.github.mob41.osumer.debug.WithDumpException) JSONObject(org.json.JSONObject) FileOutputStream(java.io.FileOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ShellLink(mslinks.ShellLink) File(java.io.File) Win32Exception(com.sun.jna.platform.win32.Win32Exception)

Example 17 with WithDumpException

use of com.github.mob41.osumer.debug.WithDumpException in project osumer by mob41.

the class Installer method uninstall.

public void uninstall() throws WithDumpException {
    File file = new File(winPath + "\\" + winFile);
    if (file.exists()) {
        file.delete();
    }
    file = new File(winPath + "\\" + verInfoFile);
    if (file.exists()) {
        file.delete();
    }
    file = new File(START_MENU_PATH + "\\osumer.lnk");
    if (file.exists()) {
        file.delete();
    }
    file = new File(START_MENU_PATH + "\\osumer (Start as Daemon).lnk");
    if (file.exists()) {
        file.delete();
    }
    try {
        final String clientRegPath = WIN_REG_CLIENTS_PATH + "\\" + WIN_REG_INTERNET_CLIENT_KEY;
        final String capRegPath = clientRegPath + "\\" + WIN_REG_CAP_KEY;
        final String shellRegPath = clientRegPath + "\\" + WIN_REG_SHELL_KEY;
        final String shellOpenRegPath = shellRegPath + "\\" + WIN_REG_SHELL_OPEN_KEY;
        // Capabilities
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_FILEASSOC_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_URLASSOC_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, capRegPath, WIN_REG_CAP_STARTMENU_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_CAP_KEY);
        // Shell open command
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, shellOpenRegPath, WIN_REG_SHELL_OPEN_COMMAND_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, shellRegPath, WIN_REG_SHELL_OPEN_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_SHELL_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_DEFAULTICON_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, clientRegPath, WIN_REG_INSTALLINFO_KEY);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLIENTS_PATH, WIN_REG_INTERNET_CLIENT_KEY);
        // Classes and Registered applications
        Advapi32Util.registryDeleteValue(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_REGISTEREDAPPS_PATH, WIN_REG_REGISTEREDAPPS_OSUMEREXPRESS_PARA);
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLASSES_PATH + "\\" + WIN_REG_CLASSES_OSUMER_KEY + "\\shell\\open", "command");
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLASSES_PATH + "\\" + WIN_REG_CLASSES_OSUMER_KEY + "\\shell", "open");
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLASSES_PATH + "\\" + WIN_REG_CLASSES_OSUMER_KEY, "shell");
        Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_CLASSES_PATH, WIN_REG_CLASSES_OSUMER_KEY);
        Advapi32Util.registryDeleteValue(WinReg.HKEY_LOCAL_MACHINE, WIN_REG_RUN_PATH, WIN_REG_RUN_OSUMERDAEMON_PARA);
    } catch (Win32Exception e) {
        e.printStackTrace();
        throw new WithDumpException(null, "(Try&catch try) Writing to registry", "Throw debuggable exception", "(End of function)", "Error writing registry", false, e);
    }
}
Also used : WithDumpException(com.github.mob41.osumer.debug.WithDumpException) File(java.io.File) Win32Exception(com.sun.jna.platform.win32.Win32Exception)

Example 18 with WithDumpException

use of com.github.mob41.osumer.debug.WithDumpException in project osumer by mob41.

the class AnnouncementChecker method getAnnouncements.

public Announcement[] getAnnouncements() throws WithDumpException {
    URL url = null;
    try {
        url = new URL(ANNOUNCEMENT_LIST + "?update=" + Calendar.getInstance().getTimeInMillis());
    } catch (MalformedURLException e) {
        throw new WithDumpException(ANNOUNCEMENT_LIST, "URL url = null;", "new URL(VERSION_LIST);", "URLConnection conn = url.openConnection();", "", false, e);
    }
    String data = "";
    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "close");
        conn.setUseCaches(false);
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        InputStream in = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            data += line;
        }
    } catch (IOException e) {
        throw new WithDumpException(ANNOUNCEMENT_LIST, "URL url = new URL(VERSION_LIST);", "(lots of code) -- Connecting and fetch data", "Data validating (isEmpty / null)", "", false, e);
    }
    if (data == null || data.isEmpty()) {
        throw new WithDumpException(ANNOUNCEMENT_LIST, "(lots of code) -- Connecting and fetch data", "Data validating (isEmpty / null)", "Create JSONObject", "No data fetched. \"data\" is null/isEmpty", false);
    }
    JSONObject json = null;
    try {
        json = new JSONObject(data);
    } catch (JSONException e) {
        throw new WithDumpException(ANNOUNCEMENT_LIST, "Data validating (isEmpty / null)", "Create JSONObject", "JSONObject validating \"announcements\" parameter", "Structure invalid", false, e);
    }
    if (json.isNull("announcements")) {
        throw new WithDumpException(ANNOUNCEMENT_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Parsing announcement json to objects", "Structure invalid, missing \"announcements\" parameter", false);
    }
    JSONArray arr = json.getJSONArray("announcements");
    Calendar cal;
    JSONObject obj;
    Announcement[] out = new Announcement[arr.length()];
    for (int i = 0; i < out.length; i++) {
        obj = arr.getJSONObject(i);
        if (obj.isNull("time") || obj.isNull("text")) {
            throw new WithDumpException(data, "JSONObject validating \"sources\" parameter", "Parsing announcement json to objects", "Output array", "Structure invalid, announcement json missing some parameters", false);
        }
        cal = Calendar.getInstance();
        cal.setTimeInMillis(obj.getLong("time"));
        out[i] = new Announcement(cal, obj.getString("text"));
    }
    return out;
}
Also used : MalformedURLException(java.net.MalformedURLException) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader)

Example 19 with WithDumpException

use of com.github.mob41.osumer.debug.WithDumpException in project osumer by mob41.

the class Updater method getVersions.

public JSONObject getVersions() throws WithDumpException {
    URL url = null;
    try {
        url = new URL(VERSION_LIST + "?update=" + Calendar.getInstance().getTimeInMillis());
    } catch (MalformedURLException e) {
        throw new WithDumpException(VERSION_LIST, "URL url = null;", "new URL(VERSION_LIST);", "URLConnection conn = url.openConnection();", "", false, e);
    }
    String data = "";
    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "close");
        conn.setUseCaches(false);
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        InputStream in = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            data += line;
        }
    } catch (IOException e) {
        throw new WithDumpException(VERSION_LIST, "URL url = new URL(VERSION_LIST);", "(lots of code) -- Connecting and fetch data", "Data validating (isEmpty / null)", "", false, e);
    }
    if (data == null || data.isEmpty()) {
        throw new WithDumpException(VERSION_LIST, "(lots of code) -- Connecting and fetch data", "Data validating (isEmpty / null)", "Create JSONObject", "No data fetched. \"data\" is null/isEmpty", false);
    }
    JSONObject json = null;
    try {
        json = new JSONObject(data);
    } catch (JSONException e) {
        throw new WithDumpException(VERSION_LIST, "Data validating (isEmpty / null)", "Create JSONObject", "JSONObject validating \"sources\" parameter", "Structure invalid", false, e);
    }
    return json;
}
Also used : MalformedURLException(java.net.MalformedURLException) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL)

Example 20 with WithDumpException

use of com.github.mob41.osumer.debug.WithDumpException in project osumer by mob41.

the class Updater method getLatestVersion.

public UpdateInfo getLatestVersion() throws WithDumpException {
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new WithDumpException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array: index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    VersionInfo thisInfo = new VersionInfo(Osumer.OSUMER_VERSION, Osumer.OSUMER_BRANCH, Osumer.OSUMER_BUILD_NUM);
    Iterator<String> it = versionsJson.keys();
    String last = null;
    String key;
    while (it.hasNext()) {
        key = it.next();
        if (last != null && compareVersion(key, last) != 1) {
            continue;
        } else {
            last = key;
        }
    }
    if (last == null) {
        return null;
    }
    buildsArr = versionsJson.getJSONArray(last);
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
    String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
    String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
    String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
    boolean isThisVersion = thisInfo != null && last.equals(thisInfo.getVersion()) && latest == thisInfo.getBuildNum() && getBranchStr(updateSource).equals(thisInfo.getBranch());
    if (thisInfo != null) {
        System.out.println(last + " to " + thisInfo.getVersion());
        System.out.println(latest + " to " + thisInfo.getBuildNum());
        System.out.println(getBranchStr(updateSource) + " to " + thisInfo.getBranch());
    } else {
        System.out.println("ThisInfoNull");
    }
    System.out.println("IsThisVer: " + isThisVersion);
    return new UpdateInfo(desc, last, updateSource, latest, webLink, exeLink, jarLink, isThisVersion, !isThisVersion);
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) VersionInfo(com.github.mob41.osumer.updater.VersionInfo) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) JSONObject(org.json.JSONObject) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray)

Aggregations

WithDumpException (com.github.mob41.osumer.debug.WithDumpException)18 IOException (java.io.IOException)11 URL (java.net.URL)7 JSONObject (org.json.JSONObject)6 MalformedURLException (java.net.MalformedURLException)5 HttpURLConnection (java.net.HttpURLConnection)4 ArrayList (java.util.ArrayList)4 OsuSong (com.github.mob41.osums.beatmap.OsuSong)3 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 List (java.util.List)3 Element (org.jsoup.nodes.Element)3 Configuration (com.github.mob41.osumer.Configuration)2 DebugDump (com.github.mob41.osumer.debug.DebugDump)2 InvalidSourceIntegerException (com.github.mob41.osumer.exceptions.InvalidSourceIntegerException)2 NoBuildsForVersionException (com.github.mob41.osumer.exceptions.NoBuildsForVersionException)2 NoSuchBuildNumberException (com.github.mob41.osumer.exceptions.NoSuchBuildNumberException)2