Search in sources :

Example 1 with Either

use of org.opencastproject.util.data.Either in project opencast by opencast.

the class WorkspaceImpl method downloadIfNecessary.

/**
 * Download content of <code>uri</code> to file <code>dst</code> only if necessary, i.e. either the file does not yet
 * exist in the workspace or a newer version is available at <code>uri</code>.
 *
 * @return the file
 */
private File downloadIfNecessary(final URI src, final File dst) throws IOException, NotFoundException {
    HttpGet get = createGetRequest(src, dst);
    while (true) {
        // run the http request and handle its response
        final Either<Exception, Either<String, Option<File>>> result = trustedHttpClient.<Either<String, Option<File>>>runner(get).run(handleDownloadResponse(src, dst));
        // right: there's an expected result
        for (Either<String, Option<File>> a : result.right()) {
            // right: either a file could be found or not
            for (Option<File> ff : a.right()) {
                for (File f : ff) {
                    return f;
                }
                FileUtils.deleteQuietly(dst);
                // none
                throw new NotFoundException();
            }
            // left: file will be ready later
            for (String token : a.left()) {
                get = createGetRequest(src, dst, tuple("token", token));
                sleep(60000);
            }
        }
        // left: an exception occurred
        for (Exception e : result.left()) {
            logger.warn(format("Could not copy %s to %s: %s", src.toString(), dst.getAbsolutePath(), e.getMessage()));
            FileUtils.deleteQuietly(dst);
            throw new NotFoundException(e);
        }
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) Either(org.opencastproject.util.data.Either) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) Option(org.opencastproject.util.data.Option) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 2 with Either

use of org.opencastproject.util.data.Either in project opencast by opencast.

the class DownloadDistributionServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    final File mediaPackageRoot = new File(getClass().getResource("/mediapackage.xml").toURI()).getParentFile();
    mp = MediaPackageParser.getFromXml(IOUtils.toString(getClass().getResourceAsStream("/mediapackage.xml"), "UTF-8"));
    distributionRoot = new File(mediaPackageRoot, "static");
    service = new DownloadDistributionServiceImpl();
    StatusLine statusLine = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpServletResponse.SC_OK).anyTimes();
    EasyMock.replay(statusLine);
    HttpResponse response = EasyMock.createNiceMock(HttpResponse.class);
    EasyMock.expect(response.getStatusLine()).andReturn(statusLine).anyTimes();
    EasyMock.replay(response);
    final TrustedHttpClient httpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    EasyMock.expect(httpClient.execute((HttpUriRequest) EasyMock.anyObject())).andReturn(response).anyTimes();
    EasyMock.expect(httpClient.run((HttpUriRequest) EasyMock.anyObject())).andAnswer(new IAnswer<Function<Function<HttpResponse, Object>, Either<Exception, Object>>>() {

        @Override
        public Function<Function<HttpResponse, Object>, Either<Exception, Object>> answer() throws Throwable {
            HttpUriRequest req = (HttpUriRequest) EasyMock.getCurrentArguments()[0];
            return StandAloneTrustedHttpClientImpl.run(httpClient, req);
        }
    }).anyTimes();
    EasyMock.replay(httpClient);
    defaultOrganization = new DefaultOrganization();
    User anonymous = new JaxbUser("anonymous", "test", defaultOrganization, new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, defaultOrganization));
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
    EasyMock.replay(userDirectoryService);
    service.setUserDirectoryService(userDirectoryService);
    Organization organization = new DefaultOrganization();
    OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
    EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
    EasyMock.replay(organizationDirectoryService);
    service.setOrganizationDirectoryService(organizationDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
    EasyMock.replay(securityService);
    service.setSecurityService(securityService);
    serviceRegistry = new ServiceRegistryInMemoryImpl(service, securityService, userDirectoryService, organizationDirectoryService, EasyMock.createNiceMock(IncidentService.class));
    service.setServiceRegistry(serviceRegistry);
    service.setTrustedHttpClient(httpClient);
    final Workspace workspace = EasyMock.createNiceMock(Workspace.class);
    service.setWorkspace(workspace);
    EasyMock.expect(workspace.get((URI) EasyMock.anyObject())).andAnswer(new IAnswer<File>() {

        @Override
        public File answer() throws Throwable {
            final URI uri = (URI) EasyMock.getCurrentArguments()[0];
            final String[] pathElems = uri.getPath().split("/");
            final String file = pathElems[pathElems.length - 1];
            return new File(mediaPackageRoot, file);
        }
    }).anyTimes();
    EasyMock.replay(workspace);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getProperty("org.opencastproject.download.directory")).andReturn(distributionRoot.toString()).anyTimes();
    EasyMock.expect(bc.getProperty("org.opencastproject.download.url")).andReturn(UrlSupport.DEFAULT_BASE_URL).anyTimes();
    ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(DistributionService.CONFIG_KEY_STORE_TYPE, "download");
    EasyMock.expect(cc.getProperties()).andReturn(p).anyTimes();
    EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.replay(bc, cc);
    service.activate(cc);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) URI(java.net.URI) Function(org.opencastproject.util.data.Function) SecurityService(org.opencastproject.security.api.SecurityService) Either(org.opencastproject.util.data.Either) ServiceRegistryInMemoryImpl(org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl) TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) ComponentContext(org.osgi.service.component.ComponentContext) Hashtable(java.util.Hashtable) HttpResponse(org.apache.http.HttpResponse) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) StatusLine(org.apache.http.StatusLine) IAnswer(org.easymock.IAnswer) JaxbRole(org.opencastproject.security.api.JaxbRole) File(java.io.File) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) Workspace(org.opencastproject.workspace.api.Workspace) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 3 with Either

