use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.
the class CertificationTest method getCertificate_head.
@Test
public void getCertificate_head() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-11");
Identity unassessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-12");
Identity author = JunitTestHelper.createAndPersistIdentityAsAuthor("cert-2");
RepositoryEntry entry = JunitTestHelper.deployBasicCourse(author);
CertificateInfos certificateInfos = new CertificateInfos(assessedIdentity, 2.0f, true);
Certificate certificate = certificatesManager.generateCertificate(certificateInfos, entry, null, false);
dbInstance.commitAndCloseSession();
Assert.assertNotNull(certificate);
sleep(1000);
URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(assessedIdentity.getKey().toString()).build();
HttpHead method = conn.createHead(uri, "application/pdf", true);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// check with a stupid number
URI nonExistentUri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(unassessedIdentity.getKey().toString()).build();
HttpHead nonExistentMethod = conn.createHead(nonExistentUri, "application/pdf", true);
HttpResponse nonExistentResponse = conn.execute(nonExistentMethod);
Assert.assertEquals(404, nonExistentResponse.getStatusLine().getStatusCode());
EntityUtils.consume(nonExistentResponse.getEntity());
conn.shutdown();
}
use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testPortrait_HEAD.
@Test
public void testPortrait_HEAD() throws IOException, URISyntaxException {
Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-1");
Identity idWithoutPortrait = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-2");
URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
Assert.assertNotNull(portraitUrl);
File portrait = new File(portraitUrl.toURI());
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login(id.getName(), "A6B7C8"));
// upload portrait
URI request = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
conn.addMultipart(method, "portrait.jpg", portrait);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// check 200
URI headRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").build();
HttpHead headMethod = conn.createHead(headRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse headResponse = conn.execute(headMethod);
assertEquals(200, headResponse.getStatusLine().getStatusCode());
EntityUtils.consume(headResponse.getEntity());
// check 404
URI headNoRequest = UriBuilder.fromUri(getContextURI()).path("users").path(idWithoutPortrait.getKey().toString()).path("portrait").build();
HttpHead headNoMethod = conn.createHead(headNoRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse headNoResponse = conn.execute(headNoMethod);
assertEquals(404, headNoResponse.getStatusLine().getStatusCode());
EntityUtils.consume(headNoResponse.getEntity());
}
use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testPortrait_HEAD_sizes.
/**
* Check the 3 sizes
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void testPortrait_HEAD_sizes() throws IOException, URISyntaxException {
Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-3");
URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
Assert.assertNotNull(portraitUrl);
File portrait = new File(portraitUrl.toURI());
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login(id.getName(), "A6B7C8"));
// upload portrait
URI request = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
conn.addMultipart(method, "portrait.jpg", portrait);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// check 200
URI headMasterRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("master").build();
HttpHead headMasterMethod = conn.createHead(headMasterRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse headMasterResponse = conn.execute(headMasterMethod);
assertEquals(200, headMasterResponse.getStatusLine().getStatusCode());
EntityUtils.consume(headMasterResponse.getEntity());
// check 200
URI headBigRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("big").build();
HttpHead headBigMethod = conn.createHead(headBigRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse headBigResponse = conn.execute(headBigMethod);
assertEquals(200, headBigResponse.getStatusLine().getStatusCode());
EntityUtils.consume(headBigResponse.getEntity());
// check 200
URI headSmallRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("small").build();
HttpHead headSmallMethod = conn.createHead(headSmallRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse headSmallResponse = conn.execute(headSmallMethod);
assertEquals(200, headSmallResponse.getStatusLine().getStatusCode());
EntityUtils.consume(headSmallResponse.getEntity());
}
use of org.apache.http.client.methods.HttpHead in project opencast by opencast.
the class HttpUtil method waitForResource.
/**
* Wait for a certain status of a resource.
*
* @return either an exception or the status code of the last http response
*/
public static Either<Exception, Integer> waitForResource(final TrustedHttpClient http, final URI resourceUri, final int expectedStatus, final long timeout, final long pollingInterval) {
long now = 0L;
while (true) {
final HttpHead head = new HttpHead(resourceUri);
final Either<Exception, Integer> result = http.<Integer>run(head).apply(getStatusCode);
for (final Integer status : result.right()) {
if (eq(status, expectedStatus) || now >= timeout) {
return right(status);
} else if (now < timeout) {
if (!sleep(pollingInterval)) {
return left(new Exception("Interrupted"));
} else {
now = now + pollingInterval;
}
}
}
for (Exception e : result.left()) {
return left(e);
}
}
}
use of org.apache.http.client.methods.HttpHead in project opencast by opencast.
the class TrustedHttpClientImplTest method testAlreadySignedUrlIgnoredByUrlSigningService.
@Test
public void testAlreadySignedUrlIgnoredByUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://alreadysigned.com?signature=thesig&policy=thepolicy&keyId=thekey";
HttpHead headRequest = new HttpHead(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(headRequest);
assertTrue(request.hasCaptured());
assertEquals(acceptsUrl, request.getValue().getURI().toString());
}
Aggregations