'Handler class
Sub Class_Globals
Private pass As String = "b0T!bn3%Ft0,zBrJ!" 'Make sure your password is in Class_Globals so it can be obfuscated
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim m As Map = ExtractRequestData(req)
Dim value As String = m.Get("key")
If value = "value" Then
Dim res As Map
res.Initialize
res.Put("RespKey", "RespValue")
Respond(res, resp)
End If
End Sub
Public Sub ExtractRequestData (req As ServletRequest) As Map
Dim res As String = DecryptAES(ReadStreamBytes(req.InputStream))
Dim json As JSONParser
json.Initialize(res)
Dim m As Map
m = json.NextObject
Return m
End Sub
Public Sub Respond (Data As Map, Resp As ServletResponse)
Dim json As JSONGenerator
json.Initialize(Data)
WriteStreamBytes(EncryptAES(json.ToString).GetBytes("UTF8"), Resp.OutputStream)
End Sub
Public Sub ReadStreamBytes(Input As InputStream) As String
Dim buffer As OutputStream
buffer.InitializeToBytesArray(0)
File.Copy2(Input, buffer)
Dim raf As RandomAccessFile
raf.Initialize3(buffer.ToBytesArray, False)
Dim b(2048) As Byte 'Maximum size of the received data
raf.ReadBytes(b, 0, raf.Size, 0)
Dim res As String = BytesToString(b, 0, raf.CurrentPosition, "UTF8")
raf.Close
Return res
End Sub
Public Sub WriteStreamBytes(Data() As Byte, Output As OutputStream)
Output.WriteBytes(Data, 0, Data.Length)
End Sub
Public Sub EncryptAES (Data As String) As String
Dim bc As ByteConverter
Dim aes As B4XCipher
Return bc.HexFromBytes(aes.Encrypt(Data.GetBytes("UTF8"), pass))
End Sub
Public Sub DecryptAES (Data As String) As String
Dim aes As B4XCipher
Dim bc As ByteConverter
Dim res() As Byte = aes.Decrypt(bc.HexToBytes(Data), pass)
Return BytesToString(res, 0, res.Length, "UTF8")
End Sub