下面列出了java.net.URI#resolve ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testURIResolve() throws Exception {
URI base1 = new URI("http://localhost:8080/jvxml/irtests");
URI base2 = new URI("http://localhost:8080/jvxml/irtests/");
Assert.assertNotSame(base1.toString(), base2.toString());
URI a1, a2;
a1 = base1.resolve("a");
a2 = base2.resolve("a");
Assert.assertEquals("http://localhost:8080/jvxml/a", a1.toString());
Assert.assertEquals("http://localhost:8080/jvxml/irtests/a", a2
.toString());
a1 = base1.resolve("/a");
a2 = base2.resolve("/a");
Assert.assertEquals("http://localhost:8080/a", a1.toString());
Assert.assertEquals("http://localhost:8080/a", a2.toString());
a1 = base1.resolve(".");
a2 = base2.resolve(".");
Assert.assertEquals("http://localhost:8080/jvxml/", a1.toString());
Assert.assertEquals("http://localhost:8080/jvxml/irtests/", a2
.toString());
}
private @Nullable URL getRelativeUrl(Path path) {
// Resolving a Path against a URL is surprisingly tricky, due to character escaping issues.
// The safest approach seems to be appending the path components one at a time, wrapping
// each one in a URI to ensure that the filename is properly escaped. Trying to append the
// entire thing at once either fails to escape illegal chars at all, or escapes characters
// that shouldn't be, like the path seperator.
try {
URL url = source.getUrl();
if(url == null) return null;
URI uri = url.toURI();
if(uri.getPath() == null || "".equals(uri.getPath())) {
uri = uri.resolve("/");
}
Path dir = Files.isDirectory(source.getPath().resolve(path)) ? path : path.getParent();
if(dir == null) return null;
for(Path part : dir) {
uri = uri.resolve(new URI(null, null, part.toString() + "/", null));
}
if(path != dir) {
uri = uri.resolve(new URI(null, null, path.getFileName().toString(), null));
}
return uri.toURL();
} catch(MalformedURLException | URISyntaxException e) {
return null;
}
}
private boolean readCompositeContent(URI repoUri, String name, P2Descriptor p2Descriptor)
throws IOException, ParseException, SAXException {
P2CompositeParser p2CompositeParser = new P2CompositeParser();
boolean exist = readJarOrXml(repoUri, name, p2CompositeParser);
if (exist) {
for (String childLocation : p2CompositeParser.getChildLocations()) {
if (!childLocation.endsWith("/")) {
childLocation += "/";
}
URI childUri = repoUri.resolve(childLocation);
readContent(childUri, p2Descriptor);
}
}
return exist;
}
public void testRfc1808AbnormalExampleRelativeScheme() throws Exception {
URI base = new URI("http://a/b/c/d;p?q");
URI uri = base.resolve("http:g");
assertEquals("http:g", uri.toString()); // this is an opaque URI
assertEquals(true, uri.isOpaque());
assertEquals(true, uri.isAbsolute());
}
private void useSuitableCatalogFile(ModelSource modelSourceOfSourceDocument) {
// if the modelSource's project has XMLCatalogProvider then use that to
// see which catalog file to use for this modelSource
if(modelSourceOfSourceDocument != null){
FileObject msfo = (FileObject) modelSourceOfSourceDocument.getLookup().
lookup(FileObject.class);
if(msfo == null)
return;
Project prj = FileOwnerQuery.getOwner(msfo);
if(prj == null)
return;
XMLCatalogProvider catPovider = (XMLCatalogProvider) prj.getLookup().
lookup(XMLCatalogProvider.class);
if(catPovider == null)
return;
URI caturi = catPovider.getCatalog(msfo);
if(caturi == null)
return;
URI prjuri = FileUtil.toFile(prj.getProjectDirectory()).toURI();
URI catFileURI = prjuri.resolve(caturi);
if(catFileURI == null)
return;
File catFile = new File(catFileURI);
if(!catFile.isFile()){
try {
catFile.createNewFile();
} catch (IOException ex) {
return;
}
}
FileObject catFO = FileUtil.toFileObject(FileUtil.normalizeFile(catFile));
if(catFO == null)
return;
//assign new catalog file that needs to be used for resolution
this.catalogFileObject = catFO;
}
}
public static void main(String[] args) {
try {
URI one = new URI("Relative%20with%20spaces");
URI two = (new File("/tmp/dir with spaces/File with spaces")).toURI();
URI three = two.resolve(one);
if (!three.getSchemeSpecificPart().equals(three.getPath()))
throw new RuntimeException("Bad encoding on URI.resolve");
} catch (URISyntaxException e) {
throw new RuntimeException("Unexpected exception: " + e);
}
}
public static URIDescriptor create(URI target) {
if(target==null) {
throw new NullPointerException("URI cannot be null");
}
if(target.isOpaque()) {
throw new IllegalArgumentException("URI must be hierarchical");
}
URI targetDir = target.resolve(".");
String targetFile=target.getPath().substring(targetDir.getPath().length());
String targetQuery = target.getQuery();
String targetFragment=target.getFragment();
return new URIDescriptor(target,targetDir.getPath(),targetFile,targetQuery,targetFragment);
}
public static void main(String[] args) {
try {
URI one = new URI("Relative%20with%20spaces");
URI two = (new File("/tmp/dir with spaces/File with spaces")).toURI();
URI three = two.resolve(one);
if (!three.getSchemeSpecificPart().equals(three.getPath()))
throw new RuntimeException("Bad encoding on URI.resolve");
} catch (URISyntaxException e) {
throw new RuntimeException("Unexpected exception: " + e);
}
}
public URI relativePath(URI base, URI uri) {
URI root = base.resolve("/");
if (!root.equals(uri.resolve("/")))
// Different protocol/host/auth
return uri;
base = base.normalize();
uri = uri.normalize();
if (base.resolve("#").equals(uri.resolve("#")))
// Same path, easy
return base.relativize(uri);
if (base.isAbsolute()) {
// Ignore hostname and protocol
base = root.relativize(base).resolve(".");
uri = root.relativize(uri);
}
// Pretend they start from /
base = root.resolve(base).resolve(".");
uri = root.resolve(uri);
URI candidate = base.relativize(uri);
URI relation = DOT;
while (candidate.getPath().startsWith("/")
&& !(base.getPath().isEmpty() || base.getPath().equals("/"))) {
base = base.resolve("../");
relation = relation.resolve("../");
candidate = base.relativize(uri);
}
// Add the ../.. again
URI resolved = relation.resolve(candidate);
return resolved;
}
@Override
public void submitForm(HtmlView htmlView, Element form, URI uri, boolean post,
List<Map.Entry<String, String>> formData) {
if (log) {
Log.d(LOG_TAG, "onSubmitForm " + form + " " + HtmlUtils.toString(uri) + " " + formData);
}
StringBuilder sb = new StringBuilder();
try {
for (Map.Entry<String, String> entry: formData) {
if (sb.length() > 0) {
sb.append('&');
}
// TODO(haustein): ASCII-Encode for safety?
sb.append(URLEncoder.encode(entry.getKey(), HtmlUtils.UTF8));
sb.append('=');
sb.append(URLEncoder.encode(entry.getValue(), HtmlUtils.UTF8));
}
byte[] postData = null;
if (post) {
postData = sb.toString().getBytes(HtmlUtils.UTF8);
} else {
uri = uri.resolve("?" + sb.toString());
}
htmlView.loadAsync(uri, postData, Onload.SHOW_HTML);
} catch(UnsupportedEncodingException e) {
// Should be impossible, as UTF8 is mandatory
Log.e(LOG_TAG, "Error encoding form data", e);
}
}
public static void badProfileDesignator(String value, Location location, VerifierContext context, URI ttmlProfileNamespaceUri, Profile.Type profileType, Set<URI> designators) {
Reporter reporter = context.getReporter();
Locator locator = location.getLocator();
Matcher m = badDelimiterSuffixPattern.matcher(value);
if (m.matches()) {
String suffix = m.group(1);
reporter.logInfo(reporter.message(locator, "*KEY*",
"Bad profile designator ''{0}'' ends with unexpected delimiter suffix ''{1}''.", value, suffix));
} else {
try {
URI uri = new URI(value);
if (!uri.isAbsolute())
uri = ttmlProfileNamespaceUri.resolve(uri);
if (!designators.contains(uri)) {
String s = uri.toString();
if (s.indexOf(ttmlProfileNamespaceUri.toString()) == 0) {
reporter.logInfo(reporter.message(locator, "*KEY*",
"Bad profile designator, unrecognized designator ''{0}'' in TT Profile Namespace.", value));
} else {
reporter.logInfo(reporter.message(locator, "*KEY*",
"Bad profile designator, unrecognized designator ''{0}'' in Other Profile Namespace.", value));
}
}
} catch (URISyntaxException e) {
reporter.logInfo(reporter.message(locator, "*KEY*",
"Bad profile designator ''{0}'', invalid designator syntax.", value));
}
}
}
@Override
public VideoInfo getLiveVideoInfo(URI videoInfoUrl, ChannelInfo channelInfo,String resolution) throws Exception {
if (videoInfoUrl == null) {
return null;
}
String videoId = videoInfoUrl.toString().substring(GET_LIVE_INFO_URL.length());
String liveDetailJson = HttpRequestUtil.downloadUrl(new URI("https://www.mirrativ.com/api/live/live?live_id=" + videoId), channelInfo != null ? channelInfo.getCookies() : null, Collections.emptyMap(), StandardCharsets.UTF_8);
JSONObject liveDetailObj = JSON.parseObject(liveDetailJson);
String videoTitle = liveDetailObj.getString("title");
URI m3u8ListUrl = new URI(liveDetailObj.getString("streaming_url_hls"));
String[] m3u8List = HttpRequestUtil.downloadUrl(m3u8ListUrl, StandardCharsets.UTF_8).split("\n");
String mediaUrl = m3u8List[3];
return new VideoInfo(channelInfo, videoId, videoTitle, videoInfoUrl, m3u8ListUrl.resolve(mediaUrl), "m3u8");
}
public static String buildUrlPath(String baseUrl, String childUrl) {
try {
URI oldUri = new URI(baseUrl);
URI resolved = oldUri.resolve(childUrl);
return resolved.toString();
} catch (URISyntaxException e) {
e.printStackTrace();
return baseUrl;
}
}
/**
* <p>
* Replace all JSON references, as per the http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
* specification, by their referants.
* </p>
* @param json
* @param duplicate
* @param done
* @return
*/
static Json expandReferences(Json json,
Json topdoc,
URI base,
Map<String, Json> resolved,
Map<Json, Json> expanded,
Function<URI, Json> uriResolver) throws Exception
{
if (expanded.containsKey(json)) return json;
if (json.isObject())
{
if (json.has("id") && json.at("id").isString()) // change scope of nest references
{
base = base.resolve(json.at("id").asString());
}
if (json.has("$ref"))
{
URI refuri = makeAbsolute(base, json.at("$ref").asString()); // base.resolve(json.at("$ref").asString());
Json ref = resolved.get(refuri.toString());
if (ref == null)
{
ref = resolveRef(base, topdoc, refuri, resolved, expanded, uriResolver);
resolved.put(refuri.toString(), ref);
ref = expandReferences(ref, topdoc, base, resolved, expanded, uriResolver);
resolved.put(refuri.toString(), ref);
}
json = ref;
}
else
{
Json O = Json.object();
for (Map.Entry<String, Json> e : json.asJsonMap().entrySet())
O.set(e.getKey(), expandReferences(e.getValue(), topdoc, base, resolved, expanded, uriResolver));
json.with(O, new Json[0]);
}
}
else if (json.isArray())
{
// Json A = Json.array();
for (int i = 0; i < json.asJsonList().size(); i++)
{
//A.add(expandReferences(j, topdoc, base, resolved));
Json el = expandReferences(json.at(i), topdoc, base, resolved, expanded, uriResolver);
json.set(i, el);
}
// return A;
}
expanded.put(json, json);
return json;
}
@Override
protected ServerStatus _doIt() {
try {
/* read deploy parameters */
JSONObject appMetadata = null;
JSONObject appEntity = null;
URI targetURI = URIUtil.toURI(target.getUrl());
/* get application details */
String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url"); //$NON-NLS-1$//$NON-NLS-2$
URI appsURI = targetURI.resolve(appsUrl);
GetMethod getAppsMethod = new GetMethod(appsURI.toString());
ServerStatus confStatus = HttpUtil.configureHttpMethod(getAppsMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
getAppsMethod.setQueryString("q=name:" + appName + "&inline-relations-depth=1"); //$NON-NLS-1$ //$NON-NLS-2$
ServerStatus appsStatus = HttpUtil.executeMethod(getAppsMethod);
if (!appsStatus.isOK())
return appsStatus;
JSONObject apps = appsStatus.getJsonData();
if (!apps.has("resources") || apps.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$
return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null);
appMetadata = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("metadata"); //$NON-NLS-1$ //$NON-NLS-2$
appEntity = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("entity"); //$NON-NLS-1$ //$NON-NLS-2$
if (application.getGuid() == null) {
String summaryAppUrl = appMetadata.getString("url") + "/summary"; //$NON-NLS-1$ //$NON-NLS-2$
URI summaryAppURI = targetURI.resolve(summaryAppUrl);
GetMethod getSummaryMethod = new GetMethod(summaryAppURI.toString());
confStatus = HttpUtil.configureHttpMethod(getSummaryMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
ServerStatus getStatus = HttpUtil.executeMethod(getSummaryMethod);
if (!getStatus.isOK())
return getStatus;
JSONObject summaryJSON = getStatus.getJsonData();
/* set known application GUID */
application.setGuid(summaryJSON.getString(CFProtocolConstants.V2_KEY_GUID));
}
/* gather application service bindings */
ArrayList<String> serviceInstances = new ArrayList<String>();
JSONArray appServiceBindings = appEntity.getJSONArray("service_bindings"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
for (int i = 0; i < appServiceBindings.length(); ++i) {
JSONObject binding = appServiceBindings.getJSONObject(i).getJSONObject("entity"); //$NON-NLS-1$
serviceInstances.add(binding.getString("service_instance_url")); //$NON-NLS-1$
}
/* delete the application */
URI appURI = targetURI.resolve("/v2/apps/" + application.getGuid()); //$NON-NLS-1$
DeleteMethod deleteAppMethod = new DeleteMethod(appURI.toString());
confStatus = HttpUtil.configureHttpMethod(deleteAppMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
deleteAppMethod.setQueryString("recursive=true"); //$NON-NLS-1$
ServerStatus status = HttpUtil.executeMethod(deleteAppMethod);
GetAppCommand.expire(target, application.getName());
return status;
} catch (Exception e) {
String msg = NLS.bind("An error occured when performing operation {0}", commandName);
logger.error(msg, e);
return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
}
}
static URI withArtifactPath(URI baseUrl, ArtifactId id) {
List<String> parts = new ArrayList<>(List.of(id.groupId().split("\\.")));
parts.add(id.artifactId());
parts.add("maven-metadata.xml");
return baseUrl.resolve(String.join("/", parts));
}
@Override
protected URI getSibling(URI basePath, String sibling) {
return basePath.resolve(sibling);
}
@Test
public void failedDirectoryDelete_noLogFile_checkSucceeds() throws Exception {
String bucketName = gcsfsIHelper.createUniqueBucket("coop-delete-check-no-log-failed");
URI bucketUri = new URI("gs://" + bucketName + "/");
String fileName = "file";
URI dirUri = bucketUri.resolve("delete_" + UUID.randomUUID() + "/");
// create file to delete
gcsfsIHelper.writeTextFile(bucketName, dirUri.resolve(fileName).getPath(), "file_content");
GoogleCloudStorageFileSystemOptions gcsFsOptions = newGcsFsOptions();
failDeleteOperation(gcsFsOptions, bucketName, dirUri);
GoogleCloudStorageFileSystem gcsFs = newGcsFs(gcsFsOptions, httpRequestInitializer);
// delete operation log file
List<URI> logFile =
gcsFs.listFileInfo(bucketUri.resolve(LOCK_DIRECTORY)).stream()
.map(FileInfo::getPath)
.filter(p -> p.toString().endsWith(".log"))
.collect(toImmutableList());
gcsFs.delete(Iterables.getOnlyElement(logFile), /* recursive */ false);
assertThat(gcsFs.exists(dirUri)).isTrue();
assertThat(gcsFs.exists(dirUri.resolve(fileName))).isTrue();
CoopLockFsck fsck = new CoopLockFsck();
fsck.setConf(getTestConfiguration());
fsck.run(new String[] {"--check", "gs://" + bucketName});
assertThat(gcsFs.exists(dirUri)).isTrue();
assertThat(gcsFs.exists(dirUri.resolve(fileName))).isTrue();
// Validate lock files
List<URI> lockFiles =
gcsFs.listFileInfo(bucketUri.resolve(LOCK_DIRECTORY)).stream()
.map(FileInfo::getPath)
.collect(toList());
assertThat(lockFiles).hasSize(2);
assertThat(matchFile(lockFiles, "all\\.lock")).isNotNull();
String filenamePattern = String.format(OPERATION_FILENAME_PATTERN_FORMAT, DELETE);
assertThat(matchFile(lockFiles, filenamePattern + "\\.log")).isEmpty();
}
@Override
protected ServerStatus _doIt() {
MultiServerStatus status = new MultiServerStatus();
try {
URI targetURI = URIUtil.toURI(target.getUrl());
// get app details
// TODO: it should be passed along with App object
String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url"); //$NON-NLS-1$//$NON-NLS-2$
URI appsURI = targetURI.resolve(appsUrl);
GetMethod getAppsMethod = new GetMethod(appsURI.toString());
ServerStatus confStatus = HttpUtil.configureHttpMethod(getAppsMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
getAppsMethod.setQueryString("q=name:" + appName + "&inline-relations-depth=1"); //$NON-NLS-1$ //$NON-NLS-2$
ServerStatus appsStatus = HttpUtil.executeMethod(getAppsMethod);
status.add(appsStatus);
if (!status.isOK())
return status;
JSONObject jsonData = appsStatus.getJsonData();
if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$
return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null);
JSONArray apps = jsonData.getJSONArray("resources");
// get app routes
String routesUrl = apps.getJSONObject(0).getJSONObject("entity").getString("routes_url");
URI routesURI = targetURI.resolve(routesUrl);
GetMethod getRoutesMethod = new GetMethod(routesURI.toString());
confStatus = HttpUtil.configureHttpMethod(getRoutesMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
ServerStatus routesStatus = HttpUtil.executeMethod(getRoutesMethod);
status.add(routesStatus);
if (!status.isOK())
return status;
jsonData = routesStatus.getJsonData();
if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$
return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, "No routes for the app", null);
JSONArray routes = jsonData.getJSONArray("resources");
for (int i = 0; i < routes.length(); ++i) {
JSONObject route = routes.getJSONObject(i);
// delete route
String routeUrl = route.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_URL);
URI routeURI = targetURI.resolve(routeUrl); //$NON-NLS-1$
DeleteMethod deleteRouteMethod = new DeleteMethod(routeURI.toString());
confStatus = HttpUtil.configureHttpMethod(deleteRouteMethod, target.getCloud());
if (!confStatus.isOK())
return confStatus;
ServerStatus deleteStatus = HttpUtil.executeMethod(deleteRouteMethod);
status.add(deleteStatus);
if (!status.isOK())
return status;
}
return status;
} catch (Exception e) {
String msg = NLS.bind("An error occured when performing operation {0}", commandName);
logger.error(msg, e);
return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
}
}
@Test
public void successfulDirectoryDelete_rollForward() throws Exception {
String bucketName = gcsfsIHelper.createUniqueBucket("coop-delete-forward-successful");
URI bucketUri = new URI("gs://" + bucketName + "/");
String fileName = "file";
URI dirUri = bucketUri.resolve("delete_" + UUID.randomUUID() + "/");
// create file to delete
gcsfsIHelper.writeTextFile(bucketName, dirUri.resolve(fileName).getPath(), "file_content");
GoogleCloudStorageFileSystemOptions gcsFsOptions = newGcsFsOptions();
GoogleCloudStorageFileSystem gcsFs = newGcsFs(gcsFsOptions, httpRequestInitializer);
assertThat(gcsFs.exists(dirUri)).isTrue();
assertThat(gcsFs.exists(dirUri.resolve(fileName))).isTrue();
gcsFs.delete(dirUri, /* recursive= */ true);
assertThat(gcsFs.exists(dirUri)).isFalse();
assertThat(gcsFs.exists(dirUri.resolve(fileName))).isFalse();
CoopLockFsck fsck = new CoopLockFsck();
fsck.setConf(getTestConfiguration());
fsck.run(new String[] {"--rollForward", "gs://" + bucketName, "all"});
assertThat(gcsFs.exists(dirUri)).isFalse();
assertThat(gcsFs.exists(dirUri.resolve(fileName))).isFalse();
// Validate lock files
List<URI> lockFiles =
gcsFs.listFileInfo(bucketUri.resolve(LOCK_DIRECTORY)).stream()
.map(FileInfo::getPath)
.collect(toList());
assertThat(lockFiles).hasSize(2);
String filenamePattern = String.format(OPERATION_FILENAME_PATTERN_FORMAT, DELETE);
URI lockFileUri = matchFile(lockFiles, filenamePattern + "\\.lock").get();
URI logFileUri = matchFile(lockFiles, filenamePattern + "\\.log").get();
String lockContent = gcsfsIHelper.readTextFile(bucketName, lockFileUri.getPath());
assertThat(GSON.fromJson(lockContent, DeleteOperation.class).setLockExpiration(null))
.isEqualTo(new DeleteOperation().setLockExpiration(null).setResource(dirUri.toString()));
assertThat(gcsfsIHelper.readTextFile(bucketName, logFileUri.getPath()))
.isEqualTo(dirUri.resolve(fileName) + "\n" + dirUri + "\n");
}