Search in sources :

Example 6 with Picture

use of org.activiti.engine.identity.Picture in project Activiti by Activiti.

the class UserPictureResource method getUserPicture.

@RequestMapping(value = "/identity/users/{userId}/picture", method = RequestMethod.GET)
public ResponseEntity<byte[]> getUserPicture(@PathVariable String userId, HttpServletRequest request, HttpServletResponse response) {
    User user = getUserFromRequest(userId);
    Picture userPicture = identityService.getUserPicture(user.getId());
    if (userPicture == null) {
        throw new ActivitiObjectNotFoundException("The user with id '" + user.getId() + "' does not have a picture.", Picture.class);
    }
    HttpHeaders responseHeaders = new HttpHeaders();
    if (userPicture.getMimeType() != null) {
        responseHeaders.set("Content-Type", userPicture.getMimeType());
    } else {
        responseHeaders.set("Content-Type", "image/jpeg");
    }
    try {
        return new ResponseEntity<byte[]>(IOUtils.toByteArray(userPicture.getInputStream()), responseHeaders, HttpStatus.OK);
    } catch (Exception e) {
        throw new ActivitiException("Error exporting picture: " + e.getMessage(), e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ActivitiException(org.activiti.engine.ActivitiException) User(org.activiti.engine.identity.User) Picture(org.activiti.engine.identity.Picture) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with Picture

use of org.activiti.engine.identity.Picture in project Activiti by Activiti.

the class UserDetailPanel method loadPicture.

protected void loadPicture() {
    Component pictureComponent = null;
    final Picture userPicture = identityService.getUserPicture(user.getId());
    if (userPicture != null) {
        StreamResource imageresource = new StreamResource(new StreamSource() {

            private static final long serialVersionUID = 1L;

            public InputStream getStream() {
                return userPicture.getInputStream();
            }
        }, user.getId() + "." + Constants.MIMETYPE_EXTENSION_MAPPING.get(userPicture.getMimeType()), ExplorerApp.get());
        pictureComponent = new Embedded(null, imageresource);
    } else {
        pictureComponent = new Label("");
    }
    pictureComponent.setHeight("200px");
    pictureComponent.setWidth("200px");
    pictureComponent.addStyleName(ExplorerLayout.STYLE_PROFILE_PICTURE);
    userDetailsLayout.addComponent(pictureComponent);
    userDetailsLayout.setComponentAlignment(pictureComponent, Alignment.MIDDLE_CENTER);
}
Also used : StreamResource(com.vaadin.terminal.StreamResource) Picture(org.activiti.engine.identity.Picture) InputStream(java.io.InputStream) StreamSource(com.vaadin.terminal.StreamResource.StreamSource) Label(com.vaadin.ui.Label) Embedded(com.vaadin.ui.Embedded) Component(com.vaadin.ui.Component)

Example 8 with Picture

use of org.activiti.engine.identity.Picture in project Activiti by Activiti.

the class UserPictureResourceTest method testUpdatePictureWithCustomMimeType.

public void testUpdatePictureWithCustomMimeType() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;
        Map<String, String> additionalFields = new HashMap<String, String>();
        additionalFields.put("mimeType", MediaType.IMAGE_PNG.toString());
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_PICTURE, newUser.getId()));
        httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("myPicture.png", "image/png", new ByteArrayInputStream("this is the picture raw byte stream".getBytes()), additionalFields));
        closeResponse(executeBinaryRequest(httpPut, HttpStatus.SC_NO_CONTENT));
        Picture picture = identityService.getUserPicture(newUser.getId());
        assertNotNull(picture);
        assertEquals("image/png", picture.getMimeType());
        assertEquals("this is the picture raw byte stream", new String(picture.getBytes()));
    } finally {
        // Delete user after test passes or fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}
Also used : User(org.activiti.engine.identity.User) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Picture(org.activiti.engine.identity.Picture) HttpPut(org.apache.http.client.methods.HttpPut)

Example 9 with Picture

use of org.activiti.engine.identity.Picture in project Activiti by Activiti.

the class UserPictureResourceTest method testUpdatePicture.

public void testUpdatePicture() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_PICTURE, newUser.getId()));
        httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("myPicture.png", "image/png", new ByteArrayInputStream("this is the picture raw byte stream".getBytes()), null));
        closeResponse(executeBinaryRequest(httpPut, HttpStatus.SC_NO_CONTENT));
        Picture picture = identityService.getUserPicture(newUser.getId());
        assertNotNull(picture);
        assertEquals("image/png", picture.getMimeType());
        assertEquals("this is the picture raw byte stream", new String(picture.getBytes()));
    } finally {
        // Delete user after test passes or fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}
Also used : User(org.activiti.engine.identity.User) ByteArrayInputStream(java.io.ByteArrayInputStream) Picture(org.activiti.engine.identity.Picture) HttpPut(org.apache.http.client.methods.HttpPut)

Example 10 with Picture

use of org.activiti.engine.identity.Picture in project Activiti by Activiti.

the class UserPictureResourceTest method testGetUserPicture.

/**
   * Test getting the picture for a user.
   */
public void testGetUserPicture() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;
        // Create picture for user
        Picture thePicture = new Picture("this is the picture raw byte stream".getBytes(), "image/png");
        identityService.setUserPicture(newUser.getId(), thePicture);
        CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_PICTURE, newUser.getId())), HttpStatus.SC_OK);
        assertEquals("this is the picture raw byte stream", IOUtils.toString(response.getEntity().getContent()));
        // Check if media-type is correct
        assertEquals("image/png", response.getEntity().getContentType().getValue());
        closeResponse(response);
    } finally {
        // Delete user after test passes or fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}
Also used : User(org.activiti.engine.identity.User) Picture(org.activiti.engine.identity.Picture) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

Picture (org.activiti.engine.identity.Picture)15 User (org.activiti.engine.identity.User)9 StreamResource (com.vaadin.terminal.StreamResource)4 Embedded (com.vaadin.ui.Embedded)4 StreamSource (com.vaadin.terminal.StreamResource.StreamSource)3 Resource (com.vaadin.terminal.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ActivitiException (org.activiti.engine.ActivitiException)2 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)2 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2 HttpPut (org.apache.http.client.methods.HttpPut)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MouseEvents (com.vaadin.event.MouseEvents)1 ClickEvent (com.vaadin.event.MouseEvents.ClickEvent)1 Component (com.vaadin.ui.Component)1 Label (com.vaadin.ui.Label)1 Upload (com.vaadin.ui.Upload)1 FinishedEvent (com.vaadin.ui.Upload.FinishedEvent)1 FinishedListener (com.vaadin.ui.Upload.FinishedListener)1