⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contentcontainer.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
字号:
/**
 * 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.
 */

/**
 * The ContentContainer is used for delivering the entry-page, error-page, ...
 */
package freecs.content;

import freecs.*;
import freecs.core.*;
import freecs.interfaces.*;
import freecs.layout.*;

import java.nio.charset.Charset;
import java.nio.charset.CharacterCodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;


public class ContentContainer implements IResponseHeaders, IContainer {
   private CharBuffer   cb;
   private ByteBuffer   buf;
   private TemplateSet  ts = null;
   private String 		tName;
   private Template		tpl = null;
   private String		cookie = null,
						contentType="text/html";

   private boolean 		chunkedHdr, 
   						nocache=false, 
   						nostore=false,
						keepAlive = false, 
						isMessages = false,
						isRedirect, 
						isHTTP11=true;

	private short 		resCode = 200;
	
   public ContentContainer () { }

	/**
	 * wraps the cntnt param into a fully http-response with the wanted headerfields
	 * @param cntnt
	 */
   public void wrap (String cntnt) {
	   StringBuffer sb = new StringBuffer ();
	   sb.append (isHTTP11 ? "HTTP/1.1" : "HTTP/1.0");
	   switch (resCode) {
	   	case OK_CODE:
	   		sb.append (OK_HDR);
	   		break;
	   	case REDIRECT_CODE:
	   		setRedirectTo (cntnt);
	   		break;
			case NOCONTENT_CODE:
				sb.append (NOCONTENT_HDR);
				cb = CharBuffer.wrap (sb.toString());	   
				return;
		   case NOTFOUND_CODE:
	   		sb.append (NOTFOUND_HDR);
				cb = CharBuffer.wrap (sb.toString());
				return;
	   }
		sb.append ("Content-Type: ");
		sb.append (contentType);
		sb.append ("; charset=");
		sb.append (Server.srv.DEFAULT_CHARSET);
		if (nocache) {
			sb.append ("\r\nPragma: no-cache\r\nCache-Control: no-cache");
		} else if (nostore) {
			sb.append ("\r\nCache-Control: no-store");
		}

		sb = appendCookie (sb);

		if (!isHTTP11 || !keepAlive || isMessages) {
			sb.append ("\r\nConnection: close\r\nProxy-Connection: close");
		} else {
			sb.append ("\r\nConnection: Keep-Alive\r\nProxy-Connection: Keep-Alive");
			if (!chunkedHdr) {
				sb.append("\r\nContent-Length: ");
				sb.append (cntnt.length ());
			}
		}
		if (chunkedHdr) {
			sb.append ("\r\nTransfer-Encoding: chunked\r\n\r\n");
			sb.append (Integer.toHexString (cntnt.length ()));
			sb.append ("\r\n");
			sb.append (cntnt);
			sb.append ("\r\n");
			cb = CharBuffer.wrap (sb.toString());
			return;
		}
		sb.append ("\r\n\r\n");
		sb.append (cntnt);
		cb = CharBuffer.wrap (sb.toString());	   
   }
   
   /**
    * sets the HTTP-Response-Code
    * @param code
    */
   public void setResCode (short code) {
		resCode=code;
   }

	/**
	 * append the cookie-header-field to the given StringBuffer
	 * @param sb the stringbuffer to append the cookie-header-field
	 * @return the stringbuffer with the cookie-header-field appended
	 */
   public StringBuffer appendCookie (StringBuffer sb) {
      if (cookie == null) return sb;
      sb.append ("\r\n");
      sb.append ("Set-Cookie: FreeCSSession=");
      sb.append (cookie);
      sb.append ("; path=/;");
      if (Server.srv.COOKIE_DOMAIN != null) {
      	sb.append (" Domain=");
      	sb.append (Server.srv.COOKIE_DOMAIN);
      }
      return sb;
   }

   /**
	* causes this content-container to use a specific template-set
	* @param ts the template-set to use
	*/
   public void useTemplateSet (TemplateSet ts) {
      this.ts = ts;
   }

	/**
	 * set the template to be rendered for this content-container
	 * @param tName the name of the template
	 */
   public void setTemplate (String tName) {
      this.tName = tName;
   }

	/**
	 * renders the template and wraps it to a full httpresponse
	 */
   public void renderTemplate () {
      if (tName.equals ("state")) {
         renderState ();
         return;
      }
      if (tpl == null) {
         if (this.ts == null) ts = Server.srv.templatemanager.getTemplateSet ("default");
         tpl = ts.getTemplate (tName);
         if (tpl == null) tpl = ts.getTemplate ("not_found");
      }
      if (tpl.isRedirect ()) {
         this.setRedirectTo(tpl.getDestination ());
         return;
      }

      String cntnt = tpl.render ();
      if (cntnt.length () < 1) {
         Server.log ("ContentContainer.renderTemplate: rendered template has no content", Server.MSG_STATE, Server.LVL_MAJOR);
         resCode=NOCONTENT_CODE;
         wrap (null);
         return;
      }
      wrap (cntnt);
   }

	/**
	 * causes the response to be a chunked response
	 * the calling method has to care, that the other chunks are sent correctly
	 */
   public void useChunkedHeader () {
      chunkedHdr = true;
      keepAlive = true;
   }

	/**
	 * set/unset the keep-alive header field
	 * @param b
	 */
   public void setKeepAlive (boolean b) {
      keepAlive = b;
   }

	/**
	 * mark this http-response as http11(true)/http10(false)
	 * @param b
	 */
   public void setHTTP11 (boolean b) {
      isHTTP11 = b;
      keepAlive = true;
   }

