All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xsocket.connection.http.client.ClientUtils Maven / Gradle / Ivy

There is a newer version: 2.0-beta-1
Show newest version
/*
 *  Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
 * The latest copy of this software may be found on http://www.xsocket.org/
 */
package org.xsocket.connection.http.client;




import java.io.IOException;
import java.lang.reflect.Method;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


import org.xsocket.Execution;
import org.xsocket.connection.http.HttpUtils;
import org.xsocket.connection.http.IHttpConnectHandler;
import org.xsocket.connection.http.IHttpConnection;
import org.xsocket.connection.http.IHttpDisconnectHandler;
import org.xsocket.connection.http.IHttpHandler;
import org.xsocket.connection.http.InvokeOn;
import org.xsocket.connection.http.HttpResponse;
import org.xsocket.connection.http.client.IHttpResponseHandler;
import org.xsocket.connection.http.server.IHttpRequestHandler;




/**
* A utility class
*
* @author [email protected]
*/
final class ClientUtils {

	private static final Logger LOG = Logger.getLogger(ClientUtils.class.getName());


	@SuppressWarnings("unchecked")
	private static final Map responseHandlerInfoCache = HttpUtils.newMapCache(25);

	static final ResponseHandlerInfo RESPONSE_HANDLER_INFO_NONTHREADED_HEADER_RECEIVED = new ResponseHandlerInfo(false, false);


	@SuppressWarnings("unchecked")
	private static final Map httpHandlerInfoCache = HttpUtils.newMapCache(25);


	public static final HttpHandlerInfo EMPTY_HTTP_HANDLER_INFO = new HttpHandlerInfo(null);








	@SuppressWarnings("unchecked")
	static ResponseHandlerInfo getResponseHandlerInfo(IHttpResponseHandler responseHandler) throws IOException {
		if (responseHandler == null) {
			throw new IOException("response handler has to be set");
		}

		ResponseHandlerInfo responseHandlerInfo = responseHandlerInfoCache.get(responseHandler.getClass());

		if (responseHandlerInfo == null) {
			responseHandlerInfo = new ResponseHandlerInfo((Class) responseHandler.getClass());
			responseHandlerInfoCache.put(responseHandler.getClass(), responseHandlerInfo);
		}

		return responseHandlerInfo;
	}

	


	@SuppressWarnings("unchecked")
	static HttpHandlerInfo getHttpHandlerInfo(IHttpHandler httpHandler) {
		if (httpHandler == null) {
			return EMPTY_HTTP_HANDLER_INFO;
		}

		HttpHandlerInfo httpHandlerInfo = httpHandlerInfoCache.get(httpHandler.getClass());

		if (httpHandlerInfo == null) {
			httpHandlerInfo = new HttpHandlerInfo((Class) httpHandler.getClass());
			httpHandlerInfoCache.put(httpHandler.getClass(), httpHandlerInfo);
		}

		return httpHandlerInfo;
	}



	static final class ResponseHandlerInfo {
		
		private boolean isResponseHandler = false;
		private boolean isOnResponseMultithreaded = true;
		private boolean invokeOnMessageReceived = false;
		
		private boolean isIoExceptionHandler = false;
		private boolean isIoExceptionHandlerMultithreaded = true;

		private boolean isSocketTimeoutExceptionHandler = false;
		private boolean isSocketTimeoutExceptionHandlerMultithreaded = true;



		
		public ResponseHandlerInfo(Class responseHandlerClass) {

			boolean isMultithreaded = isMultithreaded(responseHandlerClass);
			isOnResponseMultithreaded = isMultithreaded;
			isIoExceptionHandlerMultithreaded = isMultithreaded;
			isSocketTimeoutExceptionHandlerMultithreaded = isMultithreaded;
			
			
			
			if (IHttpResponseHandler.class.isAssignableFrom(responseHandlerClass)) {
				isResponseHandler = true;
				isOnResponseMultithreaded = isOnResponseMultithreaded(responseHandlerClass, isMultithreaded);
				
				isIoExceptionHandler = true;
				isIoExceptionHandlerMultithreaded = isOnIOExceptionMultithreaded(responseHandlerClass, isMultithreaded);
			}
			invokeOnMessageReceived = isInvokationOnMessageReceived(responseHandlerClass);

			
			if (IHttpResponseTimeoutHandler.class.isAssignableFrom(responseHandlerClass)) {
				isSocketTimeoutExceptionHandler = true;
				isSocketTimeoutExceptionHandlerMultithreaded = isOnSocketTimeoutExceptionMultithreaded(responseHandlerClass, isMultithreaded);
			}
		}

		
		ResponseHandlerInfo(boolean invokeOnMessageReceived, boolean isMultithreaded) {
			isResponseHandler = true;
			isIoExceptionHandler = false;
			isSocketTimeoutExceptionHandler = false;
			
			this.invokeOnMessageReceived = invokeOnMessageReceived; 
			this.isOnResponseMultithreaded = isMultithreaded;
		}
		
		
		static boolean isMultithreaded(Class responseHandlerClass) {
			int mode = IHttpResponseHandler.DEFAULT_EXECUTION_MODE;

			Execution execution = responseHandlerClass.getAnnotation(Execution.class);
			if (execution != null) {
				mode = execution.value();
			}
			
			return (mode == Execution.MULTITHREADED);
		}

		

