Posts

Building REST API web service using AKKA-HTTP, showing CRUD operations baked with Redis

Building REST API web service using AKKA-HTTP, showing CRUD operations baked with Redis AKKA-HTTP  is a lightweight layer that enable us to easily build our REST API over the akka actor system . In this article I will show step by step how to create simple web server with REST API Service and CRUD operations using  Rediscala  that will enable non-blocking and asynchronous I/O operations over Redis, sowing different options of completing the API response and serializing entities . And of course how to test our REST API. Full source code can be found  here Our web server will expose API for handling user passwords that will be kept in Redis (this is not a good practice to handle it like this in real life, and one might want to consider to add encryption etc’ in production). Our Rest API will expose the following functionality : Register new user Update user password Fetch user password Delete user So, let’s get our hands dirty First step — add dependencies to our Bu

Simple and useful Zip and unZip text

Simple and usefull textual zip and unzip. First we use binary zip . but if we want to send it as a text message (e.g some message bus supports only text messages) we need to convert it to text by encoding to base 64 and vice versa to unzip it. import java.io.ByteArrayInputStream import java.nio.charset.StandardCharsets import java.util.zip.{GZIPOutputStream, GZIPInputStream} import org.apache.commons.codec.binary.Base64 import org.apache.commons.io.output.ByteArrayOutputStream import scala.util.Try val zipToBinary: String => Array[Byte] = {txt => val arrOutputStream = new ByteArrayOutputStream() val zipOutputStream = new GZIPOutputStream(arrOutputStream) zipOutputStream.write(txt.getBytes(StandardCharsets.UTF_8)) zipOutputStream.close() arrOutputStream.toByteArray } val byteArrayToTxt : Array[Byte] => String = bytes => Base64.encodeBase64String(bytes) val txtToBinary : String => Array[Byte]= txt => Base64.decodeBase64(txt.getBytes(Sta

Playing with xml

XML is a convenient way to define external DSL. On one of my previous posts  ( Filtering using Scala Parser Combinators ) I used parser combinators  JavaTokenParsers with PackratParsers  This time I will use different approach using XML . XML is easy to handle by non-developers and easy to read. We can query XML using XPATH like expressions and using pattern matching. In this post we will define some business rules using XML and we will treat our XML as first class citizen. Source code can be found  here . let's define our DSL : Our rules will use AND/OR operators but to make it more interesting, our operators  will not use  the following pattern: <expression><operator><expression>  (where expression evaluates to boolean). because XML have opening and closing tags we can define something like : <operator><expression1><expression2>...<expression N></operator> now we can define our AND/OR operators : AND: a set of exp

Presenting the File Tracker

Image
This project goal is to track changes in files and manage those changes as a byte array in asynchronous way using Akka actors and Java NIO library. This is done by registering directory for the  WatchService  and filtering the files using  PathMatcher  . For each change in the file the requester will receive the byte array reflecting that change. Currently this project supports only addition to file, i.e deletion of characters in file is not supported. The Complete Source Code can be found here Let's dive in. The Ingredients : FileSyncIo . This part is copied from the  FileAsyncIo project  with some adjustments, and it is very handy for reading files asynchronously. In order to read the file we use Java NIO  AsynchronousFileChannel  . Since we are only reading the file, we open the channel with the  Read  option. The AsynchronousFileChannel.read method accepts buffer, the start position and a handler : val p = Promise [ Array [ Byte ]]() val buffe