Search in sources :

Example 1 with DisplayPortraitManager

use of org.olat.user.DisplayPortraitManager in project OpenOLAT by OpenOLAT.

the class UserMgmtTest method testPortrait.

@Test
public void testPortrait() throws IOException, URISyntaxException {
    URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    // upload portrait
    URI request = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/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 if big and small portraits exist
    DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
    File bigPortrait = dps.getBigPortrait(id1.getName());
    assertNotNull(bigPortrait);
    assertTrue(bigPortrait.exists());
    assertTrue(bigPortrait.exists());
    // check get portrait
    URI getRequest = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/portrait").build();
    HttpGet getMethod = conn.createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse getResponse = conn.execute(getMethod);
    assertEquals(200, getResponse.getStatusLine().getStatusCode());
    InputStream in = getResponse.getEntity().getContent();
    int b = 0;
    int count = 0;
    while ((b = in.read()) > -1) {
        count++;
    }
    // up to end of file
    assertEquals(-1, b);
    // enough bytes
    assertTrue(count > 1000);
    bigPortrait = dps.getBigPortrait(id1.getName());
    assertNotNull(bigPortrait);
    assertEquals(count, bigPortrait.length());
    // check get portrait as Base64
    UriBuilder getRequest2 = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).queryParam("withPortrait", "true");
    HttpGet getMethod2 = conn.createGet(getRequest2.build(), MediaType.APPLICATION_JSON, true);
    HttpResponse getCode2 = conn.execute(getMethod2);
    assertEquals(200, getCode2.getStatusLine().getStatusCode());
    UserVO userVo = conn.parse(getCode2, UserVO.class);
    assertNotNull(userVo);
    assertNotNull(userVo.getPortrait());
    byte[] datas = Base64.decodeBase64(userVo.getPortrait().getBytes());
    assertNotNull(datas);
    assertTrue(datas.length > 0);
    File smallPortrait = dps.getSmallPortrait(id1.getName());
    assertNotNull(smallPortrait);
    assertEquals(datas.length, smallPortrait.length());
    try {
        ImageIO.read(new ByteArrayInputStream(datas));
    } catch (Exception e) {
        assertFalse("Cannot read the portrait after Base64 encoding/decoding", false);
    }
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DisplayPortraitManager(org.olat.user.DisplayPortraitManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ManagedUserVO(org.olat.user.restapi.ManagedUserVO) UserVO(org.olat.user.restapi.UserVO) ByteArrayInputStream(java.io.ByteArrayInputStream) UriBuilder(javax.ws.rs.core.UriBuilder) File(java.io.File) Test(org.junit.Test)

Example 2 with DisplayPortraitManager

use of org.olat.user.DisplayPortraitManager in project openolat by klemens.

the class UserMgmtTest method testPortrait.

@Test
public void testPortrait() throws IOException, URISyntaxException {
    URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    // upload portrait
    URI request = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/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 if big and small portraits exist
    DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
    File bigPortrait = dps.getBigPortrait(id1.getName());
    assertNotNull(bigPortrait);
    assertTrue(bigPortrait.exists());
    assertTrue(bigPortrait.exists());
    // check get portrait
    URI getRequest = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/portrait").build();
    HttpGet getMethod = conn.createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse getResponse = conn.execute(getMethod);
    assertEquals(200, getResponse.getStatusLine().getStatusCode());
    InputStream in = getResponse.getEntity().getContent();
    int b = 0;
    int count = 0;
    while ((b = in.read()) > -1) {
        count++;
    }
    // up to end of file
    assertEquals(-1, b);
    // enough bytes
    assertTrue(count > 1000);
    bigPortrait = dps.getBigPortrait(id1.getName());
    assertNotNull(bigPortrait);
    assertEquals(count, bigPortrait.length());
    // check get portrait as Base64
    UriBuilder getRequest2 = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).queryParam("withPortrait", "true");
    HttpGet getMethod2 = conn.createGet(getRequest2.build(), MediaType.APPLICATION_JSON, true);
    HttpResponse getCode2 = conn.execute(getMethod2);
    assertEquals(200, getCode2.getStatusLine().getStatusCode());
    UserVO userVo = conn.parse(getCode2, UserVO.class);
    assertNotNull(userVo);
    assertNotNull(userVo.getPortrait());
    byte[] datas = Base64.decodeBase64(userVo.getPortrait().getBytes());
    assertNotNull(datas);
    assertTrue(datas.length > 0);
    File smallPortrait = dps.getSmallPortrait(id1.getName());
    assertNotNull(smallPortrait);
    assertEquals(datas.length, smallPortrait.length());
    try {
        ImageIO.read(new ByteArrayInputStream(datas));
    } catch (Exception e) {
        assertFalse("Cannot read the portrait after Base64 encoding/decoding", false);
    }
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DisplayPortraitManager(org.olat.user.DisplayPortraitManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ManagedUserVO(org.olat.user.restapi.ManagedUserVO) UserVO(org.olat.user.restapi.UserVO) ByteArrayInputStream(java.io.ByteArrayInputStream) UriBuilder(javax.ws.rs.core.UriBuilder) File(java.io.File) Test(org.junit.Test)

Example 3 with DisplayPortraitManager

use of org.olat.user.DisplayPortraitManager in project OpenOLAT by OpenOLAT.

the class UserWebService method getOriginalPortraitHead.

/**
 * Retrieves the portrait of an user
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param identityKey The identity key of the user being searched
 * @return The image
 */
@HEAD
@Path("{identityKey}/portrait/{size}")
@Produces({ "image/jpeg", "image/jpg", MediaType.APPLICATION_OCTET_STREAM })
public Response getOriginalPortraitHead(@PathParam("identityKey") Long identityKey, @PathParam("size") String size) {
    try {
        IdentityShort identity = BaseSecurityManager.getInstance().loadIdentityShortByKey(identityKey);
        if (identity == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        DisplayPortraitManager portraitManager = DisplayPortraitManager.getInstance();
        File portrait = null;
        if ("master".equals(size)) {
            portrait = portraitManager.getMasterPortrait(identity.getName());
        } else if ("big".equals(size)) {
            portrait = portraitManager.getBigPortrait(identity.getName());
        } else if ("small".equals(size)) {
            portrait = portraitManager.getSmallPortrait(identity.getName());
        }
        if (portrait == null || !portrait.exists()) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        Date lastModified = new Date(portrait.lastModified());
        return Response.ok().lastModified(lastModified).build();
    } catch (Throwable e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) DisplayPortraitManager(org.olat.user.DisplayPortraitManager) IdentityShort(org.olat.basesecurity.IdentityShort) File(java.io.File) Date(java.util.Date) Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD) Produces(javax.ws.rs.Produces)

Example 4 with DisplayPortraitManager

use of org.olat.user.DisplayPortraitManager in project openolat by klemens.

the class UserWebService method getOriginalPortraitHead.

/**
 * Retrieves the portrait of an user
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param identityKey The identity key of the user being searched
 * @return The image
 */
@HEAD
@Path("{identityKey}/portrait/{size}")
@Produces({ "image/jpeg", "image/jpg", MediaType.APPLICATION_OCTET_STREAM })
public Response getOriginalPortraitHead(@PathParam("identityKey") Long identityKey, @PathParam("size") String size) {
    try {
        IdentityShort identity = BaseSecurityManager.getInstance().loadIdentityShortByKey(identityKey);
        if (identity == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        DisplayPortraitManager portraitManager = DisplayPortraitManager.getInstance();
        File portrait = null;
        if ("master".equals(size)) {
            portrait = portraitManager.getMasterPortrait(identity.getName());
        } else if ("big".equals(size)) {
            portrait = portraitManager.getBigPortrait(identity.getName());
        } else if ("small".equals(size)) {
            portrait = portraitManager.getSmallPortrait(identity.getName());
        }
        if (portrait == null || !portrait.exists()) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        Date lastModified = new Date(portrait.lastModified());
        return Response.ok().lastModified(lastModified).build();
    } catch (Throwable e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) DisplayPortraitManager(org.olat.user.DisplayPortraitManager) IdentityShort(org.olat.basesecurity.IdentityShort) File(java.io.File) Date(java.util.Date) Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD) Produces(javax.ws.rs.Produces)

Aggregations

File (java.io.File)4 DisplayPortraitManager (org.olat.user.DisplayPortraitManager)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Date (java.util.Date)2 HEAD (javax.ws.rs.HEAD)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 HttpResponse (org.apache.http.HttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 Test (org.junit.Test)2 IdentityShort (org.olat.basesecurity.IdentityShort)2 ManagedUserVO (org.olat.user.restapi.ManagedUserVO)2