use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryImpl method getFileFromCollection.
/**
* Returns the file from the given collection.
*
* @param collectionId
* the collection identifier
* @param fileName
* the file name
* @return the file
* @throws NotFoundException
* if either the collection or the file don't exist
*/
protected File getFileFromCollection(String collectionId, String fileName) throws NotFoundException, IllegalArgumentException {
checkPathSafe(collectionId);
File directory = null;
try {
directory = getCollectionDirectory(collectionId, false);
if (directory == null) {
// getCollectionDirectory returns null on a non-existant directory which is not being created...
directory = new File(PathSupport.concat(new String[] { rootDirectory, COLLECTION_PATH_PREFIX, collectionId }));
throw new NotFoundException(directory.getAbsolutePath());
}
} catch (IOException e) {
// can be ignored, since we don't want the directory to be created, so it will never happen
}
File sourceFile = new File(directory, PathSupport.toSafeName(fileName));
File md5File = getMd5File(sourceFile);
if (!sourceFile.exists())
throw new NotFoundException(sourceFile.getAbsolutePath());
if (!md5File.exists())
throw new NotFoundException(md5File.getAbsolutePath());
return sourceFile;
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryImpl method moveTo.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#moveTo(java.lang.String, java.lang.String,
* java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public URI moveTo(String fromCollection, String fromFileName, String toMediaPackage, String toMediaPackageElement, String toFileName) throws NotFoundException, IOException {
File source = getFileFromCollection(fromCollection, fromFileName);
File sourceMd5 = getMd5File(source);
File destDir = getElementDirectory(toMediaPackage, toMediaPackageElement);
logger.debug("Moving {} from {} to {}/{}", new String[] { fromFileName, fromCollection, toMediaPackage, toMediaPackageElement });
if (!destDir.exists()) {
// we needed to create the directory, but couldn't
try {
FileUtils.forceMkdir(destDir);
} catch (IOException e) {
throw new IllegalStateException("could not create mediapackage/element directory '" + destDir.getAbsolutePath() + "' : " + e);
}
}
File dest = null;
try {
dest = getFile(toMediaPackage, toMediaPackageElement);
logger.debug("Removing existing file from target location at {}", dest);
delete(toMediaPackage, toMediaPackageElement);
} catch (NotFoundException e) {
dest = new File(getElementDirectory(toMediaPackage, toMediaPackageElement), PathSupport.toSafeName(toFileName));
}
try {
FileUtils.moveFile(source, dest);
FileUtils.moveFile(sourceMd5, getMd5File(dest));
} catch (IOException e) {
FileUtils.deleteDirectory(destDir);
throw new IllegalStateException("unable to copy file" + e);
}
return getURI(toMediaPackage, toMediaPackageElement, dest.getName());
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryRemoteImpl method getFromCollection.
/**
* {@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");
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryRemoteImpl method getCollectionContents.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getCollectionContents(java.lang.String)
*/
@Override
public URI[] getCollectionContents(String collectionId) throws NotFoundException {
String urlSuffix = UrlSupport.concat(new String[] { "list", collectionId + ".json" });
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 {
String json = EntityUtils.toString(response.getEntity());
JSONArray jsonArray = (JSONArray) JSONValue.parse(json);
URI[] uris = new URI[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
uris[i] = new URI((String) jsonArray.get(i));
}
return uris;
}
}
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException();
} finally {
closeConnection(response);
}
throw new RuntimeException("Error getting collection contents");
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryRemoteImpl method get.
/**
* {@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");
}
Aggregations