requestevaluator.java

来自「JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼」· Java 代码 · 共 381 行 · 第 1/2 页

JAVA
381
字号
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package freecs.core;

import freecs.*;
import freecs.content.*;
import freecs.interfaces.*;
import freecs.util.CookieGenerator;
import freecs.layout.TemplateSet;
import java.util.Vector;
import java.util.Enumeration;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.net.InetAddress;

/**
 * parses the request
 * has a static parse function, which automatically decides which
 * RequestEvaluator has available resources and suplies the Request to
 * this RequestEvaluator. If it fails more than MAX_THREADSEARCHES times,
 * it creates a new RequestEvaluator-Thread.
 */
public class RequestEvaluator {
   private MessageParser mp;
   private short parserID;
   private RequestReader req;

   public RequestEvaluator (RequestReader r) {
      parserID = r.getID();
      mp = new MessageParser (r);
      req=r;
   }

	/**
	 * this decides what to do with the requst and how to answere it
	 * @param cReq The IRequest-Object containing the requestparameters
	 */
   public void evaluate(IRequest cReq) {
	  ConnectionBuffer rb = cReq.getConnectionBuffer ();
	  if (!rb.isValid())
	  	 return;
   	  req.currPosition = RequestReader.EVALUATING;
      SelectionKey key = cReq.getKey ();
      try {
         String action = cReq.getAction ();
         String method = cReq.getMethod ();
         String cookie = cReq.getCookie ();
         if (Server.checkLogLvl (Server.MSG_TRAFFIC, Server.LVL_MINOR)) {
            rb.addLog (method);
            rb.addLog (action);
            rb.addLog (((HTTPRequest) cReq).isHTTP11 () ? "HTTP/1.1" : "HTTP/1.0");
            if (cookie == null)
               rb.addLog ("NO-COOKIE");
            else
               rb.addLog (cookie);
            rb.addLog (" ");
            rb.addLog (cReq.getUserAgent());
         }
         String templateset = cReq.getProperty ("value_templateset");
         User u = (cookie == null ? null : UserManager.mgr.getUserByCookie (cookie));
         boolean isHTTP11 = ((HTTPRequest) cReq).isHTTP11 ();

         // Check for templateset
         TemplateSet ts = null;
         if (templateset != null) {
            ts = Server.srv.templatemanager.getTemplateSet (templateset);
         } else if (u != null) {
            ts = u.getTemplateSet ();
         }

         ContentContainer c = new ContentContainer ();
         ((ContentContainer) c).setHTTP11 (isHTTP11);
         if (ts != null) {
            c.useTemplateSet (ts);
         }
         if (cookie == null) {
            c.setCookie (CookieGenerator.generateCookie ());
         }
         if (cookie != null && action.equals ("/SEND")) {
			req.currPosition=RequestReader.EVAL_SEND;
            if (u == null) {
               Server.log ("RequestEvaluator.evaluate: /send without user: droped key", Server.MSG_STATE, Server.LVL_MINOR);
               CentralSelector.dropKey (key);
               rb.logError("/send without user");
               return;
            }
            String msg = cReq.getProperty ("value_message");
            if (msg == null || msg.length () < 1) {
               Server.log ("RequestEvaluator: message too short", Server.MSG_TRAFFIC, Server.LVL_MINOR);
               u.wasActive ();
               CentralSelector.dropKey (key);
			   rb.logError("message too short");
               return;
            }
            mp.clear ();
            mp.setConnectionBuffer (rb);
            mp.setMessage (msg);
            mp.setHTTP11 (isHTTP11);
            if (!u.wasActive ()) {
               CentralSelector.dropKey(key);
			   rb.logError("flooded"); 
           	   return;
            }
            mp.setSender (u);
            mp.parseAndSendMessage ();
            c.wrap ("dummy");
         } else if (method.equals("GET")) {
			req.currPosition=RequestReader.EVAL_GET;
            if (action.equals("/")) {
               c.setTemplate ("start");
            } else if (action.equals("/INPUT")) {
               c.setTemplate ("input");
            } else if (action.equals("/MESSAGES")) {
				req.currPosition=RequestReader.EVAL_GET_MESSAGES;
				if (u != null) {
               		Connection conn = cReq.getConnectionObject();
					if (!u.wasActive ()) {
						CentralSelector.dropKey(key);
						rb.logError("flooded");
						return;
					}
					if (rb == null) {
						Server.log ("ConnectionBuffer for connection was null!!! Unable to init /MESSAGES", Server.MSG_ERROR, Server.LVL_MAJOR);
						u.scheduleToRemove();
						CentralSelector.dropKey(key);
						return;
					}
					rb.conn = conn;
					rb.setIsMessageFrame(true);
					u.setKey (key);
					u.setHTTP11 (isHTTP11);
					// c.useChunkedHeader ();
					c.setNoCache ();
					c.setIsMessages ();
					c.setTemplate ("welcome");
					// c.setKeepAlive (true);
					if (!c.prepareForSending ()) {
						Server.log ("RequestEvaluator.evaluate: unable to init /MESSAGES: prepareForSending failed", Server.MSG_TRAFFIC, Server.LVL_MAJOR);
						rb.logError("/MESSAGE prepare for sending failed");
						CentralSelector.dropKey(key);
						return;
					}
					req.currPosition=RequestReader.EVAL_GET_MESSAGES_APND2WRITE;
					if (!rb.isValid()) {
						CentralSelector.dropKey(key);
						rb.logError("ConnectionBuffer was invalidated");
						return;
					}
					rb.addToWrite (c);
					req.currPosition=RequestReader.EVAL_GET_MESSAGES_SND_MSGS;
					Vector msgWaiting = u.getScheduledMessages ();
					if (msgWaiting == null) 
						return;
                    for (Enumeration e = msgWaiting.elements (); e != null && e.hasMoreElements (); ) {
                       MessageParser mpr = (MessageParser) e.nextElement ();
                       u.sendMessage (mpr);
                    }
                    return;
               } else {
                  Server.log ("ReqestEvaluator.evaluate: bogous cookie or expired", Server.MSG_STATE, Server.LVL_MINOR);
                  c.setTemplate ("no_cookie");
               }
            } else if (action.equals("/DUMMY")) {
               c.wrap ("dummy");
               // c.setKeepAlive (false);
            } else if (action.equalsIgnoreCase("/STATE")) {
				req.currPosition=RequestReader.EVAL_GET_STATE;
               // do some security-checking here (only ip-restrictions at the moment)
               try {
                  SocketChannel sc = (SocketChannel) key.channel ();
                  InetAddress ia = sc.socket ().getInetAddress ();
                  if (Server.srv.isAdminHost (ia)) {
                     c.setTemplate ("state");
                  } else {
                     Server.log ("state access denied for " + ia.toString (), Server.MSG_STATE, Server.LVL_MAJOR);
                     c.setTemplate ("not_found");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?