SUSI Skill CMS is a web app to create and modify SUSI Skills. It needs API Endpoints to function and SUSI Server makes it possible. In this blogpost, we will see how to add a servlet to SUSI Server to upload images and files.
The CreateSkillService.java file is the servlet which handles the process of creating new Skills. It requires different user roles to be implemented and hence it extends the AbstractAPIHandler.
Image upload is only possible via a POST request so we will first override the doPost method in this servlet.
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Access-Control-Allow-Origin", "*"); // enable CORS
resp.setHeader enables the CORS for the servlet. This is required as POST requests must have CORS enables from the server. This is an important security feature that is provided by the browser.
Part file = req.getPart("image"); if (file == null) { json.put("accepted", false); json.put("message", "Image not given"); }
Image upload to servers is usually a Multipart Request. So we get the part which is named as “image” in the form data.
When we receive the image file, then we check if the image with the same name exists on the server or not.
Path p = Paths.get(language + File.separator + “images/” + image_name);
if (image_name == null || Files.exists(p)) { json.put("accepted", false); json.put("message", "The Image name not given or Image with same name is already present "); }
If the same file is present on the server then we return an error to the user requesting to give a unique filename to upload.
Image image = ImageIO.read(filecontent); BufferedImage bi = this.createResizedCopy(image, 512, 512, true); if(!Files.exists(Paths.get(language.getPath() + File.separator + "images"))){ new File(language.getPath() + File.separator + "images").mkdirs(); } ImageIO.write(bi, "jpg", new File(language.getPath() + File.separator + "images/" + image_name));
Then we read the content for the image in an Image object. Then we check if images directory exists or not. If there is no image directory in the skill path specified then create a folder named “images”.
We usually prefer square images at the Skill CMS. So we create a resized copy of the image of 512×512 dimensions and save that copy to the directory we created above.
BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha) { int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType); Graphics2D g = scaledBI.createGraphics(); if (preserveAlpha) { g.setComposite(AlphaComposite.Src); } g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); return scaledBI; }
The function above is used to create a resized copy of the image of specified dimensions. If the image was a PNG then it also preserves the transparency of the image while creating a copy.
Since the SUSI server follows an API centric approach, all servlets respond in JSON.
resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().write(json.toString());’
At last, we set the character encoding and the character set of the output. This helps the clients to parse the data easily.
To see this endpoint in live send a POST request at http://api.susi.ai/cms/createSkill.json.
Resources
Apache Docs: https://commons.apache.org/proper/commons-fileupload/using.html
Multipart POST Request Tutorial: http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically
Java File Upload tutorial: https://ursaj.com/upload-files-in-java-with-servlet-api
Jetty Project: https://github.com/jetty-project/