feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
546
jdkSrc/jdk8/com/sun/jndi/toolkit/url/GenericURLContext.java
Normal file
546
jdkSrc/jdk8/com/sun/jndi/toolkit/url/GenericURLContext.java
Normal file
@@ -0,0 +1,546 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.toolkit.url;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.spi.ResolveResult;
|
||||
import javax.naming.spi.NamingManager;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import com.sun.jndi.toolkit.url.Uri.ParseMode;
|
||||
|
||||
/**
|
||||
* This abstract class is a generic URL context that accepts as the
|
||||
* name argument either a string URL or a Name whose first component
|
||||
* is a URL. It resolves the URL to a target context and then continues
|
||||
* the operation using the remaining name in the target context as if
|
||||
* the first component names a junction.
|
||||
*
|
||||
* A subclass must define getRootURLContext()
|
||||
* to process the URL into head/tail pieces. If it wants to control how
|
||||
* URL strings are parsed and compared for the rename() operation, then
|
||||
* it should override getNonRootURLSuffixes() and urlEquals().
|
||||
*
|
||||
* @author Scott Seligman
|
||||
* @author Rosanna Lee
|
||||
*/
|
||||
abstract public class GenericURLContext implements Context {
|
||||
|
||||
protected Hashtable<String, Object> myEnv = null;
|
||||
|
||||
@SuppressWarnings("unchecked") // Expect Hashtable<String, Object>
|
||||
public GenericURLContext(Hashtable<?,?> env) {
|
||||
// context that is not tied to any specific URL
|
||||
myEnv =
|
||||
(Hashtable<String, Object>)(env == null ? null : env.clone());
|
||||
}
|
||||
|
||||
public void close() throws NamingException {
|
||||
myEnv = null;
|
||||
}
|
||||
|
||||
public String getNameInNamespace() throws NamingException {
|
||||
return ""; // %%% check this out: A URL context's name is ""
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves 'name' into a target context with remaining name.
|
||||
* For example, with a JNDI URL "jndi://dnsname/rest_name",
|
||||
* this method resolves "jndi://dnsname/" to a target context,
|
||||
* and returns the target context with "rest_name".
|
||||
* The definition of "root URL" and how much of the URL to
|
||||
* consume is implementation specific.
|
||||
* If rename() is supported for a particular URL scheme,
|
||||
* getRootURLContext(), getURLPrefix(), and getURLSuffix()
|
||||
* must be in sync wrt how URLs are parsed and returned.
|
||||
*/
|
||||
abstract protected ResolveResult getRootURLContext(String url,
|
||||
Hashtable<?,?> env) throws NamingException;
|
||||
|
||||
/**
|
||||
* Returns the suffix of the url. The result should be identical to
|
||||
* that of calling getRootURLContext().getRemainingName(), but
|
||||
* without the overhead of doing anything with the prefix like
|
||||
* creating a context.
|
||||
*<p>
|
||||
* This method returns a Name instead of a String because to give
|
||||
* the provider an opportunity to return a Name (for example,
|
||||
* for weakly separated naming systems like COS naming).
|
||||
*<p>
|
||||
* The default implementation uses skips 'prefix', calls
|
||||
* UrlUtil.decode() on it, and returns the result as a single component
|
||||
* CompositeName.
|
||||
* Subclass should override if this is not appropriate.
|
||||
* This method is used only by rename().
|
||||
* If rename() is supported for a particular URL scheme,
|
||||
* getRootURLContext(), getURLPrefix(), and getURLSuffix()
|
||||
* must be in sync wrt how URLs are parsed and returned.
|
||||
*<p>
|
||||
* For many URL schemes, this method is very similar to URL.getFile(),
|
||||
* except getFile() will return a leading slash in the
|
||||
* 2nd, 3rd, and 4th cases. For schemes like "ldap" and "iiop",
|
||||
* the leading slash must be skipped before the name is an acceptable
|
||||
* format for operation by the Context methods. For schemes that treat the
|
||||
* leading slash as significant (such as "file"),
|
||||
* the subclass must override getURLSuffix() to get the correct behavior.
|
||||
* Remember, the behavior must match getRootURLContext().
|
||||
*
|
||||
* URL Suffix
|
||||
* foo://host:port <empty string>
|
||||
* foo://host:port/rest/of/name rest/of/name
|
||||
* foo:///rest/of/name rest/of/name
|
||||
* foo:/rest/of/name rest/of/name
|
||||
* foo:rest/of/name rest/of/name
|
||||
*/
|
||||
protected Name getURLSuffix(String prefix, String url) throws NamingException {
|
||||
String suffix = url.substring(prefix.length());
|
||||
if (suffix.length() == 0) {
|
||||
return new CompositeName();
|
||||
}
|
||||
|
||||
if (suffix.charAt(0) == '/') {
|
||||
suffix = suffix.substring(1); // skip leading slash
|
||||
}
|
||||
|
||||
try {
|
||||
return new CompositeName().add(UrlUtil.decode(suffix));
|
||||
} catch (MalformedURLException e) {
|
||||
throw new InvalidNameException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the prefix of a URL.
|
||||
* Default implementation looks for slashes and then extracts
|
||||
* prefixes using String.substring().
|
||||
* Subclass should override if this is not appropriate.
|
||||
* This method is used only by rename().
|
||||
* If rename() is supported for a particular URL scheme,
|
||||
* getRootURLContext(), getURLPrefix(), and getURLSuffix()
|
||||
* must be in sync wrt how URLs are parsed and returned.
|
||||
*<p>
|
||||
* URL Prefix
|
||||
* foo://host:port foo://host:port
|
||||
* foo://host:port/rest/of/name foo://host:port
|
||||
* foo:///rest/of/name foo://
|
||||
* foo:/rest/of/name foo:
|
||||
* foo:rest/of/name foo:
|
||||
*/
|
||||
protected String getURLPrefix(String url) throws NamingException {
|
||||
int start = url.indexOf(":");
|
||||
|
||||
if (start < 0) {
|
||||
throw new OperationNotSupportedException("Invalid URL: " + url);
|
||||
}
|
||||
++start; // skip ':'
|
||||
|
||||
if (url.startsWith("//", start)) {
|
||||
start += 2; // skip double slash
|
||||
|
||||
// find where the authority component ends
|
||||
// and the rest of the URL starts
|
||||
int slash = url.indexOf('/', start);
|
||||
int qmark = url.indexOf('?', start);
|
||||
int fmark = url.indexOf('#', start);
|
||||
if (fmark > -1 && qmark > fmark) qmark = -1;
|
||||
if (fmark > -1 && slash > fmark) slash = -1;
|
||||
if (qmark > -1 && slash > qmark) slash = -1;
|
||||
int posn = slash > -1 ? slash
|
||||
: (qmark > -1 ? qmark
|
||||
: (fmark > -1 ? fmark
|
||||
: url.length()));
|
||||
if (posn >= 0) {
|
||||
start = posn;
|
||||
} else {
|
||||
start = url.length(); // rest of URL
|
||||
}
|
||||
}
|
||||
|
||||
// else 0 or 1 iniitial slashes; start is unchanged
|
||||
return url.substring(0, start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether two URLs are the same.
|
||||
* Default implementation uses String.equals().
|
||||
* Subclass should override if this is not appropriate.
|
||||
* This method is used by rename().
|
||||
*/
|
||||
protected boolean urlEquals(String url1, String url2) {
|
||||
return url1.equals(url2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the context in which to continue the operation. This method
|
||||
* is called when this context is asked to process a multicomponent
|
||||
* Name in which the first component is a URL.
|
||||
* Treat the first component like a junction: resolve it and then use
|
||||
* NamingManager.getContinuationContext() to get the target context in
|
||||
* which to operate on the remainder of the name (n.getSuffix(1)).
|
||||
*/
|
||||
protected Context getContinuationContext(Name n) throws NamingException {
|
||||
Object obj = lookup(n.get(0));
|
||||
CannotProceedException cpe = new CannotProceedException();
|
||||
cpe.setResolvedObj(obj);
|
||||
cpe.setEnvironment(myEnv);
|
||||
return NamingManager.getContinuationContext(cpe);
|
||||
}
|
||||
|
||||
public Object lookup(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.lookup(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Object lookup(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return lookup(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.lookup(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(String name, Object obj) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
ctx.bind(res.getRemainingName(), obj);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(Name name, Object obj) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
bind(name.get(0), obj);
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
ctx.bind(name.getSuffix(1), obj);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void rebind(String name, Object obj) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
ctx.rebind(res.getRemainingName(), obj);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void rebind(Name name, Object obj) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
rebind(name.get(0), obj);
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
ctx.rebind(name.getSuffix(1), obj);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void unbind(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
ctx.unbind(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void unbind(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
unbind(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
ctx.unbind(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void rename(String oldName, String newName) throws NamingException {
|
||||
String oldPrefix = getURLPrefix(oldName);
|
||||
String newPrefix = getURLPrefix(newName);
|
||||
if (!urlEquals(oldPrefix, newPrefix)) {
|
||||
throw new OperationNotSupportedException(
|
||||
"Renaming using different URL prefixes not supported : " +
|
||||
oldName + " " + newName);
|
||||
}
|
||||
|
||||
ResolveResult res = getRootURLContext(oldName, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
ctx.rename(res.getRemainingName(), getURLSuffix(newPrefix, newName));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void rename(Name name, Name newName) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
if (newName.size() != 1) {
|
||||
throw new OperationNotSupportedException(
|
||||
"Renaming to a Name with more components not supported: " + newName);
|
||||
}
|
||||
rename(name.get(0), newName.get(0));
|
||||
} else {
|
||||
// > 1 component with 1st one being URL
|
||||
// URLs must be identical; cannot deal with diff URLs
|
||||
if (!urlEquals(name.get(0), newName.get(0))) {
|
||||
throw new OperationNotSupportedException(
|
||||
"Renaming using different URLs as first components not supported: " +
|
||||
name + " " + newName);
|
||||
}
|
||||
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
ctx.rename(name.getSuffix(1), newName.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.list(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return list(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.list(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<Binding> listBindings(String name)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.listBindings(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return listBindings(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.listBindings(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void destroySubcontext(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
ctx.destroySubcontext(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroySubcontext(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
destroySubcontext(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
ctx.destroySubcontext(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Context createSubcontext(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.createSubcontext(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Context createSubcontext(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return createSubcontext(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.createSubcontext(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object lookupLink(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.lookupLink(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Object lookupLink(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return lookupLink(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.lookupLink(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NameParser getNameParser(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
Context ctx = (Context)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.getNameParser(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NameParser getNameParser(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return getNameParser(name.get(0));
|
||||
} else {
|
||||
Context ctx = getContinuationContext(name);
|
||||
try {
|
||||
return ctx.getNameParser(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String composeName(String name, String prefix)
|
||||
throws NamingException {
|
||||
if (prefix.equals("")) {
|
||||
return name;
|
||||
} else if (name.equals("")) {
|
||||
return prefix;
|
||||
} else {
|
||||
return (prefix + "/" + name);
|
||||
}
|
||||
}
|
||||
|
||||
public Name composeName(Name name, Name prefix) throws NamingException {
|
||||
Name result = (Name)prefix.clone();
|
||||
result.addAll(name);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Object removeFromEnvironment(String propName)
|
||||
throws NamingException {
|
||||
if (myEnv == null) {
|
||||
return null;
|
||||
}
|
||||
return myEnv.remove(propName);
|
||||
}
|
||||
|
||||
public Object addToEnvironment(String propName, Object propVal)
|
||||
throws NamingException {
|
||||
if (myEnv == null) {
|
||||
myEnv = new Hashtable<String, Object>(11, 0.75f);
|
||||
}
|
||||
return myEnv.put(propName, propVal);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // clone()
|
||||
public Hashtable<String, Object> getEnvironment() throws NamingException {
|
||||
if (myEnv == null) {
|
||||
return new Hashtable<>(5, 0.75f);
|
||||
} else {
|
||||
return (Hashtable<String, Object>)myEnv.clone();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// To test, declare getURLPrefix and getURLSuffix static.
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String[] tests = {"file://host:port",
|
||||
"file:///rest/of/name",
|
||||
"file://host:port/rest/of/name",
|
||||
"file:/rest/of/name",
|
||||
"file:rest/of/name"};
|
||||
for (int i = 0; i < tests.length; i++) {
|
||||
String pre = getURLPrefix(tests[i]);
|
||||
System.out.println(pre);
|
||||
System.out.println(getURLSuffix(pre, tests[i]));
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
411
jdkSrc/jdk8/com/sun/jndi/toolkit/url/GenericURLDirContext.java
Normal file
411
jdkSrc/jdk8/com/sun/jndi/toolkit/url/GenericURLDirContext.java
Normal file
@@ -0,0 +1,411 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package com.sun.jndi.toolkit.url;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.directory.*;
|
||||
import javax.naming.spi.ResolveResult;
|
||||
import javax.naming.spi.DirectoryManager;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* This abstract class is a generic URL DirContext that accepts as the
|
||||
* name argument either a string URL or a Name whose first component
|
||||
* is a URL. It resolves the URL to a target context and then continues
|
||||
* the operation using the remaining name in the target context as if
|
||||
* the first component names a junction.
|
||||
*
|
||||
* A subclass must define getRootURLContext()
|
||||
* to process the URL into head/tail pieces. If it wants to control how
|
||||
* URL strings are parsed and compared for the rename() operation, then
|
||||
* it should override getNonRootURLSuffixes() and urlEquals().
|
||||
*
|
||||
* @author Scott Seligman
|
||||
* @author Rosanna Lee
|
||||
*/
|
||||
|
||||
abstract public class GenericURLDirContext extends GenericURLContext
|
||||
implements DirContext {
|
||||
|
||||
protected GenericURLDirContext(Hashtable<?,?> env) {
|
||||
super(env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the context in which to continue the operation. This method
|
||||
* is called when this context is asked to process a multicomponent
|
||||
* Name in which the first component is a URL.
|
||||
* Treat the first component like a junction: resolve it and then use
|
||||
* DirectoryManager.getContinuationDirContext() to get the target context in
|
||||
* which to operate on the remainder of the name (n.getSuffix(1)).
|
||||
* Do this in case intermediate contexts are not DirContext.
|
||||
*/
|
||||
protected DirContext getContinuationDirContext(Name n) throws NamingException {
|
||||
Object obj = lookup(n.get(0));
|
||||
CannotProceedException cpe = new CannotProceedException();
|
||||
cpe.setResolvedObj(obj);
|
||||
cpe.setEnvironment(myEnv);
|
||||
return DirectoryManager.getContinuationDirContext(cpe);
|
||||
}
|
||||
|
||||
|
||||
public Attributes getAttributes(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.getAttributes(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Attributes getAttributes(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return getAttributes(name.get(0));
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.getAttributes(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Attributes getAttributes(String name, String[] attrIds)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.getAttributes(res.getRemainingName(), attrIds);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Attributes getAttributes(Name name, String[] attrIds)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return getAttributes(name.get(0), attrIds);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.getAttributes(name.getSuffix(1), attrIds);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void modifyAttributes(String name, int mod_op, Attributes attrs)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
ctx.modifyAttributes(res.getRemainingName(), mod_op, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void modifyAttributes(Name name, int mod_op, Attributes attrs)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
modifyAttributes(name.get(0), mod_op, attrs);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
ctx.modifyAttributes(name.getSuffix(1), mod_op, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void modifyAttributes(String name, ModificationItem[] mods)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
ctx.modifyAttributes(res.getRemainingName(), mods);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void modifyAttributes(Name name, ModificationItem[] mods)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
modifyAttributes(name.get(0), mods);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
ctx.modifyAttributes(name.getSuffix(1), mods);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(String name, Object obj, Attributes attrs)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
ctx.bind(res.getRemainingName(), obj, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(Name name, Object obj, Attributes attrs)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
bind(name.get(0), obj, attrs);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
ctx.bind(name.getSuffix(1), obj, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void rebind(String name, Object obj, Attributes attrs)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
ctx.rebind(res.getRemainingName(), obj, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void rebind(Name name, Object obj, Attributes attrs)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
rebind(name.get(0), obj, attrs);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
ctx.rebind(name.getSuffix(1), obj, attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DirContext createSubcontext(String name, Attributes attrs)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.createSubcontext(res.getRemainingName(), attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public DirContext createSubcontext(Name name, Attributes attrs)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return createSubcontext(name.get(0), attrs);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.createSubcontext(name.getSuffix(1), attrs);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DirContext getSchema(String name) throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
return ctx.getSchema(res.getRemainingName());
|
||||
}
|
||||
|
||||
public DirContext getSchema(Name name) throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return getSchema(name.get(0));
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.getSchema(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DirContext getSchemaClassDefinition(String name)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.getSchemaClassDefinition(res.getRemainingName());
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public DirContext getSchemaClassDefinition(Name name)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return getSchemaClassDefinition(name.get(0));
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.getSchemaClassDefinition(name.getSuffix(1));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(String name,
|
||||
Attributes matchingAttributes)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.search(res.getRemainingName(), matchingAttributes);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(Name name,
|
||||
Attributes matchingAttributes)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return search(name.get(0), matchingAttributes);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.search(name.getSuffix(1), matchingAttributes);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(String name,
|
||||
Attributes matchingAttributes,
|
||||
String[] attributesToReturn)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.search(res.getRemainingName(),
|
||||
matchingAttributes, attributesToReturn);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(Name name,
|
||||
Attributes matchingAttributes,
|
||||
String[] attributesToReturn)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return search(name.get(0), matchingAttributes,
|
||||
attributesToReturn);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.search(name.getSuffix(1),
|
||||
matchingAttributes, attributesToReturn);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(String name,
|
||||
String filter,
|
||||
SearchControls cons)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return ctx.search(res.getRemainingName(), filter, cons);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(Name name,
|
||||
String filter,
|
||||
SearchControls cons)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return search(name.get(0), filter, cons);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.search(name.getSuffix(1), filter, cons);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(String name,
|
||||
String filterExpr,
|
||||
Object[] filterArgs,
|
||||
SearchControls cons)
|
||||
throws NamingException {
|
||||
ResolveResult res = getRootURLContext(name, myEnv);
|
||||
DirContext ctx = (DirContext)res.getResolvedObj();
|
||||
try {
|
||||
return
|
||||
ctx.search(res.getRemainingName(), filterExpr, filterArgs, cons);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> search(Name name,
|
||||
String filterExpr,
|
||||
Object[] filterArgs,
|
||||
SearchControls cons)
|
||||
throws NamingException {
|
||||
if (name.size() == 1) {
|
||||
return search(name.get(0), filterExpr, filterArgs, cons);
|
||||
} else {
|
||||
DirContext ctx = getContinuationDirContext(name);
|
||||
try {
|
||||
return ctx.search(name.getSuffix(1), filterExpr, filterArgs, cons);
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
525
jdkSrc/jdk8/com/sun/jndi/toolkit/url/Uri.java
Normal file
525
jdkSrc/jdk8/com/sun/jndi/toolkit/url/Uri.java
Normal file
@@ -0,0 +1,525 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.toolkit.url;
|
||||
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
|
||||
/**
|
||||
* A Uri object represents an absolute Uniform Resource Identifier
|
||||
* (URI) as defined by RFC 2396 and updated by RFC 2373 and RFC 2732.
|
||||
* The most commonly used form of URI is the Uniform Resource Locator (URL).
|
||||
*
|
||||
* <p> The java.net.URL class cannot be used to parse URIs since it
|
||||
* requires the installation of URL stream handlers that may not be
|
||||
* available.
|
||||
*
|
||||
* <p> The {@linkplain ParseMode#STRICT strict} parsing mode uses
|
||||
* the java.net.URI class to syntactically validate URI strings.
|
||||
* The {@linkplain ParseMode#COMPAT compat} mode validate the
|
||||
* URI authority and rejects URI fragments, but doesn't perform any
|
||||
* additional validation on path and query, other than that
|
||||
* which may be implemented in the concrete the Uri subclasses.
|
||||
* The {@linkplain ParseMode#LEGACY legacy} mode should not be
|
||||
* used unless the application is capable of validating all URI
|
||||
* strings before any constructors of this class is invoked.
|
||||
*
|
||||
* <p> The format of an absolute URI (see the RFCs mentioned above) is:
|
||||
* <p><blockquote><pre>
|
||||
* absoluteURI = scheme ":" ( hier_part | opaque_part )
|
||||
*
|
||||
* scheme = alpha *( alpha | digit | "+" | "-" | "." )
|
||||
*
|
||||
* hier_part = ( net_path | abs_path ) [ "?" query ]
|
||||
* opaque_part = uric_no_slash *uric
|
||||
*
|
||||
* net_path = "//" authority [ abs_path ]
|
||||
* abs_path = "/" path_segments
|
||||
*
|
||||
* authority = server | reg_name
|
||||
* reg_name = 1*( unreserved | escaped | "$" | "," |
|
||||
* ";" | ":" | "@" | "&" | "=" | "+" )
|
||||
* server = [ [ userinfo "@" ] hostport ]
|
||||
* userinfo = *( unreserved | escaped |
|
||||
* ";" | ":" | "&" | "=" | "+" | "$" | "," )
|
||||
*
|
||||
* hostport = host [ ":" port ]
|
||||
* host = hostname | IPv4address | IPv6reference
|
||||
* port = *digit
|
||||
*
|
||||
* IPv6reference = "[" IPv6address "]"
|
||||
* IPv6address = hexpart [ ":" IPv4address ]
|
||||
* IPv4address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit
|
||||
* hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
|
||||
* hexseq = hex4 *( ":" hex4)
|
||||
* hex4 = 1*4hex
|
||||
*
|
||||
* path = [ abs_path | opaque_part ]
|
||||
* path_segments = segment *( "/" segment )
|
||||
* segment = *pchar *( ";" param )
|
||||
* param = *pchar
|
||||
* pchar = unreserved | escaped |
|
||||
* ":" | "@" | "&" | "=" | "+" | "$" | ","
|
||||
*
|
||||
* query = *uric
|
||||
*
|
||||
* uric = reserved | unreserved | escaped
|
||||
* uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
|
||||
* "&" | "=" | "+" | "$" | ","
|
||||
* reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
|
||||
* "$" | "," | "[" | "]"
|
||||
* unreserved = alphanum | mark
|
||||
* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
|
||||
* escaped = "%" hex hex
|
||||
* unwise = "{" | "}" | "|" | "\" | "^" | "`"
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* <p> Currently URIs containing <tt>userinfo</tt> or <tt>reg_name</tt>
|
||||
* are not supported.
|
||||
* The <tt>opaque_part</tt> of a non-hierarchical URI is treated as if
|
||||
* if were a <tt>path</tt> without a leading slash.
|
||||
*/
|
||||
|
||||
|
||||
public class Uri {
|
||||
|
||||
// three parsing modes
|
||||
public enum ParseMode {
|
||||
/**
|
||||
* Strict validation mode.
|
||||
* Validate the URI syntactically using {@link java.net.URI}.
|
||||
* Rejects URI fragments unless explicitly supported by the
|
||||
* subclass.
|
||||
*/
|
||||
STRICT,
|
||||
/**
|
||||
* Compatibility mode. The URI authority is syntactically validated.
|
||||
* Rejects URI fragments unless explicitly supported by the
|
||||
* subclass.
|
||||
* This is the default.
|
||||
*/
|
||||
COMPAT,
|
||||
/**
|
||||
* Legacy mode. In this mode, no validation is performed.
|
||||
*/
|
||||
LEGACY
|
||||
}
|
||||
|
||||
protected String uri;
|
||||
protected String scheme;
|
||||
protected String host = null;
|
||||
protected int port = -1;
|
||||
protected boolean hasAuthority;
|
||||
protected String path;
|
||||
protected String query = null;
|
||||
protected String fragment;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a Uri object given a URI string.
|
||||
*/
|
||||
public Uri(String uri) throws MalformedURLException {
|
||||
init(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an uninitialized Uri object. The init() method must
|
||||
* be called before any other Uri methods.
|
||||
*/
|
||||
protected Uri() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The parse mode for parsing this URI.
|
||||
* The default is {@link ParseMode#COMPAT}.
|
||||
* @return the parse mode for parsing this URI.
|
||||
*/
|
||||
protected ParseMode parseMode() {
|
||||
return ParseMode.COMPAT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a Uri object given a URI string.
|
||||
* This method must be called exactly once, and before any other Uri
|
||||
* methods.
|
||||
*/
|
||||
protected void init(String uri) throws MalformedURLException {
|
||||
this.uri = uri;
|
||||
parse(uri, parseMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URI's scheme.
|
||||
*/
|
||||
public String getScheme() {
|
||||
return scheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host from the URI's authority part, or null
|
||||
* if no host is provided. If the host is an IPv6 literal, the
|
||||
* delimiting brackets are part of the returned value (see
|
||||
* {@link java.net.URI#getHost}).
|
||||
*/
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port from the URI's authority part, or -1 if
|
||||
* no port is provided.
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URI's path. The path is never null. Note that a
|
||||
* slash following the authority part (or the scheme if there is
|
||||
* no authority part) is part of the path. For example, the path
|
||||
* of "http://host/a/b" is "/a/b".
|
||||
*/
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URI's query part, or null if no query is provided.
|
||||
* Note that a query always begins with a leading "?".
|
||||
*/
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URI as a string.
|
||||
*/
|
||||
public String toString() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
private void parse(String uri, ParseMode mode) throws MalformedURLException {
|
||||
switch (mode) {
|
||||
case STRICT:
|
||||
parseStrict(uri);
|
||||
break;
|
||||
case COMPAT:
|
||||
parseCompat(uri);
|
||||
break;
|
||||
case LEGACY:
|
||||
parseLegacy(uri);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses a URI string and sets this object's fields accordingly.
|
||||
* Use java.net.URI to validate the uri string syntax
|
||||
*/
|
||||
private void parseStrict(String uri) throws MalformedURLException {
|
||||
try {
|
||||
if (!isSchemeOnly(uri)) {
|
||||
URI u = new URI(uri);
|
||||
scheme = u.getScheme();
|
||||
if (scheme == null) throw new MalformedURLException("Invalid URI: " + uri);
|
||||
String auth = u.getRawAuthority();
|
||||
hasAuthority = auth != null;
|
||||
if (hasAuthority) {
|
||||
String host = u.getHost();
|
||||
int port = u.getPort();
|
||||
if (host != null) this.host = host;
|
||||
if (port != -1) this.port = port;
|
||||
String hostport = (host == null ? "" : host)
|
||||
+ (port == -1 ? "" : (":" + port));
|
||||
if (!hostport.equals(auth)) {
|
||||
// throw if we have user info or regname
|
||||
throw new MalformedURLException("unsupported authority: " + auth);
|
||||
}
|
||||
}
|
||||
path = u.getRawPath();
|
||||
if (u.getRawQuery() != null) {
|
||||
query = "?" + u.getRawQuery();
|
||||
}
|
||||
if (u.getRawFragment() != null) {
|
||||
if (!acceptsFragment()) {
|
||||
throw new MalformedURLException("URI fragments not supported: " + uri);
|
||||
}
|
||||
fragment = "#" + u.getRawFragment();
|
||||
}
|
||||
} else {
|
||||
// scheme-only URIs are not supported by java.net.URI
|
||||
// validate the URI by appending "/" to the uri string.
|
||||
String s = uri.substring(0, uri.indexOf(':'));
|
||||
URI u = new URI(uri + "/");
|
||||
if (!s.equals(u.getScheme())
|
||||
|| !checkSchemeOnly(uri, u.getScheme())) {
|
||||
throw newInvalidURISchemeException(uri);
|
||||
}
|
||||
scheme = s;
|
||||
path = "";
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
MalformedURLException mue = new MalformedURLException(e.getMessage());
|
||||
mue.initCause(e);
|
||||
throw mue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Parses a URI string and sets this object's fields accordingly.
|
||||
* Compatibility mode. Use java.net.URI to validate the syntax of
|
||||
* the uri string authority.
|
||||
*/
|
||||
private void parseCompat(String uri) throws MalformedURLException {
|
||||
int i; // index into URI
|
||||
|
||||
i = uri.indexOf(':'); // parse scheme
|
||||
int slash = uri.indexOf('/');
|
||||
int qmark = uri.indexOf('?');
|
||||
int fmark = uri.indexOf('#');
|
||||
if (i < 0 || slash > 0 && i > slash || qmark > 0 && i > qmark || fmark > 0 && i > fmark) {
|
||||
throw new MalformedURLException("Invalid URI: " + uri);
|
||||
}
|
||||
if (fmark > -1) {
|
||||
if (!acceptsFragment()) {
|
||||
throw new MalformedURLException("URI fragments not supported: " + uri);
|
||||
}
|
||||
}
|
||||
if (i == uri.length() - 1) {
|
||||
if (!isSchemeOnly(uri)) {
|
||||
throw newInvalidURISchemeException(uri);
|
||||
}
|
||||
}
|
||||
scheme = uri.substring(0, i);
|
||||
i++; // skip past ":"
|
||||
|
||||
hasAuthority = uri.startsWith("//", i);
|
||||
if (fmark > -1 && qmark > fmark) qmark = -1;
|
||||
int endp = qmark > -1 ? qmark : fmark > -1 ? fmark : uri.length();
|
||||
if (hasAuthority) { // parse "//host:port"
|
||||
i += 2; // skip past "//"
|
||||
int starta = i;
|
||||
// authority ends at the first appearance of /, ?, or #
|
||||
int enda = uri.indexOf('/', i);
|
||||
if (enda == -1 || qmark > -1 && qmark < enda) enda = qmark;
|
||||
if (enda == -1 || fmark > -1 && fmark < enda) enda = fmark;
|
||||
if (enda < 0) {
|
||||
enda = uri.length();
|
||||
}
|
||||
if (uri.startsWith(":", i)) {
|
||||
// LdapURL supports empty host.
|
||||
i++;
|
||||
host = "";
|
||||
if (enda > i) {
|
||||
port = Integer.parseInt(uri.substring(i, enda));
|
||||
}
|
||||
} else {
|
||||
// Use URI to parse authority
|
||||
try {
|
||||
// URI requires at least one char after authority:
|
||||
// we use "/" and expect that the resulting URI path
|
||||
// will be exactly "/".
|
||||
URI u = new URI(uri.substring(0, enda) + "/");
|
||||
String auth = uri.substring(starta, enda);
|
||||
host = u.getHost();
|
||||
port = u.getPort();
|
||||
String p = u.getRawPath();
|
||||
String q = u.getRawQuery();
|
||||
String f = u.getRawFragment();
|
||||
String ui = u.getRawUserInfo();
|
||||
if (ui != null) {
|
||||
throw new MalformedURLException("user info not supported in authority: " + ui);
|
||||
}
|
||||
if (!"/".equals(p)) {
|
||||
throw new MalformedURLException("invalid authority: " + auth);
|
||||
}
|
||||
if (q != null) {
|
||||
throw new MalformedURLException("invalid trailing characters in authority: ?" + q);
|
||||
}
|
||||
if (f != null) {
|
||||
throw new MalformedURLException("invalid trailing characters in authority: #" + f);
|
||||
}
|
||||
String hostport = (host == null ? "" : host)
|
||||
+ (port == -1?"":(":" + port));
|
||||
if (!auth.equals(hostport)) {
|
||||
// throw if we have user info or regname
|
||||
throw new MalformedURLException("unsupported authority: " + auth);
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
MalformedURLException mue = new MalformedURLException(e.getMessage());
|
||||
mue.initCause(e);
|
||||
throw mue;
|
||||
}
|
||||
}
|
||||
i = enda;
|
||||
}
|
||||
path = uri.substring(i, endp);
|
||||
// look for query
|
||||
if (qmark > -1) {
|
||||
if (fmark > -1) {
|
||||
query = uri.substring(qmark, fmark);
|
||||
} else {
|
||||
query = uri.substring(qmark);
|
||||
}
|
||||
}
|
||||
if (fmark > -1) {
|
||||
fragment = uri.substring(fmark);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A subclass of {@code Uri} that supports scheme only
|
||||
* URIs can override this method and return true in the
|
||||
* case where the URI string is a scheme-only URI that
|
||||
* the subclass supports.
|
||||
* @implSpec
|
||||
* The default implementation of this method returns false,
|
||||
* always.
|
||||
* @param uri An URI string
|
||||
* @return if this is a scheme-only URI supported by the subclass
|
||||
*/
|
||||
protected boolean isSchemeOnly(String uri) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given uri string should be considered
|
||||
* as a scheme-only URI. For some protocols - e.g. DNS, we
|
||||
* might accept "dns://" as a valid URL denoting default DNS.
|
||||
* For others - we might only accept "scheme:".
|
||||
* @implSpec
|
||||
* The default implementation of this method returns true if
|
||||
* the URI is of the form {@code "<scheme>:"} with nothing
|
||||
* after the scheme delimiter.
|
||||
* @param uri the URI
|
||||
* @param scheme the scheme
|
||||
* @return true if the URI should be considered as a scheme-only
|
||||
* URI supported by this URI scheme.
|
||||
*/
|
||||
protected boolean checkSchemeOnly(String uri, String scheme) {
|
||||
return uri.equals(scheme + ":");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code MalformedURLException} to be thrown when the
|
||||
* URI scheme is not supported.
|
||||
*
|
||||
* @param uri the URI string
|
||||
* @return a {@link MalformedURLException}
|
||||
*/
|
||||
protected MalformedURLException newInvalidURISchemeException(String uri) {
|
||||
return new MalformedURLException("Invalid URI scheme: " + uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether fragments are supported.
|
||||
* @implSpec
|
||||
* The default implementation of this method retturns false, always.
|
||||
* @return true if fragments are supported.
|
||||
*/
|
||||
protected boolean acceptsFragment() {
|
||||
return parseMode() == ParseMode.LEGACY;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses a URI string and sets this object's fields accordingly.
|
||||
* Legacy parsing mode.
|
||||
*/
|
||||
private void parseLegacy(String uri) throws MalformedURLException {
|
||||
int i; // index into URI
|
||||
|
||||
i = uri.indexOf(':'); // parse scheme
|
||||
if (i < 0) {
|
||||
throw new MalformedURLException("Invalid URI: " + uri);
|
||||
}
|
||||
scheme = uri.substring(0, i);
|
||||
i++; // skip past ":"
|
||||
|
||||
hasAuthority = uri.startsWith("//", i);
|
||||
if (hasAuthority) { // parse "//host:port"
|
||||
i += 2; // skip past "//"
|
||||
int slash = uri.indexOf('/', i);
|
||||
if (slash < 0) {
|
||||
slash = uri.length();
|
||||
}
|
||||
if (uri.startsWith("[", i)) { // at IPv6 literal
|
||||
int brac = uri.indexOf(']', i + 1);
|
||||
if (brac < 0 || brac > slash) {
|
||||
throw new MalformedURLException("Invalid URI: " + uri);
|
||||
}
|
||||
host = uri.substring(i, brac + 1); // include brackets
|
||||
i = brac + 1; // skip past "[...]"
|
||||
} else { // at host name or IPv4
|
||||
int colon = uri.indexOf(':', i);
|
||||
int hostEnd = (colon < 0 || colon > slash)
|
||||
? slash
|
||||
: colon;
|
||||
if (i < hostEnd) {
|
||||
host = uri.substring(i, hostEnd);
|
||||
}
|
||||
i = hostEnd; // skip past host
|
||||
}
|
||||
|
||||
if ((i + 1 < slash) &&
|
||||
uri.startsWith(":", i)) { // parse port
|
||||
i++; // skip past ":"
|
||||
port = Integer.parseInt(uri.substring(i, slash));
|
||||
}
|
||||
i = slash; // skip to path
|
||||
}
|
||||
int qmark = uri.indexOf('?', i); // look for query
|
||||
if (qmark < 0) {
|
||||
path = uri.substring(i);
|
||||
} else {
|
||||
path = uri.substring(i, qmark);
|
||||
query = uri.substring(qmark);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Debug
|
||||
public static void main(String args[]) throws MalformedURLException {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Uri uri = new Uri(args[i]);
|
||||
|
||||
String h = (uri.getHost() != null) ? uri.getHost() : "";
|
||||
String p = (uri.getPort() != -1) ? (":" + uri.getPort()) : "";
|
||||
String a = uri.hasAuthority ? ("//" + h + p) : "";
|
||||
String q = (uri.getQuery() != null) ? uri.getQuery() : "";
|
||||
|
||||
String str = uri.getScheme() + ":" + a + uri.getPath() + q;
|
||||
if (! uri.toString().equals(str)) {
|
||||
System.out.println(str);
|
||||
}
|
||||
System.out.println(h);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
117
jdkSrc/jdk8/com/sun/jndi/toolkit/url/UrlUtil.java
Normal file
117
jdkSrc/jdk8/com/sun/jndi/toolkit/url/UrlUtil.java
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.toolkit.url;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
/**
|
||||
* Utilities for dealing with URLs.
|
||||
* @author Vincent Ryan
|
||||
*/
|
||||
|
||||
final public class UrlUtil {
|
||||
|
||||
// To prevent creation of this static class
|
||||
private UrlUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a URI string (according to RFC 2396).
|
||||
*/
|
||||
public static final String decode(String s) throws MalformedURLException {
|
||||
try {
|
||||
return decode(s, "8859_1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// ISO-Latin-1 should always be available?
|
||||
throw new MalformedURLException("ISO-Latin-1 decoder unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a URI string (according to RFC 2396).
|
||||
*
|
||||
* Three-character sequences '%xy', where 'xy' is the two-digit
|
||||
* hexadecimal representation of the lower 8-bits of a character,
|
||||
* are decoded into the character itself.
|
||||
*
|
||||
* The string is subsequently converted using the specified encoding
|
||||
*/
|
||||
public static final String decode(String s, String enc)
|
||||
throws MalformedURLException, UnsupportedEncodingException {
|
||||
try {
|
||||
return URLDecoder.decode(s, enc);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
|
||||
mue.initCause(iae);
|
||||
throw mue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a string for inclusion in a URI (according to RFC 2396).
|
||||
*
|
||||
* Unsafe characters are escaped by encoding them in three-character
|
||||
* sequences '%xy', where 'xy' is the two-digit hexadecimal representation
|
||||
* of the lower 8-bits of the character.
|
||||
*
|
||||
* The question mark '?' character is also escaped, as required by RFC 2255.
|
||||
*
|
||||
* The string is first converted to the specified encoding.
|
||||
* For LDAP (2255), the encoding must be UTF-8.
|
||||
*/
|
||||
public static final String encode(String s, String enc)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
byte[] bytes = s.getBytes(enc);
|
||||
int count = bytes.length;
|
||||
|
||||
/*
|
||||
* From RFC 2396:
|
||||
*
|
||||
* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
|
||||
* reserved = ";" | "/" | ":" | "?" | "@" | "&" | "=" | "+" | "$" | ","
|
||||
*/
|
||||
final String allowed = "=,+;.'-@&/$_()!~*:"; // '?' is omitted
|
||||
char[] buf = new char[3 * count];
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if ((bytes[i] >= 0x61 && bytes[i] <= 0x7A) || // a..z
|
||||
(bytes[i] >= 0x41 && bytes[i] <= 0x5A) || // A..Z
|
||||
(bytes[i] >= 0x30 && bytes[i] <= 0x39) || // 0..9
|
||||
(allowed.indexOf(bytes[i]) >= 0)) {
|
||||
buf[j++] = (char) bytes[i];
|
||||
} else {
|
||||
buf[j++] = '%';
|
||||
buf[j++] = Character.forDigit(0xF & (bytes[i] >>> 4), 16);
|
||||
buf[j++] = Character.forDigit(0xF & bytes[i], 16);
|
||||
}
|
||||
}
|
||||
return new String(buf, 0, j);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user