Author: drobiazko Date: Thu Jul 28 22:26:30 2011 New Revision: 1152022 URL: http://svn.apache.org/viewvc?rev=1152022&view=rev Log: TAP5-1594: Provide edit block for Kaptcha component
Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.tml tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/ tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java (with props) tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/resources/kaptcha/demo/pages/KaptchaBeanEditFormDemo.tml Modified: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/services/KaptchaModule.java tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/org/apache/tapestry5/kaptcha/integration/KaptchaIntegrationTest.java Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java Thu Jul 28 22:26:30 2011 @@ -0,0 +1,40 @@ +// Copyright 2011 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.kaptcha.annotations; + +import org.apache.tapestry5.ioc.annotations.UseWith; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static org.apache.tapestry5.ioc.annotations.AnnotationUseContext.*; + +/** + * Used to explicitly select the <i>kaptcha</i> editor block for a property. + * This annotation may be attached to a getter or setter method, or the matching field. + * + * @see org.apache.tapestry5.corelib.components.BeanEditForm + * @since 5.3 + */ +@Target({ElementType.FIELD, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@UseWith({BEAN, COMPONENT, MIXIN, PAGE}) +public @interface Kaptcha +{ +} Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/annotations/Kaptcha.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java Thu Jul 28 22:26:30 2011 @@ -0,0 +1,29 @@ +// Copyright 2011 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.kaptcha.internal.services; + +import org.apache.tapestry5.ioc.services.PropertyAdapter; +import org.apache.tapestry5.kaptcha.annotations.Kaptcha; +import org.apache.tapestry5.services.DataTypeAnalyzer; + +public class KaptchaDataTypeAnalyzer implements DataTypeAnalyzer +{ + public String identifyDataType(PropertyAdapter adapter) + { + final Kaptcha annotation = adapter.getAnnotation(Kaptcha.class); + + return annotation == null ? null : "kaptcha"; + } +} Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/internal/services/KaptchaDataTypeAnalyzer.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java Thu Jul 28 22:26:30 2011 @@ -0,0 +1,46 @@ +// Copyright 2011 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.kaptcha.pages; + +import org.apache.tapestry5.annotations.Component; +import org.apache.tapestry5.annotations.Environmental; +import org.apache.tapestry5.annotations.Property; +import org.apache.tapestry5.kaptcha.components.KaptchaField; +import org.apache.tapestry5.kaptcha.components.KaptchaImage; +import org.apache.tapestry5.services.PropertyEditContext; + +/** + * Page containing <i>kaptcha</i> edit blocks contributed into the + * {@link org.apache.tapestry5.services.BeanBlockSource} service configuration. + * + * @see org.apache.tapestry5.services.BeanBlockContribution + * @see org.apache.tapestry5.corelib.components.BeanEditForm + * @since 5.3 + */ +public class KaptchaEditBlocks +{ + @Environmental + @Property(write = false) + private PropertyEditContext context; + + @Component + private KaptchaImage kaptchaImage; + + @Component(parameters = { + "label=prop:context.label", + "clientId=prop:context.propertyId", + "image=kaptchaImage"}) + private KaptchaField kaptchaField; +} Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/services/KaptchaModule.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/services/KaptchaModule.java?rev=1152022&r1=1152021&r2=1152022&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/services/KaptchaModule.java (original) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/services/KaptchaModule.java Thu Jul 28 22:26:30 2011 @@ -14,19 +14,23 @@ package org.apache.tapestry5.kaptcha.services; +import org.apache.tapestry5.Asset; +import org.apache.tapestry5.MarkupWriter; import org.apache.tapestry5.ioc.Configuration; import org.apache.tapestry5.ioc.OrderedConfiguration; import org.apache.tapestry5.ioc.Resource; import org.apache.tapestry5.ioc.ServiceBinder; import org.apache.tapestry5.ioc.annotations.Contribute; import org.apache.tapestry5.ioc.annotations.Value; +import org.apache.tapestry5.ioc.services.ThreadLocale; +import org.apache.tapestry5.kaptcha.internal.services.KaptchaDataTypeAnalyzer; import org.apache.tapestry5.kaptcha.internal.services.KaptchaProducerImpl; -import org.apache.tapestry5.services.ComponentClassResolver; -import org.apache.tapestry5.services.LibraryMapping; +import org.apache.tapestry5.services.*; +import org.apache.tapestry5.services.javascript.JavaScriptSupport; import org.apache.tapestry5.services.messages.ComponentMessagesSource; /** - * Defines core services for Kaptcha support. + * Defines core services for Kaptcha support. * * @since 5.3 */ @@ -51,4 +55,45 @@ public class KaptchaModule { configuration.add("TapestryKaptcha", coreCatalog, "before:AppCatalog"); } + + public static void contributeDataTypeAnalyzer(OrderedConfiguration<DataTypeAnalyzer> configuration) + { + configuration.add("Kaptcha", new KaptchaDataTypeAnalyzer(), "after:Annotation"); + } + + @Contribute(BeanBlockSource.class) + public static void provideDefaultBeanBlocks(Configuration<BeanBlockContribution> configuration) + { + configuration.add(new EditBlockContribution("kaptcha", "KaptchaEditBlocks", "kaptcha")); + + } + + @Contribute(MarkupRenderer.class) + public void provideMarkupRenderer( + OrderedConfiguration<MarkupRendererFilter> configuration, + + final AssetSource assetSource, + + final ThreadLocale threadLocale, + + final Environment environment) + { + MarkupRendererFilter importKaptchaCss = new MarkupRendererFilter() + { + public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer) + { + JavaScriptSupport javaScriptSupport = environment.peek(JavaScriptSupport.class); + + Asset css = assetSource.getAsset(null, "org/apache/tapestry5/kaptcha/kaptcha.css", + threadLocale.getLocale()); + + javaScriptSupport.importStylesheet(css); + + renderer.renderMarkup(writer); + } + }; + + + configuration.add("KaptchaAssets", importKaptchaCss, "after:*"); + } } Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css Thu Jul 28 22:26:30 2011 @@ -0,0 +1,5 @@ +.t-kaptcha-field { + margin-left: 250px; + padding: 4px 0 0 3px +} + Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/kaptcha.css ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.tml?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.tml (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/main/resources/org/apache/tapestry5/kaptcha/pages/KaptchaEditBlocks.tml Thu Jul 28 22:26:30 2011 @@ -0,0 +1,14 @@ +<div xml:space="default" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"> + + <t:block id="kaptcha"> + <t:label for="kaptchaField"/> + + <img t:id="kaptchaImage"/> + + <div class="t-kaptcha-field"> + <input t:id="kaptchaField"/> + </div> + + </t:block> + +</div> \ No newline at end of file Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java Thu Jul 28 22:26:30 2011 @@ -0,0 +1,60 @@ +// Copyright 2011 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 kaptcha.demo.model; + +import org.apache.tapestry5.beaneditor.Validate; +import org.apache.tapestry5.kaptcha.annotations.Kaptcha; + +public class RegistrationData +{ + @Validate("required") + private String name; + + @Validate("required") + private String password; + + @Kaptcha + private Object kaptcha; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } + + public Object getKaptcha() + { + return kaptcha; + } + + public void setKaptcha(Object kaptcha) + { + this.kaptcha = kaptcha; + } +} Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/model/RegistrationData.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java Thu Jul 28 22:26:30 2011 @@ -0,0 +1,42 @@ +// Copyright 2011 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 kaptcha.demo.pages; + +import kaptcha.demo.model.RegistrationData; +import org.apache.tapestry5.PersistenceConstants; +import org.apache.tapestry5.annotations.Component; +import org.apache.tapestry5.annotations.Persist; +import org.apache.tapestry5.annotations.Property; +import org.apache.tapestry5.corelib.components.BeanEditForm; +import org.apache.tapestry5.kaptcha.annotations.Kaptcha; + +public class KaptchaBeanEditFormDemo +{ + @Component(parameters = {"object=registrationData", "clientValidation=none"}) + private BeanEditForm form; + + @Property + @Kaptcha + private RegistrationData registrationData; + + @Property + @Persist(PersistenceConstants.FLASH) + private String message; + + void onSuccessFromForm() + { + message = "Kaptcha passed."; + } +} Propchange: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/kaptcha/demo/pages/KaptchaBeanEditFormDemo.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/org/apache/tapestry5/kaptcha/integration/KaptchaIntegrationTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/org/apache/tapestry5/kaptcha/integration/KaptchaIntegrationTest.java?rev=1152022&r1=1152021&r2=1152022&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/org/apache/tapestry5/kaptcha/integration/KaptchaIntegrationTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/java/org/apache/tapestry5/kaptcha/integration/KaptchaIntegrationTest.java Thu Jul 28 22:26:30 2011 @@ -38,4 +38,26 @@ public class KaptchaIntegrationTest exte assertTextPresent("Kaptcha passed."); } + + + @Test + public void bean_editor() + { + open("/kaptchabeaneditformdemo"); + + type("kaptcha", "invalid input"); + + clickAndWait(SUBMIT); + + assertTextPresent("You must provide a value for Name", "You must provide a value for Password", "Enter the text displayed in the image."); + + type("name", "admin"); + type("password", "secret"); + type("kaptcha", "i8cookies"); + + clickAndWait(SUBMIT); + + assertTextPresent("Kaptcha passed."); + + } } Added: tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/resources/kaptcha/demo/pages/KaptchaBeanEditFormDemo.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/resources/kaptcha/demo/pages/KaptchaBeanEditFormDemo.tml?rev=1152022&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/resources/kaptcha/demo/pages/KaptchaBeanEditFormDemo.tml (added) +++ tapestry/tapestry5/trunk/tapestry-kaptcha/src/test/resources/kaptcha/demo/pages/KaptchaBeanEditFormDemo.tml Thu Jul 28 22:26:30 2011 @@ -0,0 +1,7 @@ +<t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" xmlns:tx="tapestry-library:tapx"> + + <p id="message">${message}</p> + + <form t:id="form"/> + +</t:layout> \ No newline at end of file