Search in sources :

Example 26 with NotFoundException

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;
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 27 with NotFoundException

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());
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 28 with NotFoundException

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");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 29 with NotFoundException

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");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.simple.JSONArray) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 30 with NotFoundException

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");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Aggregations

NotFoundException (org.opencastproject.util.NotFoundException)382 IOException (java.io.IOException)137 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)130 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)79 MediaPackage (org.opencastproject.mediapackage.MediaPackage)69 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)67 EntityManager (javax.persistence.EntityManager)55 SeriesException (org.opencastproject.series.api.SeriesException)53 Path (javax.ws.rs.Path)52 WebApplicationException (javax.ws.rs.WebApplicationException)52 RestQuery (org.opencastproject.util.doc.rest.RestQuery)51 ConfigurationException (org.osgi.service.cm.ConfigurationException)51 URI (java.net.URI)50 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)50 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)49 Date (java.util.Date)48 Test (org.junit.Test)47 File (java.io.File)46 HttpResponse (org.apache.http.HttpResponse)46 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)46