Thursday, October 13, 2016

First Swagger

Start by editing sample or own simple one

http://editor.swagger.io/

Generate client or server code online in editor or get more customizable package by online generator

curl -X POST -H "content-type:application/json" -d '{"options": {"packageName": "rira_api"},"swaggerUrl":"https://raw.githubusercontent.com/omidmt/rira/0.7.x/api/swagger-api.yaml"}' http://generator.swagger.io/api/gen/clients/python

Response:

{"code":"55b0eb81-87ba-48ff-91f5-3a4444a95462","link":"https://generator.swagger.io/api/gen/download/55b0eb81-87ba-48ff-91f5-3a4444a95462

Sample security definition by 2 ways of using it for client (query/header)

securityDefinitions:
  apikeyHeader:
    type: apiKey
    name: apiKey
    in: header
  apikeyQuery:
    type: apiKey
    name: apiKey
    in: query
security:
  - apikeyHeader: []
  - apikeyQuery: []

[] means apply to all operations, but different definition can be applied per path/operation as well.

Enabe debug mode:
rira_api.configuration.debug = True

Friday, August 12, 2016

Reverse Range of IP to Domain Name

for i in $(seq 1 254); do dig -x 127.0.0.${i} +short; done

Tuesday, July 12, 2016

Control Pagination of Restful Controller

Problem: The default index action (scaffolded) paginate the result, even for xml and json request *that may not be needed for many cases and the result doesn't indicate it is paginated)

Solution: Using withFormat (response not request) and indicating each format (including html) individually. Using '*' overwrite all formats and results in unexpected format.

params.max = Math.min(max ?: 10, 100)
withFormat {
    'html' {
        respond UnknownNode.list(params), model: [unknownNodeInstanceCount: UnknownNode.count()]
    }
    'json' { respond UnknownNode.list(), [status: CREATED] }
    'xml' { respond UnknownNode.list(), [status: CREATED] }
}

Thursday, June 16, 2016

Install JBoss as Windows Service

Env: JBoss EAP 6.4
Windows 7, 10

1- Get the native utility of JBoss. It is available if using JBoss jar installer. If you don't have it get the jar and install somewhere and copy the "native" directory from the following path to the same path of your JBoss:


EAP-6.4.0\modules\system\layers\base\

2- Use the following script and put it in a bat file on the same directory as JBoss directory, then run it.

set BASE=%~dp0

setx /M JBOSS_HOME %BASE%\jboss-eap
setx /M NOPAUSE 1
echo %BASE%

%BASE:~0,2%

cd %BASE%\jboss-eap\modules\system\layers\base\native\sbin

service.bat install /startup /name JBossAppSrv /jbossuser admin /jbosspass 'adminPass'

Just for clarification, the mandatory parts are:
Set system env variable JBOSS_HOME and NOPAUSE=1
Run the "service.bat install "command.

Ref: JBoss 6.4 Installation Guide

Saturday, June 11, 2016

Some Grails Tricks

Set log level in running instance by console

import org.apache.log4j.*
Logger.getLogger("mt.omid.rira.DataService").level = Level.DEBUG

Set grails environment in app server (using same war file for test/release), great for testing in JBoss

Environment variable grails.env can be set to proper environment like development or production.
or

CATALINA_OPTS=-Dgrails.env=development

In JBoss System properties part in Configuration section accept key/value pair that is used for the same purpose.

Grails (Hibernate) Batch Insert/Update

To prevent memory issue and increase the performance of batch insert or update it can be divided into the smaller bunch data, e.g. 1000 records, and flush and clear the transaction after saving each bunch.

def batch = []  // temp batch list
dataList.each { d ->
    d.value = 'blah blah blah'
    batch.add(d)
    if(batch.size() > 1000)
       runBatch(batch)
}


And the runBatch can be a piece of code like this:
        log.debug("Start Data batch saving")
        Data.withTransaction {
            for(Data d in batch){
                log.debug "Saving datum: ${d} ${d.isDirty('value')}"
                // d.save(flush: true) // not required to flush here, since session will flush before clear
                d.save()
            }
        }
        batch.clear()
        def session = sessionFactory.getCurrentSession()
        session.flush()
        session.clear()
        log.debug("Batch saving transaction & clearing hbn session finished")

The hibernate details can be found here.


Friday, May 13, 2016

JBoss JNDI SQLServer and Integrated Windows Authentication

Copy the sqljdbc_auth.dll (32 or 64 according to your jre) to bin folder of your jre (in jdk also copy to jre/bin).

In JBoss data source url set integratedSecurity=true .