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

📄 mcacheserver.java.backup

📁 httptunnel.jar httptunnel java 源码
💻 BACKUP
字号:
package net.jumperz.app.MGuardian.plugin;

import java.io.*;
import java.util.*;
import net.jumperz.net.*;
import net.jumperz.util.*;
import java.net.*;

public class MCacheServer
extends MGuardianPlugin
{
private static final int DEFAULT_CACHE_SIZE = 100;

private Map cacheMap = new HashMap( DEFAULT_CACHE_SIZE );
private String clearTriggerUri;
private String listUri;
//--------------------------------------------------------------------------------
public void startup()
throws IOException
{
clearTriggerUri = control.getProperty( "cacheServer.clearTriggerUri" );
listUri = control.getProperty( "cacheServer.listUri" );
}
//--------------------------------------------------------------------------------
public Map execute( Map sessionInfo )
throws IOException
{
boolean needCache = false;
MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );

	// clear cache
if( request.getUri().equals( clearTriggerUri ) )
	{
	if( sessionInfo.get( "response" ) == null )
		{
		return clearCache();
		}
	else
		{
		return null;
		}
	}

	// list cache
if( request.getUri().equals( listUri ) )
	{
	if( sessionInfo.get( "response" ) == null )
		{
		return listUri();
		}
	else
		{
		return null;
		}
	}

String url = getUrl( sessionInfo );

if( sessionInfo.get( "response" ) == null )
	{
	return processRequest( sessionInfo, url );
	}
else
	{
	processResponse( sessionInfo, url );
	return null;
	}
}
//--------------------------------------------------------------------------------
private String getUrl( Map sessionInfo )
{
MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );

String url = null;
String uri = request.getUri();
if( uri.startsWith( "/" ) )
	{
	String host = null;
	if( request.headerExists( "Host" ) )
		{
		host = request.getHeaderValue( "Host" );
		}
	else
		{
		Socket clientSocket = ( Socket )sessionInfo.get( "clientSideSocket" );
		host = clientSocket.getLocalAddress().getHostAddress();
		}
	url = "http://" + host + uri;
	}
else
	{
	url = uri;
	}
return url;
}
//--------------------------------------------------------------------------------
private Map listUri()
throws IOException
{
Map pluginResult = new HashMap();
MHttpResponse response = new MHttpResponse();
synchronized( this )
	{
	Set keySet = cacheMap.keySet();
	Iterator p = keySet.iterator();
	StringBuffer s = new StringBuffer();
	while( p.hasNext() )
		{
		s.append( p.next() );
		s.append( "\n" );
		}
	response.setBody( s.toString() );
	}
pluginResult.put( "response", response );
return pluginResult;		
}
//--------------------------------------------------------------------------------
private Map clearCache()
throws IOException
{
Map pluginResult = new HashMap();
MHttpResponse response = new MHttpResponse();
synchronized( this )
	{
	response.setBody( cacheMap.keySet().toString() );
	cacheMap.clear();
	}
pluginResult.put( "response", response );
return pluginResult;	
}
//--------------------------------------------------------------------------------
private synchronized void processResponse( Map sessionInfo, String url )
{
	// already in cache
if( cacheMap.containsKey( url ) )
	{
	return;
	}

MHttpResponse response = ( MHttpResponse )sessionInfo.get( "response" );

response.removeHeaderValue( "Set-Cookie" );
cacheMap.put( url, response );
}
//--------------------------------------------------------------------------------
private synchronized Map processRequest( Map sessionInfo, String url )
{
if( cacheMap.containsKey( url ) )
	{
	boolean reload = false;
	MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );
	if( request.headerExists( "Pragma" ) )
		{
		reload = true;
		}
	if( request.headerExists( "Cache-Control" ) )
		{
		String value = request.getHeaderValue( "Cache-Control" );
		if( value.equalsIgnoreCase( "no-cache" ) )
			{
			reload = true;
			}
		}
	if( reload )
		{
		cacheMap.remove( url );
		return null;
		}
	else
		{
		MHttpResponse response = ( MHttpResponse )cacheMap.get( url );
		Map pluginResult = new HashMap();
		pluginResult.put( "response", response );
		pluginResult.put( "pass", new Boolean( true  ) );
		return pluginResult;		
		}
	}
else
	{
	return null;
	}
}
//--------------------------------------------------------------------------------
}

⌨️ 快捷键说明

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