Package com.zaranux.os.server.zaranuxlet

Source Code of com.zaranux.os.server.zaranuxlet.Zaranuxlet

package com.zaranux.os.server.zaranuxlet;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.zaranux.client.api.exceptions.InvalidParameter;
import com.zaranux.client.api.exceptions.NotDirectory;
import com.zaranux.client.api.exceptions.NotFile;
import com.zaranux.client.api.exceptions.ResourceAccessFailed;
import com.zaranux.client.api.exceptions.ResourceNotFound;
import com.zaranux.client.api.exceptions.SystemCallNotSupported;
import com.zaranux.client.api.exceptions.UnauthorizedAccessException;
import com.zaranux.os.server.core.Identity;
import com.zaranux.os.server.core.Response;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.swing.JApplet;

import netscape.javascript.JSException;
import netscape.javascript.JSObject;



@SuppressWarnings("serial")
public class Zaranuxlet extends JApplet {
 
  private static String VERSION = "0.2";
 
  private static final Logger logger = Logger.getLogger(Zaranuxlet.class.getName());
  static {
    logger.setLevel(Level.ALL);
      Handler[] handlers =
          Logger.getLogger( "" ).getHandlers();
        for ( int index = 0; index < handlers.length; index++ ) {
          handlers[index].setLevel( Level.ALL );
        }
    }
 
  private static String host = null;
  private static int port = -1;
  private boolean authorized = false;
 
  static String getHost()
  {
    return host;
  }
   
  static int getPort()
  {
    return port;
  }
 
     @Override
      public void init() {
      logger.finest("initialized!");
      }
     
    @Override
    public void start(){
      logger.fine("Version " + VERSION);
      JSObject window = JSObject.getWindow(this);
          JSObject doc = (JSObject) window.getMember("location");
          String host_port = (String) doc.getMember("host");
          String[] h_p = host_port.split(":");
          host = h_p[0];
          if(host.equals("localhost") || host.endsWith("zaranux.com"))
            authorized = true;
          port = h_p.length >1 ? Integer.parseInt(h_p[1]) : 80;
          logger.finest("host name : " + host);
    }
   
      @Override
      public void stop(){
         logger.finest("Stoped!");
      }
     
      public void destroy() {
         logger.finest("Destroyed!");
      }

      public void doServeAsync(final String systemcall , final String path , final String parameterList , final String BASE64data , final String id,final String identityAssertion, final String callbackID)
      {
        if(!authorized)
        {
          logger.severe("domin is not authorized: " + host);
          return;
        }
          Thread t = new Thread(new Runnable() {
             
              public void run() {
                logger.finest("Thread for callback " + callbackID + " started ...");
               
                // path must always start with "file://"
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                  public Void run(){
                    // TODO see if you can make it static and if that improves performance ..

                    Boolean isBase64 = true;
                    String data = (BASE64data == null) ? "" : BASE64data;
          
                    Identity requester = new Identity(id, identityAssertion, "");

                    logger.finest("Zaranuxlet.kernel.execute -> " + systemcall + " , " +  path +" , " + parameterList + ","  +  isBase64 + "," +  requester.getID());
                    Response output;
                  try
                  {
                    output = ( new Kernel()).execute(systemcall, path, parameterList, new ByteArrayInputStream(data.getBytes()), isBase64, requester);
                }catch(UnauthorizedAccessException e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                }catch(InvalidParameter e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                }catch(SystemCallNotSupported e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
                }catch(ResourceNotFound e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }catch(ResourceAccessFailed e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }catch(NotDirectory e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }catch(NotFile e)
                {
                  output = new Response(e);
                  //response.getWriter().write(output.toString());
                  //response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }
                   
                      try
                      {
                      final String jsCommand = "window['SUCCESS_CALLBACK']('" + callbackID + "' , '" + URLEncoder.encode(output.toString(),"UTF-8") + "')";
                        final JSObject win = JSObject.getWindow(Zaranuxlet.this);
                          win.eval(jsCommand);
                      }catch(JSException e)
                      {
                        logger.severe("callbackOnSuccess(): Unable to do window.eval : " + e.getMessage());
                      }
                      catch(UnsupportedEncodingException e)
                      {
                        logger.severe("callbackOnSuccess(): " + e);
                      }
                      return null;
                  }
                  });
                  logger.finest("Thread for callback " + callbackID + " ended!");
              }
        
          });
          t.start();
      }
     

}
TOP

Related Classes of com.zaranux.os.server.zaranuxlet.Zaranuxlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.