|
| 1 | +/* |
| 2 | + * Copyright 2022 ebres. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.netbeans.modules.python4nb.api; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.prefs.Preferences; |
| 26 | +import org.apache.netbeans.modules.python4nb.platform.PythonPlatformManager; |
| 27 | +import org.openide.filesystems.FileObject; |
| 28 | +import org.openide.filesystems.FileUtil; |
| 29 | +import org.openide.util.NbPreferences; |
| 30 | + |
| 31 | +public final class Util { |
| 32 | + private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; // NOI18N |
| 33 | + private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; // NOI18N |
| 34 | + private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; // NOI18N |
| 35 | + |
| 36 | + private Util() { |
| 37 | + throw new IllegalStateException("Utility class"); |
| 38 | + } |
| 39 | + |
| 40 | + public static String readAsString(final InputStream is) throws IOException { |
| 41 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 42 | +// ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 43 | + FileUtil.copy(is, baos); |
| 44 | + return baos.toString("UTF-8"); // NOI18N |
| 45 | + } finally { |
| 46 | + is.close(); |
| 47 | + } |
| 48 | + } |
| 49 | + public static void adjustProxy(final ProcessBuilder pb) { |
| 50 | + String proxy = Util.getNetBeansHttpProxy(); |
| 51 | + if (proxy != null) { |
| 52 | + Map<String, String> env = pb.environment(); |
| 53 | + if ((env.get("HTTP_PROXY") == null) && (env.get("http_proxy") == null)) { // NOI18N |
| 54 | + env.put("HTTP_PROXY", proxy); // NOI18N |
| 55 | + env.put("http_proxy", proxy); // NOI18N |
| 56 | + } |
| 57 | + // PENDING - what if proxy was null so the user has TURNED off |
| 58 | + // proxies while there is still an environment variable set - should |
| 59 | + // we honor their environment, or honor their NetBeans proxy |
| 60 | + // settings (e.g. unset HTTP_PROXY in the environment before |
| 61 | + // launching plugin? |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * FIXME: get rid of the whole method as soon as some NB Proxy API is |
| 67 | + * available. |
| 68 | + */ |
| 69 | + private static String getNetBeansHttpProxy() { |
| 70 | + String host = System.getProperty("http.proxyHost"); // NOI18N |
| 71 | + |
| 72 | + if (host == null) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + String portHttp = System.getProperty("http.proxyPort"); // NOI18N |
| 77 | + int port; |
| 78 | + |
| 79 | + try { |
| 80 | + port = Integer.parseInt(portHttp); |
| 81 | + } catch (NumberFormatException e) { |
| 82 | + port = 8080; |
| 83 | + } |
| 84 | + |
| 85 | + Preferences prefs = NbPreferences.root().node("org/netbeans/core"); // NOI18N |
| 86 | + boolean useAuth = prefs.getBoolean(USE_PROXY_AUTHENTICATION, false); |
| 87 | + String auth = ""; |
| 88 | + if (useAuth) { |
| 89 | + auth = prefs.get(PROXY_AUTHENTICATION_USERNAME, "") + ":" + prefs.get(PROXY_AUTHENTICATION_PASSWORD, "") + '@'; // NOI18N |
| 90 | + } |
| 91 | + |
| 92 | + // Gem requires "http://" in front of the port name if it's not already there |
| 93 | + if (host.indexOf(':') == -1) { |
| 94 | + host = "http://" + auth + host; // NOI18N |
| 95 | + } |
| 96 | + |
| 97 | + return host + ":" + port; // NOI18N |
| 98 | + } |
| 99 | + |
| 100 | + private static final String FIRST_TIME_KEY = "platform-manager-called-first-time"; // NOI18N |
| 101 | + |
| 102 | + public static Preferences getPythonPreferences() { |
| 103 | + return NbPreferences.forModule(PythonPlatformManager.class); |
| 104 | + } |
| 105 | + |
| 106 | + public static boolean isFirstPlatformTouch() { |
| 107 | + return getPythonPreferences().getBoolean(FIRST_TIME_KEY, true); |
| 108 | + } |
| 109 | + |
| 110 | + public static void setFirstPlatformTouch(boolean b) { |
| 111 | + getPythonPreferences().putBoolean(FIRST_TIME_KEY, b); |
| 112 | + } |
| 113 | + |
| 114 | + // Tested in PythonUtilsTest |
| 115 | + public static List<FileObject> findUniqueRoots(List<FileObject> originalRoots) { |
| 116 | + List<FileObject> roots = new ArrayList<>(originalRoots); |
| 117 | + int n = roots.size(); |
| 118 | + if (n > 1) { |
| 119 | + for (int i = 0; i < n; i++) { |
| 120 | + FileObject f1 = roots.get(i); |
| 121 | + if (f1 == null) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + for (int j = 0; j < n; j++) { |
| 125 | + if (i == j) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + FileObject f2 = roots.get(j); |
| 129 | + if (f1 == f2 || f2 == null) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + if (FileUtil.isParentOf(f1, f2)) { |
| 133 | + roots.set(j, null); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + List<FileObject> uniqueRoots = new ArrayList<>(); |
| 140 | + for (int i = 0; i < n; i++) { |
| 141 | + FileObject fo = roots.get(i); |
| 142 | + if (fo != null) { |
| 143 | + uniqueRoots.add(fo); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return uniqueRoots; |
| 148 | + } |
| 149 | +} |
0 commit comments