Author: aparfonov
Date: 2009-10-13 09:50:24 -0400 (Tue, 13 Oct 2009)
New Revision: 274
Added:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ResourceBinder.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/BaseResourceBinder.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/BaseRestServlet.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
Removed:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ResourceBinder.java
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ComponentLifecycleScope.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ProviderBinder.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestDispatcher.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/ProviderDescriptorImpl.java
ws/branches/2.2.x/rest/core/src/test/java/conf/standalone/test-configuration.xml
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/BaseTest.java
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestFilterTest.java
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/ResponseFilterTest.java
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/method/MethodInvokerFilterTest.java
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java
Log:
EXOJCR-185 : do not use container for internal interaction between RequestHandler,
RequestDispatcher and ResourceBinder. Simplify the ProviderBinder
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ComponentLifecycleScope.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ComponentLifecycleScope.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ComponentLifecycleScope.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -21,6 +21,7 @@
/**
* Object scope identifier.
*/
+@Deprecated
public enum ComponentLifecycleScope {
/**
* New instance of object created foe each request.
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -28,7 +28,7 @@
* @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
* @version $Id: $
*/
-
+@Deprecated
public class ContainerObjectFactory<T extends ObjectModel> implements
ObjectFactory<T>
{
Added:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ResourceBinder.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ResourceBinder.java
(rev 0)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ResourceBinder.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest;
+
+import org.exoplatform.services.rest.resource.AbstractResourceDescriptor;
+
+import java.util.List;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public interface ResourceBinder
+{
+ /**
+ * @param resourceClass class of candidate to be root resource
+ * @return true if resource was bound and false if resource was not bound
+ * cause it is not root resource
+ */
+ boolean bind(Class<?> resourceClass);
+
+ /**
+ * Register supplied Object as root resource if it has valid JAX-RS
+ * annotations and no one resource with the same UriPattern already
+ * registered.
+ *
+ * @param resource candidate to be root resource
+ * @return true if resource was bound and false if resource was not bound
+ * cause it is not root resource
+ */
+ boolean bind(Object resource);
+
+ /**
+ * @return all registered root resources
+ */
+ List<ObjectFactory<AbstractResourceDescriptor>> getResources();
+
+ /**
+ * @return number of bound resources
+ */
+ int getSize();
+
+ /**
+ * Remove root resource of supplied class from root resource collection.
+ *
+ * @param clazz root resource class
+ * @return true if resource was unbound false otherwise
+ */
+ boolean unbind(Class<?> clazz);
+
+ boolean unbind(String uriTemplate);
+
+}
Property changes on:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/ResourceBinder.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/BaseResourceBinder.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/BaseResourceBinder.java
(rev 0)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/BaseResourceBinder.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -0,0 +1,481 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.exoplatform.services.rest.Filter;
+import org.exoplatform.services.rest.ObjectFactory;
+import org.exoplatform.services.rest.PerRequestObjectFactory;
+import org.exoplatform.services.rest.RequestFilter;
+import org.exoplatform.services.rest.ResourceBinder;
+import org.exoplatform.services.rest.ResponseFilter;
+import org.exoplatform.services.rest.SingletonObjectFactory;
+import org.exoplatform.services.rest.impl.resource.AbstractResourceDescriptorImpl;
+import org.exoplatform.services.rest.impl.resource.ResourceDescriptorValidator;
+import org.exoplatform.services.rest.method.MethodInvokerFilter;
+import org.exoplatform.services.rest.resource.AbstractResourceDescriptor;
+import org.exoplatform.services.rest.resource.ResourceDescriptorVisitor;
+import org.exoplatform.services.rest.uri.UriPattern;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import javax.ws.rs.ext.RuntimeDelegate;
+
+/**
+ * Lookup for root resource eXo container components at startup and
+ * register/unregister resources via specified methods.
+ *
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class BaseResourceBinder implements ResourceBinder
+{
+
+ /**
+ * Logger.
+ */
+ private static final Log LOG =
ExoLogger.getLogger("ws.rest.core.ResourceBinder");
+
+ private static final Comparator<ObjectFactory<AbstractResourceDescriptor>>
RESOURCE_COMPARATOR =
+ new ResourceComparator();
+
+ /**
+ * Compare two {@link SingletonResourceFactory}.
+ */
+ private static final class ResourceComparator implements
Comparator<ObjectFactory<AbstractResourceDescriptor>>
+ {
+ /**
+ * Compare two ResourceClass for order.
+ *
+ * @param o1 first ResourceClass to be compared
+ * @param o2 second ResourceClass to be compared
+ * @return positive , zero or negative dependent of {@link UriPattern}
+ * comparison
+ * @see Comparator#compare(Object, Object)
+ * @see UriPattern
+ * @see UriPattern#URIPATTERN_COMPARATOR
+ */
+ public int compare(ObjectFactory<AbstractResourceDescriptor> o1,
ObjectFactory<AbstractResourceDescriptor> o2)
+ {
+ return
UriPattern.URIPATTERN_COMPARATOR.compare(o1.getObjectModel().getUriPattern(),
o2.getObjectModel()
+ .getUriPattern());
+ }
+ };
+
+ /**
+ * Root resource descriptors.
+ */
+ private final List<ObjectFactory<AbstractResourceDescriptor>>
rootResources =
+ new ArrayList<ObjectFactory<AbstractResourceDescriptor>>();
+
+ /**
+ * Validator.
+ */
+ private final ResourceDescriptorVisitor rdv =
ResourceDescriptorValidator.getInstance();
+
+ private int size = 0;
+
+ /**
+ * @see RuntimeDelegate
+ */
+ private final RuntimeDelegate rd;
+
+ public BaseResourceBinder()
+ {
+ rd = new RuntimeDelegateImpl();
+ RuntimeDelegate.setInstance(rd);
+ }
+
+ /**
+ * @param containerContext eXo container context
+ * @throws Exception if can't set instance of {@link RuntimeDelegate}
+ */
+ /*
+ @SuppressWarnings("unchecked")
+ public ResourceBinder(ExoContainerContext containerContext) throws Exception
+ {
+ // Initialize RuntimeDelegate instance
+ // This is first component in life cycle what needs.
+ // TODO better solution to initialize RuntimeDelegate
+ rd = new RuntimeDelegateImpl();
+ RuntimeDelegate.setInstance(rd);
+
+ ExoContainer container = containerContext.getContainer();
+
+ // Lookup Applications
+ List<Application> al =
container.getComponentInstancesOfType(Application.class);
+ for (Application a : al)
+ {
+ try
+ {
+ addApplication(a);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Failed add JAX-RS application " +
a.getClass().getName(), e);
+ }
+ }
+
+ // Lookup all object which implements ResourceContainer interface and
+ // process them to be add as root resources.
+ for (Object resource :
container.getComponentInstancesOfType(ResourceContainer.class))
+ {
+ bind(resource);
+ }
+
+ }
+ */
+
+ /**
+ * @param application Application
+ * @see Application
+ */
+ @SuppressWarnings("unchecked")
+ @Deprecated
+ public void addApplication(Application application)
+ {
+ ProviderBinder providers = ProviderBinder.getInstance();
+ for (Object obj : application.getSingletons())
+ {
+ if (obj.getClass().getAnnotation(Provider.class) != null)
+ {
+ // singleton provider
+ if (obj instanceof ContextResolver)
+ {
+ providers.addContextResolver((ContextResolver)obj);
+ }
+ if (obj instanceof ExceptionMapper)
+ {
+ providers.addExceptionMapper((ExceptionMapper)obj);
+ }
+ if (obj instanceof MessageBodyReader)
+ {
+ providers.addMessageBodyReader((MessageBodyReader)obj);
+ }
+ if (obj instanceof MessageBodyWriter)
+ {
+ providers.addMessageBodyWriter((MessageBodyWriter)obj);
+ }
+ }
+ else if (obj.getClass().getAnnotation(Filter.class) != null)
+ {
+ // singleton filter
+ if (obj instanceof MethodInvokerFilter)
+ {
+ providers.addMethodInvokerFilter((MethodInvokerFilter)obj);
+ }
+ if (obj instanceof RequestFilter)
+ {
+ providers.addRequestFilter((RequestFilter)obj);
+ }
+ if (obj instanceof ResponseFilter)
+ {
+ providers.addResponseFilter((ResponseFilter)obj);
+ }
+ }
+ else
+ {
+ bind(obj); // singleton resource
+ }
+ }
+ for (Class clazz : application.getClasses())
+ {
+ if (clazz.getAnnotation(Provider.class) != null)
+ {
+ // per-request provider
+ if (ContextResolver.class.isAssignableFrom(clazz))
+ {
+ providers.addContextResolver(clazz);
+ }
+ if (ExceptionMapper.class.isAssignableFrom(clazz))
+ {
+ providers.addExceptionMapper(clazz);
+ }
+ if (MessageBodyReader.class.isAssignableFrom(clazz))
+ {
+ providers.addMessageBodyReader(clazz);
+ }
+ if (MessageBodyWriter.class.isAssignableFrom(clazz))
+ {
+ providers.addMessageBodyWriter(clazz);
+ }
+ }
+ else if (clazz.getAnnotation(Filter.class) != null)
+ {
+ // per-request filter
+ if (MethodInvokerFilter.class.isAssignableFrom(clazz))
+ {
+ providers.addMethodInvokerFilter(clazz);
+ }
+ if (RequestFilter.class.isAssignableFrom(clazz))
+ {
+ providers.addRequestFilter(clazz);
+ }
+ if (ResponseFilter.class.isAssignableFrom(clazz))
+ {
+ providers.addResponseFilter(clazz);
+ }
+ }
+ else
+ {
+ bind(clazz); // per-request resource
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean bind(final Object resource)
+ {
+ final Path path = resource.getClass().getAnnotation(Path.class);
+
+ AbstractResourceDescriptor descriptor = null;
+ if (path != null)
+ {
+ try
+ {
+ descriptor = new AbstractResourceDescriptorImpl(resource);
+ }
+ catch (Exception e)
+ {
+ String msg = "Unexpected error occurs when process resource class "
+ resource.getClass().getName();
+ LOG.error(msg, e);
+ return false;
+ }
+ }
+ else
+ {
+ String msg =
+ "Resource class " + resource.getClass().getName() + " it is
not root resource. "
+ + "Path annotation javax.ws.rs.Path is not specified for this
class.";
+ LOG.warn(msg);
+ return false;
+ }
+
+ // validate AbstractResourceDescriptor
+ try
+ {
+ descriptor.accept(rdv);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Validation of root resource failed. ", e);
+ return false;
+ }
+
+ synchronized (rootResources)
+ {
+ // check does exist other resource with the same URI pattern
+ for (ObjectFactory<AbstractResourceDescriptor> exist : rootResources)
+ {
+ if
(exist.getObjectModel().getUriPattern().equals(descriptor.getUriPattern()))
+ {
+ String msg =
+ "Resource class " + descriptor.getObjectClass().getName() +
" can't be registered. Resource class "
+ + exist.getClass().getName() + " with the same pattern "
+ + exist.getObjectModel().getUriPattern().getTemplate() + "
already registered.";
+ LOG.warn(msg);
+ return false;
+ }
+ }
+
+ // Singleton resource
+ ObjectFactory<AbstractResourceDescriptor> res =
+ new SingletonObjectFactory<AbstractResourceDescriptor>(descriptor,
resource);
+ rootResources.add(res);
+ Collections.sort(rootResources, RESOURCE_COMPARATOR);
+ LOG.info("Bind new resource " +
res.getObjectModel().getUriPattern().getTemplate() + " : "
+ + descriptor.getObjectClass());
+ }
+ size++;
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean bind(final Class<?> resourceClass)
+ {
+ final Path path = resourceClass.getAnnotation(Path.class);
+
+ AbstractResourceDescriptor descriptor = null;
+ if (path != null)
+ {
+ try
+ {
+ descriptor = new AbstractResourceDescriptorImpl(resourceClass);
+ }
+ catch (Exception e)
+ {
+ String msg = "Unexpected error occurs when process resource class "
+ resourceClass.getName();
+ LOG.error(msg, e);
+ return false;
+ }
+ }
+ else
+ {
+ String msg =
+ "Resource class " + resourceClass.getName() + " it is not root
resource. "
+ + "Path annotation javax.ws.rs.Path is not specified for this
class.";
+ LOG.warn(msg);
+ return false;
+ }
+
+ // validate AbstractResourceDescriptor
+ try
+ {
+ descriptor.accept(rdv);
+ }
+ catch (Exception e)
+ {
+ LOG.error("Validation of root resource failed. ", e);
+ return false;
+ }
+
+ synchronized (rootResources)
+ {
+ // check does exist other resource with the same URI pattern
+ for (ObjectFactory<AbstractResourceDescriptor> exist : rootResources)
+ {
+ AbstractResourceDescriptor existDescriptor = exist.getObjectModel();
+ if
(exist.getObjectModel().getUriPattern().equals(descriptor.getUriPattern()))
+ {
+
+ String msg =
+ "Resource class " + descriptor.getObjectClass().getName() +
" can't be registered. Resource class "
+ + existDescriptor.getObjectClass().getName() + " with the same
pattern "
+ + exist.getObjectModel().getUriPattern().getTemplate() + "
already registered.";
+ LOG.warn(msg);
+ return false;
+ }
+ }
+ // per-request resource
+ ObjectFactory<AbstractResourceDescriptor> res =
+ new PerRequestObjectFactory<AbstractResourceDescriptor>(descriptor);
+ rootResources.add(res);
+ Collections.sort(rootResources, RESOURCE_COMPARATOR);
+ LOG.info("Bind new resource " +
res.getObjectModel().getUriPattern().getRegex() + " : " + resourceClass);
+ }
+ size++;
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("unchecked")
+ public boolean unbind(Class clazz)
+ {
+ synchronized (rootResources)
+ {
+ Iterator<ObjectFactory<AbstractResourceDescriptor>> i =
rootResources.iterator();
+ while (i.hasNext())
+ {
+ ObjectFactory<AbstractResourceDescriptor> res = i.next();
+ Class c = res.getObjectModel().getObjectClass();
+ if (clazz.equals(c))
+ {
+ i.remove();
+ LOG.info("Remove ResourceContainer " +
res.getObjectModel().getUriPattern().getTemplate() + " : " + c);
+ size--;
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean unbind(String uriTemplate)
+ {
+ synchronized (rootResources)
+ {
+ Iterator<ObjectFactory<AbstractResourceDescriptor>> i =
rootResources.iterator();
+ while (i.hasNext())
+ {
+ ObjectFactory<AbstractResourceDescriptor> res = i.next();
+ String t = res.getObjectModel().getUriPattern().getTemplate();
+ if (t.equals(uriTemplate))
+ {
+ i.remove();
+ LOG.info("Remove ResourceContainer " +
res.getObjectModel().getUriPattern().getTemplate());
+ size--;
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * Clear the list of ResourceContainer description.
+ */
+ public void clear()
+ {
+ rootResources.clear();
+ size = 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<ObjectFactory<AbstractResourceDescriptor>> getResources()
+ {
+ return rootResources;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getSize()
+ {
+ return size;
+ }
+
+ /**
+ * @return all registered root resources
+ */
+ @Deprecated
+ public List<AbstractResourceDescriptor> getRootResources()
+ {
+ List<AbstractResourceDescriptor> l = new
ArrayList<AbstractResourceDescriptor>(rootResources.size());
+ synchronized (rootResources)
+ {
+ for (ObjectFactory<AbstractResourceDescriptor> f : rootResources)
+ l.add(f.getObjectModel());
+ }
+ return l;
+ }
+
+}
Property changes on:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/BaseResourceBinder.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ProviderBinder.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ProviderBinder.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ProviderBinder.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -20,8 +20,6 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
-import org.exoplatform.services.rest.ComponentLifecycleScope;
-import org.exoplatform.services.rest.ContainerObjectFactory;
import org.exoplatform.services.rest.FilterDescriptor;
import org.exoplatform.services.rest.ObjectFactory;
import org.exoplatform.services.rest.PerRequestObjectFactory;
@@ -120,7 +118,7 @@
*/
void init()
{
- // TODO remove this hard code
+ // Add known Providers, Filters, etc with predefined life cycle.
ByteEntityProvider baep = new ByteEntityProvider();
addMessageBodyReader(baep);
addMessageBodyWriter(baep);
@@ -180,7 +178,7 @@
addMessageBodyReader(MultipartFormDataEntityProvider.class);
// JAXB context
- addContextResolver(JAXBContextResolver.class, null,
ComponentLifecycleScope.CONTAINER);
+ addContextResolver(JAXBContextResolver.class);
}
@@ -243,7 +241,10 @@
{
try
{
- addContextResolver(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addContextResolver(new
PerRequestObjectFactory<ProviderDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -262,7 +263,10 @@
Class<? extends ContextResolver> clazz = instance.getClass();
try
{
- addContextResolver(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addContextResolver(new
SingletonObjectFactory<ProviderDescriptor>(descriptor, instance));
}
catch (Exception e)
{
@@ -270,6 +274,47 @@
}
}
+ protected void addContextResolver(ObjectFactory<ProviderDescriptor>
contextResolverFactory)
+ {
+ for (Type type :
contextResolverFactory.getObjectModel().getObjectClass().getGenericInterfaces())
+ {
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType pt = (ParameterizedType)type;
+ if (ContextResolver.class == pt.getRawType())
+ {
+ Type[] atypes = pt.getActualTypeArguments();
+ if (atypes.length > 1)
+ throw new RuntimeException("Unable strong determine actual type
argument, more then one type found.");
+
+ Class<?> aclazz = (Class<?>)atypes[0];
+
+ MediaTypeMap<ObjectFactory<ProviderDescriptor>> pm =
contextResolvers.get(aclazz);
+
+ if (pm == null)
+ {
+ pm = new
MediaTypeMap<ObjectFactory<ProviderDescriptor>>();
+ contextResolvers.put(aclazz, pm);
+ }
+
+ for (MediaType mime : contextResolverFactory.getObjectModel().produces())
+ {
+ if (pm.get(mime) != null)
+ {
+ String msg =
+ "ContextResolver for " + aclazz.getName() + " and
media type " + mime + " already registered.";
+ throw new RuntimeException(msg);
+ }
+ else
+ {
+ pm.put(mime, contextResolverFactory);
+ }
+ }
+ }
+ }
+ }
+ }
+
/**
* Add per-request ExceptionMapper.
*
@@ -280,7 +325,7 @@
{
try
{
- addExceptionMapper(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ addExceptionMapper(new PerRequestObjectFactory(new
ProviderDescriptorImpl(clazz)));
}
catch (Exception e)
{
@@ -299,7 +344,7 @@
Class<? extends ExceptionMapper> clazz = instance.getClass();
try
{
- addExceptionMapper(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ addExceptionMapper(new SingletonObjectFactory(new ProviderDescriptorImpl(clazz),
instance));
}
catch (Exception e)
{
@@ -307,6 +352,33 @@
}
}
+ @SuppressWarnings("unchecked")
+ protected void addExceptionMapper(ObjectFactory<ProviderDescriptor>
exceptionMapperFactory)
+ {
+ for (Type type :
exceptionMapperFactory.getObjectModel().getObjectClass().getGenericInterfaces())
+ {
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType pt = (ParameterizedType)type;
+ if (ExceptionMapper.class == pt.getRawType())
+ {
+ Type[] atypes = pt.getActualTypeArguments();
+ if (atypes.length > 1)
+ throw new RuntimeException("Unable strong determine actual type
argument, more then one type found.");
+ Class<? extends Throwable> exc = (Class<? extends
Throwable>)atypes[0];
+
+ if (exceptionMappers.get(exc) != null)
+ {
+ String msg = "ExceptionMapper for exception " + exc + "
already registered.";
+ throw new RuntimeException(msg);
+ }
+
+ exceptionMappers.put(exc, exceptionMapperFactory);
+ }
+ }
+ }
+ }
+
/**
* Add per-request MessageBodyReader.
*
@@ -317,7 +389,10 @@
{
try
{
- addMessageBodyReader(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMessageBodyReader(new
PerRequestObjectFactory<ProviderDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -336,7 +411,10 @@
Class<? extends MessageBodyReader> clazz = instance.getClass();
try
{
- addMessageBodyReader(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMessageBodyReader(new SingletonObjectFactory(descriptor, instance));
}
catch (Exception e)
{
@@ -344,6 +422,16 @@
}
}
+ protected void addMessageBodyReader(ObjectFactory<ProviderDescriptor>
readerFactory)
+ {
+ // MessageBodyReader is smart component and can determine which type it
+ // supports, see method MessageBodyReader.isReadable. So here does not
+ // check is reader for the same Java and media type already exists.
+ // Let it be under developer's control.
+ for (MediaType mime : readerFactory.getObjectModel().consumes())
+ readProviders.getList(mime).add(readerFactory);
+ }
+
/**
* Add per-request MessageBodyWriter.
*
@@ -354,7 +442,10 @@
{
try
{
- addMessageBodyWriter(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMessageBodyWriter(new
PerRequestObjectFactory<ProviderDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -373,7 +464,10 @@
Class<? extends MessageBodyWriter> clazz = instance.getClass();
try
{
- addMessageBodyWriter(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMessageBodyWriter(new
SingletonObjectFactory<ProviderDescriptor>(descriptor, instance));
}
catch (Exception e)
{
@@ -381,6 +475,17 @@
}
}
+ protected void addMessageBodyWriter(ObjectFactory<ProviderDescriptor>
writerFactory)
+ {
+ // MessageBodyWriter is smart component and can determine which type it
+ // supports, see method MessageBodyWriter.isWriteable. So here does not
+ // check is writer for the same Java and media type already exists.
+ // Let it be under developer's control.
+ for (MediaType mime : writerFactory.getObjectModel().produces())
+ writeProviders.getList(mime).add(writerFactory);
+ }
+
+
/**
* Get list of most acceptable writer's media type for specified type.
*
@@ -491,7 +596,10 @@
{
try
{
- addMethodInvokerFilter(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMethodInvokerFilter(new
PerRequestObjectFactory<FilterDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -509,13 +617,21 @@
Class<? extends MethodInvokerFilter> clazz = instance.getClass();
try
{
- addMethodInvokerFilter(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addMethodInvokerFilter(new
SingletonObjectFactory<FilterDescriptor>(descriptor, instance));
}
catch (Exception e)
{
LOG.error("Failed add RequestFilter " + clazz.getName(), e);
}
}
+
+ protected void addMethodInvokerFilter(ObjectFactory<FilterDescriptor>
filterFactory)
+ {
+
invokerFilters.getList(filterFactory.getObjectModel().getUriPattern()).add(filterFactory);
+ }
/**
* Add per-request RequestFilter.
@@ -526,7 +642,10 @@
{
try
{
- addRequestFilter(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addRequestFilter(new
PerRequestObjectFactory<FilterDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -544,13 +663,21 @@
Class<? extends RequestFilter> clazz = instance.getClass();
try
{
- addRequestFilter(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addRequestFilter(new SingletonObjectFactory<FilterDescriptor>(descriptor,
instance));
}
catch (Exception e)
{
LOG.error("Failed add RequestFilter " + clazz.getName(), e);
}
}
+
+ protected void addRequestFilter(ObjectFactory<FilterDescriptor> filterFactory)
+ {
+
requestFilters.getList(filterFactory.getObjectModel().getUriPattern()).add(filterFactory);
+ }
/**
* Add per-request ResponseFilter.
@@ -561,7 +688,10 @@
{
try
{
- addResponseFilter(clazz, null, ComponentLifecycleScope.PER_REQUEST);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addResponseFilter(new
PerRequestObjectFactory<FilterDescriptor>(descriptor));
}
catch (Exception e)
{
@@ -579,13 +709,21 @@
Class<? extends ResponseFilter> clazz = instance.getClass();
try
{
- addResponseFilter(clazz, instance, ComponentLifecycleScope.SINGLETON);
+ FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
+ descriptor.accept(rdv);
+
+ addResponseFilter(new SingletonObjectFactory<FilterDescriptor>(descriptor,
instance));
}
catch (Exception e)
{
LOG.error("Failed add ResponseFilter " + clazz.getName(), e);
}
}
+
+ protected void addResponseFilter(ObjectFactory<FilterDescriptor> filterFactory)
+ {
+
responseFilters.getList(filterFactory.getObjectModel().getUriPattern()).add(filterFactory);
+ }
/**
* @param path request path
@@ -726,293 +864,4 @@
return null;
}
- /**
- * @param clazz ContextResolver class
- * @param instance ContextResolver instance, may be null if not singleton
- * instance
- * @param scope ComponentLifecycleScope
- */
- @SuppressWarnings("unchecked")
- public void addContextResolver(Class<? extends ContextResolver> clazz,
ContextResolver instance,
- ComponentLifecycleScope scope)
- {
- for (Type type : clazz.getGenericInterfaces())
- {
- if (type instanceof ParameterizedType)
- {
- ParameterizedType pt = (ParameterizedType)type;
- if (ContextResolver.class == pt.getRawType())
- {
- Type[] atypes = pt.getActualTypeArguments();
- if (atypes.length > 1)
- throw new RuntimeException("Unable strong determine actual type
argument, more then one type found.");
-
- Class<?> aclazz = (Class<?>)atypes[0];
-
- MediaTypeMap<ObjectFactory<ProviderDescriptor>> pm =
contextResolvers.get(aclazz);
-
- if (pm == null)
- {
- pm = new
MediaTypeMap<ObjectFactory<ProviderDescriptor>>();
- contextResolvers.put(aclazz, pm);
- }
-
- ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<ProviderDescriptor> factory = null;
- switch (scope)
- {
- case PER_REQUEST :
- factory = new
PerRequestObjectFactory<ProviderDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("ContextResolver instance is
null.");
- factory = new
SingletonObjectFactory<ProviderDescriptor>(descriptor, instance);
- break;
- case CONTAINER :
- factory = new
ContainerObjectFactory<ProviderDescriptor>(descriptor);
- break;
- }
-
- for (MediaType mime : factory.getObjectModel().produces())
- {
- if (pm.get(mime) != null)
- {
- String msg =
- "ContextResolver for " + aclazz.getName() + " and
media type " + mime + " already registered.";
- throw new RuntimeException(msg);
- }
- else
- {
- pm.put(mime, factory);
- }
- }
- }
- }
- }
- }
-
- /**
- * @param clazz MessageBodyreader class
- * @param instance MessageBodyReader, may be null if not singleton instance
- * @param scope ComponentLifecycleScope
- */
- @SuppressWarnings("unchecked")
- public void addMessageBodyReader(Class<? extends MessageBodyReader> clazz,
MessageBodyReader instance,
- ComponentLifecycleScope scope)
- {
- ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<ProviderDescriptor> factory = null;
- switch (scope)
- {
- case PER_REQUEST :
- factory = new PerRequestObjectFactory<ProviderDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("MessageBodyReader instance is
null.");
- factory = new SingletonObjectFactory<ProviderDescriptor>(descriptor,
instance);
- break;
- case CONTAINER :
- factory = new ContainerObjectFactory<ProviderDescriptor>(descriptor);
- break;
- }
-
- // MessageBodyReader is smart component and can determine which type it
- // supports, see method MessageBodyReader.isReadable. So here does not
- // check is reader for the same Java and media type already exists.
- // Let it be under developer's control.
- for (MediaType mime : factory.getObjectModel().consumes())
- readProviders.getList(mime).add(factory);
- }
-
- /**
- * @param clazz MessageBodyWriter class
- * @param instance MessageBodyWriter, may be null if not singleton instance
- * @param scope ComponentLifecycleScope
- */
- @SuppressWarnings("unchecked")
- public void addMessageBodyWriter(Class<? extends MessageBodyWriter> clazz,
MessageBodyWriter instance,
- ComponentLifecycleScope scope)
- {
- ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<ProviderDescriptor> factory = null;
- switch (scope)
- {
- case PER_REQUEST :
- factory = new PerRequestObjectFactory<ProviderDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("MessageBodyWriter instance is
null.");
- factory = new SingletonObjectFactory<ProviderDescriptor>(descriptor,
instance);
- break;
- case CONTAINER :
- factory = new ContainerObjectFactory<ProviderDescriptor>(descriptor);
- break;
- }
-
- // MessageBodyWriter is smart component and can determine which type it
- // supports, see method MessageBodyWriter.isWriteable. So here does not
- // check is writer for the same Java and media type already exists.
- // Let it be under developer's control.
- for (MediaType mime : factory.getObjectModel().produces())
- writeProviders.getList(mime).add(factory);
- }
-
- /**
- * @param clazz ExceptionMapper class
- * @param instance ExceptionMapper instance, may be null if not singleton
- * instance
- * @param scope ComponentLifecycleScope
- */
- @SuppressWarnings("unchecked")
- public void addExceptionMapper(Class<? extends ExceptionMapper> clazz,
ExceptionMapper instance,
- ComponentLifecycleScope scope)
- {
- for (Type type : clazz.getGenericInterfaces())
- {
- if (type instanceof ParameterizedType)
- {
- ParameterizedType pt = (ParameterizedType)type;
- if (ExceptionMapper.class == pt.getRawType())
- {
- Type[] atypes = pt.getActualTypeArguments();
- if (atypes.length > 1)
- throw new RuntimeException("Unable strong determine actual type
argument, more then one type found.");
- Class<? extends Throwable> exc = (Class<? extends
Throwable>)atypes[0];
-
- if (exceptionMappers.get(exc) != null)
- {
- String msg = "ExceptionMapper for exception " + exc + "
already registered.";
- throw new RuntimeException(msg);
- }
-
- ProviderDescriptor descriptor = new ProviderDescriptorImpl(clazz);
- descriptor.accept(rdv);
- ObjectFactory<ProviderDescriptor> factory = null;
-
- switch (scope)
- {
- case PER_REQUEST :
- factory = new
PerRequestObjectFactory<ProviderDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("ExceptionMapper instance is
null.");
- factory = new
SingletonObjectFactory<ProviderDescriptor>(descriptor, instance);
- break;
- case CONTAINER :
- factory = new
ContainerObjectFactory<ProviderDescriptor>(descriptor);
- break;
- }
-
- exceptionMappers.put(exc, factory);
- }
- }
- }
- }
-
- /**
- * @param clazz RequestFilter class
- * @param instance RequestFilter instance, may be null if not singleton
- * instance
- * @param scope ComponentLifecycleScope
- */
- public void addRequestFilter(Class<? extends RequestFilter> clazz, RequestFilter
instance,
- ComponentLifecycleScope scope)
- {
-
- FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<FilterDescriptor> factory = new
PerRequestObjectFactory<FilterDescriptor>(descriptor);
- switch (scope)
- {
- case PER_REQUEST :
- factory = new PerRequestObjectFactory<FilterDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("RequestFilter instance is
null.");
- factory = new SingletonObjectFactory<FilterDescriptor>(descriptor,
instance);
- break;
- case CONTAINER :
- factory = new ContainerObjectFactory<FilterDescriptor>(descriptor);
- break;
- }
-
- requestFilters.getList(descriptor.getUriPattern()).add(factory);
- }
-
- /**
- * @param clazz ResponseFilter class
- * @param instance ResponseFilter instance, may be null if not singleton
- * instance
- * @param scope ComponentLifecycleScope
- */
- public void addResponseFilter(Class<? extends ResponseFilter> clazz,
ResponseFilter instance,
- ComponentLifecycleScope scope)
- {
-
- FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<FilterDescriptor> factory = new
PerRequestObjectFactory<FilterDescriptor>(descriptor);
- switch (scope)
- {
- case PER_REQUEST :
- factory = new PerRequestObjectFactory<FilterDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("ResponseFilter instance is
null.");
- factory = new SingletonObjectFactory<FilterDescriptor>(descriptor,
instance);
- break;
- case CONTAINER :
- factory = new ContainerObjectFactory<FilterDescriptor>(descriptor);
- break;
- }
-
- responseFilters.getList(descriptor.getUriPattern()).add(factory);
- }
-
- /**
- * @param clazz MethodInvokerFilter class
- * @param instance MethodInvokerFilter instance, may be null if not singleton
- * instance
- * @param scope ComponentLifecycleScope
- */
- public void addMethodInvokerFilter(Class<? extends MethodInvokerFilter> clazz,
MethodInvokerFilter instance,
- ComponentLifecycleScope scope)
- {
-
- FilterDescriptor descriptor = new FilterDescriptorImpl(clazz);
- descriptor.accept(rdv);
-
- ObjectFactory<FilterDescriptor> factory = new
PerRequestObjectFactory<FilterDescriptor>(descriptor);
- switch (scope)
- {
- case PER_REQUEST :
- factory = new PerRequestObjectFactory<FilterDescriptor>(descriptor);
- break;
- case SINGLETON :
- if (instance == null)
- throw new NullPointerException("MethodInvokerFilter instance is
null.");
- factory = new SingletonObjectFactory<FilterDescriptor>(descriptor,
instance);
- break;
- case CONTAINER :
- factory = new ContainerObjectFactory<FilterDescriptor>(descriptor);
- break;
- }
-
- invokerFilters.getList(descriptor.getUriPattern()).add(factory);
- }
-
}
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestDispatcher.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestDispatcher.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestDispatcher.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -24,6 +24,7 @@
import org.exoplatform.services.rest.GenericContainerRequest;
import org.exoplatform.services.rest.GenericContainerResponse;
import org.exoplatform.services.rest.ObjectFactory;
+import org.exoplatform.services.rest.ResourceBinder;
import org.exoplatform.services.rest.SingletonObjectFactory;
import org.exoplatform.services.rest.impl.header.HeaderHelper;
import org.exoplatform.services.rest.impl.header.MediaTypeHelper;
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -18,23 +18,6 @@
*/
package org.exoplatform.services.rest.impl;
-import java.io.File;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.StreamingOutput;
-import javax.ws.rs.ext.ExceptionMapper;
-
-import org.exoplatform.container.component.ComponentPlugin;
-import org.exoplatform.container.xml.InitParams;
-import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.ApplicationContext;
@@ -44,18 +27,27 @@
import org.exoplatform.services.rest.ObjectFactory;
import org.exoplatform.services.rest.RequestFilter;
import org.exoplatform.services.rest.RequestHandler;
+import org.exoplatform.services.rest.ResourceBinder;
import org.exoplatform.services.rest.ResponseFilter;
-import org.exoplatform.services.rest.impl.method.MethodInvokerFilterComponentPlugin;
-import org.exoplatform.services.rest.impl.provider.EntityProviderComponentPlugin;
-import org.exoplatform.services.rest.method.MethodInvokerFilter;
-import org.exoplatform.services.rest.provider.EntityProvider;
import org.picocontainer.Startable;
+import java.io.File;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.StreamingOutput;
+import javax.ws.rs.ext.ExceptionMapper;
+
/**
* @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
* @version $Id: $
*/
-public final class RequestHandlerImpl implements RequestHandler, Startable
+public final class RequestHandlerImpl implements RequestHandler
{
/**
@@ -86,27 +78,22 @@
properties.put(name, value);
}
+ private final ResourceBinder binder;
+
/**
* Constructs new instance of {@link RequestHandler}.
- *
- * @param dispatcher See {@link RequestDispatcher}
- * @param params init parameters
*/
- public RequestHandlerImpl(RequestDispatcher dispatcher, InitParams params)
+ public RequestHandlerImpl()
{
- if (params != null)
- {
- for (Iterator<ValueParam> i = params.getValueParamIterator();
i.hasNext();)
- {
- ValueParam vp = i.next();
- properties.put(vp.getName(), vp.getValue());
- }
- }
-
- this.dispatcher = dispatcher;
-
+ this.binder = new BaseResourceBinder();
+ this.dispatcher = new RequestDispatcher(binder);
}
-
+
+ public ResourceBinder getBinder()
+ {
+ return binder;
+ }
+
// RequestHandler
/**
@@ -271,26 +258,7 @@
}
- // Startable
-
/**
- * {@inheritDoc}
- */
- public void start()
- {
- init();
- }
-
- /**
- * {@inheritDoc}
- */
- public void stop()
- {
- }
-
- //
-
- /**
* Startup initialization.
*/
public void init()
@@ -327,44 +295,4 @@
}
- /**
- * Processing {@link ComponentPlugin} for injection external components.
- *
- * @param plugin See {@link ComponentPlugin}
- */
- @SuppressWarnings("unchecked")
- public void addPlugin(ComponentPlugin plugin)
- {
- // NOTE!!! ProviderBinder should be already initialized by ResourceBinder
- ProviderBinder providers = ProviderBinder.getInstance();
- if (MethodInvokerFilterComponentPlugin.class.isAssignableFrom(plugin.getClass()))
- {
- // add method invoker filter
- for (Class<? extends MethodInvokerFilter> filter :
((MethodInvokerFilterComponentPlugin)plugin).getFilters())
- providers.addMethodInvokerFilter(filter);
- }
- else if (EntityProviderComponentPlugin.class.isAssignableFrom(plugin.getClass()))
- {
- // add external entity providers
- Set<Class<? extends EntityProvider>> eps =
((EntityProviderComponentPlugin)plugin).getEntityProviders();
- for (Class<? extends EntityProvider> ep : eps)
- {
- providers.addMessageBodyReader(ep);
- providers.addMessageBodyWriter(ep);
- }
- }
- else if (RequestFilterComponentPlugin.class.isAssignableFrom(plugin.getClass()))
- {
- Set<Class<? extends RequestFilter>> filters =
((RequestFilterComponentPlugin)plugin).getFilters();
- for (Class<? extends RequestFilter> filter : filters)
- providers.addRequestFilter(filter);
- }
- else if (ResponseFilterComponentPlugin.class.isAssignableFrom(plugin.getClass()))
- {
- Set<Class<? extends ResponseFilter>> filters =
((ResponseFilterComponentPlugin)plugin).getFilters();
- for (Class<? extends ResponseFilter> filter : filters)
- providers.addResponseFilter(filter);
- }
- }
-
}
Deleted:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ResourceBinder.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ResourceBinder.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/ResourceBinder.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -1,484 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This 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 software 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 software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.rest.impl;
-
-import org.exoplatform.container.ExoContainer;
-import org.exoplatform.container.ExoContainerContext;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-import org.exoplatform.services.rest.Filter;
-import org.exoplatform.services.rest.ObjectFactory;
-import org.exoplatform.services.rest.PerRequestObjectFactory;
-import org.exoplatform.services.rest.RequestFilter;
-import org.exoplatform.services.rest.ResponseFilter;
-import org.exoplatform.services.rest.SingletonObjectFactory;
-import org.exoplatform.services.rest.impl.resource.AbstractResourceDescriptorImpl;
-import org.exoplatform.services.rest.impl.resource.ResourceDescriptorValidator;
-import org.exoplatform.services.rest.method.MethodInvokerFilter;
-import org.exoplatform.services.rest.resource.AbstractResourceDescriptor;
-import org.exoplatform.services.rest.resource.ResourceContainer;
-import org.exoplatform.services.rest.resource.ResourceDescriptorVisitor;
-import org.exoplatform.services.rest.uri.UriPattern;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.ws.rs.Path;
-import javax.ws.rs.core.Application;
-import javax.ws.rs.ext.ContextResolver;
-import javax.ws.rs.ext.ExceptionMapper;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
-import javax.ws.rs.ext.RuntimeDelegate;
-
-/**
- * Lookup for root resource eXo container components at startup and
- * register/unregister resources via specified methods.
- *
- * @see AbstractResourceDescriptor
- * @see SingletonResourceFactory
- * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
- * @version $Id: $
- */
-public final class ResourceBinder
-{
-
- /**
- * Logger.
- */
- private static final Log LOG =
ExoLogger.getLogger("ws.rest.core.ResourceBinder");
-
- private static final Comparator<ObjectFactory<AbstractResourceDescriptor>>
RESOURCE_COMPARATOR =
- new ResourceComparator();
-
- /**
- * Compare two {@link SingletonResourceFactory}.
- */
- private static final class ResourceComparator implements
Comparator<ObjectFactory<AbstractResourceDescriptor>>
- {
- /**
- * Compare two ResourceClass for order.
- *
- * @param o1 first ResourceClass to be compared
- * @param o2 second ResourceClass to be compared
- * @return positive , zero or negative dependent of {@link UriPattern}
- * comparison
- * @see Comparator#compare(Object, Object)
- * @see UriPattern
- * @see UriPattern#URIPATTERN_COMPARATOR
- */
- public int compare(ObjectFactory<AbstractResourceDescriptor> o1,
ObjectFactory<AbstractResourceDescriptor> o2)
- {
- return
UriPattern.URIPATTERN_COMPARATOR.compare(o1.getObjectModel().getUriPattern(),
o2.getObjectModel()
- .getUriPattern());
- }
- };
-
- /**
- * Root resource descriptors.
- */
- private final List<ObjectFactory<AbstractResourceDescriptor>>
rootResources =
- new ArrayList<ObjectFactory<AbstractResourceDescriptor>>();
-
- /**
- * Validator.
- */
- private final ResourceDescriptorVisitor rdv =
ResourceDescriptorValidator.getInstance();
-
- private int size = 0;
-
- /**
- * @see RuntimeDelegate
- */
- private final RuntimeDelegate rd;
-
- /**
- * @param containerContext eXo container context
- * @throws Exception if can't set instance of {@link RuntimeDelegate}
- */
- @SuppressWarnings("unchecked")
- public ResourceBinder(ExoContainerContext containerContext) throws Exception
- {
- // Initialize RuntimeDelegate instance
- // This is first component in life cycle what needs.
- // TODO better solution to initialize RuntimeDelegate
- rd = new RuntimeDelegateImpl();
- RuntimeDelegate.setInstance(rd);
-
- ExoContainer container = containerContext.getContainer();
-
- // Lookup Applications
- List<Application> al =
container.getComponentInstancesOfType(Application.class);
- for (Application a : al)
- {
- try
- {
- addApplication(a);
- }
- catch (Exception e)
- {
- LOG.error("Failed add JAX-RS application " +
a.getClass().getName(), e);
- }
- }
-
- // Lookup all object which implements ResourceContainer interface and
- // process them to be add as root resources.
- for (Object resource :
container.getComponentInstancesOfType(ResourceContainer.class))
- {
- bind(resource);
- }
-
- }
-
- /**
- * @param application Application
- * @see Application
- */
- @SuppressWarnings("unchecked")
- public void addApplication(Application application)
- {
- ProviderBinder providers = ProviderBinder.getInstance();
- for (Object obj : application.getSingletons())
- {
- if (obj.getClass().getAnnotation(Provider.class) != null)
- {
- // singleton provider
- if (obj instanceof ContextResolver)
- {
- providers.addContextResolver((ContextResolver)obj);
- }
- if (obj instanceof ExceptionMapper)
- {
- providers.addExceptionMapper((ExceptionMapper)obj);
- }
- if (obj instanceof MessageBodyReader)
- {
- providers.addMessageBodyReader((MessageBodyReader)obj);
- }
- if (obj instanceof MessageBodyWriter)
- {
- providers.addMessageBodyWriter((MessageBodyWriter)obj);
- }
- }
- else if (obj.getClass().getAnnotation(Filter.class) != null)
- {
- // singleton filter
- if (obj instanceof MethodInvokerFilter)
- {
- providers.addMethodInvokerFilter((MethodInvokerFilter)obj);
- }
- if (obj instanceof RequestFilter)
- {
- providers.addRequestFilter((RequestFilter)obj);
- }
- if (obj instanceof ResponseFilter)
- {
- providers.addResponseFilter((ResponseFilter)obj);
- }
- }
- else
- {
- bind(obj); // singleton resource
- }
- }
- for (Class clazz : application.getClasses())
- {
- if (clazz.getAnnotation(Provider.class) != null)
- {
- // per-request provider
- if (ContextResolver.class.isAssignableFrom(clazz))
- {
- providers.addContextResolver(clazz);
- }
- if (ExceptionMapper.class.isAssignableFrom(clazz))
- {
- providers.addExceptionMapper(clazz);
- }
- if (MessageBodyReader.class.isAssignableFrom(clazz))
- {
- providers.addMessageBodyReader(clazz);
- }
- if (MessageBodyWriter.class.isAssignableFrom(clazz))
- {
- providers.addMessageBodyWriter(clazz);
- }
- }
- else if (clazz.getAnnotation(Filter.class) != null)
- {
- // per-request filter
- if (MethodInvokerFilter.class.isAssignableFrom(clazz))
- {
- providers.addMethodInvokerFilter(clazz);
- }
- if (RequestFilter.class.isAssignableFrom(clazz))
- {
- providers.addRequestFilter(clazz);
- }
- if (ResponseFilter.class.isAssignableFrom(clazz))
- {
- providers.addResponseFilter(clazz);
- }
- }
- else
- {
- bind(clazz); // per-request resource
- }
- }
- }
-
- /**
- * Register supplied Object as root resource if it has valid JAX-RS
- * annotations and no one resource with the same UriPattern already
- * registered.
- *
- * @param resource candidate to be root resource
- * @return true if resource was bound and false if resource was not bound
- * cause it is not root resource
- */
- public boolean bind(final Object resource)
- {
- final Path path = resource.getClass().getAnnotation(Path.class);
-
- AbstractResourceDescriptor descriptor = null;
- if (path != null)
- {
- try
- {
- descriptor = new AbstractResourceDescriptorImpl(resource);
- }
- catch (Exception e)
- {
- String msg = "Unexpected error occurs when process resource class "
+ resource.getClass().getName();
- LOG.error(msg, e);
- return false;
- }
- }
- else
- {
- String msg =
- "Resource class " + resource.getClass().getName() + " it is
not root resource. "
- + "Path annotation javax.ws.rs.Path is not specified for this
class.";
- LOG.warn(msg);
- return false;
- }
-
- // validate AbstractResourceDescriptor
- try
- {
- descriptor.accept(rdv);
- }
- catch (Exception e)
- {
- LOG.error("Validation of root resource failed. ", e);
- return false;
- }
-
- synchronized (rootResources)
- {
- // check does exist other resource with the same URI pattern
- for (ObjectFactory<AbstractResourceDescriptor> exist : rootResources)
- {
- if
(exist.getObjectModel().getUriPattern().equals(descriptor.getUriPattern()))
- {
- String msg =
- "Resource class " + descriptor.getObjectClass().getName() +
" can't be registered. Resource class "
- + exist.getClass().getName() + " with the same pattern "
- + exist.getObjectModel().getUriPattern().getTemplate() + "
already registered.";
- LOG.warn(msg);
- return false;
- }
- }
-
- // Singleton resource
- ObjectFactory<AbstractResourceDescriptor> res =
- new SingletonObjectFactory<AbstractResourceDescriptor>(descriptor,
resource);
- rootResources.add(res);
- Collections.sort(rootResources, RESOURCE_COMPARATOR);
- LOG.info("Bind new resource " +
res.getObjectModel().getUriPattern().getTemplate() + " : "
- + descriptor.getObjectClass());
- }
- size++;
- return true;
- }
-
- /**
- * @param resourceClass class of candidate to be root resource
- * @return true if resource was bound and false if resource was not bound
- * cause it is not root resource
- */
- public boolean bind(final Class<?> resourceClass)
- {
- final Path path = resourceClass.getAnnotation(Path.class);
-
- AbstractResourceDescriptor descriptor = null;
- if (path != null)
- {
- try
- {
- descriptor = new AbstractResourceDescriptorImpl(resourceClass);
- }
- catch (Exception e)
- {
- String msg = "Unexpected error occurs when process resource class "
+ resourceClass.getName();
- LOG.error(msg, e);
- return false;
- }
- }
- else
- {
- String msg =
- "Resource class " + resourceClass.getName() + " it is not root
resource. "
- + "Path annotation javax.ws.rs.Path is not specified for this
class.";
- LOG.warn(msg);
- return false;
- }
-
- // validate AbstractResourceDescriptor
- try
- {
- descriptor.accept(rdv);
- }
- catch (Exception e)
- {
- LOG.error("Validation of root resource failed. ", e);
- return false;
- }
-
- synchronized (rootResources)
- {
- // check does exist other resource with the same URI pattern
- for (ObjectFactory<AbstractResourceDescriptor> exist : rootResources)
- {
- AbstractResourceDescriptor existDescriptor = exist.getObjectModel();
- if
(exist.getObjectModel().getUriPattern().equals(descriptor.getUriPattern()))
- {
-
- String msg =
- "Resource class " + descriptor.getObjectClass().getName() +
" can't be registered. Resource class "
- + existDescriptor.getObjectClass().getName() + " with the same
pattern "
- + exist.getObjectModel().getUriPattern().getTemplate() + "
already registered.";
- LOG.warn(msg);
- return false;
- }
- }
- // per-request resource
- ObjectFactory<AbstractResourceDescriptor> res =
- new PerRequestObjectFactory<AbstractResourceDescriptor>(descriptor);
- rootResources.add(res);
- Collections.sort(rootResources, RESOURCE_COMPARATOR);
- LOG.info("Bind new resource " +
res.getObjectModel().getUriPattern().getRegex() + " : " + resourceClass);
- }
- size++;
- return true;
- }
-
- /**
- * Remove root resource of supplied class from root resource collection.
- *
- * @param clazz root resource class
- * @return true if resource was unbound false otherwise
- */
- @SuppressWarnings("unchecked")
- public boolean unbind(Class clazz)
- {
- synchronized (rootResources)
- {
- Iterator<ObjectFactory<AbstractResourceDescriptor>> i =
rootResources.iterator();
- while (i.hasNext())
- {
- ObjectFactory<AbstractResourceDescriptor> res = i.next();
- Class c = res.getObjectModel().getObjectClass();
- if (clazz.equals(c))
- {
- i.remove();
- LOG.info("Remove ResourceContainer " +
res.getObjectModel().getUriPattern().getTemplate() + " : " + c);
- size--;
- return true;
- }
- }
- return false;
- }
- }
-
- public boolean unbind(String uriTemplate)
- {
- synchronized (rootResources)
- {
- Iterator<ObjectFactory<AbstractResourceDescriptor>> i =
rootResources.iterator();
- while (i.hasNext())
- {
- ObjectFactory<AbstractResourceDescriptor> res = i.next();
- String t = res.getObjectModel().getUriPattern().getTemplate();
- if (t.equals(uriTemplate))
- {
- i.remove();
- LOG.info("Remove ResourceContainer " +
res.getObjectModel().getUriPattern().getTemplate());
- size--;
- return true;
- }
- }
- return false;
- }
- }
-
- /**
- * Clear the list of ResourceContainer description.
- */
- public void clear()
- {
- rootResources.clear();
- size = 0;
- }
-
- /**
- * @return all registered root resources
- */
- public List<ObjectFactory<AbstractResourceDescriptor>> getResources()
- {
- return rootResources;
- }
-
- /**
- * @return number of bound resources
- */
- public int getSize()
- {
- return size;
- }
-
- /**
- * @return all registered root resources
- */
- @Deprecated
- public List<AbstractResourceDescriptor> getRootResources()
- {
- List<AbstractResourceDescriptor> l = new
ArrayList<AbstractResourceDescriptor>(rootResources.size());
- synchronized (rootResources)
- {
- for (ObjectFactory<AbstractResourceDescriptor> f : rootResources)
- l.add(f.getObjectModel());
- }
- return l;
- }
-
-}
Modified:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/ProviderDescriptorImpl.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/ProviderDescriptorImpl.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/ProviderDescriptorImpl.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -83,6 +83,7 @@
/**
* @param provider provider instance
*/
+ @Deprecated
public ProviderDescriptorImpl(Object provider)
{
this(provider.getClass(), ComponentLifecycleScope.SINGLETON);
Added:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/BaseRestServlet.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/BaseRestServlet.java
(rev 0)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/BaseRestServlet.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.servlet;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.exoplatform.services.rest.ContainerResponseWriter;
+import org.exoplatform.services.rest.GenericContainerResponse;
+import org.exoplatform.services.rest.RequestHandler;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.EnvironmentContext;
+import org.exoplatform.services.rest.impl.header.HeaderHelper;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class BaseRestServlet extends HttpServlet
+{
+
+ /**
+ * See {@link ContainerResponseWriter}.
+ */
+ class ServletContainerResponseWriter implements ContainerResponseWriter
+ {
+
+ /**
+ * See {@link HttpServletResponse}.
+ */
+ private HttpServletResponse servletResponse;
+
+ /**
+ * @param response HttpServletResponse
+ */
+ ServletContainerResponseWriter(HttpServletResponse response)
+ {
+ this.servletResponse = response;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("unchecked")
+ public void writeBody(GenericContainerResponse response, MessageBodyWriter
entityWriter) throws IOException
+ {
+ Object entity = response.getEntity();
+ if (entity != null)
+ {
+ OutputStream out = servletResponse.getOutputStream();
+ entityWriter.writeTo(entity, entity.getClass(), response.getEntityType(),
null, response.getContentType(),
+ response.getHttpHeaders(), out);
+ out.flush();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void writeHeaders(GenericContainerResponse response) throws IOException
+ {
+ if (servletResponse.isCommitted())
+ return;
+
+ servletResponse.setStatus(response.getStatus());
+
+ if (response.getHttpHeaders() != null)
+ {
+ // content-type and content-length should be preset in headers
+ for (Map.Entry<String, List<Object>> e :
response.getHttpHeaders().entrySet())
+ {
+ String name = e.getKey();
+ for (Object o : e.getValue())
+ {
+ String value = null;
+ if (o != null && (value = HeaderHelper.getHeaderAsString(o)) !=
null)
+ servletResponse.addHeader(name, value);
+ }
+ }
+ }
+ }
+ }
+
+ private static final Log LOG = ExoLogger.getLogger(RestServlet.class.getName());
+
+ private RequestHandler requestHandler;
+
+ private ServletConfig servletConfig;
+
+ protected RequestHandler getRequestHandler()
+ {
+ return requestHandler;
+ }
+
+ public void init(ServletConfig servletConfig)
+ {
+ this.servletConfig = servletConfig;
+ requestHandler =
(RequestHandler)servletConfig.getServletContext().getAttribute(RequestHandler.class.getName());
+ }
+
+ public void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException,
+ ServletException
+ {
+
+ EnvironmentContext env = new EnvironmentContext();
+ env.put(HttpServletRequest.class, httpRequest);
+ env.put(HttpServletResponse.class, httpResponse);
+ env.put(ServletConfig.class, servletConfig);
+ env.put(ServletContext.class, getServletContext());
+
+ try
+ {
+ EnvironmentContext.setCurrent(env);
+ ServletContainerRequest request = new ServletContainerRequest(httpRequest);
+ ContainerResponse response = new ContainerResponse(new
ServletContainerResponseWriter(httpResponse));
+ requestHandler.handleRequest(request, response);
+ }
+ catch (Exception e)
+ {
+ LOG.error(e);
+ throw new ServletException(e);
+ }
+ finally
+ {
+ EnvironmentContext.setCurrent(null);
+ }
+ }
+
+}
Property changes on:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/BaseRestServlet.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
(rev 0)
+++
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.servlet;
+
+import org.exoplatform.services.rest.RequestHandler;
+import org.exoplatform.services.rest.impl.RequestHandlerImpl;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class RestInitializedListener implements ServletContextListener
+{
+
+ public void contextDestroyed(ServletContextEvent event)
+ {
+ }
+
+ public void contextInitialized(ServletContextEvent event)
+ {
+ RequestHandler handler = new RequestHandlerImpl();
+ event.getServletContext().setAttribute(RequestHandler.class.getName(), handler);
+ }
+
+}
Property changes on:
ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/servlet/RestInitializedListener.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified:
ws/branches/2.2.x/rest/core/src/test/java/conf/standalone/test-configuration.xml
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/conf/standalone/test-configuration.xml 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/conf/standalone/test-configuration.xml 2009-10-13
13:50:24 UTC (rev 274)
@@ -51,7 +51,7 @@
</init-params>
</component>
- <component>
+ <!-- component>
<type>org.exoplatform.services.rest.impl.RequestHandlerImpl</type>
</component>
<component>
@@ -59,7 +59,7 @@
</component>
<component>
<type>org.exoplatform.services.rest.impl.ResourceBinder</type>
- </component>
+ </component -->
<component>
<type>org.exoplatform.services.rest.impl.provider.JAXBContextResolver</type>
</component>
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/BaseTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/BaseTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/BaseTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -22,9 +22,9 @@
import org.exoplatform.container.StandaloneContainer;
import org.exoplatform.services.rest.impl.ApplicationContextImpl;
+import org.exoplatform.services.rest.impl.BaseResourceBinder;
import org.exoplatform.services.rest.impl.ProviderBinder;
import org.exoplatform.services.rest.impl.RequestHandlerImpl;
-import org.exoplatform.services.rest.impl.ResourceBinder;
/**
* @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
@@ -33,26 +33,31 @@
public abstract class BaseTest extends TestCase
{
- protected StandaloneContainer container;
+// protected StandaloneContainer container;
protected ProviderBinder providers;
- protected ResourceBinder binder;
+ protected BaseResourceBinder binder;
protected RequestHandlerImpl requestHandler;
public void setUp() throws Exception
{
StandaloneContainer.setConfigurationPath("src/test/java/conf/standalone/test-configuration.xml");
- container = StandaloneContainer.getInstance();
- binder =
(ResourceBinder)container.getComponentInstanceOfType(ResourceBinder.class);
- requestHandler =
(RequestHandlerImpl)container.getComponentInstanceOfType(RequestHandlerImpl.class);
+// container = StandaloneContainer.getInstance();
+
+ requestHandler = new RequestHandlerImpl();
+ requestHandler.init();
+ binder = (BaseResourceBinder)requestHandler.getBinder();
+
+// binder =
(ResourceBinder)container.getComponentInstanceOfType(ResourceBinder.class);
+// requestHandler =
(RequestHandlerImpl)container.getComponentInstanceOfType(RequestHandlerImpl.class);
// reset providers to be sure it is clean
ProviderBinder.setInstance(new ProviderBinder());
providers = ProviderBinder.getInstance();
// System.out.println("##########################"+providers);
ApplicationContextImpl.setCurrent(new ApplicationContextImpl(null, null,
providers));
- binder.clear();
+// binder.clear();
}
public void tearDown() throws Exception
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -256,7 +256,7 @@
public void testResourceConstructorsContainer() throws Exception
{
- container.registerComponentInstance(TestContainerComponent.class.getName(), new
TestContainerComponent());
+// container.registerComponentInstance(TestContainerComponent.class.getName(), new
TestContainerComponent());
registry(Resource4.class);
service("GET", "/aaa/bbb", "", null, null);
unregistry(Resource4.class);
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestFilterTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestFilterTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestFilterTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -54,18 +54,18 @@
private HttpServletRequest httpRequest;
- private ResourceBinder binder; // exo container component
+// private ResourceBinder binder; // exo container component
- public RequestFilter1(@Context Providers providers, @Context HttpServletRequest
httpRequest, ResourceBinder binder)
+ public RequestFilter1(@Context Providers providers, @Context HttpServletRequest
httpRequest/*, ResourceBinder binder*/)
{
this.providers = providers;
this.httpRequest = httpRequest;
- this.binder = binder;
+// this.binder = binder;
}
public void doFilter(GenericContainerRequest request)
{
- if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null && binder != null)
+ if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null/* && binder != null*/)
request.setMethod("POST");
}
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/ResponseFilterTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/ResponseFilterTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/ResponseFilterTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -54,19 +54,19 @@
private HttpServletRequest httpRequest;
- private ResourceBinder binder; // exo container component
+// private ResourceBinder binder; // exo container component
- public ResponseFilter1(@Context Providers providers, @Context HttpServletRequest
httpRequest,
- ResourceBinder binder)
+ public ResponseFilter1(@Context Providers providers, @Context HttpServletRequest
httpRequest/*,
+ ResourceBinder binder*/)
{
this.providers = providers;
this.httpRequest = httpRequest;
- this.binder = binder;
+// this.binder = binder;
}
public void doFilter(GenericContainerResponse response)
{
- if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null && binder != null)
+ if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null /*&& binder != null*/)
response.setResponse(Response.status(200).entity("to be or not to
be").type("text/plain").build());
}
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/method/MethodInvokerFilterTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/method/MethodInvokerFilterTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/method/MethodInvokerFilterTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -20,7 +20,6 @@
import org.exoplatform.services.rest.AbstractResourceTest;
import org.exoplatform.services.rest.Filter;
-import org.exoplatform.services.rest.impl.ResourceBinder;
import org.exoplatform.services.rest.method.MethodInvokerFilter;
import org.exoplatform.services.rest.resource.GenericMethodResource;
import org.exoplatform.services.rest.resource.ResourceMethodDescriptor;
@@ -57,18 +56,18 @@
@Context
private HttpServletRequest httpRequest;
- private ResourceBinder binder; // exo container component
+// private ResourceBinder binder; // exo container component
- public MethodInvokerFilter1(@Context UriInfo uriInfo, @Context HttpHeaders
httpHeaders, ResourceBinder binder)
+ public MethodInvokerFilter1(@Context UriInfo uriInfo, @Context HttpHeaders
httpHeaders/*, ResourceBinder binder*/)
{
this.uriInfo = uriInfo;
this.httpHeaders = httpHeaders;
- this.binder = binder;
+// this.binder = binder;
}
public void accept(GenericMethodResource genericMethodResource)
{
- if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null && binder != null)
+ if (uriInfo != null && httpHeaders != null && providers != null
&& httpRequest != null/* && binder != null*/)
{
if (genericMethodResource instanceof SubResourceMethodDescriptor)
// not invoke sub-resource method
Modified:
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java
===================================================================
---
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java 2009-10-13
09:58:47 UTC (rev 273)
+++
ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java 2009-10-13
13:50:24 UTC (rev 274)
@@ -40,15 +40,15 @@
private static final String DATA = "{\"name\":\"andrew\",
\"password\":\"hello\"}";
- private RequestHandler requestHandler;
+// private RequestHandler requestHandler;
private MediaType mediaType;
public void setUp() throws Exception
{
super.setUp();
- requestHandler =
(RequestHandler)container.getComponentInstanceOfType(RequestHandler.class);
- assertNotNull(requestHandler);
+// requestHandler =
(RequestHandler)container.getComponentInstanceOfType(RequestHandler.class);
+// assertNotNull(requestHandler);
mediaType = new MediaType("application", "json");
}