Wednesday, July 6, 2016

How to create messenger , chat in Java/JScript with source code( 1 html, 1 Java class only) with encryption, geolocation (leaflet.js) on client side (updated July 21,2016)

This is blog with source code of chat /messenger implemented in JScript and Java with requests encryption and deception in JScript. I usd blacktea.js simple JScript library to encrypt/decrepit messages, login names and chat rooms.
Source code on Github I added leaflet geolocation library to show the chatters on map . Looks amazing.
I recently created Messenger in Java and Java Script. As soon  I didn't find  Java code to do this I decide to write my own. Code which I publish here works in Azure cloud  here:
Java Asynchronous UI framework and messenger  If you have a questions or request  find me in LinkedIn my LinkedIn account.
This one of the way how possible inject it through iframe:
 


This Java code use  Asynchronouse UI services framework Here there is Java code:


package net.bloberry.async_ui.dpe.models;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.omg.PortableServer.ThreadPolicyOperations;

import net.bloberry.async_ui.common.DataExchangeConfigureException;
import net.bloberry.async_ui.common.Message;
import net.bloberry.async_ui.common.User;
import net.bloberry.async_ui.services.ServiceFactory;
import net.bloberry.async_ui.services.ServiceType;
import net.bloberry.async_ui.services.ServicesEngine;
import net.bloberry.async_ui.services.UIService;

public class Messanger extends Window {
 private static final int MAXIMUM_MESSAGES_INQUEUE = 10;
 private ServicesEngine proxy = null;
 private UIService uiService = null;
 
 private String loginName = null;
 
 private String group = null;
 Queue myMsgs;

 
 public Messanger(String wondowToken, User user) {
  super(wondowToken, user);
  setExpirationTime(1000);
  log = Logger.getLogger(Messanger.class);
  try {
   proxy = ServicesEngine.getInstance();
   uiService = (UIService)ServiceFactory.getService(ServiceType.UI); 
  } catch (DataExchangeConfigureException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 @Override
 public void updateAndSendMessage(HttpServletRequest request,
   PrintWriter writer) {
  
  String action=request.getParameter("action");

  populateLocalQueue(writer);   
 
  // UI send the message to all members of the group
  if ("message".equalsIgnoreCase(action)) 
  {
   String data = request.getParameter("data");
   
   Queue globalQueue = (Queue) uiService.getObjectByGroup(group);
   if( globalQueue == null ) // first initiate chat channel
   {
    globalQueue = new ArrayBlockingQueue(MAXIMUM_MESSAGES_INQUEUE);
    uiService.setObjectToGroup(group, globalQueue);
   }
   try { 
    if( globalQueue.size()== MAXIMUM_MESSAGES_INQUEUE ) globalQueue.poll();
    globalQueue.add(new Message(loginName, data));
   } catch (java.lang.IllegalStateException e) {
    globalQueue.poll();
    globalQueue.add(new Message(loginName, data));
   }
   
   data = loginName + "|" + data;
   setUpdated();
  }

  // UI remove itself from the group
  if ("remove".equalsIgnoreCase(action)) {
  // List windows = this.getObjectByGroup(group);
    //  windows.remove(this);
  }
   
 }
 
 
 private synchronized void populateLocalQueue(PrintWriter writer)
 {
  Queue msgs_= (Queue) uiService.getObjectByGroup(group);
  if (msgs_ != null && loginName != null)
  {
     msgs_.forEach((msg)->{
     if( myMsgs == null )
     {
      myMsgs = new ArrayBlockingQueue(MAXIMUM_MESSAGES_INQUEUE);
     }
     if(!myMsgs.contains(msg) && msg.getMessage() != null && !((String)msg.getMessage()).isEmpty()) {
      String data = msg.getLoginName() + "|" + (String) msg.getMessage();
      updateText("updateTextArea", "textArea", data, writer);
      
      try { 
       if( myMsgs.size()== MAXIMUM_MESSAGES_INQUEUE ) myMsgs.poll();
       myMsgs.add(msg);
      } catch (java.lang.IllegalStateException e) {
       myMsgs.poll();
       myMsgs.add(msg);
      }
     }
    });
    }
 }

 //@Override
 //public void restoreState() {
 //@Override
 //public void flashState()
 
 @Override
 public void doAuthByAjaxCall(String loginName, String password, HttpServletRequest request, PrintWriter writer)
 {
  this.loginName = loginName;
  this.getUser().setUserName(loginName);
  this.getUser().setPassword(password);
  this.group = request.getParameter("group");
  this.group = this.group == null || this.group.isEmpty()? "undefined": this.group;
  updateCredentials(request, writer);
 }
 
 
 
 // DO not authenticate this model use own auth. when model instantiated 
 @Override
 public void doAuthentification(HttpServletRequest request, PrintWriter writer)
 {
  this.loginName = this.getUser().getLoginName();
  this.getUser().setUserName(loginName);
  updateCredentials( loginName, null, ( loginName != null ) , writer);
 }
 
 private void updateCredentials(HttpServletRequest request, PrintWriter writer){
  // UI register itself to the group
  //if("doAuthent".equalsIgnoreCase(action))...  already  catched in parent

      setWindowToGroup(group);

  
  //If there is login functionality already on this screen send logical authentification = true (user.getLoginName()+'Y')
  updateCredentials( loginName, null, ( loginName != null ) , writer);
  
 }
}
This is client side . Advantage is Encription happened in JScript and server side even do not know about real user name and chat room ( grou) which user has been connected. This is Messenger.html file:


    

  
    
    


 
 


Messenger




_

    No comments: