cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stevend...@apache.org
Subject svn commit: r1156358 - in /cocoon/cocoon3/trunk/cocoon-rest/src: main/java/org/apache/cocoon/rest/controller/ test/java/org/apache/cocoon/rest/controller/
Date Wed, 10 Aug 2011 20:38:57 GMT
Author: stevendolg
Date: Wed Aug 10 20:38:56 2011
New Revision: 1156358

URL: http://svn.apache.org/viewvc?rev=1156358&view=rev
Log:
Reworked class to fix warnings.
Introduced more meaningful exceptions.

Added:
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
  (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/NonPrototypeControllerException.java
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/SpringRESTController.java
    cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/SpringRESTControllerTest.java

Added: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java?rev=1156358&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
Wed Aug 10 20:38:56 2011
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.rest.controller;
+
+import org.apache.cocoon.pipeline.SetupException;
+
+
+public class IllegalFieldAnnotationException extends SetupException {
+
+    private static final long serialVersionUID = 1L;
+
+    public IllegalFieldAnnotationException(String message) {
+        super(message);
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/IllegalFieldAnnotationException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/NonPrototypeControllerException.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/NonPrototypeControllerException.java?rev=1156358&r1=1156357&r2=1156358&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/NonPrototypeControllerException.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/NonPrototypeControllerException.java
Wed Aug 10 20:38:56 2011
@@ -16,9 +16,9 @@
  */
 package org.apache.cocoon.rest.controller;
 
-import org.apache.cocoon.pipeline.ProcessingException;
+import org.apache.cocoon.pipeline.SetupException;
 
-public class NonPrototypeControllerException extends ProcessingException {
+public class NonPrototypeControllerException extends SetupException {
 
     private static final long serialVersionUID = 1L;
 

Modified: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/SpringRESTController.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/SpringRESTController.java?rev=1156358&r1=1156357&r2=1156358&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/SpringRESTController.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/SpringRESTController.java
Wed Aug 10 20:38:56 2011
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
@@ -37,7 +38,6 @@ import org.apache.cocoon.controller.Cont
 import org.apache.cocoon.pipeline.caching.CacheKey;
 import org.apache.cocoon.rest.controller.annotation.BaseURL;
 import org.apache.cocoon.rest.controller.annotation.Inject;
-import org.apache.cocoon.rest.controller.annotation.RESTController;
 import org.apache.cocoon.rest.controller.annotation.RequestHeader;
 import org.apache.cocoon.rest.controller.annotation.RequestParameter;
 import org.apache.cocoon.rest.controller.annotation.SitemapParameter;
@@ -49,10 +49,9 @@ import org.apache.cocoon.servlet.collect
 import org.apache.cocoon.servlet.util.HttpContextHelper;
 import org.apache.cocoon.servlet.util.SettingsHelper;
 import org.apache.cocoon.sitemap.util.ExceptionHandler;
+import org.apache.cocoon.sitemap.util.SpringProxyHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.aop.framework.Advised;
-import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 
@@ -60,7 +59,8 @@ import org.springframework.context.Appli
  * <p>
  * This controller is responsible for the execution of instances of REST controller beans.
Note that
  * each controller implementation must be available as Spring bean. For that purpose you
can use the
- * {@link RESTController} annotation and load all beans from a particular package automatically.
See
+ * {@link org.apache.cocoon.rest.controller.annotation.RESTController RESTController} annotation
and
+ * load all beans from a particular package automatically. See
  * http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
  * for details.
  * </p>
@@ -94,11 +94,21 @@ public class SpringRESTController implem
         return readCacheKey();
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.apache.cocoon.controller.Controller#setup(java.lang.String, java.util.Map,
+     *      java.util.Map)
+     * 
+     * @throws UnknownControllerException if a controller is requested that is not registered
as
+     *             Spring bean.
+     * @throws NonPrototypeControllerException if the requested controller is not configured
to run
+     *             in the prototype scope.
+     * @throws IllegalFieldAnnotationException if the requested controller contains dependency
+     *             injection fields that cannot be satisfied.
+     */
     public void setup(String controllerName, Map<String, Object> inputParameters, Map<String,
? extends Object> configuration) {
-        if (!this.applicationContext.isPrototype(controllerName)) {
-            throw new NonPrototypeControllerException("Requested REST controller '" + controllerName
-                    + "' is defined within the 'prototype' scope.");
-        }
+        this.validateControllerBean(controllerName);
 
         try {
             // get the prepared controller
@@ -126,7 +136,18 @@ public class SpringRESTController implem
         } catch (Exception e) {
             throw ExceptionHandler.getInvocationException(e);
         }
+    }
+
+    private void validateControllerBean(String controllerName) {
+        if (!this.applicationContext.containsBean(controllerName)) {
+            throw new UnknownControllerException("Requested REST controller '" + controllerName
+                    + "' is not defined within the Spring application context.");
+        }
 
+        if (!this.applicationContext.isPrototype(controllerName)) {
+            throw new NonPrototypeControllerException("Requested REST controller '" + controllerName
+                    + "' is not defined within the 'prototype' scope.");
+        }
     }
 
     public void execute(OutputStream outputStream) {
@@ -146,7 +167,7 @@ public class SpringRESTController implem
      *
      * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
      */
-    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
+    public void setApplicationContext(ApplicationContext applicationContext) {
         this.applicationContext = applicationContext;
     }
 
@@ -155,9 +176,9 @@ public class SpringRESTController implem
     }
 
     private Object getController(String controllerName, Map<String, Object> inputParameters,
-            Map<String, ? extends Object> configuration) throws Exception {
+            Map<String, ? extends Object> configuration) throws IllegalAccessException,
IOException {
         Object controller = this.applicationContext.getBean(controllerName);
-        Object unpackedController = unpackProxy(controller);
+        Object unpackedController = SpringProxyHelper.unpackProxy(controller);
         Map<Class<? extends Annotation>, List<Field>> annotatedFields =
this.annotationCollector
                 .getAnnotatedFields(unpackedController.getClass());
 
@@ -165,195 +186,232 @@ public class SpringRESTController implem
         populateInjectFields(inputParameters, unpackedController, annotatedFields);
         populateRequestFields(inputParameters, unpackedController, annotatedFields);
         populateRequestHeaderFields(inputParameters, unpackedController, annotatedFields);
-        populateSitemapParameters(configuration, unpackedController, annotatedFields);
-        populateBaseURL(configuration, unpackedController, annotatedFields);
+        populateSitemapParameterFields(configuration, unpackedController, annotatedFields);
+        populateBaseURLFields(configuration, unpackedController, annotatedFields);
 
         return controller;
     }
 
-    private static void populateBaseURL(Map<String, ? extends Object> configuration,
Object controller,
-            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, Exception {
-        List<Field> baseURLFields = annotatedFields.get(BaseURL.class);
-        if (baseURLFields == null || baseURLFields.isEmpty()) {
-            return;
+    private static boolean isBlank(String string) {
+        if (string == null) {
+            return true;
         }
 
-        for (Field field : baseURLFields) {
-            field.setAccessible(true);
-
-            Class<?> fieldType = field.getType();
-            if (fieldType == URL.class) {
-                field.set(controller, configuration.get("baseUrl"));
-            } else {
-                throw new Exception("The annotation " + BaseURL.class.getName() + " can only
be set on fields of type "
-                        + URL.class.getName() + "." + " " + "(field=" + field.getName() +
", type="
-                        + fieldType.getName() + ")");
+        for (int i = 0; i < string.length(); i++) {
+            if (!Character.isWhitespace(string.charAt(i))) {
+                return false;
             }
         }
+
+        return true;
     }
 
-    public static <T> T unpackProxy(T proxy) throws Exception {
-        if (proxy instanceof Advised) {
-            Advised advised = (Advised) proxy;
-            @SuppressWarnings("unchecked")
-            T target = (T) advised.getTargetSource().getTarget();
-            return target;
+    private static void populateBaseURLFields(Map<String, ? extends Object> configuration,
Object controller,
+            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException {
+        List<Field> baseURLFields = annotatedFields.get(BaseURL.class);
+        if (baseURLFields == null || baseURLFields.isEmpty()) {
+            return;
         }
 
-        return proxy;
+        for (Field field : baseURLFields) {
+            populateBaseURLField(configuration, controller, field);
+        }
     }
 
-    private static boolean isBlank(String str) {
-        int strLen;
-        if (str == null || (strLen = str.length()) == 0) {
-            return true;
-        }
-        for (int i = 0; i < strLen; i++) {
-            if (Character.isWhitespace(str.charAt(i)) == false) {
-                return false;
-            }
+    private static void populateBaseURLField(Map<String, ? extends Object> configuration,
Object controller, Field field)
+            throws IllegalAccessException {
+        field.setAccessible(true);
+
+        Class<?> fieldType = field.getType();
+        if (fieldType == URL.class) {
+            field.set(controller, configuration.get("baseUrl"));
+        } else {
+            throwIllegalFieldAnnotationException(BaseURL.class, field, URL.class);
         }
-        return true;
     }
 
     private static void populateInjectFields(Map<String, ? extends Object> parameters,
Object controller,
-            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, IOException,
-            Exception {
+            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, IOException {
         List<Field> injectFields = annotatedFields.get(Inject.class);
         if (injectFields == null || injectFields.isEmpty()) {
             return;
         }
 
-        HttpServletRequest request = HttpContextHelper.getRequest(parameters);
-        HttpServletResponse response = HttpContextHelper.getResponse(parameters);
-
         for (Field field : injectFields) {
-            field.setAccessible(true);
+            populateInjectField(parameters, controller, field);
+        }
+    }
 
-            Class<?> fieldType = field.getType();
-            if (fieldType == HttpServletRequest.class) {
-                field.set(controller, request);
-            } else if (fieldType == HttpServletResponse.class) {
-                field.set(controller, response);
-            } else if (fieldType == Logger.class) {
-                field.set(controller, LoggerFactory.getLogger(controller.getClass()));
-            } else if (fieldType == ServletInputStream.class || fieldType == InputStream.class)
{
-                field.set(controller, request.getInputStream());
-            } else if (fieldType == ServletOutputStream.class || fieldType == OutputStream.class)
{
-                field.set(controller, response.getOutputStream());
-            } else if (fieldType == ServletContext.class) {
-                ServletContext servletContext = HttpContextHelper.getServletContext(parameters);
-                field.set(controller, servletContext);
-            } else if (fieldType == Settings.class) {
-                Settings settings = SettingsHelper.getSettings(parameters);
-                field.set(controller, settings);
-            } else {
-                throw new Exception("The annotation " + Inject.class.getName()
-                        + " doesn't support the injection of type " + fieldType.getName()
+ "." + " " + "(field="
-                        + field.getName() + ", type=" + fieldType.getName() + ")");
-            }
+    private static void populateInjectField(Map<String, ? extends Object> parameters,
Object controller, Field field)
+            throws IllegalAccessException, IOException {
+        field.setAccessible(true);
+
+        Class<?> fieldType = field.getType();
+        if (fieldType == HttpServletRequest.class) {
+            HttpServletRequest request = HttpContextHelper.getRequest(parameters);
+            field.set(controller, request);
+        } else if (fieldType == HttpServletResponse.class) {
+            HttpServletResponse response = HttpContextHelper.getResponse(parameters);
+            field.set(controller, response);
+        } else if (fieldType == Logger.class) {
+            field.set(controller, LoggerFactory.getLogger(controller.getClass()));
+        } else if (fieldType == ServletInputStream.class || fieldType == InputStream.class)
{
+            HttpServletRequest request = HttpContextHelper.getRequest(parameters);
+            field.set(controller, request.getInputStream());
+        } else if (fieldType == ServletOutputStream.class || fieldType == OutputStream.class)
{
+            HttpServletResponse response = HttpContextHelper.getResponse(parameters);
+            field.set(controller, response.getOutputStream());
+        } else if (fieldType == ServletContext.class) {
+            ServletContext servletContext = HttpContextHelper.getServletContext(parameters);
+            field.set(controller, servletContext);
+        } else if (fieldType == Settings.class) {
+            Settings settings = SettingsHelper.getSettings(parameters);
+            field.set(controller, settings);
+        } else {
+            throwIllegalFieldAnnotationException(Inject.class, field, HttpServletRequest.class,
+                    HttpServletResponse.class, Logger.class, ServletInputStream.class, InputStream.class,
+                    ServletOutputStream.class, OutputStream.class, ServletContext.class,
Settings.class);
         }
     }
 
     private static void populateRequestFields(Map<String, ? extends Object> parameters,
Object controller,
-            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, Exception {
+            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException {
         List<Field> requestFields = annotatedFields.get(RequestParameter.class);
         if (requestFields == null || requestFields.isEmpty()) {
             return;
         }
 
-        HttpServletRequest request = HttpContextHelper.getRequest(parameters);
         for (Field field : requestFields) {
-            field.setAccessible(true);
+            populateRequestField(parameters, controller, field);
+        }
+    }
 
-            String requestParameterName = field.getAnnotation(RequestParameter.class).value();
-            if (isBlank(requestParameterName)) {
-                requestParameterName = field.getName();
-            }
+    private static void populateRequestField(Map<String, ? extends Object> parameters,
Object controller, Field field)
+            throws IllegalAccessException {
+        field.setAccessible(true);
+
+        HttpServletRequest request = HttpContextHelper.getRequest(parameters);
 
-            Class<?> fieldType = field.getType();
-            if (fieldType == String.class) {
-                String parameter = request.getParameter(requestParameterName);
-                if (parameter != null) {
-                    field.set(controller, parameter);
-                }
-            } else if (fieldType == int.class) {
-                String parameter = request.getParameter(requestParameterName);
-                if (parameter != null) {
-                    field.set(controller, Integer.parseInt(parameter));
-                }
-            } else if (fieldType == boolean.class) {
-                String parameter = request.getParameter(requestParameterName);
-                if (parameter != null) {
-                    field.set(controller, Boolean.parseBoolean(parameter));
-                }
-            } else if (fieldType == String[].class) {
-                String[] parameterValues = request.getParameterValues(requestParameterName);
-                if (parameterValues != null) {
-                    field.set(controller, parameterValues);
-                }
-            } else {
-                throw new Exception("The annotation " + RequestParameter.class.getName()
-                        + " can only be set on fields of type " + String.class.getName()
+ ", "
-                        + String[].class.getName() + ", " + int.class.getName() + " or "
+ boolean.class.getName()
-                        + ". (field=" + field.getName() + ", type=" + fieldType.getName()
+ ")");
+        String requestParameterName = field.getAnnotation(RequestParameter.class).value();
+        if (isBlank(requestParameterName)) {
+            requestParameterName = field.getName();
+        }
+
+        Class<?> fieldType = field.getType();
+        if (fieldType == String.class) {
+            String parameter = request.getParameter(requestParameterName);
+            if (parameter != null) {
+                field.set(controller, parameter);
+            }
+        } else if (fieldType == int.class) {
+            String parameter = request.getParameter(requestParameterName);
+            if (parameter != null) {
+                field.set(controller, Integer.parseInt(parameter));
+            }
+        } else if (fieldType == boolean.class) {
+            String parameter = request.getParameter(requestParameterName);
+            if (parameter != null) {
+                field.set(controller, Boolean.parseBoolean(parameter));
             }
+        } else if (fieldType == String[].class) {
+            String[] parameterValues = request.getParameterValues(requestParameterName);
+            if (parameterValues != null) {
+                field.set(controller, parameterValues);
+            }
+        } else {
+            throwIllegalFieldAnnotationException(RequestParameter.class, field, String.class,
int.class,
+                    boolean.class, String[].class);
         }
     }
 
     private static void populateRequestHeaderFields(Map<String, ? extends Object> parameters,
Object controller,
-            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, Exception {
+            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException {
         List<Field> requestHeaderFields = annotatedFields.get(RequestHeader.class);
         if (requestHeaderFields == null || requestHeaderFields.isEmpty()) {
             return;
         }
 
-        HttpServletRequest request = HttpContextHelper.getRequest(parameters);
         for (Field field : requestHeaderFields) {
-            field.setAccessible(true);
+            populateRequestHeaderField(parameters, controller, field);
+        }
+    }
 
-            String name = field.getAnnotation(RequestHeader.class).value();
-            if (isBlank(name)) {
-                name = field.getName();
-            }
+    private static void populateRequestHeaderField(Map<String, ? extends Object> parameters,
Object controller,
+            Field field) throws IllegalAccessException {
+        field.setAccessible(true);
+
+        HttpServletRequest request = HttpContextHelper.getRequest(parameters);
+
+        String name = field.getAnnotation(RequestHeader.class).value();
+        if (isBlank(name)) {
+            name = field.getName();
+        }
 
-            Class<?> fieldType = field.getType();
-            if (fieldType == String.class) {
-                String header = request.getHeader(name);
-                if (header != null) {
-                    field.set(controller, header);
-                }
-            } else {
-                throw new Exception("The annotation " + RequestHeader.class.getName()
-                        + " can only be set on fields of type " + String.class.getName()
+ "." + " " + "(field="
-                        + field.getName() + ", type=" + fieldType.getName() + ")");
+        Class<?> fieldType = field.getType();
+        if (fieldType == String.class) {
+            String header = request.getHeader(name);
+            if (header != null) {
+                field.set(controller, header);
             }
+        } else {
+            throwIllegalFieldAnnotationException(RequestHeader.class, field, String.class);
         }
     }
 
-    private static void populateSitemapParameters(Map<String, ? extends Object> configuration,
Object controller,
-            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException, Exception {
+    private static void populateSitemapParameterFields(Map<String, ? extends Object>
configuration, Object controller,
+            Map<Class<? extends Annotation>, List<Field>> annotatedFields)
throws IllegalAccessException {
         List<Field> sitemapParameterFields = annotatedFields.get(SitemapParameter.class);
         if (sitemapParameterFields == null || sitemapParameterFields.isEmpty()) {
             return;
         }
 
         for (Field field : sitemapParameterFields) {
-            field.setAccessible(true);
+            populateSitemapParameterField(configuration, controller, field);
+        }
+    }
 
-            String name = field.getAnnotation(SitemapParameter.class).value();
-            if (isBlank(name)) {
-                name = field.getName();
-            }
+    private static void populateSitemapParameterField(Map<String, ? extends Object>
configuration, Object controller,
+            Field field) throws IllegalAccessException {
+        field.setAccessible(true);
 
-            Class<?> fieldType = field.getType();
-            if (fieldType == String.class) {
-                field.set(controller, configuration.get(name));
-            } else {
-                throw new Exception("The annotation " + SitemapParameter.class.getName()
-                        + " can only be set on fields of type " + String.class.getName()
+ "." + " " + "(field="
-                        + field.getName() + ", type=" + fieldType.getName() + ")");
+        String name = field.getAnnotation(SitemapParameter.class).value();
+        if (isBlank(name)) {
+            name = field.getName();
+        }
+
+        Class<?> fieldType = field.getType();
+        if (fieldType == String.class) {
+            field.set(controller, configuration.get(name));
+        } else {
+            throwIllegalFieldAnnotationException(SitemapParameter.class, field, String.class);
+        }
+    }
+
+    private static void throwIllegalFieldAnnotationException(Class<? extends Annotation>
annotationClass,
+            Field field, Class<?>... supportedTypes) {
+        StringBuilder stringBuilder = new StringBuilder();
+
+        stringBuilder.append("The annotation @");
+        stringBuilder.append(annotationClass.getSimpleName());
+        stringBuilder.append(" can only be set on fields of type ");
+
+        for (int i = 0; i < supportedTypes.length; i++) {
+            stringBuilder.append(supportedTypes[i].getSimpleName());
+
+            if (i < supportedTypes.length - 2) {
+                stringBuilder.append(", ");
+            } else if (i == supportedTypes.length - 2) {
+                stringBuilder.append(" or ");
             }
         }
+        stringBuilder.append(". (The field was '");
+        stringBuilder.append(Modifier.toString(field.getModifiers()));
+        stringBuilder.append(" ");
+        stringBuilder.append(field.getType().getName());
+        stringBuilder.append(" ");
+        stringBuilder.append(field.getName());
+        stringBuilder.append("')");
+
+        throw new IllegalFieldAnnotationException(stringBuilder.toString());
     }
 }

Added: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java?rev=1156358&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
Wed Aug 10 20:38:56 2011
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.rest.controller;
+
+import org.apache.cocoon.pipeline.SetupException;
+
+public class UnknownControllerException extends SetupException {
+
+    private static final long serialVersionUID = 1L;
+
+    public UnknownControllerException(String message) {
+        super(message);
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/UnknownControllerException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/SpringRESTControllerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/SpringRESTControllerTest.java?rev=1156358&r1=1156357&r2=1156358&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/SpringRESTControllerTest.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/test/java/org/apache/cocoon/rest/controller/SpringRESTControllerTest.java
Wed Aug 10 20:38:56 2011
@@ -34,10 +34,11 @@ import org.apache.cocoon.configuration.M
 import org.apache.cocoon.rest.controller.response.Status;
 import org.apache.cocoon.servlet.util.HttpContextHelper;
 import org.apache.cocoon.servlet.util.SettingsHelper;
+import org.apache.cocoon.sitemap.InvocationException;
+import org.apache.cocoon.sitemap.util.ExceptionHandler;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.mock.web.MockHttpServletResponse;
@@ -46,13 +47,13 @@ import org.springframework.test.context.
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 @RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = { "/META-INF/cocoon/spring/cocoon-rest.xml", "/cocoon-rest-test-controller.xml"
})
+@ContextConfiguration(locations = {"/META-INF/cocoon/spring/cocoon-rest.xml", "/cocoon-rest-test-controller.xml"})
 public class SpringRESTControllerTest {
 
     @Autowired
     private SpringRESTController springRESTController;
 
-    @Test(expected = NoSuchBeanDefinitionException.class)
+    @Test(expected = UnknownControllerException.class)
     public void requestNonExistingController() {
         Map<String, Object> inputParameters = Collections.emptyMap();
         Map<String, ? extends Object> configuration = Collections.emptyMap();
@@ -89,54 +90,85 @@ public class SpringRESTControllerTest {
         }
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void requestControllerWithIllegalInjectField() {
         Map<String, Object> inputParameters = new HashMap<String, Object>();
         HttpContextHelper.storeRequest(new MockHttpServletRequest(), inputParameters);
         HttpContextHelper.storeResponse(new MockHttpServletResponse(), inputParameters);
 
         Map<String, ? extends Object> configuration = Collections.emptyMap();
-        this.springRESTController.setup("illegal-inject-field-controller", inputParameters,
configuration);
+
+        try {
+            this.springRESTController.setup("illegal-inject-field-controller", inputParameters,
configuration);
+        } catch (InvocationException e) {
+            assertCauseIsIllegalFieldAnnotation(e);
+        }
+    }
+
+    private static void assertCauseIsIllegalFieldAnnotation(InvocationException e) {
+        Throwable cause = ExceptionHandler.getCause(e);
+        assertTrue(cause instanceof IllegalFieldAnnotationException);
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void requestControllerWithIllegalRequestField() {
         Map<String, Object> inputParameters = new HashMap<String, Object>();
         HttpContextHelper.storeRequest(new MockHttpServletRequest(), inputParameters);
         HttpContextHelper.storeResponse(new MockHttpServletResponse(), inputParameters);
 
         Map<String, ? extends Object> configuration = Collections.emptyMap();
-        this.springRESTController.setup("illegal-request-field-controller", inputParameters,
configuration);
+
+        try {
+            this.springRESTController.setup("illegal-request-field-controller", inputParameters,
configuration);
+        } catch (InvocationException e) {
+            assertCauseIsIllegalFieldAnnotation(e);
+        }
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void requestControllerWithIllegalRequestHeaderField() {
         Map<String, Object> inputParameters = new HashMap<String, Object>();
         HttpContextHelper.storeRequest(new MockHttpServletRequest(), inputParameters);
         HttpContextHelper.storeResponse(new MockHttpServletResponse(), inputParameters);
 
         Map<String, ? extends Object> configuration = Collections.emptyMap();
-        this.springRESTController.setup("illegal-request-header-field-controller", inputParameters,
configuration);
+
+        try {
+            this.springRESTController.setup("illegal-request-header-field-controller", inputParameters,
configuration);
+        } catch (InvocationException e) {
+            assertCauseIsIllegalFieldAnnotation(e);
+        }
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void requestControllerWithIllegalSitemapParameterField() {
         Map<String, Object> inputParameters = new HashMap<String, Object>();
         HttpContextHelper.storeRequest(new MockHttpServletRequest(), inputParameters);
         HttpContextHelper.storeResponse(new MockHttpServletResponse(), inputParameters);
 
         Map<String, ? extends Object> configuration = Collections.emptyMap();
-        this.springRESTController.setup("illegal-sitemap-parameter-field-controller", inputParameters,
configuration);
+
+        try {
+            this.springRESTController.setup("illegal-sitemap-parameter-field-controller",
inputParameters,
+                    configuration);
+        } catch (InvocationException e) {
+            assertCauseIsIllegalFieldAnnotation(e);
+        }
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void requestControllerWithIllegalBaseURLField() {
         Map<String, Object> inputParameters = new HashMap<String, Object>();
         HttpContextHelper.storeRequest(new MockHttpServletRequest(), inputParameters);
         HttpContextHelper.storeResponse(new MockHttpServletResponse(), inputParameters);
 
         Map<String, ? extends Object> configuration = Collections.emptyMap();
-        this.springRESTController.setup("illegal-base-url-field-controller", inputParameters,
configuration);
+
+        try {
+            this.springRESTController.setup("illegal-base-url-field-controller", inputParameters,
configuration);
+        } catch (InvocationException e) {
+            assertCauseIsIllegalFieldAnnotation(e);
+        }
     }
 
     @Test
@@ -158,8 +190,8 @@ public class SpringRESTControllerTest {
         httpServletRequest.setParameter("alias-request-parameter", String.valueOf(Math.random()));
         httpServletRequest.setParameter("intRequestParameter", String.valueOf((int) Math.random()
* 100));
         httpServletRequest.setParameter("booleanRequestParameter", String.valueOf(Math.random()
< 0.5));
-        httpServletRequest.setParameter("stringArrayRequestParameter", new String[] { String.valueOf(Math.random()),
-                String.valueOf(Math.random()) });
+        httpServletRequest.setParameter("stringArrayRequestParameter", new String[] {String.valueOf(Math.random()),
+                String.valueOf(Math.random())});
 
         httpServletRequest.addHeader("stringRequestHeader", String.valueOf(Math.random()));
         httpServletRequest.addHeader("alias-request-header", String.valueOf(Math.random()));



Mime
View raw message