use of org.opencastproject.util.data.Either in project opencast by opencast.

the class WorkspaceImplTest method testGetNoFilename.

@Test
public void testGetNoFilename() throws Exception {
    final File expectedFile = testFolder.newFile("test.txt");
    FileUtils.write(expectedFile, "asdf");
    expectedFile.deleteOnExit();
    WorkingFileRepository repo = EasyMock.createNiceMock(WorkingFileRepository.class);
    EasyMock.expect(repo.getBaseUri()).andReturn(new URI("http://localhost:8080/files")).anyTimes();
    EasyMock.replay(repo);
    workspace.setRepository(repo);
    RequestRunner<Either<String, Option<File>>> requestRunner = new TrustedHttpClient.RequestRunner<Either<String, Option<File>>>() {

        @Override
        public Either<Exception, Either<String, Option<File>>> run(Function<HttpResponse, Either<String, Option<File>>> f) {
            Either<String, Option<File>> right = Either.right(Option.some(expectedFile));
            return Either.right(right);
        }
    };
    TrustedHttpClient trustedHttpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    EasyMock.expect(trustedHttpClient.<Either<String, Option<File>>>runner(EasyMock.anyObject(HttpUriRequest.class))).andReturn(requestRunner).anyTimes();
    EasyMock.replay(trustedHttpClient);
    workspace.setTrustedHttpClient(trustedHttpClient);
    File resultingFile = workspace.get(URI.create("http://foo.com/myaccount/videos/"));
    Assert.assertEquals(expectedFile, resultingFile);
}
Also used : TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) Function(org.opencastproject.util.data.Function) RequestRunner(org.opencastproject.security.api.TrustedHttpClient.RequestRunner) Either(org.opencastproject.util.data.Either) Option(org.opencastproject.util.data.Option) File(java.io.File) URI(java.net.URI) WorkingFileRepository(org.opencastproject.workingfilerepository.api.WorkingFileRepository) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Aggregations

File (java.io.File)3 Either (org.opencastproject.util.data.Either)3 URI (java.net.URI)2 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)2 NotFoundException (org.opencastproject.util.NotFoundException)2 Function (org.opencastproject.util.data.Function)2 Option (org.opencastproject.util.data.Option)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 IAnswer (org.easymock.IAnswer)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 JaxbRole (org.opencastproject.security.api.JaxbRole)1 JaxbUser (org.opencastproject.security.api.JaxbUser)1