List of usage examples for java.net URI isAbsolute
public boolean isAbsolute()
From source file:cn.mailchat.mail.store.WebDavStore.java
/** * Performs form-based authentication.//w w w .ja va2 s. co m * * @throws MessagingException */ public void doFBA(ConnectionInfo info) throws IOException, MessagingException { // Clear out cookies from any previous authentication. mAuthCookies.clear(); WebDavHttpClient httpClient = getHttpClient(); String loginUrl; if (info != null) { loginUrl = info.guessedAuthUrl; } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) { loginUrl = mCachedLoginUrl; } else { throw new MessagingException("No valid login URL available for form-based authentication."); } HttpGeneric request = new HttpGeneric(loginUrl); request.setMethod("POST"); // Build the POST data. ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); pairs.add(new BasicNameValuePair("destination", mUrl)); pairs.add(new BasicNameValuePair("username", mUsername)); pairs.add(new BasicNameValuePair("password", mPassword)); pairs.add(new BasicNameValuePair("flags", "0")); pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On")); pairs.add(new BasicNameValuePair("forcedownlevel", "0")); pairs.add(new BasicNameValuePair("trusted", "0")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs); request.setEntity(formEntity); HttpResponse response = httpClient.executeOverride(request, mContext); boolean authenticated = testAuthenticationResponse(response); if (!authenticated) { // Check the response from the authentication request above for a form action. String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); if (formAction == null) { // If there is no form action, try using our redirect URL from the initial connection. if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) { loginUrl = info.redirectUrl; request = new HttpGeneric(loginUrl); request.setMethod("GET"); response = httpClient.executeOverride(request, mContext); formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); } } if (formAction != null) { try { URI formActionUri = new URI(formAction); URI loginUri = new URI(loginUrl); if (formActionUri.isAbsolute()) { // The form action is an absolute URL, just use it. loginUrl = formAction; } else { // Append the form action to our current URL, minus the file name. String urlPath; if (formAction.startsWith("/")) { urlPath = formAction; } else { urlPath = loginUri.getPath(); int lastPathPos = urlPath.lastIndexOf('/'); if (lastPathPos > -1) { urlPath = urlPath.substring(0, lastPathPos + 1); urlPath = urlPath.concat(formAction); } } // Reconstruct the login URL based on the original login URL and the form action. URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(), loginUri.getPort(), urlPath, null, null); loginUrl = finalUri.toString(); } // Retry the login using our new URL. request = new HttpGeneric(loginUrl); request.setMethod("POST"); request.setEntity(formEntity); response = httpClient.executeOverride(request, mContext); authenticated = testAuthenticationResponse(response); } catch (URISyntaxException e) { Log.e(MailChat.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e)); throw new MessagingException("URISyntaxException caught", e); } } else { throw new MessagingException("A valid URL for Exchange authentication could not be found."); } } if (authenticated) { mAuthentication = AUTH_TYPE_FORM_BASED; mCachedLoginUrl = loginUrl; } else { throw new MessagingException("Invalid credentials provided for authentication."); } }
From source file:com.top.Ertebat.mail.store.WebDavStore.java
/** * Performs form-based authentication.//from w ww . j ava 2s. c o m * * @throws MessagingException */ public void doFBA(ConnectionInfo info) throws IOException, MessagingException { // Clear out cookies from any previous authentication. mAuthCookies.clear(); WebDavHttpClient httpClient = getHttpClient(); String loginUrl; if (info != null) { loginUrl = info.guessedAuthUrl; } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) { loginUrl = mCachedLoginUrl; } else { throw new MessagingException("No valid login URL available for form-based authentication."); } HttpGeneric request = new HttpGeneric(loginUrl); request.setMethod("POST"); // Build the POST data. ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); pairs.add(new BasicNameValuePair("destination", mUrl)); pairs.add(new BasicNameValuePair("username", mUsername)); pairs.add(new BasicNameValuePair("password", mPassword)); pairs.add(new BasicNameValuePair("flags", "0")); pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On")); pairs.add(new BasicNameValuePair("forcedownlevel", "0")); pairs.add(new BasicNameValuePair("trusted", "0")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs); request.setEntity(formEntity); HttpResponse response = httpClient.executeOverride(request, mContext); boolean authenticated = testAuthenticationResponse(response); if (!authenticated) { // Check the response from the authentication request above for a form action. String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); if (formAction == null) { // If there is no form action, try using our redirect URL from the initial connection. if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) { loginUrl = info.redirectUrl; request = new HttpGeneric(loginUrl); request.setMethod("GET"); response = httpClient.executeOverride(request, mContext); formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); } } if (formAction != null) { try { URI formActionUri = new URI(formAction); URI loginUri = new URI(loginUrl); if (formActionUri.isAbsolute()) { // The form action is an absolute URL, just use it. loginUrl = formAction; } else { // Append the form action to our current URL, minus the file name. String urlPath; if (formAction.startsWith("/")) { urlPath = formAction; } else { urlPath = loginUri.getPath(); int lastPathPos = urlPath.lastIndexOf('/'); if (lastPathPos > -1) { urlPath = urlPath.substring(0, lastPathPos + 1); urlPath = urlPath.concat(formAction); } } // Reconstruct the login URL based on the original login URL and the form action. URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(), loginUri.getPort(), urlPath, null, null); loginUrl = finalUri.toString(); } // Retry the login using our new URL. request = new HttpGeneric(loginUrl); request.setMethod("POST"); request.setEntity(formEntity); response = httpClient.executeOverride(request, mContext); authenticated = testAuthenticationResponse(response); } catch (URISyntaxException e) { Log.e(Ertebat.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e)); throw new MessagingException("URISyntaxException caught", e); } } else { throw new MessagingException("A valid URL for Exchange authentication could not be found."); } } if (authenticated) { mAuthentication = AUTH_TYPE_FORM_BASED; mCachedLoginUrl = loginUrl; } else { throw new MessagingException("Invalid credentials provided for authentication."); } }
From source file:com.c0124.k9.mail.store.WebDavStore.java
/** * Performs form-based authentication./*from ww w .j av a 2 s. c o m*/ * * @throws MessagingException */ public void doFBA(ConnectionInfo info) throws IOException, MessagingException { // Clear out cookies from any previous authentication. mAuthCookies.clear(); WebDavHttpClient httpClient = getHttpClient(); String loginUrl; if (info != null) { loginUrl = info.guessedAuthUrl; } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) { loginUrl = mCachedLoginUrl; } else { throw new MessagingException("No valid login URL available for form-based authentication."); } HttpGeneric request = new HttpGeneric(loginUrl); request.setMethod("POST"); // Build the POST data. ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); pairs.add(new BasicNameValuePair("destination", mUrl)); pairs.add(new BasicNameValuePair("username", mUsername)); pairs.add(new BasicNameValuePair("password", mPassword)); pairs.add(new BasicNameValuePair("flags", "0")); pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On")); pairs.add(new BasicNameValuePair("forcedownlevel", "0")); pairs.add(new BasicNameValuePair("trusted", "0")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs); request.setEntity(formEntity); HttpResponse response = httpClient.executeOverride(request, mContext); boolean authenticated = testAuthenticationResponse(response); if (!authenticated) { // Check the response from the authentication request above for a form action. String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); if (formAction == null) { // If there is no form action, try using our redirect URL from the initial connection. if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) { loginUrl = info.redirectUrl; request = new HttpGeneric(loginUrl); request.setMethod("GET"); response = httpClient.executeOverride(request, mContext); formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); } } if (formAction != null) { try { URI formActionUri = new URI(formAction); URI loginUri = new URI(loginUrl); if (formActionUri.isAbsolute()) { // The form action is an absolute URL, just use it. loginUrl = formAction; } else { // Append the form action to our current URL, minus the file name. String urlPath; if (formAction.startsWith("/")) { urlPath = formAction; } else { urlPath = loginUri.getPath(); int lastPathPos = urlPath.lastIndexOf('/'); if (lastPathPos > -1) { urlPath = urlPath.substring(0, lastPathPos + 1); urlPath = urlPath.concat(formAction); } } // Reconstruct the login URL based on the original login URL and the form action. URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(), loginUri.getPort(), urlPath, null, null); loginUrl = finalUri.toString(); } // Retry the login using our new URL. request = new HttpGeneric(loginUrl); request.setMethod("POST"); request.setEntity(formEntity); response = httpClient.executeOverride(request, mContext); authenticated = testAuthenticationResponse(response); } catch (URISyntaxException e) { Log.e(K9.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e)); throw new MessagingException("URISyntaxException caught", e); } } else { throw new MessagingException("A valid URL for Exchange authentication could not be found."); } } if (authenticated) { mAuthentication = AUTH_TYPE_FORM_BASED; mCachedLoginUrl = loginUrl; } else { throw new MessagingException("Invalid credentials provided for authentication."); } }
From source file:com.bernard.beaconportal.activities.mail.store.WebDavStore.java
/** * Performs form-based authentication.//from w ww . ja v a2s .co m * * @throws MessagingException */ public void doFBA(ConnectionInfo info) throws IOException, MessagingException { // Clear out cookies from any previous authentication. mAuthCookies.clear(); WebDavHttpClient httpClient = getHttpClient(); String loginUrl; if (info != null) { loginUrl = info.guessedAuthUrl; } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) { loginUrl = mCachedLoginUrl; } else { throw new MessagingException("No valid login URL available for form-based authentication."); } HttpGeneric request = new HttpGeneric(loginUrl); request.setMethod("POST"); // Build the POST data. ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); pairs.add(new BasicNameValuePair("destination", mUrl)); pairs.add(new BasicNameValuePair("username", mUsername)); pairs.add(new BasicNameValuePair("password", mPassword)); pairs.add(new BasicNameValuePair("flags", "0")); pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On")); pairs.add(new BasicNameValuePair("forcedownlevel", "0")); pairs.add(new BasicNameValuePair("trusted", "0")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs); request.setEntity(formEntity); HttpResponse response = httpClient.executeOverride(request, mContext); boolean authenticated = testAuthenticationResponse(response); if (!authenticated) { // Check the response from the authentication request above for a // form action. String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); if (formAction == null) { // If there is no form action, try using our redirect URL from // the initial connection. if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) { loginUrl = info.redirectUrl; request = new HttpGeneric(loginUrl); request.setMethod("GET"); response = httpClient.executeOverride(request, mContext); formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity())); } } if (formAction != null) { try { URI formActionUri = new URI(formAction); URI loginUri = new URI(loginUrl); if (formActionUri.isAbsolute()) { // The form action is an absolute URL, just use it. loginUrl = formAction; } else { // Append the form action to our current URL, minus the // file name. String urlPath; if (formAction.startsWith("/")) { urlPath = formAction; } else { urlPath = loginUri.getPath(); int lastPathPos = urlPath.lastIndexOf('/'); if (lastPathPos > -1) { urlPath = urlPath.substring(0, lastPathPos + 1); urlPath = urlPath.concat(formAction); } } // Reconstruct the login URL based on the original login // URL and the form action. URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(), loginUri.getPort(), urlPath, null, null); loginUrl = finalUri.toString(); } // Retry the login using our new URL. request = new HttpGeneric(loginUrl); request.setMethod("POST"); request.setEntity(formEntity); response = httpClient.executeOverride(request, mContext); authenticated = testAuthenticationResponse(response); } catch (URISyntaxException e) { Log.e(K9.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e)); throw new MessagingException("URISyntaxException caught", e); } } else { throw new MessagingException("A valid URL for Exchange authentication could not be found."); } } if (authenticated) { mAuthentication = AUTH_TYPE_FORM_BASED; mCachedLoginUrl = loginUrl; } else { throw new MessagingException("Invalid credentials provided for authentication."); } }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * Get the <em>relative</em> {@linkplain #DEFAULT_PATH_PROP path} (within the * {@linkplain #getProxiedBaseURI(Page.Request) proxied base URI}) to the document which should be displayed within * this channel by default.//from ww w.j a v a2s .c om * * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed. * @return The {@linkplain #DEFAULT_PATH_PROP path} to the document which should be displayed within this channel by * default. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #DEFAULT_PATH_PROP * @see #getProxiedFileLocalURI(Page.Request, Channel.Mode, boolean) * @see #PROXIED_FILE_PATH_DEFAULT_HOOK */ protected @Nullable URI getProxiedFilePathDefault(final Page.Request pageRequest) throws WWWEEEPortal.Exception { URI defaultPath = PROXIED_FILE_PATH_DEFAULT_HOOK.value(plugins, null, pageRequest); if (defaultPath == null) { defaultPath = getConfigPropOpt(DEFAULT_PATH_PROP, pageRequest, STRING_TO_URI_FUNCTION).orElse(null); if ((defaultPath != null) && ((defaultPath.isAbsolute()) || (defaultPath.getPath() == null) || (defaultPath.getPath().startsWith("/")))) { throw new ConfigManager.ConfigException("The '" + DEFAULT_PATH_PROP + "' property is not a relative path: '" + defaultPath.toString() + '\'', null); } } defaultPath = PROXIED_FILE_PATH_DEFAULT_HOOK.filter(plugins, null, pageRequest, defaultPath); return defaultPath; }
From source file:it.geosolutions.geoserver.rest.GeoServerRESTPublisher.java
/** * Publish a collection of shapefiles.//w w w . j av a2 s . co m * <P> * Will automatically create the store and publish each shapefile as a layer. * * @param workspace the name of the workspace to use * @param storeName the name of the store to create * @param resource the shapefile collection. It can be: * <ul> * <li>A path to a directory containing shapefiles in the server. <li>A local zip file containing shapefiles that will be uploaded. <li>A * URL pointing to a shapefile collection in the wild web (not tested). * </ul> * @return {@code true} if publication successful. * @throws FileNotFoundException if the specified zip file does not exist. */ public boolean publishShpCollection(String workspace, String storeName, URI resource) throws FileNotFoundException { // Deduce upload method & mime type from resource syntax. UploadMethod method = null; String mime = null; if (resource.getScheme().equals("file") || resource.isAbsolute() == false) { File f = new File(resource); if (f.exists() && f.isFile() && f.toString().endsWith(".zip")) { method = UploadMethod.FILE; mime = "application/zip"; } else if (f.isDirectory()) { method = UploadMethod.EXTERNAL; mime = "text/plain"; } } else { try { if (resource.toURL() != null) { method = UploadMethod.URL; mime = "text/plain"; } } catch (MalformedURLException e) { throw new IllegalArgumentException( "Resource is not recognized as a zip file, or a directory, or a valid URL", e); } } // Create store, upload data, and publish layers return createStore(workspace, StoreType.DATASTORES, storeName, method, DataStoreExtension.SHP, mime, resource, ParameterConfigure.ALL, new NameValuePair[0]); }
From source file:net.sf.taverna.t2.security.credentialmanager.impl.CredentialManagerImpl.java
protected LinkedHashSet<URI> getPossibleServiceURIsToLookup(URI serviceURI, boolean usePathRecursion) { try {//from w w w . j a v a 2 s .c o m serviceURI = serviceURI.normalize(); serviceURI = dnParser.setUserInfoForURI(serviceURI, null); } catch (URISyntaxException ex) { logger.warn("Could not strip userinfo from " + serviceURI, ex); } /* * We'll use a LinkedHashSet to avoid checking for duplicates, like if * serviceURI.equals(withoutQuery) Only the first hit should be added to * the set. */ LinkedHashSet<URI> possibles = new LinkedHashSet<URI>(); possibles.add(serviceURI); if (!usePathRecursion || !serviceURI.isAbsolute()) return possibles; /* * We'll preserve the fragment, as it is used to indicate the realm */ String rawFragment = serviceURI.getRawFragment(); if (rawFragment == null) rawFragment = ""; URI withoutQuery = serviceURI.resolve(serviceURI.getRawPath()); addFragmentedURI(possibles, withoutQuery, rawFragment); // Immediate parent URI parent = withoutQuery.resolve("."); addFragmentedURI(possibles, parent, rawFragment); URI oldParent = null; // Top parent (to be added later) URI root = parent.resolve("/"); while (!parent.equals(oldParent) && !parent.equals(root) && parent.getPath().length() > 0) { // Intermediate parents, but not for "http://bla.org" as we would // find "http://bla.org.." oldParent = parent; parent = parent.resolve(".."); addFragmentedURI(possibles, parent, rawFragment); } // In case while-loop did not do so, also include root addFragmentedURI(possibles, root, rawFragment); if (rawFragment.length() > 0) // Add the non-fragment versions in the bottom of the list for (URI withFragment : new ArrayList<>(possibles)) try { possibles.add(dnParser.setFragmentForURI(withFragment, null)); } catch (URISyntaxException e) { logger.warn("Could not non-fragment URI " + withFragment); } return possibles; }
From source file:tufts.vue.URLResource.java
private URI findRelativeURI(URI root) { final URI absURI = toAbsoluteURI(); if (root.getScheme() == null || !root.getScheme().equals(absURI.getScheme())) { //if (DEBUG.Enabled) out("differing schemes: " + root + " - " + absURI + "; can't be relative"); if (DEBUG.RESOURCE) Log.info(this + "; scheme=" + absURI.getScheme() + "; different scheme: " + root + "; can't be relative"); return null; }/* w ww. j av a2s .c om*/ // if (DEBUG.Enabled) { // //System.out.println("\n======================================================="); // Log.debug("attempting to relativize [" + this + "] against: " + root); // } if (!absURI.isAbsolute()) Log.warn("findRelativeURI: non-absolute URI: " + absURI); //Log.warn("Non absolute URI: " + absURI + "; from URL " + url); // if (absURI == null) { // System.out.println("URL INVALID FOR URI: " + url + "; in " + this); // return null; // } if (DEBUG.RESOURCE) Resource.dumpURI(absURI, "CURRENT ABSOLUTE:"); final URI relativeURI = root.relativize(absURI); if (relativeURI == absURI) { // oldRoot was unable to relativize absURI -- this resource // was not relative to it's map in it's previous incarnation. return null; } if (relativeURI != absURI) { if (DEBUG.RESOURCE) Resource.dumpURI(relativeURI, "RELATIVE FOUND:"); } if (DEBUG.Enabled) { out(TERM_GREEN + "FOUND RELATIVE: " + relativeURI + TERM_CLEAR); } else { Log.info("found relative to " + root + ": " + relativeURI.getPath()); } return relativeURI; }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * <p>/* w w w . j a v a 2s .c o m*/ * Rewrite a <code>linkURI</code> associated with a <code>proxiedFileURL</code>, if required. * </p> * * <p> * Technically, there are two types of links within a document. First, a link within a document can be to an * <em>external resource</em>, which is loaded automatically by the browser to augment that document (ie a link to an * image, style sheet, script, etc). Or, second, a link within a document can be a <em>hyperlink</em>, which, when * activated by the user, will cause the browser to stop displaying that document and navigate to displaying the * linked document instead. * </p> * * <p> * If the portal is configured to display a website to clients through this <code>ProxyChannel</code>, it is generally * expected that if the client navigates a hyperlink from one document to another within the proxied site, that the * linked document would also be rendered within the channel ({@linkplain net.www_eee.portal.Channel.Mode#VIEW view * mode}), and that any external resource links will continue to resolve correctly. Link rewriting is required for * each of these two scenarios to work. * </p> * * <p> * To continue rendering within {@linkplain net.www_eee.portal.Channel.Mode#VIEW view mode} while navigating between * website documents, any hyperlink from within a proxied document to another document within the * {@linkplain #getProxiedBaseURI(Page.Request) proxied site} will, by default, be rewritten to point back through * this channel (alternatively, hyperlinks may {@linkplain #isLinkRewritingHyperlinksToChannelDisabled(Page.Request) * optionally} be resolved into an {@linkplain URI#isAbsolute() absolute} link pointing directly back to their * {@linkplain #getProxiedBaseURI(Page.Request) origin/source location} instead). * </p> * * <p> * If this channel were to blindly return unmodified source HTML from a proxied document for aggregation into a * {@link Page}, any relative link would break when it was incorrectly resolved relative to the * {@link net.www_eee.portal.ContentDef.Page.Key#getPageURI(UriInfo, Map, String, boolean) URL} of that page, instead * of relative to the {@linkplain #BASE_URI_PROP base URL} of the document providing it. To avoid this, any relative * link to an external resource from within a proxied document will, by default, be resolved into an * {@linkplain URI#isAbsolute() absolute} link pointing directly back to the * {@linkplain #getProxiedBaseURI(Page.Request) origin/source location} for that resource (alternatively, resource * links may {@linkplain #isLinkRewritingResourceLinksToChannelEnabled(Page.Request) optionally} be rewritten to point * back through this channel using {@linkplain net.www_eee.portal.Channel.Mode#RESOURCE resource mode} instead). * </p> * * <p> * For link rewriting to work, the <code>ProxyChannel</code> obviously needs to know which attributes of a proxied * document constitute <em>links</em>. But since the implementation is generic, and doesn't actually understand any * particular dialect of markup language on it's own, <em>including HTML</em>, you will likely want to configure this * channel alongside a plugin which does, such as the * {@linkplain net.www_eee.portal.channelplugins.ProxyChannelHTMLSource HTML plugin}. * </p> * * @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}? * @return A Map.Entry containing the rewritten link value, and a Boolean specifying if the returned link points back * through the channel. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #isLinkRewritingHyperlinksToChannelDisabled(Page.Request) * @see #isLinkRewritingResourceLinksToChannelEnabled(Page.Request) * @see net.www_eee.portal.channelplugins.ProxyChannelHTMLSource */ public Map.Entry<URI, Boolean> rewriteProxiedFileLink(final Page.Request pageRequest, final URL proxiedFileURL, final @Nullable URI linkURI, final boolean hyperlink, final boolean absoluteURLRequired) throws WWWEEEPortal.Exception { if ((linkURI != null) && (linkURI.isOpaque())) { return new AbstractMap.SimpleImmutableEntry<URI, Boolean>(linkURI, Boolean.FALSE); // Leave all the opaque ones alone (stuff like "mailto" links, etc), as we can't do anything with those. } final @NonNull URL resolvedLinkURL; // First, resolve the URL for the link so we know what server+file we are actually talking about here. try { if (linkURI == null) { resolvedLinkURL = proxiedFileURL; // An empty (null) link is equivalent to one back to the same proxied file. } else if (linkURI.isAbsolute()) { resolvedLinkURL = linkURI.toURL(); } else { resolvedLinkURL = new URL(proxiedFileURL, linkURI.toString()); // Resolve the link relative to the file it came from. } } catch (MalformedURLException mue) { throw new ContentManager.ContentException("Error resolving proxied link URL", mue); } if (((hyperlink) && (isLinkRewritingHyperlinksToChannelDisabled(pageRequest))) || ((!hyperlink) && (!isLinkRewritingResourceLinksToChannelEnabled(pageRequest)))) { // We are configured to not write this link back through the portal. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point, in order to determine what modifications to the link might be required, we need to figure out if * the link points to something either within, or outside of, the channel base URI's folder? */ if ((linkURI != null) && (linkURI.isAbsolute()) && (!equalHostAndPort(resolvedLinkURL, proxiedFileURL))) { // This is an absolute link which doesn't even point to the same server as the proxied file. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point we know the link at least points to the same server as the proxied file, but is it within the * channel base URI's folder? */ final String resolvedLinkPath = StringUtil.toString(StringUtil.mkNull(resolvedLinkURL.getPath()), "/"); final URI baseURI = getProxiedBaseURI(pageRequest); final URI resolvedBaseURI; if (baseURI.isAbsolute()) { resolvedBaseURI = baseURI; } else { resolvedBaseURI = ConfigManager.getContextResourceLocalHostURI(pageRequest.getUriInfo(), baseURI.getPath(), NetUtil.getQueryParams(baseURI), baseURI.getFragment(), true); } final String baseURIPath = resolvedBaseURI.getPath(); final String baseURIFolder; if ((baseURIPath.length() == 1) || (baseURIPath.charAt(baseURIPath.length() - 1) == '/')) { baseURIFolder = baseURIPath; // Path is a folder. } else { final int lastSlashIndex = baseURIPath.lastIndexOf('/'); baseURIFolder = (lastSlashIndex > 0) ? baseURIPath.substring(0, lastSlashIndex + 1) : String.valueOf('/'); } if (!resolvedLinkPath.startsWith(baseURIFolder)) { // We have determined this link is not within the channel base URI folder. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point we know the link points to within the channel base URI's folder, and that we need to rewrite it to * point back through the channel. */ final String linkChannelLocalPath = StringUtil.mkNull(resolvedLinkPath.substring(baseURIFolder.length())); final Mode channelMode = ((hyperlink) && (!isMaximizationDisabled(pageRequest))) ? Mode.VIEW : Mode.RESOURCE; final ContentDef.ChannelSpec<?> channelSpec = pageRequest.getChannelSpec(this); return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( channelSpec.getKey().getChannelURI(pageRequest.getUriInfo(), channelMode, linkChannelLocalPath, (linkURI != null) ? NetUtil.getQueryParams(linkURI) : null, (linkURI != null) ? linkURI.getFragment() : null, absoluteURLRequired), Boolean.TRUE); }