You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by ml...@apache.org on 2013/08/03 14:08:49 UTC

[02/10] git commit: TAP5-2070 annotation and service response handler to get access to page with exact activation context

TAP5-2070 annotation and service response handler to get access to page with exact activation context


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/8fecc8e1
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/8fecc8e1
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/8fecc8e1

Branch: refs/heads/master
Commit: 8fecc8e18e46919387dae2683ebc231f6beb1a74
Parents: 84d26fd
Author: Massimo Lusetti <ml...@apache.org>
Authored: Sat Aug 3 13:52:30 2013 +0200
Committer: Massimo Lusetti <ml...@apache.org>
Committed: Sat Aug 3 13:52:30 2013 +0200

----------------------------------------------------------------------
 .../UnknownActivationContextCheck.java          | 48 +++++++++++++++++
 .../UnknownActivationContextHandler.java        | 41 ++++++++++++++
 .../UnknownActivationContextHandlerImpl.java    | 57 ++++++++++++++++++++
 .../meta/UnknownActivationContextExtractor.java | 29 ++++++++++
 4 files changed, 175 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8fecc8e1/tapestry-core/src/main/java/org/apache/tapestry5/annotations/UnknownActivationContextCheck.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/annotations/UnknownActivationContextCheck.java b/tapestry-core/src/main/java/org/apache/tapestry5/annotations/UnknownActivationContextCheck.java
new file mode 100644
index 0000000..7752693
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/annotations/UnknownActivationContextCheck.java
@@ -0,0 +1,48 @@
+// Copyright 2008, 2009 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.tapestry5.annotations;
+
+import org.apache.tapestry5.ioc.annotations.AnnotationUseContext;
+import org.apache.tapestry5.ioc.annotations.UseWith;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * A marker annotation that indicates that the page in question may only be accessed with a exact actvation context.
+ *
+ * @see org.apache.tapestry5.MetaDataConstants#UNKNOWN_ACTIVATION_CONTEXT_CHECK
+ * @since 5.4
+ */
+@Target(TYPE)
+@Retention(RUNTIME)
+@Documented
+@UseWith(AnnotationUseContext.PAGE)
+public @interface UnknownActivationContextCheck
+{
+	/**
+	 * If <code>true</code>, the default, the framework will check for an exact (number and type of parameters)
+	 * activation context method and if not found will respond with a 404 Not Found status code, if <code>false</code>
+	 * the activation context is ignored as it was before 5.4 release.
+	 *
+	 * @see org.apache.tapestry5.MetaDataConstants#UNKNOWN_ACTIVATION_CONTEXT_CHECK
+	 * @see org.apache.tapestry5.services.HttpError
+	 */
+	boolean value() default true;
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8fecc8e1/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandler.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandler.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandler.java
new file mode 100644
index 0000000..1bc0d5b
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandler.java
@@ -0,0 +1,41 @@
+// Copyright 2030 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.tapestry5.internal.services;
+
+import org.apache.tapestry5.ComponentResources;
+import org.apache.tapestry5.EventContext;
+
+import java.io.IOException;
+
+/**
+ * Responsible for handling the response for a page called with an unknown activation context when the check itself is
+ * enabled.
+ * The default implementation is to respond with a 404 NOT FOUND.
+ *
+ * @see http://issues.apache.org/jira/browse/TAP5-2070
+ * @see org.apache.tapestry5.annotations.UnknownActivationContextCheck
+ * @see org.apache.tapestry5.services.HttpError
+ *
+ * @since 5.4
+ */
+public interface UnknownActivationContextHandler
+{
+    /**
+     * Answer the client in the case of a request coming in with an unknown activation context.
+     */
+    @SuppressWarnings("unchecked")
+	void handleUnknownContext(ComponentResources pageResources, EventContext activationContext)
+							throws IOException;
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8fecc8e1/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandlerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandlerImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandlerImpl.java
new file mode 100644
index 0000000..6b33595
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/UnknownActivationContextHandlerImpl.java
@@ -0,0 +1,57 @@
+// Copyright 2030 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.tapestry5.internal.services;
+
+import org.apache.tapestry5.ComponentResources;
+import org.apache.tapestry5.EventContext;
+import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.services.ComponentEventResultProcessor;
+import org.apache.tapestry5.services.HttpError;
+import org.apache.tapestry5.services.Traditional;
+import org.slf4j.Logger;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * Default implementation for {@link UnknownActivationContextHandler} which answer with a 404 NOT FOUND error.
+ */
+public class UnknownActivationContextHandlerImpl implements UnknownActivationContextHandler
+{
+	private final Logger logger;
+
+	private final ComponentEventResultProcessor resultProcessor;
+
+
+	public UnknownActivationContextHandlerImpl(Logger logger,
+											   @Traditional @Primary
+											   ComponentEventResultProcessor resultProcessor)
+	{
+		this.logger = logger;
+		this.resultProcessor = resultProcessor;
+	}
+
+	public void handleUnknownContext(ComponentResources pageResources, EventContext activationContext)
+			throws IOException
+	{
+		logger.warn("Activate event on page {} was fired with context {} but was not handled",
+				pageResources.getPage().getClass(), activationContext);
+
+		String message = String.format("Activation context %s unrecognized for page %s",
+				activationContext, pageResources.getPage().getClass());
+
+		resultProcessor.processResultValue(new HttpError(HttpServletResponse.SC_NOT_FOUND, message));
+	}
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8fecc8e1/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/meta/UnknownActivationContextExtractor.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/meta/UnknownActivationContextExtractor.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/meta/UnknownActivationContextExtractor.java
new file mode 100644
index 0000000..ee03175
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/meta/UnknownActivationContextExtractor.java
@@ -0,0 +1,29 @@
+// Copyright 2010 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.tapestry5.internal.services.meta;
+
+import org.apache.tapestry5.MetaDataConstants;
+import org.apache.tapestry5.annotations.ContentType;
+import org.apache.tapestry5.annotations.UnknownActivationContextCheck;
+import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.services.meta.MetaDataExtractor;
+
+public class UnknownActivationContextExtractor implements MetaDataExtractor<UnknownActivationContextCheck>
+{
+    public void extractMetaData(MutableComponentModel model, UnknownActivationContextCheck annotation)
+    {
+		model.setMeta(MetaDataConstants.UNKNOWN_ACTIVATION_CONTEXT_CHECK, Boolean.toString(annotation.value()));
+    }
+}