Search in sources :

Example 1 with RequestRunner

use of org.opencastproject.security.api.TrustedHttpClient.RequestRunner in project opencast by opencast.

the class WorkspaceImplTest method testLongFilenames.

@Test
public void testLongFilenames() throws Exception {
    WorkingFileRepository repo = EasyMock.createNiceMock(WorkingFileRepository.class);
    EasyMock.expect(repo.getBaseUri()).andReturn(new URI("http://localhost:8080/files")).anyTimes();
    EasyMock.replay(repo);
    workspace.setRepository(repo);
    File source = new File("target/test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/../test-classes/opencast_header.gif");
    URL urlToSource = source.toURI().toURL();
    final TrustedHttpClient httpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    HttpEntity entity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(entity.getContent()).andReturn(new FileInputStream(source));
    StatusLine statusLine = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpServletResponse.SC_OK);
    HttpResponse response = EasyMock.createNiceMock(HttpResponse.class);
    EasyMock.expect(response.getEntity()).andReturn(entity);
    EasyMock.expect(response.getStatusLine()).andReturn(statusLine).anyTimes();
    EasyMock.replay(response, entity, statusLine);
    EasyMock.expect(httpClient.execute((HttpUriRequest) EasyMock.anyObject())).andReturn(response);
    EasyMock.expect(httpClient.runner((HttpUriRequest) EasyMock.anyObject())).andAnswer(new IAnswer<TrustedHttpClient.RequestRunner<Object>>() {

        @Override
        public RequestRunner<Object> answer() throws Throwable {
            HttpUriRequest req = (HttpUriRequest) EasyMock.getCurrentArguments()[0];
            return StandAloneTrustedHttpClientImpl.runner(httpClient, req);
        }
    });
    EasyMock.replay(httpClient);
    workspace.setTrustedHttpClient(httpClient);
    Assert.assertTrue(urlToSource.toString().length() > 255);
    try {
        Assert.assertNotNull(workspace.get(urlToSource.toURI()));
    } catch (NotFoundException e) {
    // This happens on some machines, so we catch and handle it.
    }
}
Also used : TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestRunner(org.opencastproject.security.api.TrustedHttpClient.RequestRunner) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream) StatusLine(org.apache.http.StatusLine) File(java.io.File) WorkingFileRepository(org.opencastproject.workingfilerepository.api.WorkingFileRepository) Test(org.junit.Test)

Example 2 with RequestRunner

use of org.opencastproject.security.api.TrustedHttpClient.RequestRunner 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)2 URI (java.net.URI)2 Test (org.junit.Test)2 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)2 RequestRunner (org.opencastproject.security.api.TrustedHttpClient.RequestRunner)2 NotFoundException (org.opencastproject.util.NotFoundException)2 WorkingFileRepository (org.opencastproject.workingfilerepository.api.WorkingFileRepository)2 FileInputStream (java.io.FileInputStream)1 URL (java.net.URL)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 Either (org.opencastproject.util.data.Either)1 Function (org.opencastproject.util.data.Function)1 Option (org.opencastproject.util.data.Option)1