
org.xsocket.connection.http.server.ServerUtils Maven / Gradle / Ivy
/*
* 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.server;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xsocket.Execution;
import org.xsocket.ILifeCycle;
import org.xsocket.Resource;
import org.xsocket.connection.IConnectionScoped;
import org.xsocket.connection.IServer;
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.HttpRequest;
import org.xsocket.connection.http.server.HttpProtocolAdapter;
import org.xsocket.connection.http.server.IHttpRequestHandler;
/**
* A utility class
*
* @author [email protected]
*/
final class ServerUtils {
private static final Logger LOG = Logger.getLogger(ServerUtils.class.getName());
@SuppressWarnings("unchecked")
private static final Map httpHandlerInfoCache = HttpUtils.newMapCache(25);
public static final HttpHandlerInfo EMPTY_HTTP_HANDLER_INFO = new HttpHandlerInfo(null);
private static String componentInfo = null;
private static Map mimeTypeMap = null;
private ServerUtils() { }
/**
* get version string
*
* @return the version string
*/
public static String getVersionInfo() {
return HttpUtils.getVersionInfo();
}
/**
* get the mime type file to extension map
*
* @return the mime type file to extension map
*/
synchronized static Map getMimeTypeMapping() {
if (mimeTypeMap == null) {
Map map = new HashMap();
mimeTypeMap = Collections.unmodifiableMap(map);
try {
InputStreamReader isr = new InputStreamReader(HttpUtils.class.getResourceAsStream("/org/xsocket/connection/http/server/mime.types"));
if (isr != null) {
LineNumberReader lnr = new LineNumberReader(isr);
String line = null;
do {
line = lnr.readLine().trim();
if (line != null) {
if (!line.startsWith("#")) {
StringTokenizer st = new StringTokenizer(line);
if (st.hasMoreTokens()) {
String mimeType = st.nextToken();
while (st.hasMoreTokens()) {
String extension = st.nextToken();
map.put(extension, mimeType);
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("mapping " + extension + " -> " + mimeType + " added");
}
}
} else {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("line " + line + "ignored");
}
}
}
}
} while (line != null);
lnr.close();
}
} catch (Exception ignore) { }
}
return mimeTypeMap;
}
/**
* get the component info
* @return the component info
*/
public static String getComponentInfo() {
if (componentInfo == null) {
componentInfo = "xSocket-http/" + ServerUtils.getVersionInfo();
}
return componentInfo;
}
/**
* injects a server field
*
* @param handler the handler
* @param server the server to inject
*/
static void injectServerField(Object handler, IServer server) {
Field[] fields = handler.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Resource.class)) {
Resource res = field.getAnnotation(Resource.class);
if ((field.getType() == IServer.class) || (res.type() == IServer.class)) {
field.setAccessible(true);
try {
field.set(handler, server);
} catch (IllegalAccessException iae) {
LOG.warning("could not set HandlerContext for attribute " + field.getName() + ". Reason " + iae.toString());
}
}
}
}
}
/**
* inject a protocol adapter
*
* @param handler the handler
* @param adapter the adapter to inject
*/
static void injectProtocolAdapter(Object handler, HttpProtocolAdapter adapter) {
Field[] fields = handler.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Resource.class)) {
Resource res = field.getAnnotation(Resource.class);
if ((field.getType() == HttpProtocolAdapter.class) || (res.type() == HttpProtocolAdapter.class)) {
field.setAccessible(true);
try {
field.set(handler, adapter);
} catch (IllegalAccessException iae) {
LOG.warning("could not set HandlerContext for attribute " + field.getName() + ". Reason " + iae.toString());
}
}
}
}
}
@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 HttpHandlerInfo {
private boolean isInvokeOnMessageReceived = false;;
private boolean isMultithreaded = true;
private boolean isConnectHandler = false;
private boolean isConnectHandlerMultithreaded = true;
private boolean isRequestHandler = false;
private boolean isDisconnectHandler = false;
private boolean isDisconnectHandlerMultithreaded = true;
private boolean isRequestTimoutHandler = false;
private boolean isRequestTimoutHandlerMultithreaded = true;
private boolean isConnectionScoped = false;
private boolean isLifeCycle = false;
@SuppressWarnings("unchecked")
public HttpHandlerInfo(Class clazz) {
if (clazz == null) {
return;
}
isConnectionScoped = IConnectionScoped.class.isAssignableFrom(clazz);
isLifeCycle = ILifeCycle.class.isAssignableFrom(clazz);
if (IHttpRequestHandler.class.isAssignableFrom(clazz)) {
isRequestHandler = true;
isMultithreaded = isOnRequestMultithreaded((Class) clazz);
isInvokeOnMessageReceived = isInvokeOnMessageReceived((Class) clazz);
}
if (IHttpConnectHandler.class.isAssignableFrom(clazz)) {
isConnectHandler = true;
isConnectHandlerMultithreaded = isOnConnectMultithreaded(clazz);
}
if (IHttpDisconnectHandler.class.isAssignableFrom(clazz)) {
isDisconnectHandler = true;
isDisconnectHandlerMultithreaded = isOnDisconnectMultithreaded(clazz);
}
if (IHttpRequestTimeoutHandler.class.isAssignableFrom(clazz)) {
isRequestTimoutHandler = true;
isRequestTimoutHandlerMultithreaded = isOnRequestTimeoutMultithreaded(clazz);
}
}
static boolean isOnRequestMultithreaded(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("onRequest", new Class[] { HttpRequest.class, HttpResponseContext.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 isInvokeOnMessageReceived(Class serverHandlerClass) {
int mode = IHttpRequestHandler.DEFAULT_INVOKE_ON_MODE;
InvokeOn invokeOn = serverHandlerClass.getAnnotation(InvokeOn.class);
if (invokeOn != null) {
mode = invokeOn.value();
}
try {
Method meth = serverHandlerClass.getMethod("onRequest", new Class[] { HttpRequest.class, HttpResponseContext.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);
}
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 isOnRequestTimeoutMultithreaded(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("onRequestTimeout", 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;
}
public boolean isRequestTimeoutHandler() {
return isRequestTimoutHandler;
}
public boolean isRequestTimeoutHandlerMultithreaded() {
return isRequestTimoutHandlerMultithreaded;
}
public boolean isConnectionScoped() {
return isConnectionScoped;
}
public boolean isLifeCycle() {
return isLifeCycle;
}
public boolean isRequestHandler() {
return isRequestHandler;
}
public boolean isInvokeOnMessageReceived() {
return isInvokeOnMessageReceived;
}
public boolean isMultithreaded() {
return isMultithreaded;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy