Search in sources :

Example 1 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class IngestDownloadWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String baseUrl = workspace.getBaseUri().toString();
    List<URI> externalUris = new ArrayList<URI>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (element.getElementType() == MediaPackageElement.Type.Publication) {
            logger.debug("Skipping downloading media package element {} from media package {} " + "because it is a publication: {}", element.getIdentifier(), mediaPackage.getIdentifier().compact(), element.getURI());
            continue;
        }
        URI originalElementUri = element.getURI();
        if (originalElementUri.toString().startsWith(baseUrl)) {
            logger.info("Skipping downloading already existing element {}", originalElementUri);
            continue;
        }
        // Download the external URI
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (Exception e) {
            logger.warn("Unable to download the external element {}", element.getURI());
            throw new WorkflowOperationException("Unable to download the external element " + element.getURI(), e);
        }
        // Put to working file repository and rewrite URI on element
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), FilenameUtils.getName(element.getURI().getPath()), in);
            element.setURI(uri);
        } catch (Exception e) {
            logger.warn("Unable to store downloaded element '{}': {}", element.getURI(), e.getMessage());
            throw new WorkflowOperationException("Unable to store downloaded element " + element.getURI(), e);
        } finally {
            IOUtils.closeQuietly(in);
            try {
                workspace.delete(originalElementUri);
            } catch (Exception e) {
                logger.warn("Unable to delete ingest-downloaded element {}: {}", element.getURI(), e);
            }
        }
        logger.info("Downloaded the external element {}", originalElementUri);
        // Store origianl URI for deletion
        externalUris.add(originalElementUri);
    }
    if (!deleteExternal || externalUris.size() == 0)
        return createResult(mediaPackage, Action.CONTINUE);
    // Find all external working file repository base Urls
    logger.debug("Assembling list of external working file repositories");
    List<String> externalWfrBaseUrls = new ArrayList<String>();
    try {
        for (ServiceRegistration reg : serviceRegistry.getServiceRegistrationsByType(WorkingFileRepository.SERVICE_TYPE)) {
            if (baseUrl.startsWith(reg.getHost())) {
                logger.trace("Skpping local working file repository");
                continue;
            }
            externalWfrBaseUrls.add(UrlSupport.concat(reg.getHost(), reg.getPath()));
        }
        logger.debug("{} external working file repositories found", externalWfrBaseUrls.size());
    } catch (ServiceRegistryException e) {
        logger.error("Unable to load WFR services from service registry: {}", e.getMessage());
        throw new WorkflowOperationException(e);
    }
    for (URI uri : externalUris) {
        String elementUri = uri.toString();
        // Delete external working file repository URI's
        String wfrBaseUrl = null;
        for (String url : externalWfrBaseUrls) {
            if (elementUri.startsWith(url)) {
                wfrBaseUrl = url;
                break;
            }
        }
        if (wfrBaseUrl == null) {
            logger.info("Unable to delete external URI {}, no working file repository found", elementUri);
            continue;
        }
        HttpDelete delete;
        if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX))) {
            String wfrDeleteUrl = elementUri.substring(0, elementUri.lastIndexOf("/"));
            delete = new HttpDelete(wfrDeleteUrl);
        } else if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.COLLECTION_PATH_PREFIX))) {
            delete = new HttpDelete(elementUri);
        } else {
            logger.info("Unable to handle working file repository URI {}", elementUri);
            continue;
        }
        HttpResponse response = null;
        try {
            response = client.execute(delete);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_OK) {
                logger.info("Sucessfully deleted external URI {}", delete.getURI());
            } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
                logger.info("External URI {} has already been deleted", delete.getURI());
            } else {
                logger.info("Unable to delete external URI {}, status code '{}' returned", delete.getURI(), statusCode);
            }
        } catch (TrustedHttpClientException e) {
            logger.warn("Unable to execute DELETE request on external URI {}", delete.getURI());
            throw new WorkflowOperationException(e);
        } finally {
            client.close(response);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) File(java.io.File) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 2 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class StandAloneTrustedHttpClientImpl method manuallyHandleDigestAuthentication.

/**
 * Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
 *
 * @param httpUriRequest
 *         The request location to get the digest authentication for.
 * @param httpClient
 *         The client to send the request through.
 * @throws org.opencastproject.security.api.TrustedHttpClientException
 *         Thrown if the client cannot be shutdown.
 */
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient) throws TrustedHttpClientException {
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);
    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);
        // Add the authentication header
        try {
            httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class IoSupport method readFileFromURL.

/**
 * Convenience method to read in a file from either a remote or local source.
 *
 * @param url
 *          The {@code URL} to read the source data from.
 * @param trustedClient
 *          The {@code TrustedHttpClient} which should be used to communicate with the remote server. This can be null
 *          for local file reads.
 * @return A String containing the source data or null in the case of an error.
 * @deprecated this method doesn't support UTF8 or handle HTTP response codes
 */
@Deprecated
public static String readFileFromURL(URL url, TrustedHttpClient trustedClient) {
    StringBuilder sb = new StringBuilder();
    DataInputStream in = null;
    HttpResponse response = null;
    try {
        // Do different things depending on what we're reading...
        if ("file".equals(url.getProtocol())) {
            in = new DataInputStream(url.openStream());
        } else {
            if (trustedClient == null) {
                logger.error("Unable to read from remote source {} because trusted client is null!", url.getFile());
                return null;
            }
            HttpGet get = new HttpGet(url.toURI());
            try {
                response = trustedClient.execute(get);
            } catch (TrustedHttpClientException e) {
                logger.warn("Unable to fetch file from {}.", url, e);
                trustedClient.close(response);
                return null;
            }
            in = new DataInputStream(response.getEntity().getContent());
        }
        int c = 0;
        while ((c = in.read()) != -1) {
            sb.append((char) c);
        }
    } catch (IOException e) {
        logger.warn("IOException attempting to get file from {}.", url);
        return null;
    } catch (URISyntaxException e) {
        logger.warn("URI error attempting to get file from {}.", url);
        return null;
    } catch (NullPointerException e) {
        logger.warn("Nullpointer attempting to get file from {}.", url);
        return null;
    } finally {
        IOUtils.closeQuietly(in);
        if (response != null && trustedClient != null) {
            trustedClient.close(response);
            response = null;
        }
    }
    return sb.toString();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) DataInputStream(java.io.DataInputStream) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException)

Example 4 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class TrustedHttpClientImpl method manuallyHandleDigestAuthentication.

/**
 * Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
 *
 * @param httpUriRequest
 *         The request location to get the digest authentication for.
 * @param httpClient
 *         The client to send the request through.
 * @throws TrustedHttpClientException
 *         Thrown if the client cannot be shutdown.
 */
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient) throws TrustedHttpClientException {
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);
    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);
        // Add the authentication header
        try {
            httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ClientProtocolException(org.apache.http.client.ClientProtocolException) UrlSigningException(org.opencastproject.security.urlsigning.exception.UrlSigningException) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 5 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class CleanupWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    cleanUpJobArgument(workflowInstance);
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    String flavors = currentOperation.getConfiguration(PRESERVE_FLAVOR_PROPERTY);
    final List<MediaPackageElementFlavor> flavorsToPreserve = new ArrayList<MediaPackageElementFlavor>();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String delayStr = currentOperation.getConfiguration(DELAY);
    int delay = 1;
    if (delayStr != null) {
        try {
            delay = Integer.parseInt(delayStr);
        } catch (NumberFormatException e) {
            logger.warn("Invalid value '{}' for delay in workflow operation configuration (should be integer)", delayStr);
        }
    }
    if (delay > 0) {
        try {
            logger.debug("Sleeping {}s before removing workflow files", delay);
            Thread.sleep(delay * 1000);
        } catch (InterruptedException e) {
        // ignore
        }
    }
    // If the configuration does not specify flavors, remove them all
    for (String flavor : asList(flavors)) flavorsToPreserve.add(MediaPackageElementFlavor.parseFlavor(flavor));
    List<MediaPackageElement> elementsToRemove = new ArrayList<>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (!isPreserved(element, flavorsToPreserve))
            elementsToRemove.add(element);
    }
    List<String> externalBaseUrls = null;
    if (deleteExternal) {
        externalBaseUrls = getAllWorkingFileRepositoryUrls();
        externalBaseUrls.remove(workspace.getBaseUri().toString());
    }
    for (MediaPackageElement elementToRemove : elementsToRemove) {
        if (deleteExternal) {
            // cleanup external working file repositories
            for (String repository : externalBaseUrls) {
                logger.debug("Removing {} from repository {}", elementToRemove.getURI(), repository);
                try {
                    removeElementFromRepository(elementToRemove, repository);
                } catch (TrustedHttpClientException ex) {
                    logger.debug("Removing media package element {} from repository {} failed: {}", elementToRemove.getURI(), repository, ex.getMessage());
                }
            }
        }
        // cleanup workspace and also the internal working file repository
        logger.debug("Removing {} from the workspace", elementToRemove.getURI());
        try {
            mediaPackage.remove(elementToRemove);
            workspace.delete(elementToRemove.getURI());
        } catch (NotFoundException ex) {
            logger.debug("Workspace doesn't contain element with Id '{}' from media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
        } catch (IOException ex) {
            logger.warn("Unable to remove element with Id '{}' from the media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Aggregations

TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)12 IOException (java.io.IOException)8 HttpResponse (org.apache.http.HttpResponse)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 Header (org.apache.http.Header)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 HttpClient (org.opencastproject.kernel.http.api.HttpClient)3 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)3 UrlSigningException (org.opencastproject.security.urlsigning.exception.UrlSigningException)3 ArrayList (java.util.ArrayList)2 HeaderElement (org.apache.http.HeaderElement)2 HttpGet (org.apache.http.client.methods.HttpGet)2 DigestScheme (org.apache.http.impl.auth.DigestScheme)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)2 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)2 HttpResponseWrapper (org.opencastproject.security.util.HttpResponseWrapper)2 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)2 DataInputStream (java.io.DataInputStream)1