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();
}
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();
}
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);
}
}
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);
}
}
Aggregations