Search in sources :

Example 6 with VBoardException

use of com.vsct.vboard.models.VBoardException in project vboard by voyages-sncf-technologies.

the class UploadsManager method getAvatar.

/**
 * Get the base64 encoded avatar of a profil (user or team)
 *
 * @return String base64 encoded image
 */
public String getAvatar(String name) {
    try {
        BufferedImage img = ImageIO.read(getAvatarImagesDirectory().resolve(name + ".png").toFile());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(img, "png", bos);
        byte[] imageBytes = bos.toByteArray();
        bos.close();
        return Base64.getEncoder().encodeToString(imageBytes);
    } catch (IOException e) {
        throw new VBoardException("Avatar encoding error", e);
    }
}
Also used : VBoardException(com.vsct.vboard.models.VBoardException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 7 with VBoardException

use of com.vsct.vboard.models.VBoardException in project vboard by voyages-sncf-technologies.

the class PinsControllerTest method update.

@Test
public void update() throws IOException {
    this.pinDAO.save(new Pin("0", "title", "", 0, "", "", "content", "auth", new DateTime()));
    String[] labels = { "label" };
    ArrayList<Pin> pins = new ArrayList<>();
    pins.add(this.pinDAO.findByPinId("0"));
    Mockito.doReturn(pins).when(elsClient).searchPinsById("0");
    try {
        this.pinsController.updatePin(new AddNewPinParams("titleupdate", "url", "imgtype", "contentupdate", labels, "auth"), "notfound");
        Assert.fail("Epingle non trouvee, sans erreur");
    } catch (VBoardException e) {
    }
    Assert.assertEquals(this.pinDAO.findByPinId("0").getPinTitle(), "title");
    this.pinsController.updatePin(new AddNewPinParams("titleupdate", "url", "imgtype", "contentupdate", labels, "author"), "0");
    Pin pin = this.pinDAO.findByPinId("0");
    Assert.assertEquals("titleupdate", pin.getPinTitle());
    Assert.assertEquals("url", pin.getHrefUrl());
    Assert.assertEquals("/pinImg/0.png", pin.getImgType());
    Assert.assertEquals("contentupdate", pin.getIndexableTextContent());
    Assert.assertEquals("label", pin.getLabels());
    Assert.assertEquals("auth", pin.getAuthor());
    this.pinsController.updatePin(new AddNewPinParams("titleupdate", "url", null, "contentupdate", labels, "author"), "0");
    pin = this.pinDAO.findByPinId("0");
    Assert.assertNull(pin.getImgType());
}
Also used : VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) ArrayList(java.util.ArrayList) AddNewPinParams(com.vsct.vboard.parameterFormat.AddNewPinParams) DateTime(org.joda.time.DateTime) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with VBoardException

use of com.vsct.vboard.models.VBoardException in project vboard by voyages-sncf-technologies.

the class AuthenticationController method getSessionUserWithSyncFromDB.

@NotNull
public User getSessionUserWithSyncFromDB() {
    final User sessionUser = this.getSessionUser();
    if (ANONYMOUS_USER.equals(sessionUser)) {
        return ANONYMOUS_USER;
    }
    final User dbUser = this.userDAO.findByEmail(sessionUser.getEmail());
    if (dbUser == null) {
        throw new VBoardException("No user found in DB for email=" + sessionUser.getEmail());
    }
    if (!dbUser.equals(this.getSessionUser())) {
        this.logger.debug("Updating user in session cache");
        this.session.setAttribute("User", dbUser);
    }
    return dbUser;
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) NotNull(javax.validation.constraints.NotNull)

Example 9 with VBoardException

use of com.vsct.vboard.models.VBoardException in project vboard by voyages-sncf-technologies.

