You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2009/01/02 23:20:22 UTC

svn commit: r730851 - in /wicket/trunk/wicket-examples/src: main/java/org/apache/wicket/examples/ main/java/org/apache/wicket/examples/helloworld/ main/java/org/apache/wicket/examples/source/ test/java/org/apache/wicket/filtertest/

Author: jdonnerstag
Date: Fri Jan  2 14:20:21 2009
New Revision: 730851

URL: http://svn.apache.org/viewvc?rev=730851&view=rev
Log:
applied wicket-1431: Make the Source Code pages for examples stateless

Added:
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleRequestCycle.java
Modified:
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleApplication.java
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleHeader.java
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/helloworld/HelloWorldApplication.java
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.html
    wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java
    wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/Application.java
    wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/HelloWorld.java

Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleApplication.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleApplication.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleApplication.java (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleApplication.java Fri Jan  2 14:20:21 2009
@@ -18,14 +18,18 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.wicket.Request;
+import org.apache.wicket.RequestCycle;
+import org.apache.wicket.Response;
 import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.WebRequest;
 import org.apache.wicket.settings.ISecuritySettings;
 import org.apache.wicket.util.crypt.ClassCryptFactory;
 import org.apache.wicket.util.crypt.NoCrypt;
 
 
 /**
- * Wicket Application class for hello world example.
+ * Wicket Example Application class.
  * 
  * @author Jonathan Locke
  */
@@ -46,6 +50,7 @@
 	/**
 	 * @see org.apache.wicket.protocol.http.WebApplication#init()
 	 */
+	@Override
 	protected void init()
 	{
 		// WARNING: DO NOT do this on a real world application unless
@@ -57,6 +62,17 @@
 		// and we want them to be able to run the examples out of the
 		// box.
 		getSecuritySettings().setCryptFactory(
-				new ClassCryptFactory(NoCrypt.class, ISecuritySettings.DEFAULT_ENCRYPTION_KEY));
+			new ClassCryptFactory(NoCrypt.class, ISecuritySettings.DEFAULT_ENCRYPTION_KEY));
+	}
+
+	/**
+	 * 
+	 * @see org.apache.wicket.protocol.http.WebApplication#newRequestCycle(org.apache.wicket.Request,
+	 *      org.apache.wicket.Response)
+	 */
+	@Override
+	public final RequestCycle newRequestCycle(Request request, Response response)
+	{
+		return new WicketExampleRequestCycle(this, (WebRequest)request, response);
 	}
 }

Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleHeader.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleHeader.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleHeader.java (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleHeader.java Fri Jan  2 14:20:21 2009
@@ -16,7 +16,6 @@
  */
 package org.apache.wicket.examples;
 
-import org.apache.wicket.Page;
 import org.apache.wicket.PageMap;
 import org.apache.wicket.RequestContext;
 import org.apache.wicket.examples.debug.InspectorBug;
@@ -24,7 +23,7 @@
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.basic.Label;
-import org.apache.wicket.markup.html.link.Link;
+import org.apache.wicket.markup.html.link.BookmarkablePageLink;
 import org.apache.wicket.markup.html.link.PopupSettings;
 import org.apache.wicket.markup.html.panel.Panel;
 
@@ -62,15 +61,8 @@
 		add(hideInPortlet);
 		hideInPortlet.add(new InspectorBug("inspector", page));
 		add(new Label("exampleTitle", exampleTitle));
-		Link link = new Link("sources")
-		{
-			@SuppressWarnings("unchecked")
-			@Override
-			public void onClick()
-			{
-				setResponsePage(new SourcesPage((Class<? extends Page>)getPage().getClass()));
-			}
-		};
+		BookmarkablePageLink<Void> link = new BookmarkablePageLink<Void>("sources",
+			SourcesPage.class, SourcesPage.generatePageParameters(page));
 		add(link);
 
 		PopupSettings settings = new PopupSettings(PageMap.forName("sources"),

Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleRequestCycle.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleRequestCycle.java?rev=730851&view=auto
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleRequestCycle.java (added)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/WicketExampleRequestCycle.java Fri Jan  2 14:20:21 2009
@@ -0,0 +1,96 @@
+/*
+ * 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.wicket.examples;
+
+import org.apache.wicket.Page;
+import org.apache.wicket.Request;
+import org.apache.wicket.Response;
+import org.apache.wicket.examples.source.SourcesPage;
+import org.apache.wicket.protocol.http.BufferedWebResponse;
+import org.apache.wicket.protocol.http.PageExpiredException;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.WebRequest;
+import org.apache.wicket.protocol.http.WebRequestCycle;
+import org.apache.wicket.protocol.http.servlet.AbortWithWebErrorCodeException;
+
+/**
+ * Handles the PageExpiredException so that the SourcesPage can recover from a session expired.
+ * 
+ * @author rgravener
+ */
+public class WicketExampleRequestCycle extends WebRequestCycle
+{
+	/**
+	 * Construct.
+	 * 
+	 * @param application
+	 * @param request
+	 * @param response
+	 */
+	public WicketExampleRequestCycle(WebApplication application, WebRequest request,
+		Response response)
+	{
+		super(application, request, response);
+	}
+
+	/**
+	 * @see org.apache.wicket.RequestCycle#onRuntimeException(org.apache.wicket.Page,
+	 *      java.lang.RuntimeException)
+	 */
+	@Override
+	public Page onRuntimeException(final Page page, final RuntimeException e)
+	{
+		final Throwable cause;
+		if (e.getCause() != null)
+		{
+			cause = e.getCause();
+		}
+		else
+		{
+			cause = e;
+		}
+
+		if (cause instanceof PageExpiredException)
+		{
+			handlePageExpiredException((PageExpiredException)cause);
+		}
+		return super.onRuntimeException(page, e);
+	}
+
+	/**
+	 * Checks to see if the request was ajax based. If so we send a 404 so that the
+	 * org.apache.wicket.ajax.IAjaxCallDecorator failure script is executed.
+	 * 
+	 * @param e
+	 */
+	private void handlePageExpiredException(final PageExpiredException e)
+	{
+		Response response = getOriginalResponse();
+		if (response instanceof BufferedWebResponse)
+		{
+			BufferedWebResponse bufferedWebResponse = (BufferedWebResponse)response;
+			Request request = getRequest();
+			if (bufferedWebResponse.isAjax() &&
+				request.getParameter(SourcesPage.PAGE_CLASS) != null)
+			{
+				// If there is a better way to figure out if SourcesPage was the request, we should
+				// do that.
+				throw new AbortWithWebErrorCodeException(404);
+			}
+		}
+	}
+}

Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/helloworld/HelloWorldApplication.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/helloworld/HelloWorldApplication.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/helloworld/HelloWorldApplication.java (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/helloworld/HelloWorldApplication.java Fri Jan  2 14:20:21 2009
@@ -17,12 +17,7 @@
 package org.apache.wicket.examples.helloworld;
 
 import org.apache.wicket.Page;
-import org.apache.wicket.Request;
-import org.apache.wicket.RequestCycle;
-import org.apache.wicket.Response;
 import org.apache.wicket.examples.WicketExampleApplication;
-import org.apache.wicket.protocol.http.WebRequest;
-import org.apache.wicket.protocol.http.WebRequestCycle;
 
 /**
  * Application class for hello world example.
@@ -36,37 +31,13 @@
 	 */
 	public HelloWorldApplication()
 	{
-
-	}
-
-	/**
-	 * @see org.apache.wicket.protocol.http.WebApplication#newRequestCycle(org.apache.wicket.Request,
-	 *      org.apache.wicket.Response)
-	 */
-	@Override
-	public RequestCycle newRequestCycle(Request request, Response response)
-	{
-		return new WebRequestCycle(this, (WebRequest)request, response)
-		{
-			@Override
-			protected void onBeginRequest()
-			{
-				// open session
-			}
-
-			@Override
-			protected void onEndRequest()
-			{
-				// close session
-			}
-		};
 	}
 
 	/**
 	 * @see org.apache.wicket.Application#getHomePage()
 	 */
 	@Override
-	public Class< ? extends Page> getHomePage()
+	public Class<? extends Page> getHomePage()
 	{
 		return HelloWorld.class;
 	}

Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.html?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.html (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.html Fri Jan  2 14:20:21 2009
@@ -166,7 +166,7 @@
 	<div id="header">
 		<div class="inner">
 			<div class="left">Files</div>
-			<span class="close">[<a href="#" wicket:id="close">Close</a>]</span>
+			<span class="close">[<a href="javascript:self.close()">Close</a>]</span>
 			<div class="right">Code for <span wicket:id="filename"></span></div>
 			
 		</div>

Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java Fri Jan  2 14:20:21 2009
@@ -33,28 +33,31 @@
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
-import com.uwyn.jhighlight.renderer.Renderer;
-import com.uwyn.jhighlight.renderer.XhtmlRendererFactory;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.wicket.Component;
 import org.apache.wicket.Page;
+import org.apache.wicket.PageParameters;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.IAjaxCallDecorator;
 import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
+import org.apache.wicket.authorization.UnauthorizedInstantiationException;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.basic.Label;
-import org.apache.wicket.markup.html.link.PopupCloseLink;
 import org.apache.wicket.markup.html.list.ListItem;
 import org.apache.wicket.markup.html.list.ListView;
 import org.apache.wicket.model.AbstractReadOnlyModel;
-import org.apache.wicket.model.PropertyModel;
+import org.apache.wicket.protocol.http.servlet.AbortWithWebErrorCodeException;
 import org.apache.wicket.util.io.IOUtils;
 import org.apache.wicket.util.lang.PackageName;
 import org.apache.wicket.util.string.AppendingStringBuffer;
 import org.apache.wicket.util.string.Strings;
 
+import com.uwyn.jhighlight.renderer.Renderer;
+import com.uwyn.jhighlight.renderer.XhtmlRendererFactory;
+
 
 /**
  * Displays the resources in a packages directory in a browsable format.
@@ -86,19 +89,21 @@
 		public String getObject()
 		{
 			// name contains the name of the selected file
-			if (Strings.isEmpty(name))
+			if (Strings.isEmpty(name) &&
+				Strings.isEmpty(getPage().getRequest().getParameter(SOURCE)))
 			{
 				return "";
 			}
 			BufferedReader br = null;
+			String source = null;
 			try
 			{
 				StringBuffer sb = new StringBuffer();
-
-				InputStream resourceAsStream = page.getResourceAsStream(name);
+				source = (name != null) ? name : getPage().getRequest().getParameter(SOURCE);
+				InputStream resourceAsStream = getPageTargetClass().getResourceAsStream(source);
 				if (resourceAsStream == null)
 				{
-					return "Unable to read the source for " + name;
+					return "Unable to read the source for " + source;
 				}
 				br = new BufferedReader(new InputStreamReader(resourceAsStream));
 
@@ -107,14 +112,14 @@
 					sb.append(br.readLine());
 					sb.append("\n");
 				}
-				int lastDot = name.lastIndexOf('.');
+				int lastDot = source.lastIndexOf('.');
 				if (lastDot != -1)
 				{
-					String type = name.substring(lastDot + 1);
+					String type = source.substring(lastDot + 1);
 					Renderer renderer = XhtmlRendererFactory.getRenderer(type);
 					if (renderer != null)
 					{
-						return renderer.highlight(name, sb.toString(), "UTF-8", true);
+						return renderer.highlight(source, sb.toString(), "UTF-8", true);
 					}
 				}
 				return Strings.escapeMarkup(sb.toString(), false, true).toString().replaceAll("\n",
@@ -122,7 +127,7 @@
 			}
 			catch (IOException e)
 			{
-				log.error("Unable to read resource stream for: " + name + "; Page=" +
+				log.error("Unable to read resource stream for: " + source + "; Page=" +
 					page.toString(), e);
 				return "";
 			}
@@ -165,7 +170,7 @@
 		{
 			if (resources.isEmpty())
 			{
-				get(page);
+				get(getPageTargetClass());
 // PackageName name = PackageName.forClass(page);
 // ClassLoader loader = page.getClassLoader();
 // String path = Strings.replaceAll(name.getName(), ".", "/").toString();
@@ -373,9 +378,10 @@
 			ListView<String> lv = new ListView<String>("file", new PackagedResourcesModel())
 			{
 				@Override
-				protected void populateItem(ListItem<String> item)
+				protected void populateItem(final ListItem<String> item)
 				{
-					AjaxFallbackLink<String> link = new AjaxFallbackLink<String>("link", item.getModel())
+					AjaxFallbackLink<String> link = new AjaxFallbackLink<String>("link",
+						item.getModel())
 					{
 						@Override
 						public void onClick(AjaxRequestTarget target)
@@ -388,6 +394,53 @@
 								target.addComponent(filename);
 							}
 						}
+
+						@Override
+						protected CharSequence getURL()
+						{
+							CharSequence url = urlFor(SourcesPage.class,
+								SourcesPage.generatePageParameters(getPageTargetClass(),
+									item.getModel().getObject()));
+							return url;
+						}
+
+						@Override
+						protected IAjaxCallDecorator getAjaxCallDecorator()
+						{
+							return new IAjaxCallDecorator()
+							{
+
+								public CharSequence decorateOnFailureScript(CharSequence script)
+								{
+									return "window.location=this.href;";
+									// return "alert('It\\'s ok!')";
+								}
+
+								public CharSequence decorateOnSuccessScript(CharSequence script)
+								{
+									if (script == null)
+									{
+										return "";
+									}
+									return script;
+								}
+
+								public CharSequence decorateScript(CharSequence script)
+								{
+									int index = script.toString().indexOf('?');
+									if (index >= 0)
+									{
+										String test = script.subSequence(0, index + 1) +
+											PAGE_CLASS + "=1" + "&" +
+											script.subSequence(index + 1, script.length());
+										return test;
+
+									}
+									return script;
+								}
+
+							};
+						}
 					};
 					link.add(new Label("name", item.getDefaultModelObjectAsString()));
 					item.add(link);
@@ -420,14 +473,21 @@
 	}
 
 	/**
-	 * The selected name of the packaged resource to display.
+	 * Parameter key for identifying the page class. UUID generated.
 	 */
-	private String name;
+	public static final String PAGE_CLASS = SourcesPage.class.getSimpleName() + "_class";
+
+	/**
+	 * Parameter key for identify the name of the source file in the package.
+	 */
+	public static final String SOURCE = "source";
 
 	/**
-	 * The class of the page of which the sources need to be displayed.
+	 * The selected name of the packaged resource to display.
 	 */
-	private final Class<? extends Page> page;
+	private String name;
+
+	private transient Class<? extends Page> page;
 
 	/**
 	 * The panel for setting the ajax calls.
@@ -462,27 +522,98 @@
 	 */
 	public SourcesPage()
 	{
-		this(SourcesPage.class);
+		this(new PageParameters(PAGE_CLASS + "=" + SourcesPage.class.getName()));
 	}
 
 	/**
-	 * Constructor.
 	 * 
-	 * @param <C>
+	 * Construct.
 	 * 
-	 * @param page
-	 *            the page where the sources need to be shown from.
+	 * @param <C>
+	 * @param params
 	 */
-	public <C extends Page> SourcesPage(Class<C> page)
+	public <C extends Page> SourcesPage(final PageParameters params)
 	{
-		this.page = page;
+		filename = new Label("filename", new AbstractReadOnlyModel<String>()
+		{
 
-		filename = new Label("filename", new PropertyModel<String>(this, "name"));
+			@Override
+			public String getObject()
+			{
+				return name != null ? name : getPage().getRequest().getParameter(SOURCE);
+			}
+
+		});
 		filename.setOutputMarkupId(true);
 		add(filename);
 		codePanel = new CodePanel("codepanel").setOutputMarkupId(true);
 		add(codePanel);
 		add(new FilesBrowser("filespanel"));
-		add(new PopupCloseLink("close"));
+	}
+
+	/**
+	 * 
+	 * @param page
+	 * @return PageParamets for reconstructing the bookmarkable page.
+	 */
+	public static PageParameters generatePageParameters(Page page)
+	{
+		return generatePageParameters(page.getClass(), null);
+	}
+
+	/**
+	 * 
+	 * @param clazz
+	 * @param fileName
+	 * @return PageParameters for reconstructing the bookmarkable page.
+	 */
+	public static PageParameters generatePageParameters(Class<? extends Page> clazz, String fileName)
+	{
+		PageParameters p = new PageParameters();
+		p.put(PAGE_CLASS, clazz.getName());
+		if (fileName != null)
+			p.put(SOURCE, fileName);
+		return p;
+	}
+
+	private String getPageParam()
+	{
+		return getPage().getRequest().getParameter(PAGE_CLASS);
+	}
+
+	private Class<? extends Page> getPageTargetClass()
+	{
+		if (page == null)
+		{
+			try
+			{
+				String pageParam = getPageParam();
+				if (pageParam == null)
+				{
+					if (log.isErrorEnabled())
+					{
+						log.error("key: " + PAGE_CLASS + " is null.");
+					}
+					throw new AbortWithWebErrorCodeException(404,
+						"Could not find sources for the page you requested");
+				}
+				if (!pageParam.startsWith("org.apache.wicket.examples"))
+				{
+					if (log.isErrorEnabled())
+					{
+						log.error("user is trying to access class: " + pageParam +
+							" which is not in the scope of org.apache.wicket.examples");
+					}
+					throw new UnauthorizedInstantiationException(getClass());
+				}
+				page = (Class<? extends Page>)Class.forName(getPageParam());
+			}
+			catch (ClassNotFoundException e)
+			{
+				throw new AbortWithWebErrorCodeException(404,
+					"Could not find sources for the page you requested");
+			}
+		}
+		return page;
 	}
 }

Modified: wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/Application.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/Application.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/Application.java (original)
+++ wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/Application.java Fri Jan  2 14:20:21 2009
@@ -29,12 +29,9 @@
 	 */
 	public Application()
 	{
-
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * 
+	/**
 	 * @see org.apache.wicket.examples.WicketExampleApplication#init()
 	 */
 	@Override

Modified: wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/HelloWorld.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/HelloWorld.java?rev=730851&r1=730850&r2=730851&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/HelloWorld.java (original)
+++ wicket/trunk/wicket-examples/src/test/java/org/apache/wicket/filtertest/HelloWorld.java Fri Jan  2 14:20:21 2009
@@ -30,6 +30,8 @@
 {
 	/**
 	 * Constructor
+	 * 
+	 * @param params
 	 */
 	public HelloWorld(PageParameters params)
 	{