Java Code Examples for java.net.URL#getRef()
The following examples show how to use
java.net.URL#getRef() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StrUtils.java From TrackRay with GNU General Public License v3.0 | 6 votes |
public static String urltoSchemaHostPortFile(URL u) { int len = u.getProtocol().length() + 1; if (u.getAuthority() != null && u.getAuthority().length() > 0) len += 2 + u.getAuthority().length(); if (u.getPath() != null) { len += u.getPath().length(); } if (u.getQuery() != null) { len += 1 + u.getQuery().length(); } if (u.getRef() != null) len += 1 + u.getRef().length(); StringBuffer result = new StringBuffer(len); result.append(u.getProtocol()); result.append(":"); if (u.getAuthority() != null && u.getAuthority().length() > 0) { result.append("//"); result.append(u.getAuthority()); } if (u.getPath() != null) { result.append(u.getFile()); } return result.toString(); }
Example 2
Source File: SpdyStream.java From whiskey with Apache License 2.0 | 6 votes |
Headers getCanonicalHeaders() { Headers canonical = new Headers(request.getHeaders()); URL url = request.getUrl(); String path = url.getPath(); // Though RFC-3986 allows path to be empty, most user agents will send // a trailing slash in lieu of an empty path, and Twitter seems to // require it. if (path.length() == 0) path = "/"; String query = url.getQuery(); String fragment = url.getRef(); String fullPath = path + (query != null ? "?" + query : "") + (fragment != null ? "#" + fragment : ""); canonical.put(":path", fullPath); canonical.put(":method", request.getMethod().toString()); canonical.put(":version", "HTTP/1.1"); canonical.put(":host", url.getHost()); canonical.put(":scheme", url.getProtocol()); return canonical; }
Example 3
Source File: ParseUtil.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
public static java.net.URI toURI(URL url) { String protocol = url.getProtocol(); String auth = url.getAuthority(); String path = url.getPath(); String query = url.getQuery(); String ref = url.getRef(); if (path != null && !(path.startsWith("/"))) path = "/" + path; // // In java.net.URI class, a port number of -1 implies the default // port number. So get it stripped off before creating URI instance. // if (auth != null && auth.endsWith(":-1")) auth = auth.substring(0, auth.length() - 3); java.net.URI uri; try { uri = createURI(protocol, auth, path, query, ref); } catch (java.net.URISyntaxException e) { uri = null; } return uri; }
Example 4
Source File: ParseUtil.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public static java.net.URI toURI(URL url) { String protocol = url.getProtocol(); String auth = url.getAuthority(); String path = url.getPath(); String query = url.getQuery(); String ref = url.getRef(); if (path != null && !(path.startsWith("/"))) path = "/" + path; // // In java.net.URI class, a port number of -1 implies the default // port number. So get it stripped off before creating URI instance. // if (auth != null && auth.endsWith(":-1")) auth = auth.substring(0, auth.length() - 3); java.net.URI uri; try { uri = createURI(protocol, auth, path, query, ref); } catch (java.net.URISyntaxException e) { uri = null; } return uri; }
Example 5
Source File: ImplicitResponseUrl.java From android-oauth-client with Apache License 2.0 | 5 votes |
ImplicitResponseUrl(URL url) { this(url.getProtocol(), url.getHost(), url.getPort(), url.getPath(), url.getRef(), url.getQuery(), url.getUserInfo()); }
Example 6
Source File: URLUtil.java From netbeans with Apache License 2.0 | 5 votes |
/** Creates a URL that is suitable for using in a different process on the * same node, similarly to URLMapper.EXTERNAL. May just return the original * URL if that's good enough. * @param url URL that needs to be displayed in browser * @param allowJar is <CODE>jar:</CODE> acceptable protocol? * @return browsable URL or null */ public static URL createExternalURL(URL url, boolean allowJar) { if (url == null) return null; URL compliantURL = getFullyRFC2396CompliantURL(url); // return if the protocol is fine if (isAcceptableProtocol(compliantURL, allowJar)) return compliantURL; // remove the anchor String anchor = compliantURL.getRef(); String urlString = compliantURL.toString (); int ind = urlString.indexOf('#'); if (ind >= 0) { urlString = urlString.substring(0, ind); } // map to an external URL using the anchor-less URL try { FileObject fo = URLMapper.findFileObject(new URL(urlString)); if (fo != null) { URL newUrl = getURLOfAppropriateType(fo, allowJar); if (newUrl != null) { // re-add the anchor if exists urlString = newUrl.toString(); if (ind >=0) { urlString = urlString + "#" + anchor; // NOI18N } return new URL(urlString); } } } catch (MalformedURLException e) { Logger.getLogger("global").log(Level.INFO, null, e); } return compliantURL; }
Example 7
Source File: URLUtil.java From storm-crawler with Apache License 2.0 | 5 votes |
public static String toASCII(String url) { try { URL u = new URL(url); URI p = new URI(u.getProtocol(), null, IDN.toASCII(u.getHost()), u.getPort(), u.getPath(), u.getQuery(), u.getRef()); return p.toString(); } catch (Exception e) { return null; } }
Example 8
Source File: UrlUtils.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * <p>Encodes illegal characters in the specified URL's path, query string and anchor according to the URL * encoding rules observed in real browsers.</p> * * <p>For example, this method changes <tt>"http://first/?a=b c"</tt> to <tt>"http://first/?a=b%20c"</tt>.</p> * * @param url the URL to encode * @param minimalQueryEncoding whether or not to perform minimal query encoding, like IE does * @param charset the charset * @return the encoded URL */ public static URL encodeUrl(final URL url, final boolean minimalQueryEncoding, final Charset charset) { if (!isNormalUrlProtocol(URL_CREATOR.getProtocol(url))) { return url; // javascript:, about:, data: and anything not supported like foo: } try { String path = url.getPath(); if (path != null) { path = encode(path, PATH_ALLOWED_CHARS, UTF_8); } String query = url.getQuery(); if (query != null) { if (minimalQueryEncoding) { query = org.apache.commons.lang3.StringUtils.replace(query, " ", "%20"); } else { query = encode(query, QUERY_ALLOWED_CHARS, charset); } } String anchor = url.getRef(); if (anchor != null) { anchor = encode(anchor, ANCHOR_ALLOWED_CHARS, UTF_8); } return createNewUrl(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), path, anchor, query); } catch (final MalformedURLException e) { // Impossible... I think. throw new RuntimeException(e); } }
Example 9
Source File: SqlFunctionUtils.java From flink with Apache License 2.0 | 5 votes |
/** * Parse url and return various components of the URL. * If accept any null arguments, return null. * * @param urlStr URL string. * @param partToExtract determines which components would return. * accept values: * HOST,PATH,QUERY,REF, * PROTOCOL,FILE,AUTHORITY,USERINFO * @return target value. */ public static String parseUrl(String urlStr, String partToExtract) { URL url; try { url = URL_CACHE.get(urlStr); } catch (Exception e) { LOG.error("Parse URL error: " + urlStr, e); return null; } if ("HOST".equals(partToExtract)) { return url.getHost(); } if ("PATH".equals(partToExtract)) { return url.getPath(); } if ("QUERY".equals(partToExtract)) { return url.getQuery(); } if ("REF".equals(partToExtract)) { return url.getRef(); } if ("PROTOCOL".equals(partToExtract)) { return url.getProtocol(); } if ("FILE".equals(partToExtract)) { return url.getFile(); } if ("AUTHORITY".equals(partToExtract)) { return url.getAuthority(); } if ("USERINFO".equals(partToExtract)) { return url.getUserInfo(); } return null; }
Example 10
Source File: RedirectHelper.java From CrappaLinks with GNU General Public License v3.0 | 5 votes |
public static String getAbsoluteUrl(String urlString, String baseUrlString) throws URISyntaxException, MalformedURLException { if (urlString.startsWith("http")) { return urlString; } else { URL url = new URL(baseUrlString); URI baseUri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return baseUri.resolve(urlString).toString(); } }
Example 11
Source File: UrlUtils.java From short-url with Apache License 2.0 | 5 votes |
public static String encodeUrl(String url) throws MalformedURLException { URL u = new URL(url); String path = u.getPath(); String query = u.getQuery(); String fragment = u.getRef(); StringBuilder sb = new StringBuilder(); sb.append(u.getProtocol()); sb.append("://"); sb.append(u.getHost()); if (!path.isEmpty()) { path = encodePath(path); sb.append(path); } if (query != null && !query.isEmpty()) { query = encodeQuery(query); sb.append("?"); sb.append(query); } if (fragment != null && !fragment.isEmpty()) { fragment = encodeFragment(fragment); sb.append("#"); sb.append(fragment); } return sb.toString(); }
Example 12
Source File: DocDownloader.java From netbeans with Apache License 2.0 | 5 votes |
@CheckForNull public static Documentation parseSection( @NonNull final String html, @NonNull final URL url) { final String ref = url.getRef(); if (ref != null && !ref.isEmpty()) { final Pattern p = Pattern.compile("<h(\\d)\\s+id\\s*=\\s*[\"']"+Pattern.quote(ref)+"[\"']"); //NOI18N final Matcher m = p.matcher(html); if (m.find()) { try { final int start = m.start(); final int headerType = Integer.parseInt(m.group(1)); int end = m.end(); do { end = html.indexOf("<h", end+1); //NOI18N if (end < 0) { break; } if (html.charAt(end+2) - '0' <= headerType) { //NOI18N break; } } while (true); return Documentation.create(html.substring( start, end < 0 ? html.length() : end), url); } catch (NumberFormatException e) { LOG.log( Level.WARNING, "Wrong documentation header: {0}", //NOI18N m.group(0)); //pass } } } return Documentation.create(html, url); }
Example 13
Source File: AsyncHttpClient.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Will encode url, if not disabled, and adds params on the end of it * * @param url String with URL, should be valid URL without params * @param params RequestParams to be appended on the end of URL * @param shouldEncodeUrl whether url should be encoded (replaces spaces with %20) * @return encoded url if requested with params appended if any available */ public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) { if (url == null) return null; if (shouldEncodeUrl) { try { String decodedURL = URLDecoder.decode(url, "UTF-8"); URL _url = new URL(decodedURL); URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(), _url.getPath(), _url.getQuery(), _url.getRef()); url = _uri.toASCIIString(); } catch (Exception ex) { // Should not really happen, added just for sake of validity Log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex); } } if (params != null) { // Construct the query string and trim it, in case it // includes any excessive white spaces. String paramString = params.getParamString().trim(); // Only add the query string if it isn't empty and it // isn't equal to '?'. if (!paramString.equals("") && !paramString.equals("?")) { url += url.contains("?") ? "&" : "?"; url += paramString; } } return url; }
Example 14
Source File: JavadocHelpContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private String getURLString(URL url) { String location= url.toExternalForm(); if (url.getRef() != null) { int anchorIdx= location.lastIndexOf('#'); if (anchorIdx != -1) { return location.substring(0, anchorIdx) + "?noframes=true" + location.substring(anchorIdx); //$NON-NLS-1$ } } return location + "?noframes=true"; //$NON-NLS-1$ }
Example 15
Source File: URLUtil.java From sparkler with Apache License 2.0 | 5 votes |
public static String toUNICODE(String url) { try { URL u = new URL(url); String host = u.getHost(); if (host == null || host.isEmpty()) { // no host name => no punycoded domain name // also do not add additional slashes for file: URLs (NUTCH-1880) return url; } StringBuilder sb = new StringBuilder(); sb.append(u.getProtocol()); sb.append("://"); if (u.getUserInfo() != null) { sb.append(u.getUserInfo()); sb.append('@'); } sb.append(IDN.toUnicode(host)); if (u.getPort() != -1) { sb.append(':'); sb.append(u.getPort()); } sb.append(u.getFile()); // includes query if (u.getRef() != null) { sb.append('#'); sb.append(u.getRef()); } return sb.toString(); } catch (Exception e) { return null; } }
Example 16
Source File: BrowserPane.java From SwingBox with GNU Lesser General Public License v3.0 | 4 votes |
private void loadPage(final URL newPage, final URL oldPage, final InputStream in, final Document doc) { boolean done = false; try { synchronized (this) { if (loadingStream != null) { // we are loading asynchronously, so we need to cancel // the old stream. loadingStream.close(); loadingStream = null; } loadingStream = in; } // read the content read(loadingStream, doc); // set the document to the component setDocument(doc); final String reference = newPage.getRef(); // Have to scroll after painted. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { scrollRectToVisible(new Rectangle(0, 0, 1, 1)); // top of the pane if (reference != null) scrollToReference(reference); } }); done = true; } catch (IOException ioe) { UIManager.getLookAndFeel().provideErrorFeedback(this); } finally { synchronized (this) { if (loadingStream != null) { try { loadingStream.close(); } catch (IOException ignored) { } } loadingStream = null; } if (done) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { firePropertyChange("page", oldPage, newPage); } }); } } }
Example 17
Source File: WebClient.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * Send a request to a server and return a Page that represents the * response from the server. This page will be used to populate the provided window. * <p> * The returned {@link Page} will be created by the {@link PageCreator} * configured by {@link #setPageCreator(PageCreator)}, if any. * <p> * The {@link DefaultPageCreator} will create a {@link Page} depending on the content type of the HTTP response, * basically {@link HtmlPage} for HTML content, {@link com.gargoylesoftware.htmlunit.xml.XmlPage} for XML content, * {@link TextPage} for other text content and {@link UnexpectedPage} for anything else. * * @param webWindow the WebWindow to load the result of the request into * @param webRequest the web request * @param addToHistory true if the page should be part of the history * @param <P> the page type * @return the page returned by the server when the specified request was made in the specified window * @throws IOException if an IO error occurs * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property * {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true * * @see WebRequest */ @SuppressWarnings("unchecked") <P extends Page> P getPage(final WebWindow webWindow, final WebRequest webRequest, final boolean addToHistory) throws IOException, FailingHttpStatusCodeException { final Page page = webWindow.getEnclosedPage(); if (page != null) { final URL prev = page.getUrl(); final URL current = webRequest.getUrl(); if (UrlUtils.sameFile(current, prev) && current.getRef() != null && !StringUtils.equals(current.getRef(), prev.getRef())) { // We're just navigating to an anchor within the current page. page.getWebResponse().getWebRequest().setUrl(current); if (addToHistory) { webWindow.getHistory().addPage(page); } final Window window = (Window) webWindow.getScriptableObject(); if (window != null) { // js enabled window.getLocation().setHash(current.getRef()); window.clearComputedStyles(); } return (P) page; } if (page.isHtmlPage()) { final HtmlPage htmlPage = (HtmlPage) page; if (!htmlPage.isOnbeforeunloadAccepted()) { if (LOG.isDebugEnabled()) { LOG.debug("The registered OnbeforeunloadHandler rejected to load a new page."); } return (P) page; } } } if (LOG.isDebugEnabled()) { LOG.debug("Get page for window named '" + webWindow.getName() + "', using " + webRequest); } final WebResponse webResponse; final String protocol = webRequest.getUrl().getProtocol(); if ("javascript".equals(protocol)) { webResponse = makeWebResponseForJavaScriptUrl(webWindow, webRequest.getUrl(), webRequest.getCharset()); if (webWindow.getEnclosedPage() != null && webWindow.getEnclosedPage().getWebResponse() == webResponse) { // a javascript:... url with result of type undefined didn't changed the page return (P) webWindow.getEnclosedPage(); } } else { webResponse = loadWebResponse(webRequest); } printContentIfNecessary(webResponse); loadWebResponseInto(webResponse, webWindow); // start execution here // note: we have to do this also if the server reports an error! // e.g. if the server returns a 404 error page that includes javascript if (scriptEngine_ != null) { scriptEngine_.registerWindowAndMaybeStartEventLoop(webWindow); } // check and report problems if needed throwFailingHttpStatusCodeExceptionIfNecessary(webResponse); return (P) webWindow.getEnclosedPage(); }
Example 18
Source File: PacmanUtils.java From pacbot with Apache License 2.0 | 4 votes |
public static List<String> getVolumeIdFromElasticSearch(String id, String esUrl, String attributeName) { JsonParser jsonParser = new JsonParser(); List<String> volList = new ArrayList<>(); try { HttpClient client = HttpClientBuilder.create().build(); URL url = new URL(esUrl); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); // prepare Json pay load for GET query. JsonObject innerJson = new JsonObject(); JsonObject matchPhrase = new JsonObject(); JsonObject must = new JsonObject(); JsonObject bool = new JsonObject(); JsonObject query = new JsonObject(); innerJson.addProperty(attributeName, id); matchPhrase.add(PacmanRuleConstants.MATCH_PHRASE, innerJson); must.add("must", matchPhrase); bool.add("bool", must); query.add(PacmanRuleConstants.QUERY, bool); StringEntity strjson = new StringEntity(query.toString()); // Qurying the ES HttpPost httpPost = new HttpPost(); httpPost.setURI(uri); httpPost.setEntity(strjson); httpPost.setHeader(PacmanRuleConstants.CONTENT_TYPE, PacmanRuleConstants.APPLICATION_JSON); HttpResponse response = client.execute(httpPost); String jsonString = EntityUtils.toString(response.getEntity()); JsonObject resultJson = (JsonObject) jsonParser.parse(jsonString); String hitsJsonString = resultJson.get(PacmanRuleConstants.HITS).toString(); JsonObject hitsJson = (JsonObject) jsonParser.parse(hitsJsonString); JsonArray jsonArray = hitsJson.getAsJsonObject().get(PacmanRuleConstants.HITS).getAsJsonArray(); volList = getVolumeList(jsonArray); } catch (Exception me) { logger.error(me.getMessage()); } return volList; }
Example 19
Source File: ComposedRecordingService.java From openvidu with Apache License 2.0 | 4 votes |
private String processCustomLayoutUrlFormat(URL url, String shortSessionId) { String finalUrl = url.getProtocol() + "://" + url.getAuthority(); if (!url.getPath().isEmpty()) { finalUrl += url.getPath(); } finalUrl = finalUrl.endsWith("/") ? finalUrl.substring(0, finalUrl.length() - 1) : finalUrl; if (url.getQuery() != null) { URI uri; try { uri = url.toURI(); finalUrl += "?"; } catch (URISyntaxException e) { String error = "\"customLayout\" property has URL format and query params (" + url.toString() + "), but does not comply with RFC2396 URI format"; log.error(error); throw new OpenViduException(Code.RECORDING_PATH_NOT_VALID, error); } List<NameValuePair> params = URLEncodedUtils.parse(uri, Charset.forName("UTF-8")); Iterator<NameValuePair> it = params.iterator(); boolean hasSessionId = false; boolean hasSecret = false; while (it.hasNext()) { NameValuePair param = it.next(); finalUrl += param.getName() + "=" + param.getValue(); if (it.hasNext()) { finalUrl += "&"; } if (!hasSessionId) { hasSessionId = param.getName().equals("sessionId"); } if (!hasSecret) { hasSecret = param.getName().equals("secret"); } } if (!hasSessionId) { finalUrl += "&sessionId=" + shortSessionId; } if (!hasSecret) { finalUrl += "&secret=" + openviduConfig.getOpenViduSecret(); } } if (url.getRef() != null) { finalUrl += "#" + url.getRef(); } return finalUrl; }
Example 20
Source File: BasicVertex.java From BotLibre with Eclipse Public License 1.0 | 4 votes |
@SuppressWarnings("unchecked") public void setDataValue(String value) { if (value == null) { this.data = null; return; } if (this.dataType instanceof String) { try { if (this.dataType.equals("Primitive")) { this.data = new Primitive(value); } else if (this.dataType.equals("String")) { this.data = value; } else if (this.dataType.equals("Time")) { this.data = Utils.parseTime((String)value); } else if (this.dataType.equals("Date")) { this.data = Utils.parseDate((String)value); } else if (this.dataType.equals("Timestamp")) { this.data = Utils.parseTimestamp(value); } else if (this.dataType.equals("Image")) { this.data = new BinaryData((String)value); } else if (this.dataType.equals("Binary")) { this.data = new BinaryData((String)value); } else if (this.dataType.equals("Text")) { this.data = new TextData((String)value); } else if (this.dataType.equals("URI")) { this.data = new URI((String)value); } else { Class<Object> typeClass = (Class<Object>)Class.forName((String)this.dataType); this.data = typeClass.getConstructor(String.class).newInstance(value); if (this.data instanceof URL) { try { this.data = ((URL) (this.data)).toURI(); } catch (Exception invalid) { URL url = (URL) this.data; this.data = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef()); } } } } catch (Exception error) { if (this.network == null) { System.out.println("DataValue error:" + this.id + "-" + value); error.printStackTrace(); } else { this.network.getBot().log(this.id, "DataValue error", Bot.WARNING, value); this.network.getBot().log(this, error); } } } else { this.data = value; } }