		static boolean isOnResponseMultithreaded(Class responseHandlerClass, boolean defaultMode) {
			boolean isMultithreaded = defaultMode;

			try {
				Method meth = responseHandlerClass.getMethod("onResponse", new Class[] { HttpResponse.class });
				Execution execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					isMultithreaded = (execution.value() == Execution.MULTITHREADED);
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return isMultithreaded;
		}


		static boolean isOnIOExceptionMultithreaded(Class responseHandlerClass, boolean defaultMode) {
			boolean isMultithreaded = defaultMode;

			try {
				Method meth = responseHandlerClass.getMethod("onException", new Class[] { IOException.class });
				Execution execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					isMultithreaded = (execution.value() == Execution.MULTITHREADED);
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return isMultithreaded;
		}
		

		
		static boolean isOnSocketTimeoutExceptionMultithreaded(Class responseHandlerClass, boolean defaultMode) {
			boolean isMultithreaded = defaultMode;

			try {
				Method meth = responseHandlerClass.getMethod("onException", new Class[] { SocketTimeoutException.class });
				Execution execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					isMultithreaded = (execution.value() == Execution.MULTITHREADED);
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return isMultithreaded;
		}


		static boolean isInvokationOnMessageReceived(Class responseHandlerClass) {
			int mode = IHttpResponseHandler.DEFAULT_INVOKE_ON_MODE;

			InvokeOn invokeOn = responseHandlerClass.getAnnotation(InvokeOn.class);
			if (invokeOn != null) {
				mode = invokeOn.value();
			}

			try {
				Method meth = responseHandlerClass.getMethod("onResponse", new Class[] { HttpResponse.class });
				invokeOn = meth.getAnnotation(InvokeOn.class);
				if (invokeOn != null) {
					mode = invokeOn.value();
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because response handler has to have such a method " + nsme.toString());
				}
			}

			return (mode == InvokeOn.MESSAGE_RECEIVED);
		}

		public boolean isResponseHandler() {
			return isResponseHandler;
		}
		
	
		public boolean isInvokationOnMessageReceived() {
			return invokeOnMessageReceived;
		}

		public boolean isOnResponseMultithreaded() {
			return isOnResponseMultithreaded;
		}
		
		
		public boolean isIOExceptionHandlerMultithreaded() {
			return isIoExceptionHandlerMultithreaded;
		}
		
		public boolean isSocketTimeoutExceptionHandler() {
			return isSocketTimeoutExceptionHandler;
		}
		
		public boolean isIOExceptionHandler() {
			return isIoExceptionHandler;
		}
		
		public boolean isSocketTimeoutExceptionHandlerMultithreaded() {
			return isSocketTimeoutExceptionHandlerMultithreaded;
		}

	}





	static final class HttpHandlerInfo {

		private boolean isConnectHandler = false;
		private boolean isConnectHandlerMultithreaded = true;

		private boolean isDisconnectHandler = false;
		private boolean isDisconnectHandlerMultithreaded = true;

		@SuppressWarnings("unchecked")
		public HttpHandlerInfo(Class clazz) {

			if (clazz == null) {
				return;
			}


			if (IHttpConnectHandler.class.isAssignableFrom(clazz)) {
				isConnectHandler = true;
				isConnectHandlerMultithreaded = isOnConnectMultithreaded(clazz);
			}

			if (IHttpDisconnectHandler.class.isAssignableFrom(clazz)) {
				isDisconnectHandler = true;
				isDisconnectHandlerMultithreaded = isOnDisconnectMultithreaded(clazz);
			}
		}





		static boolean isOnConnectMultithreaded(Class serverHandlerClass) {
			int mode = IHttpRequestHandler.DEFAULT_EXECUTION_MODE;

			Execution execution = serverHandlerClass.getAnnotation(Execution.class);
			if (execution != null) {
				mode = execution.value();
			}

			try {
				Method meth = serverHandlerClass.getMethod("onConnect", new Class[] { IHttpConnection.class });
				execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					mode = execution.value();
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return (mode == Execution.MULTITHREADED);
		}


		static boolean isOnDisconnectMultithreaded(Class serverHandlerClass) {
			int mode = IHttpRequestHandler.DEFAULT_EXECUTION_MODE;

			Execution execution = serverHandlerClass.getAnnotation(Execution.class);
			if (execution != null) {
				mode = execution.value();
			}

			try {
				Method meth = serverHandlerClass.getMethod("onDisconnect", new Class[] { IHttpConnection.class });
				execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					mode = execution.value();
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return (mode == Execution.MULTITHREADED);
		}


		static boolean isOnIdleTimeoutMultithreaded(Class serverHandlerClass) {
			int mode = IHttpRequestHandler.DEFAULT_EXECUTION_MODE;

			Execution execution = serverHandlerClass.getAnnotation(Execution.class);
			if (execution != null) {
				mode = execution.value();
			}

			try {
				Method meth = serverHandlerClass.getMethod("onIdleTimeout", new Class[] { IHttpConnection.class });
				execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					mode = execution.value();
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return (mode == Execution.MULTITHREADED);
		}


		static boolean isOnConnectionTimeoutMultithreaded(Class serverHandlerClass) {
			int mode = IHttpRequestHandler.DEFAULT_EXECUTION_MODE;

			Execution execution = serverHandlerClass.getAnnotation(Execution.class);
			if (execution != null) {
				mode = execution.value();
			}

			try {
				Method meth = serverHandlerClass.getMethod("onConnectionTimeout", new Class[] { IHttpConnection.class });
				execution = meth.getAnnotation(Execution.class);
				if (execution != null) {
					mode = execution.value();
				}

			} catch (NoSuchMethodException nsme) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("shouldn't occure because body handler has to have such a method " + nsme.toString());
				}
			}

			return (mode == Execution.MULTITHREADED);
		}
		

		public boolean isConnectHandler() {
			return isConnectHandler;
		}

		public boolean isConnectHandlerMultithreaded() {
			return isConnectHandlerMultithreaded;
		}

		public boolean isDisconnectHandler() {
			return isDisconnectHandler;
		}

		public boolean isDisconnectHandlerMultithreaded() {
			return isDisconnectHandlerMultithreaded;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy