/** * Concatenates the two urls with respect to leading and trailing slashes. * <p> * Note that returned path will only end with a slash if <code>suffix</code> does. If you need a trailing slash, see * {@link #concat(String, String, boolean)}. * * @return the concatenated url of the two arguments */ public static String concat(String prefix, String suffix) { return concat(prefix, suffix, false); }
/** * Concatenates the two urls with respect to leading and trailing slashes. The path will always end with a trailing * slash. * * @return the concatenated url of the two arguments */ public static String concat(String prefix, String suffix, boolean close) { if (prefix == null) throw new IllegalArgumentException("Argument prefix is null"); if (suffix == null) throw new IllegalArgumentException("Argument suffix is null"); prefix = checkSeparator(prefix); suffix = checkSeparator(suffix); prefix = removeDoubleSeparator(prefix); suffix = removeDoubleSeparator(suffix); if (!prefix.endsWith("/") && !suffix.startsWith("/")) prefix += "/"; if (prefix.endsWith("/") && suffix.startsWith("/")) suffix = suffix.substring(1); prefix += suffix; // Close? if (close && !prefix.endsWith("/")) { prefix += "/"; } return prefix; }
/** * Returns <code>true</code> if the url is valid, that is, if it contains only allowed characters. * * @return <code>true</code> or the invalid character */ public static boolean isValid(String url) { return (checkUrl(url) == null); }
/** * Returns <code>true</code> if url <code>a</code> is a direct prefix of url <code>b</code>. For example, * <code>/news</code> is the parent of <code>/news/today</code>. * <p> * Note that <code>a</code> is also an extended prefix of <code>b</code> if <code>a</code> and <code>b</code> are * equal. * * @param a * the first url * @param b * the second url * @return <code>true</code> if <code>a</code> is the direct prefix of <code>b</code> */ public static boolean isPrefix(String a, String b) { if (isExtendedPrefix(a, b)) { if (a.length() < b.length()) { String bRest = b.substring(a.length() + 1); if (bRest.endsWith("/")) bRest = bRest.substring(0, bRest.length() - 2); return bRest.indexOf("/", 1) < 0; } else { return true; } } return false; }
/** * Strips off the extension and returns the pure url. * * @param url * the url with extension * @return the url */ public static String stripExtension(String url) { String extension = getExtension(url); if (extension == null) return url; else return url.substring(0, url.length() - extension.length()); }
/** * Returns the trimmed url. Trimmed means that the url is free from leading or trailing whitespace characters, and * that a directory url like <code>/news/</code> is closed by a slash (<code>/</code>). * * @param url * the url to trim * @return the trimmed url */ public static String trim(String url) { if (url == null) throw new IllegalArgumentException("Argument url is null"); url = url.trim(); url = checkSeparator(url); if (url.endsWith("/") || (url.length() == 1)) return url; int index = url.lastIndexOf("/"); index = url.indexOf(".", index); if (index == -1) url += "/"; return url; }
/** * Returns the url extension that <code>url</code> defines over <code>prefix</code>. For example, the extension of url * <code>/news/today</code> and prefix <code>/news</code> is <code>today</code>. * <p> * If <code>prefix</code> is not a prefix of <code>url</code>, this method returns <code>null</code>, if * <code>url</code> and <code>prefix</code> match, the empty string is returned. * * @param prefix * the url prefix * @param url * the url * @return the url extension over the prefix */ public static String getExtension(String prefix, String url) { prefix = prefix.trim(); if (isExtendedPrefix(prefix, url)) { if (url.length() > prefix.length()) { String extension = url.substring(prefix.length() + 1); if (extension.endsWith("/")) { extension = extension.substring(0, extension.length() - 1); } return extension; } else return ""; } return null; }
@Override public String apply(String s, String s1) { return concat(s, s1); } });
/** * Returns <code>null</code> if the url is valid, that is, if it contains only allowed characters. Otherwhise, the * invalid character is returned. * * @return <code>null</code> or the invalid character */ public static Character getInvalidCharacter(String url) { Character c = checkUrl(url); return c; }
public static String path(String... path) { return UrlSupport.concat(path); }
/** * Concatenates the urls with respect to leading and trailing slashes. * * @param parts * the parts to concat * @return the concatenated url */ public static String concat(String... parts) { if (parts == null) throw new IllegalArgumentException("Argument parts is null"); if (parts.length == 0) throw new IllegalArgumentException("Array parts is empty"); String path = parts[0]; for (int i = 1; i < parts.length; i++) { if (parts[i] != null) { path = concat(path, parts[i]); } } return path; }
/** * @see org.opencastproject.mediapackage.identifier.HandleBuilder#createNew(java.net.URL) */ public Handle createNew(URL url) throws IllegalStateException { if (url == null) throw new IllegalArgumentException("Argument url must not be null"); String localName = UUID.randomUUID().toString(); if (namespace != null) { if (namespace.endsWith("/")) localName = UrlSupport.concat(namespace, localName); else localName = namespace + localName; } return new HandleImpl(namingAuthority, localName, url, this); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getURI(java.lang.String, java.lang.String, * java.lang.String) */ @Override public URI getURI(String mediaPackageID, String mediaPackageElementID, String fileName) { String url = UrlSupport.concat(new String[] { "uri", mediaPackageID, mediaPackageElementID }); if (fileName != null) url = UrlSupport.concat(url, fileName); HttpGet get = new HttpGet(url); HttpResponse response = getResponse(get); try { if (response != null) { return new URI(EntityUtils.toString(response.getEntity())); } } catch (Exception e) { throw new RuntimeException(e); } finally { closeConnection(response); } throw new RuntimeException("Unable to get URI"); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getBaseUri() */ @Override public URI getBaseUri() { if (securityService.getOrganization() != null) { Map<String, String> orgProps = securityService.getOrganization().getProperties(); if (orgProps != null && orgProps.containsKey(OpencastConstants.WFR_URL_ORG_PROPERTY)) { try { return new URI(UrlSupport.concat(orgProps.get(OpencastConstants.WFR_URL_ORG_PROPERTY), servicePath)); } catch (URISyntaxException ex) { logger.warn("Organization working file repository URL not set, fallback to server URL"); } } } return URI.create(UrlSupport.concat(serverUrl, servicePath)); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#cleanupOldFilesFromCollection */ @Override public boolean cleanupOldFilesFromCollection(String collectionId, long days) throws IOException { String url = UrlSupport.concat(new String[] { COLLECTION_PATH_PREFIX, collectionId, Long.toString(days) }); HttpDelete del = new HttpDelete(url); HttpResponse response = getResponse(del, SC_NO_CONTENT, SC_NOT_FOUND); try { if (response != null) return SC_NO_CONTENT == response.getStatusLine().getStatusCode(); } finally { closeConnection(response); } throw new RuntimeException("Error removing older files from collection"); }
/** * Returns the runtime information as a JSON string. * * @return the runtime information * @throws IOException * if reading the response body fails */ private String getRuntimeInfo(String hostUrl) throws IOException { HttpGet get = new HttpGet(UrlSupport.concat(hostUrl, "/info/components.json")); HttpResponse response = null; try { response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) return null; return EntityUtils.toString(response.getEntity()); } finally { httpClient.close(response); } }
uri = new URI(UrlSupport.concat(packageRoot.toExternalForm(), path)); return uri;
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getCollectionURI(java.lang.String, * java.lang.String) */ @Override public URI getCollectionURI(String collectionID, String fileName) { String url = UrlSupport.concat(new String[] { "collectionuri", collectionID, fileName }); HttpGet get = new HttpGet(url); HttpResponse response = getResponse(get); try { if (response != null) { return new URI(EntityUtils.toString(response.getEntity())); } } catch (Exception e) { throw new RuntimeException(e); } finally { closeConnection(response); } throw new RuntimeException("Unable to get collection URI"); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#delete(java.lang.String, java.lang.String) */ @Override public boolean delete(String mediaPackageID, String mediaPackageElementID) { String urlSuffix = UrlSupport .concat(new String[] { MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID }); HttpDelete del = new HttpDelete(urlSuffix); HttpResponse response = getResponse(del, SC_OK, SC_NOT_FOUND); try { if (response != null) { return HttpStatus.SC_OK == response.getStatusLine().getStatusCode(); } } finally { closeConnection(response); } throw new RuntimeException("Error removing file"); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#deleteFromCollection(java.lang.String, * java.lang.String,boolean) */ @Override public boolean deleteFromCollection(String collectionId, String fileName, boolean removeCollection) { // The removeCollection parameter is ignored here, as this remote implementation is not currently used. String url = UrlSupport.concat(new String[] { COLLECTION_PATH_PREFIX, collectionId, fileName }); HttpDelete del = new HttpDelete(url); HttpResponse response = getResponse(del, SC_NO_CONTENT, SC_NOT_FOUND); try { if (response != null) return SC_NO_CONTENT == response.getStatusLine().getStatusCode(); } finally { closeConnection(response); } throw new RuntimeException("Error removing file from collection"); }