You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2008/01/28 20:28:48 UTC

svn commit: r616010 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/annotations/ main/java/org/apache/tapestry/internal/services/ main/java/org/apache/tapestry/services/ test/java/org/apache/tapestry/internal/services/

Author: hlship
Date: Mon Jan 28 11:28:44 2008
New Revision: 616010

URL: http://svn.apache.org/viewvc?rev=616010&view=rev
Log:
TAPESTRY-2002: Add annotations to easily set content type and response encoding

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ContentType.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ResponseEncoding.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ContentTypeWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ResponseEncodingWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ContentTypeWorkerTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ResponseEncodingWorkerTest.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ContentType.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ContentType.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ContentType.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ContentType.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,36 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.annotations;
+
+import java.lang.annotation.*;
+
+
+/**
+ * An annotation on a page component used to identify the content type the page returns. An alternative to the {@link
+ * org.apache.tapestry.annotations.Meta} annotation with the {@link org.apache.tapestry.TapestryConstants#RESPONSE_CONTENT_TYPE}
+ * key.
+ *
+ * @see org.apache.tapestry.annotations.ResponseEncoding
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface ContentType
+{
+    /**
+     * The content type for the page.  Typically, something like "text/html".
+     */
+    String value();
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ResponseEncoding.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ResponseEncoding.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ResponseEncoding.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/ResponseEncoding.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,36 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.annotations;
+
+import java.lang.annotation.*;
+
+
+/**
+ * An annotation on a page component used to identify the respones encoding (the character set of the text sent in the
+ * response). An alternative to the {@link org.apache.tapestry.annotations.Meta} annotation with the {@link
+ * org.apache.tapestry.TapestryConstants#RESPONSE_ENCODING} key.
+ *
+ * @see org.apache.tapestry.annotations.ContentType
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface ResponseEncoding
+{
+    /**
+     * The response encoding, a value such as "utf-8".
+     */
+    String value();
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ContentTypeWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ContentTypeWorker.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ContentTypeWorker.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ContentTypeWorker.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,35 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.services;
+
+import org.apache.tapestry.TapestryConstants;
+import org.apache.tapestry.annotations.ContentType;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.services.ComponentClassTransformWorker;
+
+/**
+ * Checks for the {@link org.apache.tapestry.annotations.ContentType} annotation, adding a corresponding meta-data value
+ * when found.
+ */
+public class ContentTypeWorker implements ComponentClassTransformWorker
+{
+    public void transform(ClassTransformation transformation, MutableComponentModel model)
+    {
+        ContentType annotation = transformation.getAnnotation(ContentType.class);
+
+        if (annotation != null) model.setMeta(TapestryConstants.RESPONSE_CONTENT_TYPE, annotation.value());
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ResponseEncodingWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ResponseEncodingWorker.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ResponseEncodingWorker.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ResponseEncodingWorker.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,33 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.services;
+
+import org.apache.tapestry.TapestryConstants;
+import org.apache.tapestry.annotations.ResponseEncoding;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.services.ComponentClassTransformWorker;
+
+public class ResponseEncodingWorker implements ComponentClassTransformWorker
+{
+    public void transform(ClassTransformation transformation, MutableComponentModel model)
+    {
+        ResponseEncoding annotation = transformation.getAnnotation(ResponseEncoding.class);
+
+        if (annotation == null) return;
+
+        model.setMeta(TapestryConstants.RESPONSE_ENCODING, annotation.value());
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java?rev=616010&r1=616009&r2=616010&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java Mon Jan 28 11:28:44 2008
@@ -195,24 +195,27 @@
     }
 
     /**
-     * Adds a number of standard component class transform workers: <ul> <li>Retain -- allows fields to retain their
-     * values between requests</li> <li>Persist -- allows fields to store their their value persistently between
-     * requests</li> <li>Parameter -- identifies parameters based on the {@link org.apache.tapestry.annotations.Parameter}
-     * annotation</li> <li>Component -- identifies embedded components based on the {@link
-     * org.apache.tapestry.annotations.Component} annotation</li> <li>Mixin -- adds a mixin as part of a component's
-     * implementation</li> <li>Environment -- allows fields to contain values extracted from the {@link Environment}
-     * service</li> <li>Inject -- used with the {@link Inject} annotation, when a value is supplied</li> <li>InjectPage
-     * -- adds code to allow access to other pages via the {@link InjectPage} field annotation</li> <li>InjectBlock --
-     * allows a block from the template to be injected into a field</li> <li>IncludeStylesheet -- supports the {@link
-     * org.apache.tapestry.annotations.IncludeStylesheet} annotation</li> <li>IncludeJavaScriptLibrary -- supports the
-     * {@link org.apache.tapestry.annotations.IncludeJavaScriptLibrary} annotation</li> <li>SupportsInformalParameters
-     * -- checks for the annotation</li> <li>Meta -- checks for meta data and adds it to the component model
-     * <li>ApplicationState -- converts fields that reference application state objects <li>UnclaimedField -- identifies
-     * unclaimed fields and resets them to null/0/false at the end of the request</li> <li>RenderCommand -- ensures all
-     * components also implement {@link RenderCommand}</li> <li>SetupRender, BeginRender, etc. -- correspond to
-     * component render phases and annotations</li> <li>InvokePostRenderCleanupOnResources -- makes sure {@link
+     * Adds a number of standard component class transform workers: <dl> <dt>Retain </dt> <dd> allows fields to retain
+     * their values between requests</dd> <dt>Persist </dt> <dd> allows fields to store their their value persistently
+     * between requests</dd> <dt>Parameter </dt> <dd> identifies parameters based on the {@link
+     * org.apache.tapestry.annotations.Parameter} annotation</dd> <dt>Component </dt> <dd> identifies embedded
+     * components based on the {@link org.apache.tapestry.annotations.Component} annotation</dd> <dt>Mixin </dt> <dd>
+     * adds a mixin as part of a component's implementation</dd> <dt>Environment </dt> <dd> allows fields to contain
+     * values extracted from the {@link Environment} service</dd> <dt>Inject </dt> <dd> used with the {@link Inject}
+     * annotation, when a value is supplied</dd> <dt>InjectPage </dt> <dd> adds code to allow access to other pages via
+     * the {@link InjectPage} field annotation</dd> <dt>InjectBlock </dt> <dd> allows a block from the template to be
+     * injected into a field</dd> <dt>IncludeStylesheet </dt> <dd> supports the {@link
+     * org.apache.tapestry.annotations.IncludeStylesheet} annotation</dd> <dt>IncludeJavaScriptLibrary </dt> <dd>
+     * supports the {@link org.apache.tapestry.annotations.IncludeJavaScriptLibrary} annotation</dd>
+     * <dt>SupportsInformalParameters </dt> <dd> checks for the annotation</dd> <dt>Meta </dt> <dd> checks for meta data
+     * and adds it to the component model <dt>ApplicationState </dt> <dd> converts fields that reference application
+     * state objects <dt>UnclaimedField </dt> <dd> identifies unclaimed fields and resets them to null/0/false at the
+     * end of the request</dd> <dt>RenderCommand </dt> <dd> ensures all components also implement {@link
+     * RenderCommand}</dd> <dt>SetupRender, BeginRender, etc. </dt> <dd> correspond to component render phases and
+     * annotations</dd> <dt>InvokePostRenderCleanupOnResources </dt> <dd> makes sure {@link
      * org.apache.tapestry.internal.InternalComponentResources#postRenderCleanup()} is invoked after a component
-     * finishes rendering</li> </ul>
+     * finishes rendering</dd> <dt>ContentType</dt> <dd>Checks for {@link org.apache.tapestry.annotations.ContentType}
+     * annotation</dd> <dt>ResponseEncoding</dt> <dd>Checks for the {@link ResponseEncoding} annotation</dd> </dl>
      */
     public static void contributeComponentClassTransformWorker(
             OrderedConfiguration<ComponentClassTransformWorker> configuration,
@@ -284,6 +287,9 @@
                           "after:SetupRender");
 
         configuration.add("InvokePostRenderCleanupOnResources", new InvokePostRenderCleanupOnResourcesWorker());
+
+        configuration.add("ContentType", new ContentTypeWorker());
+        configuration.add("ResponseEncoding", new ResponseEncodingWorker());
 
         // This one is always last. Any additional private fields that aren't annotated will
         // be converted to clear out at the end of the request.

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ContentTypeWorkerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ContentTypeWorkerTest.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ContentTypeWorkerTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ContentTypeWorkerTest.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,63 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.services;
+
+import org.apache.tapestry.TapestryConstants;
+import org.apache.tapestry.annotations.ContentType;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.test.TapestryTestCase;
+import org.testng.annotations.Test;
+
+public class ContentTypeWorkerTest extends TapestryTestCase
+{
+
+    @Test
+    public void annotation_missing()
+    {
+        ClassTransformation ct = mockClassTransformation();
+        MutableComponentModel model = mockMutableComponentModel();
+
+        train_getAnnotation(ct, ContentType.class, null);
+
+        replay();
+
+        new ContentTypeWorker().transform(ct, model);
+
+        verify();
+    }
+
+    @Test
+    public void annotation_present()
+    {
+        ClassTransformation ct = mockClassTransformation();
+        MutableComponentModel model = mockMutableComponentModel();
+        String value = "text/pdf";
+
+        ContentType annotation = newMock(ContentType.class);
+
+        train_getAnnotation(ct, ContentType.class, annotation);
+
+        expect(annotation.value()).andReturn(value);
+
+        model.setMeta(TapestryConstants.RESPONSE_CONTENT_TYPE, value);
+
+        replay();
+
+        new ContentTypeWorker().transform(ct, model);
+
+        verify();
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ResponseEncodingWorkerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ResponseEncodingWorkerTest.java?rev=616010&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ResponseEncodingWorkerTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ResponseEncodingWorkerTest.java Mon Jan 28 11:28:44 2008
@@ -0,0 +1,62 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.services;
+
+import org.apache.tapestry.TapestryConstants;
+import org.apache.tapestry.annotations.ResponseEncoding;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.services.ClassTransformation;
+import org.apache.tapestry.test.TapestryTestCase;
+import org.testng.annotations.Test;
+
+public class ResponseEncodingWorkerTest extends TapestryTestCase
+{
+    @Test
+    public void annotation_missing()
+    {
+        ClassTransformation ct = mockClassTransformation();
+        MutableComponentModel model = mockMutableComponentModel();
+
+        train_getAnnotation(ct, ResponseEncoding.class, null);
+
+        replay();
+
+        new ResponseEncodingWorker().transform(ct, model);
+
+        verify();
+    }
+
+    @Test
+    public void annotation_present()
+    {
+        ClassTransformation ct = mockClassTransformation();
+        MutableComponentModel model = mockMutableComponentModel();
+        String value = "UTF-8";
+
+        ResponseEncoding annotation = newMock(ResponseEncoding.class);
+
+        train_getAnnotation(ct, ResponseEncoding.class, annotation);
+
+        expect(annotation.value()).andReturn(value);
+
+        model.setMeta(TapestryConstants.RESPONSE_ENCODING, value);
+
+        replay();
+
+        new ResponseEncodingWorker().transform(ct, model);
+
+        verify();
+    }
+}