Search in sources :

Example 11 with HttpHead

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();
}
Also used : CertificateInfos(org.olat.course.certificate.model.CertificateInfos) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) Certificate(org.olat.course.certificate.Certificate) Test(org.junit.Test)

Example 12 with HttpHead

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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) File(java.io.File) URI(java.net.URI) URL(java.net.URL) HttpHead(org.apache.http.client.methods.HttpHead) Test(org.junit.Test)

Example 13 with HttpHead

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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) File(java.io.File) URI(java.net.URI) URL(java.net.URL) HttpHead(org.apache.http.client.methods.HttpHead) Test(org.junit.Test)

Example 14 with HttpHead

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);
        }
    }
}
Also used : HttpHead(org.apache.http.client.methods.HttpHead) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 15 with HttpHead

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());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) UrlSigningService(org.opencastproject.security.urlsigning.service.UrlSigningService) HttpParams(org.apache.http.params.HttpParams) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpClient(org.opencastproject.kernel.http.api.HttpClient) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) HttpHead(org.apache.http.client.methods.HttpHead) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)100 HttpResponse (org.apache.http.HttpResponse)40 HttpGet (org.apache.http.client.methods.HttpGet)28 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)25 Test (org.junit.Test)24 IOException (java.io.IOException)23 URI (java.net.URI)22 HttpPut (org.apache.http.client.methods.HttpPut)22 Header (org.apache.http.Header)21 HttpPost (org.apache.http.client.methods.HttpPost)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)15 HttpDelete (org.apache.http.client.methods.HttpDelete)13 InputStream (java.io.InputStream)12 HttpEntity (org.apache.http.HttpEntity)10 File (java.io.File)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpOptions (org.apache.http.client.methods.HttpOptions)8 URISyntaxException (java.net.URISyntaxException)6 URL (java.net.URL)6