Package anvil.core.net

Source Code of anvil.core.net.AnyContext

/*
* $Id: AnyContext.java,v 1.44 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;

import anvil.Log;
import anvil.ForgingException;
import anvil.core.Any;
import anvil.core.AnyString;
import anvil.core.AnyAbstractClass;
import anvil.core.Array;
import anvil.script.Type;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.Module;
import anvil.script.ScriptException;
import anvil.session.Session;
import anvil.server.Address;
import anvil.server.Realm;
import anvil.server.Tribe;
import anvil.server.Citizen;
import anvil.server.Zone;
import anvil.server.AccessPreferences;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;


///
/// @class Context
///   The Context holds information about the request, response,
///   session, and user.
///

/// @attribute session
/// Session for the request, if enabled.
/// @synopsis Session self.session

/// @attribute citizen
/// Authorized user, if enabled.
/// @synopsis Citizen self.citizen

/// @attribute request
/// HTTP request.
/// @synopsis Request self.request

/// @attribute response
/// HTTP response.
/// @synopsis Response self.response

/// @attribute parameter
/// Named parameters from request.
/// @synopsis object self.parameter

/// @reference parameter
/// Named parameters from request.
/// @synopsis object self[parameter]


/**
* class AnyContext
*
* @author: Jani Lehtim�ki
* @author: Jaripekka Salminen
*/
public class AnyContext extends AnyAbstractClass
{


  private anvil.server.Context _context;
  private AnyRequest  _any_request;
  private AnyResponse _any_response;
  private Any         _any_session;
  private Any         _any_citizen;
  private String      _parentID = "";
  private String      _selfID   = "";

 
  public AnyContext(anvil.server.Context context)
  {
    Session session = context.getSession();
    Citizen citizen = context.getCitizen();
    _context      = context;
    _any_request  = new AnyRequest(context.getRequest());
    _any_response = new AnyResponse(context.getResponse());
    _any_session  = (session != null) ? new AnySession(session) : Any.UNDEFINED;
    _any_citizen  = (citizen != null) ? new AnyCitizen(citizen) : Any.UNDEFINED;
  }
 
 
  public final anvil.script.ClassType classOf()
  {
    return __class__;
  }


  public Object toObject()
  {
    return _context;
  }


  public Any getAttribute(anvil.script.Context context, String attribute)
  {
    int n = attribute.length();
    switch(n) {
    case 7:
      if (attribute.equals("request")) {
        return _any_request;
      } else if (attribute.equals("session")) {
        return _any_session;
      } else if (attribute.equals("citizen")) {
        context.checkAccess(AnyCitizen.CAN_READ);
        if (_any_citizen == Any.UNDEFINED) {
          Citizen tmpc = _context.getCitizen();
          if (tmpc != null) {
            _any_citizen = new AnyCitizen(tmpc);
          }
        }
        return _any_citizen;
      }     
      break;
   
    case 8:
      if (attribute.equals("response")) {
        return _any_response;
      }
    }
    return _any_request.getAttribute(context, attribute);
  }
 

  public Any checkAttribute(anvil.script.Context context, String attribute)
  {
    return _any_request.getAttribute(context, attribute);
  }


  public Any setAttribute(anvil.script.Context context, String attribute, Any value)
  {
    return _any_request.setAttribute(context, attribute, value);
  }


  public boolean deleteAttribute(anvil.script.Context context, String attribute)
  {
    return _any_request.deleteAttribute(context, attribute);
  }


  public Any getReference(anvil.script.Context context, Any index)
  { 
    return _any_request.getReference(context, index);
  }


  public Any checkReference(anvil.script.Context context, Any index)
  {
    return _any_request.checkReference(context, index);
  }


  public Any setReference(anvil.script.Context context, Any index, Any value)
  { 
    return _any_request.setReference(context, index, value);
  }

 
  public boolean deleteReference(anvil.script.Context context, Any index)
  { 
    return _any_request.deleteReference(context, index);
  }


  ///
  /// @method getResource
  /// If dispatcher is set for zone, returns the extra part after
  /// zone's path. Returned value always starts with slash (/).
  /// @synopsis string getResource()
  public Any m_getResource()
  {
    return Any.create(_context.getResource());
  }


  ///
  /// @method setResource
  /// Sets the pointed resource.
  /// @synopsis Context setResource(string resource)
  ///
  public static final Object[] p_setResource = { "resource" };
  public Any m_setResource(String resource)
  {
    _context.setResource(resource);
    return this;
  }


  ///
  /// @method getSession
  /// Returns the specified session from container, or <code>null</code> if it didn't exist
  /// @synopsis Session getSession(string id)
  /// @throws AccessDenied If security policy denies this operation
  ///
  public static final Object[] p_getSession = { null, "id" };
  public Any m_getSession(Context context, String id)
  {
    context.checkAccess(AnySessionContainer.CAN_READ);
    Session session = _context.getSession(id);
    return (session != null) ? new AnySession(session) : UNDEFINED;
  }


