Tuesday, 21 November 2017

using java lambdas as groovy closure



using java lambdas from groovy

I have a scala method that takes java.util.function.Supplier as parameter.
class TestKit(system: ActorSystem) {
   def awaitCond(p: Supplier[Boolean]): Unit = tp.awaitCond(p.get)
}
syntactic sugar for Supplier functor would be () -> ... in java, but is not compiled in groovy.
def actorSystem = ActorSystem.create("actor-system")
def verifier = new TestKit(actorSystem)
verifier.awaitCond { -> return verifier.msgAvailable()}

Thursday, 16 November 2017

debugging scala web artifact deployed on docker + tomcat, from intellij



STEP 1 - enable "Java Platform Debugger Architecture" port in docker
FROM tomcat:8.0.46-jre8                                                                                
                                                                                                       
ENV JAVA_OPTS="-Denvironment=local"                                                                    
#ENV CATALINA_OPTS="$CATALINA_OPTS -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
                                                                                                       
ENV JPDA_ADDRESS 8000                                                                                  
COPY nlu-processor-service/target/*.war /usr/local/tomcat/webapps/nlu-processor-1.0.war
                                                                                                       
EXPOSE 8080                                                                                                                                            
CMD ["catalina.sh", "jpda", "run"]

If it's simply tomcat(without docker), create bin/setenv.sh and add jpda port address.

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"


STEP 2 - add remote debugging to intellij

Set the JPDA port as set in setenv.sh or Dockerfile







Wednesday, 15 November 2017

scala pattern matches



collect pattern matches,

scala> val data = Map((1 -> ("prog" -> "Pink floyd")), (2 -> ("metal" -> "Architects")), (3 -> ("prog" -> "Porcupine Tree")))
data: scala.collection.immutable.Map[Int,(String, String)] = Map(1 -> (prog,Pink floyd), 2 -> (metal,Architects), 3 -> (prog,Porcupine Tree))

scala> data.collect {case (k, v) if v._1 == "prog" => v._2}
res9: scala.collection.immutable.Iterable[String] = List(Pink floyd, Porcupine Tree)

scala> data.collect {case (k, (v1, v2)) if v1 == "prog" => v2}

res10: scala.collection.immutable.Iterable[String] = List(Pink floyd, Porcupine Tree)

scala foldLeft pattern matches,

scala> val data = Map((1 -> ("prog" -> "Pink floyd")), (2 -> ("metal" -> "Architects")), (3 -> ("prog" -> "Porcupine Tree")))
data: scala.collection.immutable.Map[Int,(String, String)] = Map(1 -> (prog,Pink floyd), 2 -> (metal,Architects), 3 -> (prog,Porcupine Tree))

scala> data.foldLeft(""){case (text, (id, (genre, artist))) => text + "\n" + artist}
res17: String =
"
Pink floyd
Architects

Porcupine Tree"




Friday, 3 November 2017

collectFirst on Map of Sequence


To find a product first bought from a map of year to List of Items

scala> def firstGuitar(list: Seq[String]) = list.collectFirst { case item if item == "Guitar" => item }
firstGuitar: (list: Seq[String])Option[String]

scala> Map(2016 -> Seq("Cap", "Guitar", "Cap"), 
           2017 -> Seq("Here and now album", "Drum Kits", "Cap", "Guitar")).collectFirst { case (year, itemsBought) 
                        if firstGuitar(itemsBought).nonEmpty => (year -> firstGuitar(itemsBought))}
res2: Option[(Int, Option[String])] = Some((2016,Some(Guitar)))

scala> Map(2016 -> Seq("Cap", "Camera", "Cap"), 
            2017 -> Seq("Here and now album", "Drum Kits", "Cap", "Tshirt")).collectFirst { case (year, itemsBought) 
                       if firstGuitar(itemsBought).nonEmpty => (year -> firstGuitar(itemsBought))}
res3: Option[(Int, Option[String])] = None


Monday, 23 October 2017

clojure epoch time to LocalDateTime to UTC

Part 1 - epoch millis to LocalDateTime in machine with PST Zone 


user=> (import java.util.TimeZone)
java.util.TimeZone
user=> (TimeZone/getDefault)
#object[sun.util.calendar.ZoneInfo 0x36a7961e "sun.util.calendar.ZoneInfo[id=\"America/Los_Angeles\",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]"]

user=> (import java.time.ZoneId)
user=> (import java.time.Instant)

user=> (.atZone (Instant/ofEpochMilli (long 1500318045000)) (ZoneId/systemDefault))
#object[java.time.ZonedDateTime 0x386969fa "2017-07-17T12:00:45-07:00[America/Los_Angeles]"]

user=> (.toLocalDateTime (.atZone (Instant/ofEpochMilli (long 1500318045000)) (ZoneId/systemDefault)))
#object[java.time.LocalDateTime 0x778e9284 "2017-07-17T12:00:45"]

Same epoch millis on a machine with UTC Zone, 

scala> import java.time.Instant
scala> import java.util.TimeZone

scala> TimeZone.getDefault
res9: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

scala> Instant.ofEpochMilli(1500318045000l).atZone(ZoneId.systemDefault()).toLocalDateTime
res10: java.time.LocalDateTime = 2017-07-17T19:00:45

// to milis to UTC
scala> Instant.ofEpochMilli(1500318045000l).atZone(ZoneId.of("UTC")).toInstant.toString
res36: String = 2017-07-17T19:00:45Z

Part 2 - Converting date-string to LocalDateTime and ZonedDateTime

user=> (import java.time.format.DateTimeFormatter)
user=> (import java.time.ZoneId)
user=> (import java.util.TimeZone)
user=> (import java.time.LocalDateTime)

(let [formatter  (DateTimeFormatter/ofPattern "yyyy-MM-dd'T'HH:mm")
       localDate   (LocalDateTime/parse "2017-01-28T04:00" formatter)
       myZone     (ZoneId/of "America/Chicago")
       zonedDate (.atZone localDate myZone)
       instant     (.toInstant zonedDate)]
       (print (.toString instant)))

2017-01-28T10:00:00Z

Sunday, 16 July 2017

color theme for javascript and HTML programming

I was using solarised dark for java/scala programming for last couple of years. But since last year sept, I'm more addicted to solarised light with increased font size (15).

I like to try new themes if they work well for my eyes. And recently for javascript/html programming I started using emacs color theme which is very cool. Looks similar to solarized light, but I can see all the keywords unlike in some themes where js keywords like console.log are too pink can't even recognize.


javascript/ typescript



html


color theme IngweLAND is pretty cool too.




Friday, 28 April 2017

HTTP request headers


Check request header with cURL (see URL)
- cURL  is  a tool to transfer data from or to a server, using one of the supported protocols like HTTPS/ SMTPS/ gopher etc. see https://curl.haxx.se/


$ curl -v -XGET 172.24.14.158
* Rebuilt URL to: 172.24.14.158/
*   Trying 172.24.14.158...
* Connected to 172.24.14.158 (172.24.14.158) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.24.14.158
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: text/html;charset=ISO-8859-1
< Date: Sun, 30 Apr 2017 06:29:31 GMT
< Set-Cookie: JSESSIONID=C565042EC3A26A25D4ECF6787B54CE76;path=/;HttpOnly
< Content-Length: 14
< Connection: keep-alive
< 
some.her.data
* Connection #0 to host 172.24.14.158 left intact

Check request header with browser



Sunday, 26 March 2017

Playing with java date format


TimeZone examples

On a machine with default timezone as US/Pacific

scala> import java.text.SimpleDateFormat
scala> import java.util.TimeZone
scala> import java.util.Date

scala> TimeZone.getDefault()
res26: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="US/Pacific",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=US/Pacific,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]


scala> val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("US/Pacific"))

scala> val date = dateFormat.format(new Date(2017-1900, 10-1, 28)) //because new Date(2017-1900, 10-1, 28) takes default timezone
warning: there was one deprecation warning; re-run with -deprecation for details
date: String = 2017-10-28

scala> dateFormat.parse(date).getTime
res1: Long = 1509174000000

On a machine with default timezone as America/Los_Angeles,

scala> TimeZone.getDefault()
res11: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

scala> val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))

scala> val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
<console>:18: warning: constructor Date in class Date is deprecated: see corresponding Javadoc for more information.
       val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
                                    ^
date: String = 2017-10-28

scala> dateFormat.parse(date).getTime
res13: Long = 1509174000000

On a machine with default timezone as Etc/UTC

scala> import java.text.SimpleDateFormat
scala> import java.util.TimeZone
scala> import java.util.Date

scala> TimeZone.getDefault()
res9: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

scala> val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("US/Pacific"))

scala>  val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
warning: there was one deprecation warning; re-run with -deprecation for details
date: String = 2017-10-27

scala>  dateFormat.parse(date).getTime
res2: Long = 1509087600000

date formats

scala> import java.util.Calendar
import java.util.Calendar

scala> val today = Calendar.getInstance().getTime()
today: java.util.Date = Sun Mar 26 00:58:12 PDT 2017

scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat

scala> val hourMinuteSecs = new SimpleDateFormat("kkmmss").format(today)
hourMinuteSecs: String = 245812

scala> val hourMinuteSecs = new SimpleDateFormat("hhmmss").format(today)
hourMinuteSecs: String = 125812

scala> val hourMinuteSecs = new SimpleDateFormat("HHmmss").format(today)
hourMinuteSecs: String = 005812

scala> val hourMinuteSecs = new SimpleDateFormat("HHmmss a").format(today)
hourMinuteSecs: String = 005812 AM

Tuesday, 14 March 2017

scala value class vs ref class

Scala Value class vs Ref class

Int (including FloatDoubleChar etc) is a value class in scala, which is totally different thing than Null trait when you do val x: Int = null.
final abstract class Int() extends scala.AnyVal {}
What is a value class?
Value class is a class whose instances are not represented as
objects by the underlying host system. All value classes inherit from
class AnyVal.
You can try your creating your own value class,
scala> case class ValString(str: String) extends AnyVal
defined class ValString

scala> val string: ValString = null
<console>:13: error: type mismatch;
 found   : Null(null)
 required: ValString
       val string: ValString = null
                               ^
value class needs to have some value or none. Thats why its recommended to use Option[ValString]
On the other hand, String is AnyRef. Actually AnyRef corresponds to java.lang.Object.
example,
scala> class RefString(str: String) extends AnyRef
defined class RefString

scala> val refString : RefString = null
refString: RefString = null

Saturday, 14 January 2017

hash functions/ cryptographic hash functions


hash functions/ cryptographic hash functions

a mathematical algorithm that maps data of arbitrary size to a bit string of a fixed size (a hash function)
which is designed to also be a one-way function

secure hash(s-ha)

SHA-1 produces a 160-bit (20-byte) hash value known as a message digest.
A SHA-1 hash value is typically rendered as a hexadecimal number, 40 digits long = 40 digits/2 bytes.
$ echo "1" | shasum -a 1
e5fa 44f2 b31c 1fb5 53b6 021e 7360 d07d 5d91 ff5e  -

$ echo "11" | shasum -a 1
dd71 038f 3463 f511 ee74 03db cbc8 7195 302d 891c  -

$ printf 'dd71038f3463f511ee7403dbcbc87195302d891c' | wc -c
      40

$ shasum downloadData.log -a 1
f500ddd45af385b3bbdffdc3457701bf5b9a37a1  downloadData.log

Java security API

scala> val hash = java.security.MessageDigest.getInstance("SHA-1").digest("1".getBytes())
x: Array[Byte] = Array(53, 106, 25, 43, 121, 19, -80, 76, 84, 87, 77, 24, -62, -115, 70, -26, 57, 84, 40, -85)

scala> new String(hash)
res5: String = 5j?+y?LTWM?F�9T(�

scala> import java.math.BigInteger
import java.math.BigInteger

scala> new BigInteger(java.security.MessageDigest.getInstance("SHA-1").digest("1".getBytes()))
res8: java.math.BigInteger = 304942582444936629325699363757435820077590259883

//distributing in a cluster of 10 nodes

scala> new BigInteger(java.security.MessageDigest.getInstance("SHA-1").digest("1".getBytes())).mod(BigInteger.valueOf(10))
res19: java.math.BigInteger = 3
40 bytes Hex representation
scala> new BigInteger(java.security.MessageDigest.getInstance("SHA-1").digest("1".getBytes())).toString(16)
res9: String = 356a192b7913b04c54574d18c28d46e6395428ab

Merkle–Damgård 5

The MD5 algorithm is a widely used hash function producing a 128-bit hash value.
Used for checksum match against a file, but is very vulnerable with collisions within seconds.

Following is BSD example, Linux has md5sum.

$ echo -n '1' | openssl md5
c4ca4238a0b923820dcc509a6f75849b

$ echo -n "1" | md5
c4ca4238a0b923820dcc509a6f75849b

$ echo "1" | md5 # don't do this, as includes \n
b026324c 6904b2a9 cb4b88d6 d61c81d1 ##is a [hex number, compressed to base16](http://stackoverflow.com/q/43556742/432903)

#hash bytes
printf "%s" "c4ca4238a0b923820dcc509a6f75849b" | wc -c
      32
# hash bits = (32/2=16bytes)*8 = 128bits

## another way
$ md5 <<<"1"
b026324c6904b2a9cb4b88d6d61c81d1

$ md5 build.sbt 
MD5 (build.sbt) = d10c 6aff 431a 61c5 b3bd 1a03 8519 900c


Java MD5 API

scala> val hash = MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8"))
hash: Array[Byte] = Array(-60, -54, 66, 56, -96, -71, 35, -126, 13, -52, 80, -102, 111, 117, -124, -101)

scala> val hash = MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8")).length
hash: Int = 16

scala> import java.math.BigInteger
import java.math.BigInteger

scala> new BigInteger(1, MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8")))
res5: java.math.BigInteger = 261578874264819908609102035485573088411 //length 39

scala> new BigInteger(1, MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8"))).toString(16)
res6: String = c4ca4238a0b923820dcc509a6f75849b //length 32

scala> String.format("%032x", new BigInteger(1, MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8"))))
res7: String = c4ca4238a0b923820dcc509a6f75849b


Usage

1) data distribution/partitioning
http://docs.aws.amazon.com/streams/latest/dev/key-concepts.html

2) How do I ensure data integrity of objects uploaded to or downloaded from Amazon S3? - eTag/ Content-MD5

References