/** * 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); }
@Override public String apply(String s, String s1) { return concat(s, s1); } });
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)); }
/** * 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); } }
/** * {@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"); }
/** * {@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"); }
@Override public boolean hasRepo(String id) { for (HttpResponse r : option(getResponse(get(UrlSupport.concat("hasrepo", id)), HttpStatus.SC_OK))) { try { return Boolean.parseBoolean(EntityUtils.toString(r.getEntity())); } catch (IOException e) { logger.error("Cannot contact remote service", e); chuck(e); } } return false; }
/** * {@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"); }
protected JSONObject getStorageReport() { String url = UrlSupport.concat(new String[] { "storage" }); HttpGet get = new HttpGet(url); HttpResponse response = getResponse(get); try { if (response != null) { String json = EntityUtils.toString(response.getEntity()); return (JSONObject) JSONValue.parse(json); } } catch (Exception e) { throw new RuntimeException(e); } finally { closeConnection(response); } throw new RuntimeException("Error getting storage report"); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getFromCollection(java.lang.String, * java.lang.String) */ @Override public InputStream getFromCollection(String collectionId, String fileName) throws NotFoundException { String url = UrlSupport.concat(new String[] { COLLECTION_PATH_PREFIX, collectionId, fileName }); HttpGet get = new HttpGet(url); HttpResponse response = getResponse(get, SC_OK, SC_NOT_FOUND); try { if (response != null) { if (SC_NOT_FOUND == response.getStatusLine().getStatusCode()) throw new NotFoundException(); // Do not close this response. It will be closed when the caller closes the input stream return new HttpClientClosingInputStream(response); } } catch (NotFoundException e) { throw e; } catch (Exception e) { throw new RuntimeException(); } throw new RuntimeException("Error get from collection"); }
/** * Get the URI for a static file resource depending on whether to get it direct from Opencast or from a webserver. * * @param uuid * The unique identifier for the static file. * @return The URL for the static file resource. * @throws NotFoundException * if the resource couldn't been found */ public URI getStaticFileURL(String uuid) throws NotFoundException { if (useWebserver && webserverURL.isSome()) { return URI.create(UrlSupport.concat(webserverURL.get(), securityService.getOrganization().getId(), uuid, staticFileService.getFileName(uuid))); } else { return URI.create(UrlSupport.concat(serverUrl, STATICFILES_URL_PATH, uuid)); } }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#get(java.lang.String, java.lang.String) */ @Override public InputStream get(String mediaPackageID, String mediaPackageElementID) throws NotFoundException { String urlSuffix = UrlSupport .concat(new String[] { MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID }); HttpGet get = new HttpGet(urlSuffix); HttpResponse response = getResponse(get, SC_OK, SC_NOT_FOUND); try { if (response != null) { if (SC_NOT_FOUND == response.getStatusLine().getStatusCode()) { throw new NotFoundException(); } else { // Do not close this response. It will be closed when the caller closes the input stream return new HttpClientClosingInputStream(response); } } } catch (Exception e) { throw new RuntimeException(); } throw new RuntimeException("Error getting file"); }
@PUT @Path("{id}") @Produces(MediaType.TEXT_XML) @RestQuery(name = "change", description = "Changes the value of an annotation specified by its identifier ", returnDescription = "The user annotation.", pathParameters = {@RestParameter(name = "id", description = "The annotation identifier", isRequired = true, type = Type.STRING) }, restParameters = {@RestParameter(name = "value", description = "The value of the annotation", isRequired = true, type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "The URL to this annotation is returned in the Location header, and an XML representation of the annotation itelf is returned in the response body.") }) public Response change(@PathParam("id") String idAsString, @FormParam("value") String value, @Context HttpServletRequest request) throws NotFoundException { Long id = null; try { id = Long.parseLong(idAsString); } catch (NumberFormatException e) { return Response.status(Status.INTERNAL_SERVER_ERROR).build(); } Annotation a = annotationService.getAnnotation(id); a.setValue(value); a = annotationService.changeAnnotation(a); URI uri; try { uri = new URI( UrlSupport.concat(new String[] { serverUrl, serviceUrl, Long.toString(a.getAnnotationId()), ".xml" })); } catch (URISyntaxException e) { throw new WebApplicationException(e); } return Response.created(uri).entity(a).build(); }
/** * {@inheritDoc} * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#put(java.lang.String, java.lang.String, * java.lang.String, java.io.InputStream) */ @Override public URI put(String mediaPackageID, String mediaPackageElementID, String filename, InputStream in) { String url = UrlSupport.concat(new String[] { MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID }); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity(); ContentBody body = new InputStreamBody(in, filename); entity.addPart("file", body); post.setEntity(entity); HttpResponse response = getResponse(post); try { if (response != null) { String content = EntityUtils.toString(response.getEntity()); return new URI(content); } } catch (Exception e) { throw new RuntimeException(e); } finally { closeConnection(response); } throw new RuntimeException("Unable to put file"); }
/** Create a URI from the given parts. */ public static URI uri(Object... parts) { return URI.create(concat(mlist(parts).map(asStringNull()).value())); }