  ///
  /// @method createSession
  /// Creates and returns a new session.
  /// @synopsis Session createSession()
  /// @throws AccessDenied If security policy denies this operation
  ///
  public Any m_createSession(Context context)
  {
    context.checkAccess(AnySessionContainer.CAN_WRITE);
    return new AnySession(_context.createSession());
  }


  ///
  /// @method getLinkUrl
  ///   Returns an URL that can be used for creating events.
  ///   For example: &lt;a href=${context.getLinkUrl("click","true")}&gt;click&lt;/a&gt;
  ///   Returns, for example "/$hsyukrxkrxjemljuhaow/components/test/index.nvl?root_foo.click=true"
  ///
  /// @synopsis string getLinkUrl() ; Returns the full URI to the current script.
  /// @synopsis string getLinkUrl(string name, string value) ; One name-value pair.
  /// @synopsis string getLinkUrl(string name1, string value1, string name2, string value2 [, ...]) ; Many name-value pairs.
  /// @synopsis string getLinkUrl(array params) ; Takes name-value pairs from array.
  ///
  /// @param name name of event
  /// @param value value of event
  /// @param params array of event names and values
  /// @return an URL that can be used for creating events
  ///
  public static final Object[] p_getLinkUrl = { "params" };
  public Any m_getLinkUrl(Any[] parameters)
  {
    StringBuffer url = new StringBuffer(_context.getRequest().getRequestURI());
    String name;
    String value;
    boolean first = true;

    switch (parameters.length) {

      /* getLinkUrl() */
      case 0:
        break;

      /* getLinkUrl(Array params) */
      case 1:
        if (parameters[0].isArray()) {
          Enumeration enu = ((Array)parameters[0]).elements();
          while (enu.hasMoreElements()) {
            name = ((Any)enu.nextElement()).toString();
            if (enu.hasMoreElements()) {
              value = ((Any)enu.nextElement()).toString();
              if (first) {
                url.append('?');
                first = false;
              } else {
                url.append('&');
              }
              url.append(_selfID);
              url.append('.');
              url.append(name);
              url.append('=');
              url.append(value);
            }
          }
        }
        break;

      /* getLinkUrl(name, value [, name2, value2, ... ] ) */
      default:
        for (int i=0; i<parameters.length; i += 2) {
          name  = parameters[i].toString();
          if ((i+1) < parameters.length) {
            value = parameters[i+1].toString();
            if (first) {
              url.append('?');
              first = false;
            } else {
              url.append('&');
            }
            url.append(_selfID);
            url.append('.');
            url.append(name);
            url.append('=');
            url.append(value);
          }
        }
        break;
    }

    return new AnyString(url.toString());
  }


  /// @method getRealm
  /// Returns the authentication realm used.
  /// @synopsis Realm getRealm()
  /// @synopsis Realm getRealm(string realmName)
  /// @throws AccessDenied If security policy denies this operation
  public static final Object[] p_getRealm = { null, "*name", null };
  public Any m_getRealm(Context context, String name)
  {
    context.checkAccess(AnyRealm.CAN_READ);
    Zone zone = _context.getZone();
    if (name == null) {
      AccessPreferences access = zone.getAccessPreferences();
      if (access != null) {
        name = access.getRealm();
      }
    }
    if (name == null) {
      return UNDEFINED;
    }
    context.checkRealm(name);
    Realm realm = zone.getRealm(name);
    if (realm != null) {
      return new AnyRealm(realm);
    }
    return UNDEFINED;
  }


  /// @method getCitizen
  /// Gets, and authenticates, specified user.
  /// @synopsis Citizen getCitizen(string username, string credentials)
  /// @synopsis Citizen getCitizen(string username)
  /// @param username Username
  /// @param credentials Credentials
  /// @return citizen, or <code>null</code> if authentication failed
  /// @throws AccessDenied If security policy denies this operation
  public static final Object[] p_getCitizen = { null, "name", "*credentials", null };
  public Any m_getCitizen(Context context, String name, String credentials)
  {
    context.checkAccess(AnyCitizen.CAN_READ);
    Zone zone = _context.getZone();
    AccessPreferences access = zone.getAccessPreferences();
    if (access != null) {
      String realmName = access.getRealm();
      context.checkRealm(realmName);
      Realm realm = zone.getRealm(realmName);
      if (realm != null) {
        Citizen citizen = realm.getCitizen(name);
        if (citizen != null) {
          if (credentials != null) {     
            if (citizen.verifyCredentials(credentials)) {
              return new AnyCitizen(citizen);
            }
          } else {
            return new AnyCitizen(citizen);
          }
        }
      }
    }
    return NULL;
  }


  /// @method getTribe
  /// Gets named tribe from current realm, if any.
  /// @synopsis Citizen getTribe(string name)
  /// @param name Tribe name
  /// @return tribe, or <code>null</code> if no tribe with given name were found
  /// @throws AccessDenied If security policy denies this operation
  public static final Object[] p_getTribe = { null, "name" };
  public Any m_getTribe(Context context, String name)
  {
    context.checkAccess(AnyTribe.CAN_READ);
    Zone zone = _context.getZone();
    AccessPreferences access = zone.getAccessPreferences();
    if (access != null) {
      String realmName = access.getRealm();
      context.checkRealm(realmName);
      Realm realm = zone.getRealm(realmName);
      if (realm != null) {
        Tribe tribe = realm.getTribe(name);
        if (tribe != null) {
          return new AnyTribe(tribe);
        }
      }
    }
    return NULL;
  }


