@Test public void testGetHostBatches() throws Exception { URL url; String[] batches; url = new URL("http://subdomain.example.edu.tr"); batches = URLUtil.getHostBatches(url); assertEquals("subdomain", batches[0]); assertEquals("example", batches[1]); assertEquals("edu", batches[2]); assertEquals("tr", batches[3]); url = new URL("http://"); batches = URLUtil.getHostBatches(url); assertEquals(1, batches.length); assertEquals("", batches[0]); url = new URL("http://140.211.11.130/foundation/contributing.html"); batches = URLUtil.getHostBatches(url); assertEquals(1, batches.length); assertEquals("140.211.11.130", batches[0]); // test non-ascii url = new URL("http://www.example.商業.tw"); batches = URLUtil.getHostBatches(url); assertEquals("www", batches[0]); assertEquals("example", batches[1]); assertEquals("商業", batches[2]); assertEquals("tw", batches[3]); }
@Test public void testToASCII() throws Exception { assertEquals("http://www.xn--evir-zoa.com", URLUtil.toASCII("http://www.çevir.com")); assertEquals("http://xn--uni-tbingen-xhb.de/", URLUtil.toASCII("http://uni-tübingen.de/")); assertEquals( "http://www.medizin.xn--uni-tbingen-xhb.de:8080/search.php?q=abc#p1", URLUtil.toASCII("http://www.medizin.uni-tübingen.de:8080/search.php?q=abc#p1")); }
/** * Create an image descriptor for the given image property in the text.properties file. * * @param propertyName * @return */ public static ImageDescriptor getDescriptor(String propertyName) { try { if (propertyName == null) { return null; } // get image path String path = Messages.getString(propertyName); if (path == null || path.trim().length() == 0) { SqlBuilderPlugin.log( Messages.getString("ImageUtil.logMessage2", propertyName), null); // $NON-NLS-1$ return null; } // create image URL url = URLUtil.getResourceURL(path); return ImageDescriptor.createFromURL(url); } catch (Exception e) { SqlBuilderPlugin.log( Messages.getString("ImageUtil.logMessage3", propertyName), e); // $NON-NLS-1$ return null; } }
@Nullable public static Icon getIcon(@NotNull String path, @NotNull final Class aClass) { URL url = aClass.getResource(path); if (url == null) { return null; } InputStream inputStream = null; try { inputStream = URLUtil.openStream(url); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buf = new byte[16 * 1024]; while (true) { int readBytes = inputStream.read(buf); if (readBytes == -1) { break; } byteArrayOutputStream.write(buf, 0, readBytes); } return new ImageIcon(byteArrayOutputStream.toByteArray()); } catch (IOException e) { e.printStackTrace(); return null; } finally { FileUtil.closeFileSafe(inputStream); } }
/** * Implements getResource() See getRealPath(), it have to be local to the current Context - and * can't go to a sub-context. That means we don't need any overhead. */ public URL getResource(String rpath) throws MalformedURLException { if (rpath == null) return null; if (URLUtil.hasEscape(rpath)) return null; URL url = null; String absPath = getAbsolutePath(); if ("".equals(rpath)) return new URL("file", null, 0, absPath); if (!rpath.startsWith("/")) rpath = "/" + rpath; String realPath = FileUtil.safePath(absPath, rpath); if (realPath == null) { log("Unsafe path " + absPath + " " + rpath); return null; } try { url = new URL("file", null, 0, realPath); if (debug > 9) log("getResourceURL=" + url + " request=" + rpath); return url; } catch (IOException ex) { ex.printStackTrace(); return null; } }
/** * 构造,相对于classes读取文件 * * @param path 相对路径 * @param clazz 基准类 * @param charset 字符集 * @param isUseVariable 是否使用变量 */ public Setting(String path, Class<?> clazz, String charset, boolean isUseVariable) { final URL url = URLUtil.getURL(path, clazz); if (url == null) { throw new RuntimeException(StrUtil.format("Can not find Setting file: [{}]", path)); } this.init(url, charset, isUseVariable); }
/** Sets the current URL associated with this load. */ void setUrl(String url) { if (url != null) { if (URLUtil.isDataUrl(url)) { // Don't strip anchor as that is a valid part of the URL mUrl = url; } else { mUrl = URLUtil.stripAnchor(url); } mUri = null; if (URLUtil.isNetworkUrl(mUrl)) { try { mUri = new WebAddress(mUrl); } catch (ParseException e) { e.printStackTrace(); } } } }
@Test public void testResolveURL() throws Exception { // test NUTCH-436 URL u436 = new URL("http://a/b/c/d;p?q#f"); assertEquals("http://a/b/c/d;p?q#f", u436.toString()); URL abs = URLUtil.resolveURL(u436, "?y"); assertEquals("http://a/b/c/d;p?y", abs.toString()); // test NUTCH-566 URL u566 = new URL("http://www.fleurie.org/entreprise.asp"); abs = URLUtil.resolveURL(u566, "?id_entrep=111"); assertEquals("http://www.fleurie.org/entreprise.asp?id_entrep=111", abs.toString()); URL base = new URL(baseString); assertEquals("base url parsing", baseString, base.toString()); for (int i = 0; i < targets.length; i++) { URL u = URLUtil.resolveURL(base, targets[i][0]); assertEquals(targets[i][1], targets[i][1], u.toString()); } }
/* * Helper method to return the install location. Return null if it is unavailable. */ public static File getInstallLocation() { Location installLocation = ServiceHelper.getService( TestActivator.getContext(), Location.class, Location.INSTALL_FILTER); if (installLocation == null || !installLocation.isSet()) return null; URL url = installLocation.getURL(); if (url == null) return null; return URLUtil.toFile(url); }
/** * Load a url from the network or the filesystem into the main frame. Following the same behaviour * as Safari, javascript: URLs are not passed to the main frame, instead they are evaluated * immediately. * * @param url The url to load. */ public void loadUrl(String url) { mLoadInitFromJava = true; if (URLUtil.isJavaScriptUrl(url)) { // strip off the scheme and evaluate the string stringByEvaluatingJavaScriptFromString(url.substring("javascript:".length())); } else { nativeLoadUrl(url); } mLoadInitFromJava = false; }
/* * Helper method to return the eclipse.home location. Return * null if it is unavailable. */ public static File getEclipseHome() { Location eclipseHome = (Location) ServiceHelper.getService( Activator.getContext(), Location.class.getName(), Location.ECLIPSE_HOME_FILTER); if (eclipseHome == null || !eclipseHome.isSet()) return null; URL url = eclipseHome.getURL(); if (url == null) return null; return URLUtil.toFile(url); }
/** * 构造 * * @param configFile 配置文件对象 * @param charset 字符集 * @param isUseVariable 是否使用变量 */ public Setting(File configFile, String charset, boolean isUseVariable) { if (configFile == null) { throw new RuntimeException("Null Setting file!"); } final URL url = URLUtil.getURL(configFile); if (url == null) { throw new RuntimeException( StrUtil.format("Can not find Setting file: [{}]", configFile.getAbsolutePath())); } this.init(url, charset, isUseVariable); }
/** * 构造,使用相对于Class文件根目录的相对路径 * * @param pathBaseClassLoader 相对路径(相对于当前项目的classes路径) * @param charset 字符集 * @param isUseVariable 是否使用变量 */ public Setting(String pathBaseClassLoader, String charset, boolean isUseVariable) { if (null == pathBaseClassLoader) { pathBaseClassLoader = StrUtil.EMPTY; } final URL url = URLUtil.getURL(pathBaseClassLoader); if (url == null) { throw new RuntimeException( StrUtil.format("Can not find Setting file: [{}]", pathBaseClassLoader)); } this.init(url, charset, isUseVariable); }
/** * Issues the load request. * * <p>Return value does not indicate if the load was successful or not. It simply indicates that * the load request is reasonable. * * @return true if the load is reasonable. */ public boolean executeLoad() { String url = mListener.url(); // Attempt to decode the percent-encoded url. try { url = new String(URLUtil.decode(url.getBytes())); } catch (IllegalArgumentException e) { // Fail with a bad url error if the decode fails. mListener.error( EventHandler.ERROR_BAD_URL, mListener.getContext().getString(com.android.internal.R.string.httpErrorBadUrl)); return false; } if (URLUtil.isNetworkUrl(url)) { if (mSettings.getBlockNetworkLoads()) { mListener.error( EventHandler.ERROR_BAD_URL, mListener.getContext().getString(com.android.internal.R.string.httpErrorBadUrl)); return false; } mNetwork = Network.getInstance(mListener.getContext()); return handleHTTPLoad(); } else if (handleLocalFile(url, mListener, mSettings)) { return true; } if (WebView.LOGV_ENABLED) { Log.v(LOGTAG, "FrameLoader.executeLoad: url protocol not supported:" + mListener.url()); } mListener.error( EventHandler.ERROR_UNSUPPORTED_SCHEME, mListener .getContext() .getText(com.android.internal.R.string.httpErrorUnsupportedScheme) .toString()); return false; }
/* package */ static boolean handleLocalFile(String url, LoadListener loadListener, WebSettings settings) { if (URLUtil.isAssetUrl(url)) { FileLoader.requestUrl( url, loadListener, loadListener.getContext(), true, settings.getAllowFileAccess()); return true; } else if (URLUtil.isFileUrl(url)) { FileLoader.requestUrl( url, loadListener, loadListener.getContext(), false, settings.getAllowFileAccess()); return true; } else if (URLUtil.isContentUrl(url)) { // Send the raw url to the ContentLoader because it will do a // permission check and the url has to match.. ContentLoader.requestUrl(loadListener.url(), loadListener, loadListener.getContext()); return true; } else if (URLUtil.isDataUrl(url)) { DataLoader.requestUrl(url, loadListener); return true; } else if (URLUtil.isAboutUrl(url)) { loadListener.data(mAboutBlank.getBytes(), mAboutBlank.length()); loadListener.endData(); return true; } return false; }
public void doNav() { String url = urlField.getText().toString(); if (URLUtil.isValidUrl(url) == false) { url = "http://" + url; } urlField.setText(url); webView.requestFocus(); webView.loadUrl(url); navbar.startAnimation(slideUp); }
/** * Guesses MIME type if one was not specified. Defaults to 'text/html'. In addition, tries to * guess the MIME type based on the extension. */ private void guessMimeType() { // Data urls must have a valid mime type or a blank string for the mime // type (implying text/plain). if (URLUtil.isDataUrl(mUrl) && mMimeType.length() != 0) { cancel(); final String text = mContext.getString(com.android.internal.R.string.httpErrorBadUrl); error(EventHandler.ERROR_BAD_URL, text); } else { // Note: This is ok because this is used only for the main content // of frames. If no content-type was specified, it is fine to // default to text/html. mMimeType = "text/html"; String newMimeType = guessMimeTypeFromExtension(); if (newMimeType != null) { mMimeType = newMimeType; } } }
public static ImageDescriptor getFragmentDescriptor(String fragmentId, String path) { try { if (path == null || path.trim().length() == 0) { return null; } // create image URL url = URLUtil.getFragmentResourceURL(fragmentId, path); return ImageDescriptor.createFromURL(url); } catch (Exception e) { SqlBuilderPlugin.log( Messages.getString("ImageUtil.logMessage3") + fragmentId + ": " + path, e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } }
@Test public void testGetDomainSuffix() throws Exception { URL url = null; url = new URL("http://lucene.apache.org/nutch"); assertEquals("org", URLUtil.getDomainSuffix(url).getDomain()); url = new URL("http://140.211.11.130/foundation/contributing.html"); assertNull(URLUtil.getDomainSuffix(url)); url = new URL("http://www.example.co.uk:8080/index.html"); assertEquals("co.uk", URLUtil.getDomainSuffix(url).getDomain()); url = new URL("http://com"); assertEquals("com", URLUtil.getDomainSuffix(url).getDomain()); url = new URL("http://www.example.co.uk.com"); assertEquals("com", URLUtil.getDomainSuffix(url).getDomain()); // "nn" is not a tld url = new URL("http://example.com.nn"); assertNull(URLUtil.getDomainSuffix(url)); url = new URL("http://"); assertNull(URLUtil.getDomainSuffix(url)); url = new URL("http://www.edu.tr.xyz"); assertNull(URLUtil.getDomainSuffix(url)); url = new URL("http://subdomain.example.edu.tr"); assertEquals("edu.tr", URLUtil.getDomainSuffix(url).getDomain()); url = new URL("http://subdomain.example.presse.fr"); assertEquals("presse.fr", URLUtil.getDomainSuffix(url).getDomain()); url = new URL("http://subdomain.example.presse.tr"); assertEquals("tr", URLUtil.getDomainSuffix(url).getDomain()); // plc.co.im is listed as a domain suffix url = new URL("http://www.example.plc.co.im"); assertEquals("plc.co.im", URLUtil.getDomainSuffix(url).getDomain()); // 2000.hu is listed as a domain suffix url = new URL("http://www.example.2000.hu"); assertEquals("2000.hu", URLUtil.getDomainSuffix(url).getDomain()); // test non-ascii url = new URL("http://www.example.商業.tw"); assertEquals("商業.tw", URLUtil.getDomainSuffix(url).getDomain()); }
@Test public void testFileProtocol() throws Exception { // keep one single slash NUTCH-XXX assertEquals("file:/path/file.html", URLUtil.toASCII("file:/path/file.html")); assertEquals("file:/path/file.html", URLUtil.toUNICODE("file:/path/file.html")); }
public String getLoginHomePage() { return getSettings("loginHomepage", URLUtil.getApplicationURL() + "/look/jsp/Main.jsp"); }
@Test public void testGetDomainName() throws Exception { URL url = null; url = new URL("http://nutch.apache.org"); assertEquals("apache.org", URLUtil.getDomainName(url)); url = new URL("http://en.wikipedia.org/wiki/Java_coffee"); assertEquals("wikipedia.org", URLUtil.getDomainName(url)); url = new URL("http://140.211.11.130/foundation/contributing.html"); assertEquals("140.211.11.130", URLUtil.getDomainName(url)); url = new URL("http://www.example.co.uk:8080/index.html"); assertEquals("example.co.uk", URLUtil.getDomainName(url)); url = new URL("http://com"); assertEquals("com", URLUtil.getDomainName(url)); url = new URL("http://www.example.co.uk.com"); assertEquals("uk.com", URLUtil.getDomainName(url)); // "nn" is not a tld url = new URL("http://example.com.nn"); assertEquals("nn", URLUtil.getDomainName(url)); url = new URL("http://"); assertEquals("", URLUtil.getDomainName(url)); url = new URL("http://www.edu.tr.xyz"); assertEquals("xyz", URLUtil.getDomainName(url)); url = new URL("http://www.example.c.se"); assertEquals("example.c.se", URLUtil.getDomainName(url)); // plc.co.im is listed as a domain suffix url = new URL("http://www.example.plc.co.im"); assertEquals("example.plc.co.im", URLUtil.getDomainName(url)); // 2000.hu is listed as a domain suffix url = new URL("http://www.example.2000.hu"); assertEquals("example.2000.hu", URLUtil.getDomainName(url)); // test non-ascii url = new URL("http://www.example.商業.tw"); assertEquals("example.商業.tw", URLUtil.getDomainName(url)); }
public void setReferrer(String ref) { // only set referrer for http or https if (URLUtil.isNetworkUrl(ref)) mReferrer = ref; }
@Test public void testChooseRepr() throws Exception { String aDotCom = "http://www.a.com"; String bDotCom = "http://www.b.com"; String aSubDotCom = "http://www.news.a.com"; String aQStr = "http://www.a.com?y=1"; String aPath = "http://www.a.com/xyz/index.html"; String aPath2 = "http://www.a.com/abc/page.html"; String aPath3 = "http://www.news.a.com/abc/page.html"; // 1) different domain them keep dest, temp or perm // a.com -> b.com* assertEquals(bDotCom, URLUtil.chooseRepr(aDotCom, bDotCom, true)); assertEquals(bDotCom, URLUtil.chooseRepr(aDotCom, bDotCom, false)); // 2) permanent and root, keep src // *a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html assertEquals(aDotCom, URLUtil.chooseRepr(aDotCom, aQStr, false)); assertEquals(aDotCom, URLUtil.chooseRepr(aDotCom, aPath, false)); // 3) permanent and not root and dest root, keep dest // a.com/xyz/index.html -> a.com* assertEquals(aDotCom, URLUtil.chooseRepr(aPath, aDotCom, false)); // 4) permanent and neither root keep dest // a.com/xyz/index.html -> a.com/abc/page.html* assertEquals(aPath2, URLUtil.chooseRepr(aPath, aPath2, false)); // 5) temp and root and dest not root keep src // *a.com -> a.com/xyz/index.html assertEquals(aDotCom, URLUtil.chooseRepr(aDotCom, aPath, true)); // 6) temp and not root and dest root keep dest // a.com/xyz/index.html -> a.com* assertEquals(aDotCom, URLUtil.chooseRepr(aPath, aDotCom, true)); // 7) temp and neither root, keep shortest, if hosts equal by path else by // hosts // a.com/xyz/index.html -> a.com/abc/page.html* // *www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html assertEquals(aPath2, URLUtil.chooseRepr(aPath, aPath2, true)); assertEquals(aPath, URLUtil.chooseRepr(aPath, aPath3, true)); // 8) temp and both root keep shortest sub domain // *www.a.com -> www.news.a.com assertEquals(aDotCom, URLUtil.chooseRepr(aDotCom, aSubDotCom, true)); }
/* * Perform the actual redirection. This involves setting up the new URL, * informing WebCore and then telling the Network to start loading again. */ private void doRedirect() { // as cancel() can cancel the load before doRedirect() is // called through handleMessage, needs to check to see if we // are canceled before proceed if (mCancelled) { return; } String redirectTo = mHeaders.getLocation(); if (redirectTo != null) { int nativeResponse = createNativeResponse(); redirectTo = nativeRedirectedToUrl(mUrl, redirectTo, nativeResponse); // nativeRedirectedToUrl() may call cancel(), e.g. when redirect // from a https site to a http site, check mCancelled again if (mCancelled) { return; } if (redirectTo == null) { Log.d(LOGTAG, "Redirection failed for " + mHeaders.getLocation()); cancel(); return; } else if (!URLUtil.isNetworkUrl(redirectTo)) { final String text = mContext.getString(com.android.internal.R.string.open_permission_deny) + "\n" + redirectTo; nativeAddData(text.getBytes(), text.length()); nativeFinished(); clearNativeLoader(); return; } if (mOriginalUrl == null) { mOriginalUrl = mUrl; } // Cache the redirect response if (mCacheResult != null) { if (getErrorID() == OK) { CacheManager.saveCacheFile(mUrl, mCacheResult); } mCacheResult = null; } setUrl(redirectTo); // Redirect may be in the cache if (mRequestHeaders == null) { mRequestHeaders = new HashMap<String, String>(); } if (!checkCache(mRequestHeaders)) { // mRequestHandle can be null when the request was satisfied // by the cache, and the cache returned a redirect if (mRequestHandle != null) { mRequestHandle.setupRedirect(redirectTo, mStatusCode, mRequestHeaders); } else { String method = mMethod; if (method == null) { return; } Network network = Network.getInstance(getContext()); network.requestURL(method, mRequestHeaders, mPostData, this, mIsHighPriority); } } } else { cancel(); } if (Config.LOGV) { Log.v(LOGTAG, "LoadListener.onRedirect(): redirect to: " + redirectTo); } }