下面列出了java.net.URI#getRawFragment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static URI toURI(SmbFile file) {
URL url = file.getURL(); // may contain spaces in path
try {
// %-encode path, query and fragment...
URI uri = new URI(url.getProtocol(), null, url.getPath(), url.getQuery(), url.getRef());
// ... but do not %-encode authority (use authority from URL, everything else from URI)
StringBuilder sb = new StringBuilder();
sb.append(uri.getScheme()).append("://").append(url.getAuthority()).append(uri.getRawPath());
if (uri.getRawQuery() != null) {
sb.append('?').append(uri.getRawQuery());
}
if (uri.getRawFragment() != null) {
sb.append('#').append(uri.getRawFragment());
}
return new URI(sb.toString());
} catch (URISyntaxException e) {
// shouldn't happen
throw new JetelRuntimeException(e);
}
}
public URIBuilder digestURI(final URI uri) {
this.scheme = uri.getScheme();
this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
this.encodedAuthority = uri.getRawAuthority();
this.host = uri.getHost();
this.port = uri.getPort();
this.encodedUserInfo = uri.getRawUserInfo();
this.userInfo = uri.getUserInfo();
this.encodedPath = uri.getRawPath();
this.path = uri.getPath();
this.encodedQuery = uri.getRawQuery();
this.queryParams = parseQuery(uri.getRawQuery());
this.encodedFragment = uri.getRawFragment();
this.fragment = uri.getFragment();
return this;
}
public static URI applyToUri(URI uri, DiscoveryInstance instance)
{
if ( instance != null )
{
try
{
String scheme = instance.isForceSsl() ? "https" : ((uri.getScheme() != null) ? uri.getScheme() : "http");
return new URI(scheme, uri.getUserInfo(), instance.getHost(), instance.getPort(), uri.getRawPath(), uri.getRawQuery(), uri.getRawFragment());
}
catch ( URISyntaxException e )
{
log.error("Could not parse uri", e);
throw new RuntimeException(e);
}
}
return uri;
}
private static boolean hasIllegalComponents(URI moduleName) {
// All components except for 'path' must be empty.
if (moduleName.getScheme() != null) {
return true;
}
if (moduleName.getRawAuthority() != null) {
return true;
}
if (moduleName.getRawUserInfo() != null) {
return true;
}
if (moduleName.getHost() != null) {
return true;
}
if (moduleName.getPort() != -1) {
return true;
}
if (moduleName.getRawQuery() != null) {
return true;
}
if (moduleName.getRawFragment() != null) {
return true;
}
return false;
}
/**
* Normalize the given openid as a standard openid
*/
public static String normalize(String openID) {
openID = openID.trim();
if (!openID.startsWith("http://") && !openID.startsWith("https://")) {
openID = "http://" + openID;
}
try {
URI url = new URI(openID);
String frag = url.getRawFragment();
if (frag != null && frag.length() > 0) {
openID = openID.replace("#" + frag, "");
}
if (url.getPath().equals("")) {
openID += "/";
}
openID = new URI(openID).toString();
} catch (Exception e) {
throw new RuntimeException(openID + " is not a valid URL");
}
return openID;
}
private static void checkURI(URI uri) {
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("URI is not absolute: " + uri);
} else if (!uri.getRawSchemeSpecificPart().startsWith("/")) {
throw new IllegalArgumentException("URI is not hierarchical: " + uri);
}
if (!"file".equals(uri.getScheme())) {
throw new IllegalArgumentException("Expected file scheme in URI: " + uri);
}
String rawPath = uri.getRawPath();
if (rawPath == null || rawPath.isEmpty()) {
throw new IllegalArgumentException("Expected non-empty path in URI: " + uri);
}
if (uri.getRawAuthority() != null) {
throw new IllegalArgumentException("Found authority in URI: " + uri);
}
if (uri.getRawQuery() != null) {
throw new IllegalArgumentException("Found query in URI: " + uri);
}
if (uri.getRawFragment() != null) {
throw new IllegalArgumentException("Found fragment in URI: " + uri);
}
}
/**
* Appends a path to a URI correctly using the given separator.
*
* @param uri
* The base Uri.
* @param relativeUri
* The relative URI.
* @param separator
* the separator to use.
* @return The appended Uri.
* @throws URISyntaxException
* a valid Uri cannot be constructed
*/
public static URI appendPathToSingleUri(final URI uri, final String relativeUri, final String separator)
throws URISyntaxException {
if (uri == null) {
return null;
}
if (relativeUri == null || relativeUri.isEmpty()) {
return uri;
}
if (uri.getPath().length() == 0 && relativeUri.startsWith(separator)) {
return new URI(uri.getScheme(), uri.getAuthority(), relativeUri, uri.getRawQuery(), uri.getRawFragment());
}
final StringBuilder pathString = new StringBuilder(uri.getPath());
if (uri.getPath().endsWith(separator)) {
pathString.append(relativeUri);
}
else {
pathString.append(separator);
pathString.append(relativeUri);
}
return new URI(uri.getScheme(), uri.getAuthority(), pathString.toString(), uri.getQuery(), uri.getFragment());
}
private URI createUri(URI uri, String queryString) throws URISyntaxException {
String queryPart = queryString == null ? "" : "?" + queryString; //$NON-NLS-1$ //$NON-NLS-2$
String fragmentPart = uri.getFragment() == null ? "" : "#" + uri.getRawFragment();//$NON-NLS-1$ //$NON-NLS-2$
URI pathonly = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
StringBuilder buf = new StringBuilder();
buf.append(pathonly.toString()).append(queryPart).append(fragmentPart);
return new URI(buf.toString());
}
public static URL parse(URI uri) throws MalformedURLException {
String protocol = uri.getScheme();
if (protocol == null || !protocol.matches("^https?|wss?$")) {
protocol = "https";
}
int port = uri.getPort();
if (port == -1) {
if (PATTERN_HTTP.matcher(protocol).matches()) {
port = 80;
} else if (PATTERN_HTTPS.matcher(protocol).matches()) {
port = 443;
}
}
String path = uri.getRawPath();
if (path == null || path.length() == 0) {
path = "/";
}
String userInfo = uri.getRawUserInfo();
String query = uri.getRawQuery();
String fragment = uri.getRawFragment();
return new URL(protocol + "://"
+ (userInfo != null ? userInfo + "@" : "")
+ uri.getHost()
+ (port != -1 ? ":" + port : "")
+ path
+ (query != null ? "?" + query : "")
+ (fragment != null ? "#" + fragment : ""));
}
private void checkUri(URI uri) {
if (!uri.getScheme().equalsIgnoreCase(getScheme()))
throw new IllegalArgumentException("URI does not match this provider");
if (uri.getRawAuthority() != null)
throw new IllegalArgumentException("Authority component present");
String path = uri.getPath();
if (path == null)
throw new IllegalArgumentException("Path component is undefined");
if (!path.equals("/"))
throw new IllegalArgumentException("Path component should be '/'");
if (uri.getRawQuery() != null)
throw new IllegalArgumentException("Query component present");
if (uri.getRawFragment() != null)
throw new IllegalArgumentException("Fragment component present");
}
/**
* Constructs from a URI.
*
* @param uri URI
* @param verbatim flag, to specify if URL should be used as is (without encoding, decoding and
* escaping)
*/
public GenericUrl(URI uri, boolean verbatim) {
this(
uri.getScheme(),
uri.getHost(),
uri.getPort(),
uri.getRawPath(),
uri.getRawFragment(),
uri.getRawQuery(),
uri.getRawUserInfo(),
verbatim);
}
public static String toRelativeURI(URI uri) {
StringBuilder sb = new StringBuilder();
if (uri.getRawPath() != null)
sb.append(uri.getRawPath());
if (uri.getRawQuery() != null) {
sb.append('?');
sb.append(uri.getRawQuery());
}
if (uri.getRawFragment() != null) {
sb.append('#');
sb.append(uri.getRawFragment());
}
return sb.toString();
}
private void digestURI(final URI uri) {
this.scheme = uri.getScheme();
this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
this.encodedAuthority = uri.getRawAuthority();
this.host = uri.getHost();
this.port = uri.getPort();
this.encodedUserInfo = uri.getRawUserInfo();
this.userInfo = uri.getUserInfo();
this.encodedPath = uri.getRawPath();
this.path = uri.getPath();
this.encodedQuery = uri.getRawQuery();
this.queryParams = parseQuery(uri.getRawQuery());
this.encodedFragment = uri.getRawFragment();
this.fragment = uri.getFragment();
}
private static boolean isValidOrigin(URI origin) {
return origin != null
&& origin.getScheme() != null
&& origin.getHost() != null
&& isBlank(origin.getRawPath())
&& origin.getRawQuery() == null
&& origin.getRawFragment() == null
&& origin.getRawUserInfo() == null;
}
/**
* The URL string that is returned will have '/' in the end
*/
public static String getLocationWithoutParams(URI location) {
try {
StringBuilder baseUrl = new StringBuilder(location.toURL().toExternalForm());
if (location.getQuery() != null) {
baseUrl.delete(baseUrl.indexOf("?" + location.getQuery()), baseUrl.length());
} else if (location.getRawFragment() != null) {
baseUrl.delete(baseUrl.indexOf("#" + location.getRawFragment()), baseUrl.length());
}
String baseUrlString = baseUrl.toString();
return baseUrlString.endsWith("/") ? baseUrlString : baseUrlString + "/";
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to get location without params", e);
}
}
public static URL parse(URI uri) throws MalformedURLException {
String protocol = uri.getScheme();
if (protocol == null || !protocol.matches("^https?|wss?$")) {
protocol = "https";
}
int port = uri.getPort();
if (port == -1) {
if (PATTERN_HTTP.matcher(protocol).matches()) {
port = 80;
} else if (PATTERN_HTTPS.matcher(protocol).matches()) {
port = 443;
}
}
String path = uri.getRawPath();
if (path == null || path.length() == 0) {
path = "/";
}
String userInfo = uri.getRawUserInfo();
String query = uri.getRawQuery();
String fragment = uri.getRawFragment();
return new URL(protocol + "://"
+ (userInfo != null ? userInfo + "@" : "")
+ uri.getHost()
+ (port != -1 ? ":" + port : "")
+ path
+ (query != null ? "?" + query : "")
+ (fragment != null ? "#" + fragment : ""));
}
/**
* Read fragment part from the given URI.
* @param link The link from which the fragment needs to be returned.
* @return Opt with fragment or empty if there is no fragment.
*/
private static Opt<String> readFragment(final URI link) {
final Opt<String> fragment;
if (link.getRawFragment() == null) {
fragment = new Opt.Empty<>();
} else {
fragment = new Opt.Single<>(link.getRawFragment());
}
return fragment;
}
/**
*
* @param urlString
* @param username
* @param password value is cloned, if you want to null the field, call {@link #clearPassword()}
* @throws URISyntaxException
*/
public HgURL(String urlString, String username, char[] password) throws URISyntaxException {
URI originalUri;
if (urlString == null) {
throw new IllegalArgumentException("<null> URL string"); //NOI18N
}
if (urlString.length() == 0) {
throw new IllegalArgumentException("empty URL string"); //NOI18N
}
if (looksLikePlainFilePath(urlString)) {
originalUri = new File(urlString).toURI();
scheme = Scheme.FILE;
} else {
originalUri = new URI(urlString).parseServerAuthority();
String originalScheme = originalUri.getScheme();
scheme = (originalScheme != null) ? determineScheme(originalScheme)
: null;
}
if (scheme == null) {
throw new URISyntaxException(
urlString,
NbBundle.getMessage(HgURL.class,
"MSG_UNSUPPORTED_PROTOCOL", //NOI18N
originalUri.getScheme()));
}
verifyUserInfoData(scheme, username, password);
if (username != null) {
this.username = username;
this.password = password == null ? null : (char[])password.clone();
} else {
String rawUserInfo = originalUri.getRawUserInfo();
if (rawUserInfo == null) {
this.username = null;
this.password = null;
} else {
int colonIndex = rawUserInfo.indexOf(':');
if (colonIndex == -1) {
this.username = rawUserInfo;
this.password = null;
} else {
this.username = rawUserInfo.substring(0, colonIndex);
this.password = rawUserInfo.substring(colonIndex + 1).toCharArray();
}
}
}
host = originalUri.getHost();
port = originalUri.getPort();
rawPath = originalUri.getRawPath();
rawQuery = originalUri.getRawQuery();
rawFragment = originalUri.getRawFragment();
path = originalUri.getPath();
}
/**
* Add query parameter to an existing Uri. This takes care of any existing query parameters in the Uri.
*
* @param uri
* the original uri.
* @return the appended uri
* @throws URISyntaxException
* if the resulting uri is invalid.
* @throws StorageException
*/
public URI addToURI(final URI uri) throws URISyntaxException, StorageException {
final String origRawQuery = uri.getRawQuery();
final String rawFragment = uri.getRawFragment();
final String uriString = uri.resolve(uri).toASCIIString();
final HashMap<String, String[]> origQueryMap = PathUtility.parseQueryString(origRawQuery);
// Try/Insert original queries to map
for (final Entry<String, String[]> entry : origQueryMap.entrySet()) {
for (final String val : entry.getValue()) {
this.insertKeyValue(entry.getKey(), val);
}
}
final StringBuilder retBuilder = new StringBuilder();
// has a fragment
if (Utility.isNullOrEmpty(origRawQuery) && !Utility.isNullOrEmpty(rawFragment)) {
final int bangDex = uriString.indexOf('#');
retBuilder.append(uriString.substring(0, bangDex));
}
else if (!Utility.isNullOrEmpty(origRawQuery)) {
// has a query
final int queryDex = uriString.indexOf('?');
retBuilder.append(uriString.substring(0, queryDex));
}
else {
// no fragment or query
retBuilder.append(uriString);
if (uri.getRawPath().length() <= 0) {
retBuilder.append("/");
}
}
final String finalQuery = this.toString();
if (finalQuery.length() > 0) {
retBuilder.append("?");
retBuilder.append(finalQuery);
}
if (!Utility.isNullOrEmpty(rawFragment)) {
retBuilder.append("#");
retBuilder.append(rawFragment);
}
return new URI(retBuilder.toString());
}
/**
* Converts given URI to a Path
*/
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getRawFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getRawQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String path = uri.getPath();
if (path.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// UNC
String auth = uri.getRawAuthority();
if (auth != null && !auth.equals("")) {
String host = uri.getHost();
if (host == null)
throw new IllegalArgumentException("URI authority component has undefined host");
if (uri.getUserInfo() != null)
throw new IllegalArgumentException("URI authority component has user-info");
if (uri.getPort() != -1)
throw new IllegalArgumentException("URI authority component has port number");
// IPv6 literal
// 1. drop enclosing brackets
// 2. replace ":" with "-"
// 3. replace "%" with "s" (zone/scopeID delimiter)
// 4. Append .ivp6-literal.net
if (host.startsWith("[")) {
host = host.substring(1, host.length()-1)
.replace(':', '-')
.replace('%', 's');
host += IPV6_LITERAL_SUFFIX;
}
// reconstitute the UNC
path = "\\\\" + host + path;
} else {
if ((path.length() > 2) && (path.charAt(2) == ':')) {
// "/c:/foo" --> "c:/foo"
path = path.substring(1);
}
}
return WindowsPath.parse(fs, path);
}