  /// @method setCitizen
  /// Sets the citizen for current session
  /// @synopsis Context setCitizen(Citizen citizen)
  /// @throws AccessDenied If security policy denies this operation
  public static final Object[] p_setCitizen = { null, "citizen" };
  public Any m_setCitizen(Context context, Any citizen)
  {
    if (citizen instanceof AnyCitizen) {
      _context.setCitizen((Citizen)citizen.toObject());
      return this;
    } else {
      throw context.BadParameter("Citizen expected");
    }
   
  }



  /// @method getZone
  /// Returns the zone that received this request.
  /// @synopsis Configurable getZone()
  /// @throws AccessDenied If security policy denies this operation
  public Any m_getZone(Context context)
  {
    context.checkAccess(anvil.core.system.AnyConfigurable.CAN_READ);
    return new anvil.core.system.AnyConfigurable(_context.getZone());
  }


  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Context", AnyContext.class,
      //DOC{{
    ""+
      "\n" +
      " @class Context\n" +
      "   The Context holds information about the request, response,\n" +
      "   session, and user.\n" +
      "\n" +
      " @attribute session\n" +
      " Session for the request, if enabled.\n" +
      " @synopsis Session self.session\n" +
      " @attribute citizen\n" +
      " Authorized user, if enabled.\n" +
      " @synopsis Citizen self.citizen\n" +
      " @attribute request\n" +
      " HTTP request.\n" +
      " @synopsis Request self.request\n" +
      " @attribute response\n" +
      " HTTP response.\n" +
      " @synopsis Response self.response\n" +
      " @attribute parameter\n" +
      " Named parameters from request.\n" +
      " @synopsis object self.parameter\n" +
      " @reference parameter\n" +
      " Named parameters from request.\n" +
      " @synopsis object self[parameter]\n" +
      "\n" +
      " @method getResource\n" +
      " If dispatcher is set for zone, returns the extra part after\n" +
      " zone's path. Returned value always starts with slash (/).\n" +
      " @synopsis string getResource()\n" +
      "\n" +
      " @method setResource\n" +
      " Sets the pointed resource.\n" +
      " @synopsis Context setResource(string resource)\n" +
      "\n" +
      "\n" +
      " @method getSession\n" +
      " Returns the specified session from container, or <code>null</code> if it didn't exist\n" +
      " @synopsis Session getSession(string id)\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      "\n" +
      "\n" +
      " @method createSession\n" +
      " Creates and returns a new session.\n" +
      " @synopsis Session createSession()\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      "\n" +
      "\n" +
      " @method getLinkUrl\n" +
      "   Returns an URL that can be used for creating events.\n" +
      "   For example: &lt;a href=${context.getLinkUrl(\"click\",\"true\")}&gt;click&lt;/a&gt;\n" +
      "   Returns, for example \"/$hsyukrxkrxjemljuhaow/components/test/index.nvl?root_foo.click=true\"\n" +
      "\n" +
      " @synopsis string getLinkUrl() ; Returns the full URI to the current script.\n" +
      " @synopsis string getLinkUrl(string name, string value) ; One name-value pair.\n" +
      " @synopsis string getLinkUrl(string name1, string value1, string name2, string value2 [, ...]) ; Many name-value pairs.\n" +
      " @synopsis string getLinkUrl(array params) ; Takes name-value pairs from array.\n" +
      "\n" +
      " @param name name of event\n" +
      " @param value value of event\n" +
      " @param params array of event names and values\n" +
      " @return an URL that can be used for creating events\n" +
      "\n" +
      " @method getRealm\n" +
      " Returns the authentication realm used.\n" +
      " @synopsis Realm getRealm()\n" +
      " @synopsis Realm getRealm(string realmName)\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      " @method getCitizen\n" +
      " Gets, and authenticates, specified user.\n" +
      " @synopsis Citizen getCitizen(string username, string credentials)\n" +
      " @synopsis Citizen getCitizen(string username)\n" +
      " @param username Username\n" +
      " @param credentials Credentials\n" +
      " @return citizen, or <code>null</code> if authentication failed\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      " @method getTribe\n" +
      " Gets named tribe from current realm, if any.\n" +
      " @synopsis Citizen getTribe(string name)\n" +
      " @param name Tribe name\n" +
      " @return tribe, or <code>null</code> if no tribe with given name were found\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      " @method setCitizen\n" +
      " Sets the citizen for current session\n" +
      " @synopsis Context setCitizen(Citizen citizen)\n" +
      " @throws AccessDenied If security policy denies this operation\n" +
      " @method getZone\n" +
      " Returns the zone that received this request.\n" +
      " @synopsis Configurable getZone()\n" +
      " @throws AccessDenied If security policy denies this operation\n"
    //}}DOC
    );
  static {
    NetModule.class.getName();
  }

 
}
TOP

Related Classes of anvil.core.net.AnyContext

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.