click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r996232 - in /click/trunk/click/examples: src/org/apache/click/examples/page/ajax/ webapp/WEB-INF/ webapp/ajax/
Date Sun, 12 Sep 2010 01:28:16 GMT
Author: sabob
Date: Sun Sep 12 01:28:15 2010
New Revision: 996232

URL: http://svn.apache.org/viewvc?rev=996232&view=rev
Log:
added Ajax demos showing how to redirect and implement an onSecurityCheck implementation

Added:
    click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxRedirectPage.java
    click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSecurePage.java
    click/trunk/click/examples/webapp/ajax/ajax-redirect.htm
    click/trunk/click/examples/webapp/ajax/ajax-secure.htm
Modified:
    click/trunk/click/examples/webapp/WEB-INF/menu.xml

Added: click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxRedirectPage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxRedirectPage.java?rev=996232&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxRedirectPage.java
(added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxRedirectPage.java
Sun Sep 12 01:28:15 2010
@@ -0,0 +1,64 @@
+/*
+ * 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.click.examples.page.ajax;
+
+import java.util.Date;
+import org.apache.click.ActionResult;
+import org.apache.click.Context;
+import org.apache.click.Control;
+import org.apache.click.ajax.AjaxBehavior;
+import org.apache.click.control.ActionLink;
+import org.apache.click.examples.page.BorderPage;
+
+/**
+ * Provides an <tt>onSecurityCheck</tt> example secure Page for handling Ajax
+ * requests. Two links are presented to the user. Clicking on the first link will
+ * redirect the user to this page and show an error message. Clicking on the
+ * second link will show an error message without redirecting to another page.
+ */
+public class AjaxRedirectPage extends BorderPage {
+
+    private static final long serialVersionUID = 1L;
+    private ActionLink redirectLink = new ActionLink("redirectLink");
+
+    @Override
+    public void onInit() {
+        super.onInit();
+        redirectLink.setId("redirectLinkId");
+        addControl(redirectLink);
+
+        redirectLink.addBehavior(new AjaxBehavior() {
+
+            @Override
+            public ActionResult onAction(Control source) {
+                Context context = getContext();
+                context.setFlashAttribute("flash", "Redirected at " + new Date());
+
+            String redirectUrl = context.getPagePath(AjaxRedirectPage.class);
+            String contextPath = context.getRequest().getContextPath();
+            redirectUrl = contextPath + redirectUrl;
+
+            context.getResponse().setHeader("REDIRECT_URL",redirectUrl);
+
+            // We are redirecting so no response is returned
+            return null;
+            }
+        });
+    }
+}

Added: click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSecurePage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSecurePage.java?rev=996232&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSecurePage.java
(added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSecurePage.java
Sun Sep 12 01:28:15 2010
@@ -0,0 +1,85 @@
+/*
+ * 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.click.examples.page.ajax;
+
+import org.apache.click.ActionResult;
+import org.apache.click.Context;
+import org.apache.click.Page;
+import org.apache.click.control.ActionLink;
+import org.apache.click.examples.page.BorderPage;
+import org.apache.click.util.ClickUtils;
+
+/**
+ * Provides an <tt>onSecurityCheck</tt> example secure Page for handling Ajax
+ * requests. Two links are presented to the user. Clicking on the first link will
+ * redirect the user to this page and show an error message. Clicking on the
+ * second link will show an error message without redirecting to another page.
+ */
+public class AjaxSecurePage extends BorderPage {
+
+    private static final long serialVersionUID = 1L;
+
+    private ActionLink secureLinkWithRedirect = new ActionLink("secureLinkWithRedirect");
+    private ActionLink secureLinkWithMessage = new ActionLink("secureLinkWithMessage");
+
+    /**
+     * @see Page#onSecurityCheck()
+     */
+    @Override
+    public boolean onSecurityCheck() {
+        // As onSecurityCheck event occurs bfore controls are processed, we use
+        // explicit binding to determine which link was clicked
+
+        ClickUtils.bind(secureLinkWithRedirect);
+        ClickUtils.bind(secureLinkWithMessage);
+
+        boolean performRedirect = secureLinkWithRedirect.isClicked();
+        boolean performShowMessage = secureLinkWithMessage.isClicked();
+
+        Context context = getContext();
+        if (performRedirect) {
+            context.setFlashAttribute("flash", "You have been <b>Redirected</b>
"
+                + "since you don't have permission to access the link.");
+
+            String redirectUrl = context.getPagePath(AjaxSecurePage.class);
+            String contextPath = context.getRequest().getContextPath();
+            redirectUrl = contextPath + redirectUrl;
+
+            context.getResponse().setHeader("REDIRECT_URL",redirectUrl);
+            return false;
+
+        } else if (performShowMessage) {
+            ActionResult result = new ActionResult("You do not have permission "
+                + "to access the link.");
+            result.render(context);
+            return false;
+        }
+
+            return true;
+    }
+
+    @Override
+    public void onInit() {
+        super.onInit();
+        secureLinkWithRedirect.setId("secureLinkWithRedirectId");
+        secureLinkWithMessage.setId("secureLinkWithMessageId");
+        addControl(secureLinkWithRedirect);
+        addControl(secureLinkWithMessage);
+    }
+}

Modified: click/trunk/click/examples/webapp/WEB-INF/menu.xml
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/WEB-INF/menu.xml?rev=996232&r1=996231&r2=996232&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/WEB-INF/menu.xml (original)
+++ click/trunk/click/examples/webapp/WEB-INF/menu.xml Sun Sep 12 01:28:15 2010
@@ -161,6 +161,9 @@
     <menu label="Auto Complete Field" path="ajax/auto-complete.htm"/>
     <menu label="Live Scroller " path="ajax/scroller/ajax-live-scroller.htm"/>
     <menu label="Select" path="ajax/select/ajax-select.htm"/>
+    <menu separator="true"/>
+    <menu label="Ajax Redirect" path="ajax/ajax-redirect.htm"/>
+    <menu label="Ajax Security" path="ajax/ajax-secure.htm"/>
   </menu>
 
   <menu label=" Integration" path="#" imageSrc="/assets/images/cayenne.png">

Added: click/trunk/click/examples/webapp/ajax/ajax-redirect.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/ajax/ajax-redirect.htm?rev=996232&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/ajax/ajax-redirect.htm (added)
+++ click/trunk/click/examples/webapp/ajax/ajax-redirect.htm Sun Sep 12 01:28:15 2010
@@ -0,0 +1,77 @@
+<!--
+#* 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.*#
+-->
+
+$redirectLink
+
+<div id="result" class="infoMsg">
+  #set ($flash = "")
+  #set ($flash = $session.flash)
+  #if ($flash != "")
+    $flash
+  #end
+</div>
+
+Clicking the link above will send an Ajax request to the server which returns
+a url to redirect to. The url is returned from the server in a custom response
+header called <tt>'REDIRECT_URL'</tt>.
+<p/>
+Since Ajax does not support redirects, JavaScript is used to navigate the user to
+the given url by setting the <tt>window.location</tt> value to the url.
+
+<script type="text/javascript" src="$context/assets/js/jquery-1.4.2.js"></script>
+
+<script type="text/javascript">
+    // This example uses jQuery for making Ajax requests:
+    // http://api.jquery.com/jQuery.get/
+    // http://api.jquery.com/jQuery.ajax/
+
+    // Register a function that is invoked as soon as the DOM is loaded
+    jQuery(document).ready(function() {
+        var resultElm = jQuery("#result")
+
+        // Hide the result div initially if it contains no content
+        if (jQuery.trim(resultElm.text()).length == 0) {
+          resultElm.css("display", "none");
+        } else {
+            resultElm.css("display", "block");
+        }
+
+        // Register a 'click' handler that makes an Ajax request
+        jQuery("#redirectLinkId").click(function(event){
+            // Make ajax request
+            redirectLinkClicked();
+
+            // Prevent the default browser behavior of navigating to the link
+            return false;
+        })
+    })
+
+    function redirectLinkClicked() {
+        var link = jQuery('#redirectLinkId');
+        var extraData = link.attr('id') + '=1';
+        var url = link.attr('href');
+        jQuery.get(url, extraData, function(data, textStatus, xhr) {
+            // Retrieve the url to redirect from the Ajax response header
+            var redirect_url = xhr.getResponseHeader('REDIRECT_URL');
+            // Perform the redirect
+            window.location = redirect_url;
+
+        });
+    }
+</script>
\ No newline at end of file

Added: click/trunk/click/examples/webapp/ajax/ajax-secure.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/ajax/ajax-secure.htm?rev=996232&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/ajax/ajax-secure.htm (added)
+++ click/trunk/click/examples/webapp/ajax/ajax-secure.htm Sun Sep 12 01:28:15 2010
@@ -0,0 +1,104 @@
+<!--
+#* 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.*#
+-->
+
+<!-- Ajax response will be set here -->
+<div id="result" class="errorMsg">
+  #set ($flash = "")
+  #set ($flash = $session.flash)
+  #if ($flash != "")
+    $flash
+  #end
+</div>
+
+Clicking the link will perform a redirect and display a permission denied message: $secureLinkWithRedirect
+
+<p/>
+
+Clicking the link will show a permission denied message without doing a redirect: $secureLinkWithMessage
+
+
+<script type="text/javascript" src="$context/assets/js/jquery-1.4.2.js"></script>
+
+<script type="text/javascript">
+    // This example uses jQuery for making Ajax requests:
+    // http://api.jquery.com/jQuery.get/
+    // http://api.jquery.com/jQuery.ajax/
+
+    // Register a function that is invoked as soon as the DOM is loaded
+    jQuery(document).ready(function() {
+        var resultElm = jQuery("#result")
+
+        // Hide the result div initially if it contains no content
+        if (jQuery.trim(resultElm.text()).length == 0) {
+          resultElm.css("display", "none");
+        } else {
+            resultElm.css("display", "block");
+        }
+
+        // Register a 'click' handler that makes an Ajax request
+        jQuery("#secureLinkWithRedirectId").click(function(event){
+            // Make ajax request
+            redirectLinkClicked();
+
+            // Prevent the default browser behavior of navigating to the link
+            return false;
+        })
+
+ // Register a 'click' handler that makes an Ajax request
+        jQuery("#secureLinkWithMessageId").click(function(event){
+            // Make ajax request
+            messageLinkClicked();
+
+            // Prevent the default browser behavior of navigating to the link
+            return false;
+        })
+    })
+
+    function redirectLinkClicked() {
+        var link = jQuery('#secureLinkWithRedirectId');
+        var extraData = link.attr('id') + '=1';
+        var url = link.attr('href');
+        jQuery.get(url, extraData, function(data, textStatus, xhr) {
+            // Retrieve the url to redirect from the Ajax response header
+            var redirect_url = xhr.getResponseHeader('REDIRECT_URL');
+            // Perform the redirect
+            window.location = redirect_url;
+
+        });
+    }
+
+    function messageLinkClicked() {
+        var link = jQuery('#secureLinkWithMessageId');
+        var extraData = link.attr('id') + '=1';
+        var url = link.attr('href');
+
+        jQuery.get(url, extraData, function(data) {
+            // 'data' is the response received from the server
+
+            // We select the div element with the ID 'result' and set its
+            // content to the server response
+            var el = jQuery("#result").html(data);
+
+            // Make the element visible
+            el.css("display", "block");
+
+            
+        });
+    }
+</script>
\ No newline at end of file



Mime
View raw message