	/**
	 * check if this response is a HTTP11 response
	 * @return boolean true if response is a HTTP11 response, false if not
	 */
   public boolean isHttp11 () {
      return isHTTP11;
   }

	/**
	 * set the cookievalue to append to the response-header-fields
	 * @param cookie
	 */
   public void setCookie (String cookie) {
      this.cookie = cookie;
   }

	/**
	 * return the bytebuffer which is ready to send
	 */
   public ByteBuffer getByteBuffer () {
      return buf;
   }

	/**
	 * prepares the response for sending
	 * if a template is set, it will be rendered
	 * if no charbuffer is present, even after rendering the template, 
	 * there is nothing to send and prepareForSending will just return
	 */
   public boolean prepareForSending () {
      if (tName != null || tpl != null) renderTemplate ();
      if (cb == null) return false;
      try {
         buf = Charset.forName (Server.srv.DEFAULT_CHARSET).newEncoder ().encode (cb);
         return true;
      } catch (CharacterCodingException cce) {
         Server.debug ("ContentContainer.prepareForSending: ", cce, Server.MSG_ERROR, Server.LVL_MINOR);
         try {
            String s = cb.toString ();
            buf = ByteBuffer.wrap (s.getBytes ());
            return true;
         } catch (Exception e) {
            Server.debug ("ContentContainer.prepareForSending: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
            return false;
         }
      }
   }

   public boolean hasContent () {
      return (buf != null);
   }

   public boolean closeSocket () {
      return (!isHTTP11 || !keepAlive) && !isMessages;
   }
   public void setIsMessages () {
	  this.isMessages = true;
   }
   public void setNoCache () {
	  nocache = true;
   }
   public void setNoStore () {
	  nostore = true;
   }

/*   public void finalize () {
      Server.log ("ContentContainer FINALIZED*******************************", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
   } */

   private void renderState () {
      StringBuffer c = new StringBuffer ();
      c.append ("<html><head><title>state query</title><meta http-equiv=\"refresh\" content=\"4\"></head><body bgcolor=ffff99 text=000000><table border=0><tr><td colspan=4 align=center>FreeCS (");
      c.append (Server.getVersion ());
      c.append (")</td></tr><tr><td width=10></td><td witdh=200 align=right>Anzahl der User:</td><td width=10></td><td width=400>");
      UserManager umgr = UserManager.mgr;
      c.append (umgr.getActiveUserCount ());
      c.append ("(");
      c.append (umgr.getHighWaterMark());
      c.append (" max)");
      c.append ("</td></tr><tr><td colspan=4 bgcolor=000000 height=1></td></tr><tr><td width=10></td><td witdh=200 align=right>Anzahl der R鋟me:</td><td width=10></td><td width=400>");
      GroupManager gmgr = GroupManager.mgr;
      c.append (gmgr.size ());
	  c.append ("(");
	  c.append (gmgr.getHighWaterMark());
	  c.append (" max)");
      c.append ("</td></tr><tr><td colspan=4 bgcolor=000000 height=1></td></tr><tr><td width=10></td><td witdh=200 align=right>Anzahl der Reader-Threads:</td><td width=10></td><td width=400>");
      c.append (RequestReader.activeReaders ());
      if (!Server.srv.THREAD_PER_READ) {
		  c.append ("</td></tr><tr><td colspan=4 bgcolor=000000 height=1></td></tr><tr><td width=10></td><td witdh=200 align=right>Reader-Queue-Usage:</td><td width=10></td><td width=400>");
		  double d[] = RequestReader.getOveralUsage();
		  for (int i = 0; i < d.length; i++) {
			 c.append (d[i]);
			 c.append ("%");
			 if (i < d.length) 
			 	c.append (", ");
		  }
      }
	  c.append ("</td></tr><tr><td colspan=4 bgcolor=000000 height=1></td></tr><tr><td width=10></td><td witdh=200 align=right>Speicher:</td><td width=10></td><td width=400>");
      Runtime r = Runtime.getRuntime ();
      long free = r.freeMemory ();
      long total = r.totalMemory ();
      long max = r.maxMemory ();
      long used = total - free;
      c.append ("free: ");
      c.append (free);
      c.append ("<br>used: ");
      c.append (used);
      c.append ("<br>total/maxTotal: ");
      c.append (total);
      c.append ("/");
      c.append (max);
      c.append ("<br>Usagepercentage: ");
      c.append (100 - (free / (total / 100)));
      c.append ("</td></tr></table></body></html>");
      wrap (c.toString());
   }
   
   /**
	* construct a HTTP-Redirect-Response
	* @param dest the destination to redirect to
	*/
  public void setRedirectTo (String dest) {
	 StringBuffer cntnt = new StringBuffer();
	 cntnt.append ("<html><head><title>redirection</title><head><body>Redirected to <a href=\"");
	 cntnt.append (dest);
	 cntnt.append ("\">");
	 cntnt.append (dest);
	 cntnt.append ("</a>");
	 cntnt.append ("</body></html>");
	 int len = cntnt.length ();
	 StringBuffer sb = new StringBuffer ();
	 sb.append (isHTTP11 ? "HTTP/1.1" : "HTTP/1.0");
	 sb.append (REDIRECT_HDR);
	 sb.append (Server.srv.DEFAULT_CHARSET);
	 sb.append ("\r\nLocation: ");
	 sb.append (dest);
	 sb.append ("\r\nContent-Length: ");
	 sb.append (len);
	 sb = appendCookie (sb);
	 sb.append ("\r\n\r\n");
	 sb.append (cntnt);
	 cb = CharBuffer.wrap (sb.toString ());
	 isRedirect = true;
  }
}

⌨️ 快捷键说明

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