Search in sources :

Example 11 with VBoardException

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

the class UsersController method addNewUser.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public // Parsing the params in the JSON body requires using a dedicated @RequestBody annotated class instead of simple @RequestParam arguments
User addNewUser(@Valid @RequestBody UserParams params) {
    final String email = params.getEmail();
    final String firstName = params.getFirst_name();
    final String lastName = params.getLast_name();
    final User newUser = new User(email, firstName, lastName);
    try {
        this.logger.debug("addNewUser: email={} - first_name={} - last_name={}", email, firstName, lastName);
        return this.userDAO.save(newUser);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 12 with VBoardException

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

the class UploadsManager method savePinImage.

// img should be the base64 encoded image (exception: url)
public void savePinImage(String img, String name) {
    if (!img.startsWith("http")) {
        // If the image is an url, the image is downloaded and uploaded on the pinImg folder (NAS)
        byte[] data = Base64.getDecoder().decode(img);
        try (OutputStream stream = new FileOutputStream(getPinsImagesDirectory().resolve(name + ".png").toFile())) {
            stream.write(data);
            stream.close();
        } catch (Exception e) {
            this.logger.error(e.getMessage());
        }
    } else if (!img.endsWith("svg") && !img.endsWith("gif")) {
        URL url;
        try {
            if (proxyConfig.getProxy() != Proxy.NO_PROXY) {
                url = new URL("http", proxyConfig.getHostname(), proxyConfig.getPort(), img);
            } else {
                url = new URL(img);
            }
        } catch (MalformedURLException e) {
            throw new VBoardException("Could not retrieve pin image from the web", e);
        }
        try {
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(getPinsImagesDirectory().resolve(name + ".png").toFile());
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
        } catch (IOException e) {
            throw new VBoardException("Could not write pin image to filesystem", e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) VBoardException(com.vsct.vboard.models.VBoardException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) MalformedURLException(java.net.MalformedURLException) VBoardException(com.vsct.vboard.models.VBoardException) URL(java.net.URL)

Example 13 with VBoardException

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

the class UploadsManager method getImage.

/**
 * Get the base64 encoded image of a pin (entry parameter: id)
 *
 * @return String base64 encoded image
 */
public String getImage(String name) {
    try {
        BufferedImage img = ImageIO.read(getPinsImagesDirectory().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("Image encoding error", e);
    }
}
Also used : VBoardException(com.vsct.vboard.models.VBoardException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage)

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