the class CommentsController method addComment.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public Comment addComment(@Valid @RequestBody CommentParams params) {
    String author = params.getAuthor();
    String pinId = params.getPinId();
    String text = params.getText();
    DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
    Comment comment = new Comment(pinId, author, text, postDateUTC);
    // Check if the author given is the same as the session user (the author param could be deleted and replaced with the session one)
    permission.ensureNewEntityAuthorMatchesSessionUser(author);
    try {
        this.logger.debug("addComment: author={} - pin={} -text={}", author, pinId, text);
        this.commentDAO.save(comment);
        Pin pin = this.pinDAO.findByPinId(pinId);
        if (pin != null) {
            pin.increaseCommentsNumber();
            this.pinDAO.save(pin);
        }
        // Increase the number of comments for the given pin in elasticsearch
        this.elsClient.addComment(comment.getPinId());
        // Update the stats (for the user, and for the author of the pin where the comment has been added)
        this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
        if (pin != null && User.getEmailFromString(pin.getAuthor()).isPresent()) {
            User userAuthor = this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get());
            if (userAuthor != null && userAuthor != permission.getSessionUserWithSyncFromDB()) {
                this.gamification.updateStats(userAuthor);
            }
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    // Send comment notifications
    notifications.addNotificationsFromComment(pinId);
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 10 with VBoardException

use of com.vsct.vboard.models.VBoardException in project vboard by voyages-sncf-technologies.

the class CommentsController method addCommentFromVblog.

// Comments posted from VBlog (wordpress)
@RequestMapping(value = "/vblog", method = RequestMethod.POST, consumes = { "application/x-www-form-urlencoded" })
@ResponseBody
@Valid
public Comment addCommentFromVblog(@RequestParam("text") final String text, @RequestParam("pinId") String pinId, @RequestParam("author") String author, @RequestParam("ID") final String ID, HttpServletRequest request) {
    Comment comment = this.commentDAO.findById("vblog-" + ID);
    // Should restrict the host name from wordpress (vblog)
    /*if (!request.getRemoteHost().equals(hostName.getHostName())) {
            throw new VBoardException("Unknown web site - The hostname that is using this method is not authorized: hostname: " + request.getRemoteHost());
        }*/
    User user = this.userDAO.findByEmail(author);
    if (user != null) {
        author = user.getUserString();
    }
    if (comment == null) {
        DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
        comment = new Comment("vblog-" + ID, "vblog-" + pinId, author, text, postDateUTC.toString());
        Pin pin = this.pinDAO.findByPinId("vblog-" + pinId);
        if (pin != null) {
            pin.increaseCommentsNumber();
            this.pinDAO.save(pin);
            if (User.getEmailFromString(pin.getAuthor()).isPresent()) {
                User userAuthor = this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get());
                if (userAuthor != null && userAuthor != permission.getSessionUserWithSyncFromDB()) {
                    // Update the pin's author stats
                    this.gamification.updateStats(userAuthor);
                }
            }
        }
    } else {
        comment.setText(text);
        comment.setAuthor(author);
    }
    try {
        this.logger.debug("addComment: author={} - pin={} -text={}", author, "vblog-" + pinId, text);
        this.commentDAO.save(comment);
        // Increase the number of comments for the given pin in elasticsearch
        this.elsClient.addComment(comment.getPinId());
        // Send comment notifications
        notifications.addNotificationsFromComment(comment.getPinId());
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    if (user != null) {
        // Update the user's stats
        this.gamification.updateStats(user);
    }
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Aggregations

VBoardException (com.vsct.vboard.models.VBoardException)13 User (com.vsct.vboard.models.User)8 Valid (javax.validation.Valid)6 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)6 Pin (com.vsct.vboard.models.Pin)4 Comment (com.vsct.vboard.models.Comment)3 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)3 DateTime (org.joda.time.DateTime)3 BufferedImage (java.awt.image.BufferedImage)2 AddNewPinParams (com.vsct.vboard.parameterFormat.AddNewPinParams)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 NotNull (javax.validation.constraints.NotNull)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1