List of usage examples for java.net URL getRef
public String getRef()
From source file:org.sakaiproject.util.impl.FormattedTextImpl.java
public String sanitizeHrefURL(String urlToSanitize) { if (urlToSanitize == null) return null; if (StringUtils.isBlank(urlToSanitize)) return null; if (ABOUT_BLANK.equals(urlToSanitize)) return ABOUT_BLANK; boolean trimProtocol = false; boolean trimHost = false; // For a protocol-relative URL, we validate with protocol attached // RFC 1808 Section 4 if ((urlToSanitize.startsWith("//")) && (urlToSanitize.indexOf("://") == -1)) { urlToSanitize = PROTOCOL_PREFIX + urlToSanitize; trimProtocol = true;/*from w ww . j ava2 s.c o m*/ } // For a site-relative URL, we validate with host name and protocol attached // SAK-13787 SAK-23752 if ((urlToSanitize.startsWith("/")) && (urlToSanitize.indexOf("://") == -1)) { urlToSanitize = HOST_PREFIX + urlToSanitize; trimHost = true; } // KNL-1105 try { URL rawUrl = new URL(urlToSanitize); URI uri = new URI(rawUrl.getProtocol(), rawUrl.getUserInfo(), rawUrl.getHost(), rawUrl.getPort(), rawUrl.getPath(), rawUrl.getQuery(), rawUrl.getRef()); URL encoded = uri.toURL(); String retval = encoded.toString(); // Un-trim the added bits if (trimHost && retval.startsWith(HOST_PREFIX)) { retval = retval.substring(HOST_PREFIX.length()); } if (trimProtocol && retval.startsWith(PROTOCOL_PREFIX)) { retval = retval.substring(PROTOCOL_PREFIX.length()); } // http://stackoverflow.com/questions/7731919/why-doesnt-uri-escape-escape-single-quotes // We want these to be usable in JavaScript string values so we map single quotes retval = retval.replace("'", "%27"); // We want anchors to work retval = retval.replace("%23", "#"); // Sorry - these just need to come out - they cause to much trouble // Note that ampersand is not encoded as it is used for parameters. retval = retval.replace("&#", ""); retval = retval.replace("%25", "%"); return retval; } catch (java.net.URISyntaxException e) { M_log.info("Failure during encode of href url: " + e); return null; } catch (java.net.MalformedURLException e) { M_log.info("Failure during encode of href url: " + e); return null; } }
From source file:org.georchestra.security.Proxy.java
private URI buildUri(URL url) throws URISyntaxException { // Let URI constructor encode Path part URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), null, // Don't use query part because URI constructor will try to double encode it // (query part is already encoded in sURL) url.getRef()); // Reconstruct URL with encoded path from URI class and others parameters from URL class StringBuilder rawUrl = new StringBuilder(url.getProtocol() + "://" + url.getHost()); if (url.getPort() != -1) rawUrl.append(":" + String.valueOf(url.getPort())); rawUrl.append(uri.getRawPath()); // Use encoded version from URI class if (url.getQuery() != null) rawUrl.append("?" + url.getQuery()); // Use already encoded query part return new URI(rawUrl.toString()); }
From source file:org.opencms.workplace.tools.database.CmsHtmlImport.java
/** * Calculates an absolute uri from a relative "uri" and the given absolute "baseUri".<p> * //from www . j av a2s . co m * If "uri" is already absolute, it is returned unchanged. * This method also returns "uri" unchanged if it is not well-formed.<p> * * @param relativeUri the relative uri to calculate an absolute uri for * @param baseUri the base uri, this must be an absolute uri * @return an absolute uri calculated from "uri" and "baseUri" */ public String getAbsoluteUri(String relativeUri, String baseUri) { if ((relativeUri == null) || (relativeUri.charAt(0) == '/') || (relativeUri.startsWith("#"))) { return relativeUri; } // if we are on a windows system, we must add a ":" in the uri later String windowsAddition = ""; if (File.separator.equals("\\")) { windowsAddition = ":"; } try { URL baseUrl = new URL("file://"); URL url = new URL(new URL(baseUrl, "file://" + baseUri), relativeUri); if (url.getQuery() == null) { if (url.getRef() == null) { return url.getHost() + windowsAddition + url.getPath(); } else { return url.getHost() + windowsAddition + url.getPath() + "#" + url.getRef(); } } else { return url.getHost() + windowsAddition + url.getPath() + "?" + url.getQuery(); } } catch (MalformedURLException e) { return relativeUri; } }
From source file:com.stoutner.privacybrowser.MainWebViewActivity.java
private void loadUrlFromTextBox() throws UnsupportedEncodingException { // Get the text from urlTextBox and convert it to a string. String unformattedUrlString = urlTextBox.getText().toString(); URL unformattedUrl = null; Uri.Builder formattedUri = new Uri.Builder(); // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { // Add http:// at the beginning if it is missing. Otherwise the app will segfault. if (!unformattedUrlString.startsWith("http")) { unformattedUrlString = "http://" + unformattedUrlString; }/*from w w w . j ava 2s . co m*/ // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components. try { unformattedUrl = new URL(unformattedUrlString); } catch (MalformedURLException e) { e.printStackTrace(); } // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value. final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); formattedUrlString = formattedUri.build().toString(); } else { // Sanitize the search input and convert it to a DuckDuckGo search. final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); // Use the correct search URL based on javaScriptEnabled. if (javaScriptEnabled) { formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString; } else { // JavaScript is disabled. formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString; } } mainWebView.loadUrl(formattedUrlString); // Hides the keyboard so we can see the webpage. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); }
From source file:org.sakaiproject.content.tool.ResourcesHelperAction.java
public void doContinue(RunData data) { logger.debug(this + ".doContinue()"); SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid()); ParameterParser params = data.getParameters(); int requestStateId = params.getInt("requestStateId", 0); ResourcesAction.restoreRequestState(state, new String[] { ResourcesAction.PREFIX + ResourcesAction.REQUEST }, requestStateId); String content = params.getString("content"); if (content == null) { addAlert(state, rb.getString("text.notext")); return;//from w ww . j av a 2 s.c o m } ToolSession toolSession = SessionManager.getCurrentToolSession(); // Tool tool = ToolManager.getCurrentTool(); // String url = (String) toolSession.getAttribute(tool.getId() + Tool.HELPER_DONE_URL); // toolSession.removeAttribute(tool.getId() + Tool.HELPER_DONE_URL); ResourceToolActionPipe pipe = (ResourceToolActionPipe) toolSession .getAttribute(ResourceToolAction.ACTION_PIPE); if (pipe == null) { return; } if (pipe != null) { String pipe_init_id = pipe.getInitializationId(); String response_init_id = params.getString(ResourcesAction.PIPE_INIT_ID); if (pipe_init_id == null || response_init_id == null || !response_init_id.equalsIgnoreCase(pipe_init_id)) { // in this case, prevent upload to wrong folder pipe.setErrorMessage(rb.getString("alert.try-again")); pipe.setActionCanceled(false); pipe.setErrorEncountered(true); pipe.setActionCompleted(false); return; } toolSession.setAttribute(ResourceToolAction.ACTION_PIPE, pipe); } String resourceType = pipe.getAction().getTypeId(); String mimetype = pipe.getMimeType(); ListItem item = new ListItem(pipe.getContentEntity()); // notification int noti = determineNotificationPriority(params, item.isDropbox, item.userIsMaintainer()); pipe.setRevisedMimeType(pipe.getMimeType()); if (ResourceType.TYPE_TEXT.equals(resourceType) || ResourceType.MIME_TYPE_TEXT.equals(mimetype)) { pipe.setRevisedMimeType(ResourceType.MIME_TYPE_TEXT); pipe.setRevisedResourceProperty(ResourceProperties.PROP_CONTENT_ENCODING, ResourcesAction.UTF_8_ENCODING); pipe.setNotification(noti); } else if (ResourceType.TYPE_HTML.equals(resourceType) || ResourceType.MIME_TYPE_HTML.equals(mimetype)) { StringBuilder alertMsg = new StringBuilder(); content = FormattedText.processHtmlDocument(content, alertMsg); pipe.setRevisedMimeType(ResourceType.MIME_TYPE_HTML); pipe.setRevisedResourceProperty(ResourceProperties.PROP_CONTENT_ENCODING, ResourcesAction.UTF_8_ENCODING); pipe.setNotification(noti); if (alertMsg.length() > 0) { addAlert(state, alertMsg.toString()); return; } } else if (ResourceType.TYPE_URL.equals(resourceType)) { // SAK-23587 - properly escape the URL where required try { URL url = new URL(content); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); content = uri.toString(); } catch (Exception e) { //ok to ignore, just use the original url logger.debug("URL can not be encoded: " + e.getClass() + ":" + e.getCause()); } pipe.setRevisedMimeType(ResourceType.MIME_TYPE_URL); pipe.setNotification(noti); } else if (ResourceType.TYPE_FOLDER.equals(resourceType)) { MultiFileUploadPipe mfp = (MultiFileUploadPipe) pipe; int count = params.getInt("folderCount"); mfp.setFileCount(count); List<ResourceToolActionPipe> pipes = mfp.getPipes(); for (int i = 0; i < pipes.size(); i++) { ResourceToolActionPipe fp = pipes.get(i); String folderName = params.getString("folder" + (i + 1)); fp.setFileName(folderName); fp.setNotification(noti); } } try { pipe.setRevisedContent(content.getBytes(ResourcesAction.UTF_8_ENCODING)); pipe.setActionCanceled(false); pipe.setErrorEncountered(false); pipe.setActionCompleted(true); } catch (UnsupportedEncodingException e) { logger.warn(this + ": " + e.toString()); addAlert(state, rb.getString("alert.utf8encoding")); pipe.setActionCanceled(false); pipe.setErrorEncountered(true); pipe.setActionCompleted(false); } toolSession.setAttribute(ResourceToolAction.DONE, Boolean.TRUE); }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * Perform any desired modifications to a link which is <em>not</em> being * {@linkplain #rewriteProxiedFileLink(Page.Request, URL, URI, boolean, boolean) rewritten} to point back through this * channel.//from w ww .j a va 2 s . c o m * * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed. * @param proxiedFileURL The {@linkplain #getProxiedFileURL(Page.Request, Channel.Mode, boolean) proxied file URL}. * @param linkURI The {@link URI} of the link to rewrite. * @param hyperlink Is the <code>linkURI</code> a hyperlink? * @param absoluteURLRequired Does the result need to be {@linkplain URI#isAbsolute() absolute}? * @param resolvedLinkURL The <code>linkURI</code> after being resolved to it's actual location. * @return The rewritten link {@link URI}. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #rewriteProxiedFileLink(Page.Request, URL, URI, boolean, boolean) */ protected static final URI rewriteProxiedFileLinkOutsideChannel(final Page.Request pageRequest, final URL proxiedFileURL, final @Nullable URI linkURI, final boolean hyperlink, final boolean absoluteURLRequired, final URL resolvedLinkURL) throws WWWEEEPortal.Exception { if ((linkURI != null) && (linkURI.isAbsolute())) { return linkURI; // If a document author includes an absolute link, we generally want to just leave that as-is. } try { if ((!absoluteURLRequired) && (equalHostAndPort(proxiedFileURL, pageRequest.getBaseURL()))) { // They didn't author an absolute link, we don't require one, and since the resolved link points to our host/port, we have the opportunity to return a nice short non-absolute link... final StringBuffer sb = new StringBuffer(); sb.append(resolvedLinkURL.getPath()); if (resolvedLinkURL.getQuery() != null) { sb.append('?'); sb.append(resolvedLinkURL.getQuery()); } if (resolvedLinkURL.getRef() != null) { sb.append('#'); sb.append(resolvedLinkURL.getRef()); } return new URI(sb.toString()); } return resolvedLinkURL.toURI(); } catch (URISyntaxException urise) { throw new ContentManager.ContentException("Error constructing resolved link URI", urise); } }
From source file:com.gargoylesoftware.htmlunit.WebClient.java
/** * 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>//ww w. j a va2 s . co m * 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 <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") public <P extends Page> P getPage(final WebWindow webWindow, final WebRequest webRequest) throws IOException, FailingHttpStatusCodeException { final Page page = webWindow.getEnclosedPage(); if (page != null) { final URL prev = page.getUrl(); final URL current = webRequest.getUrl(); if (page.getWebResponse().getWebRequest().isCloneForHistoryAPI() || (current.sameFile(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); webWindow.getHistory().addPage(page); final Window window = (Window) webWindow.getScriptableObject(); if (window != null) { // js enabled window.getLocation().setHash(current.getRef()); } 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(); }
From source file:com.gargoylesoftware.htmlunit.WebClient.java
/** * Builds a WebResponse for a file URL./* ww w. j a va2 s .com*/ * This first implementation is basic. * It assumes that the file contains an HTML page encoded with the specified encoding. * @param url the file URL * @param charset encoding to use * @return the web response * @throws IOException if an IO problem occurs */ private WebResponse makeWebResponseForFileUrl(final WebRequest webRequest) throws IOException { URL cleanUrl = webRequest.getUrl(); if (cleanUrl.getQuery() != null) { // Get rid of the query portion before trying to load the file. cleanUrl = UrlUtils.getUrlWithNewQuery(cleanUrl, null); } if (cleanUrl.getRef() != null) { // Get rid of the ref portion before trying to load the file. cleanUrl = UrlUtils.getUrlWithNewRef(cleanUrl, null); } String fileUrl = cleanUrl.toExternalForm(); fileUrl = URLDecoder.decode(fileUrl, "UTF-8"); final File file = new File(fileUrl.substring(5)); if (!file.exists()) { // construct 404 final List<NameValuePair> compiledHeaders = new ArrayList<>(); compiledHeaders.add(new NameValuePair("Content-Type", "text/html")); final WebResponseData responseData = new WebResponseData( TextUtil.stringToByteArray("File: " + file.getAbsolutePath(), "UTF-8"), 404, "Not Found", compiledHeaders); return new WebResponse(responseData, webRequest, 0); } final String contentType = guessContentType(file); final DownloadedContent content = new DownloadedContent.OnFile(file, false); final List<NameValuePair> compiledHeaders = new ArrayList<>(); compiledHeaders.add(new NameValuePair("Content-Type", contentType)); final WebResponseData responseData = new WebResponseData(content, 200, "OK", compiledHeaders); return new WebResponse(responseData, webRequest, 0); }
From source file:org.lockss.util.UrlUtil.java
/** Normalize URL to a canonical form: lowercase scheme and hostname, * normalize path. Removes any reference part. XXX need to add * character escaping//from ww w .j av a 2s. c o m * @throws MalformedURLException */ public static String normalizeUrl(String urlString, int pathTraversalAction) throws MalformedURLException { log.debug3("Normalizing " + urlString); urlString = trimNewlinesAndLeadingWhitespace(urlString); if ("".equals(urlString)) { // permit empty return urlString; } URL url = new URL(urlString); String protocol = url.getProtocol(); // returns lowercase proto String host = url.getHost(); int port = url.getPort(); String path = url.getPath(); String query = url.getQuery(); if (log.isDebug3()) { log.debug3("protocol: " + protocol); log.debug3("host: " + host); log.debug3("port: " + port); log.debug3("path: " + path); log.debug3("query: " + query); } boolean changed = false; if (!urlString.startsWith(protocol)) { // protocol was lowercased changed = true; } if (normalizeAkamaiUrl && StringUtil.endsWithIgnoreCase(host, ".akamai.net")) { Matcher m = AKAMAI_PATH_PAT.matcher(path); if (m.find()) { host = m.group(1); path = m.group(2); changed = true; log.debug2("Akamai rewrite newhost, newpath: " + host + ", " + path); } } // if ("http".equals(protocol) || "ftp".equals(protocol)) { if (host != null) { String newHost = host.toLowerCase(); // lowercase host if (!host.equals(newHost)) { host = newHost; changed = true; } } if (port == getDefaultPort(protocol)) { // if url includes a port that is the default port for the protocol, // remove it (by passing port -1 to constructor) port = -1; changed = true; } if (StringUtil.isNullString(path)) { path = "/"; changed = true; } else { String normPath = normalizePath(path, pathTraversalAction); if (!normPath.equals(path)) { if (log.isDebug3()) log.debug3("Normalized " + path + " to " + normPath); path = normPath; changed = true; } } if (!StringUtil.isNullString(query)) { String normQuery = normalizeUrlEncodingCase(query); if (!normQuery.equals(query)) { if (log.isDebug3()) log.debug3("Normalized query " + query + " to " + normQuery); query = normQuery; changed = true; } } else if (normalizeEmptyQuery && "".equals(query)) { query = null; changed = true; } if (url.getRef() != null) { // remove the ref changed = true; } // } if (changed) { urlString = new URL(protocol, host, port, (StringUtil.isNullString(query) ? path : (path + "?" + query))).toString(); log.debug3("Changed, so returning " + urlString); } return urlString; }
From source file:com.gargoylesoftware.htmlunit.WebClient.java
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * Perform the downloads and stores it for loading later into a window. * In the future downloads should be performed in parallel in separated threads. * TODO: refactor it before next release. * @param requestingWindow the window from which the request comes * @param target the name of the target window * @param request the request to perform * @param checkHash if true check for hashChenage * @param forceLoad if true always load the request even if there is already the same in the queue * @param description information about the origin of the request. Useful for debugging. *//*from www. j a v a2s .c o m*/ public void download(final WebWindow requestingWindow, final String target, final WebRequest request, final boolean checkHash, final boolean forceLoad, final String description) { final WebWindow win = resolveWindow(requestingWindow, target); final URL url = request.getUrl(); boolean justHashJump = false; if (!request.isCloneForHistoryAPI()) { if (win != null && HttpMethod.POST != request.getHttpMethod()) { final Page page = win.getEnclosedPage(); if (page != null) { if (page.isHtmlPage() && !((HtmlPage) page).isOnbeforeunloadAccepted()) { return; } if (checkHash) { final URL current = page.getUrl(); justHashJump = HttpMethod.GET == request.getHttpMethod() && url.sameFile(current) && null != url.getRef(); } } } synchronized (loadQueue_) { // verify if this load job doesn't already exist for (final LoadJob loadJob : loadQueue_) { if (loadJob.response_ == null) { continue; } final WebRequest otherRequest = loadJob.request_; final URL otherUrl = otherRequest.getUrl(); // TODO: investigate but it seems that IE considers query string too but not FF if (!forceLoad && url.getPath().equals(otherUrl.getPath()) // fail fast && url.toString().equals(otherUrl.toString()) && request.getRequestParameters().equals(otherRequest.getRequestParameters()) && StringUtils.equals(request.getRequestBody(), otherRequest.getRequestBody())) { return; // skip it; } } } } final LoadJob loadJob; if (justHashJump || request.isCloneForHistoryAPI()) { loadJob = new LoadJob(request, requestingWindow, target, url); } else { try { final WebResponse response = loadWebResponse(request); loadJob = new LoadJob(request, requestingWindow, target, response); } catch (final IOException e) { throw new RuntimeException(e); } } synchronized (loadQueue_) { loadQueue_.add(loadJob); } }