Same styling of photo albums as with tapestry based application.
authorErik Brakkee <erik@brakkee.org>
Sat, 21 Sep 2013 22:58:09 +0000 (00:58 +0200)
committerErik Brakkee <erik@brakkee.org>
Sat, 21 Sep 2013 22:58:09 +0000 (00:58 +0200)
More robustness when wrong URLs are used.

src/main/java/org/wamblee/photos/wicket/BasePage.java
src/main/java/org/wamblee/photos/wicket/HomePage.html
src/main/java/org/wamblee/photos/wicket/HomePage.java
src/main/java/org/wamblee/photos/wicket/WicketApplication.java
src/main/java/org/wamblee/photos/wicket/photos.css
src/main/webapp/404.jsp [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml
src/main/webapp/login.jsp

index d4bdc115b3c0a57264c773f0b94c6009ce7f7d16..f6c63d00ccd104552e5e17047c48a40f3f8de390 100644 (file)
  */
 package org.wamblee.photos.wicket;
 
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wicket.RedirectToUrlException;
 import org.apache.wicket.markup.html.CSSPackageResource;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.basic.Label;
@@ -28,6 +32,9 @@ import org.wamblee.wicket.page.WebApplicationBasePage;
 
 public class BasePage extends WebApplicationBasePage {
 
+    @Inject
+    private HttpServletRequest request;
+
     private boolean isExpired = false;
 
     public BasePage() {
@@ -36,10 +43,14 @@ public class BasePage extends WebApplicationBasePage {
 
     public BasePage(IModel aModel) {
         super(aModel);
+
+        if (request.getUserPrincipal() == null) {
+            redirectToLoginPage();
+        }
+
         add(new ResetCssBehavior());
         add(new TitleAttributeTooltipBehavior());
-        add(CSSPackageResource.getHeaderContribution(BasePage.class,
-            "photos.css"));
+        add(CSSPackageResource.getHeaderContribution(BasePage.class, "photos.css"));
         disableCaching();
 
         add(new FeedbackPanel("feedback"));
@@ -56,10 +67,15 @@ public class BasePage extends WebApplicationBasePage {
             @Override
             public void onClick() {
                 getRequestCycle().getSession().invalidate();
+                throw redirectToLoginPage();
             }
         });
     }
 
+    private RedirectToUrlException redirectToLoginPage() {
+        return new RedirectToUrlException("login.jsp");
+    }
+
     public void setExpired(boolean aExpired) {
         isExpired = aExpired;
     }
index 705199afcca57460b888d338db13320b93118680..26ac07ebf7d3c6b13daf4763f5c509735d307d44 100644 (file)
 
     <span wicket:id="message">Message here.</span>
 
-    <div id="album">
-        <table border=1>
+    <div id="photos">
+        <table>
             <tr wicket:id="row">
 
-                <td wicket:id="column">
+                <td wicket:id="column" id="photoentry">
 
                     <a href="#" wicket:id="thumbnail">
                         <table>
                             <tr>
-                                <td><img wicket:id="image" border="0"/></td>
+                                <td><img wicket:id="image"/></td>
                             </tr>
                             <tr>
                                 <td><span wicket:id="name">photo name</span></td>
index 289f783e35340e3358eb48ecb0e5669475007446..a7aa1a3785d1568ec50504b57d24db05aed19460 100644 (file)
@@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
+import java.util.logging.Logger;
 import javax.inject.Inject;
 
 import org.apache.wicket.PageParameters;
@@ -41,6 +42,8 @@ import org.wamblee.security.authentication.UserAdministration;
  */
 public class HomePage extends BasePage {
 
+    private static final Logger LOGGER = Logger.getLogger(HomePage.class.getName());
+
     private static final long serialVersionUID = 1L;
 
     @Inject
@@ -71,9 +74,14 @@ public class HomePage extends BasePage {
         @Override
         public void onClick() {
             System.out.println("Entry " + path + " was clicked");
+            PageParameters pars = new PageParameters();
+            pars.put("path", path);
+            setResponsePage(HomePage.class, pars);
         }
     }
 
+    private String path;
+
     /**
      * Constructor that is invoked when page is invoked without a session.
      *
@@ -81,6 +89,13 @@ public class HomePage extends BasePage {
      */
     public HomePage(final PageParameters parameters) throws Exception {
         super();
+
+        path = parameters.getString("path", "/");
+        if (!path.startsWith("/")) {
+            info("Invalid album '" + path + "', showing root album instead");
+            path = "/";
+        }
+
         add(new Label("message", "Hello world!"));
 
         System.out.println("Currently logged in user: " + user);
@@ -103,21 +118,29 @@ public class HomePage extends BasePage {
             System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
         }
 
+        PhotoEntry current = authorized.getEntry(path);
+
+        if (current instanceof Photo) {
+            throw new RuntimeException("Photo entry viewing not yet implemented");
+        }
+
+        Album album = (Album) current;
+
         int ientry = 0;
         int irow = 0;
         RepeatingView row = new RepeatingView("row");
         add(row);
-        while (irow < 5 && ientry < authorized.size()) {
+        while (irow < 5 && ientry < album.size()) {
             int icolumn = 0;
             WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
             row.add(columns);
             RepeatingView column = new RepeatingView("column");
             columns.add(column);
-            while (icolumn < 5 && ientry < authorized.size()) {
+            while (icolumn < 5 && ientry < album.size()) {
                 WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
                 column.add(thumbnail);
 
-                final PhotoEntry entry = authorized.getEntry(ientry);
+                final PhotoEntry entry = album.getEntry(ientry);
                 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
                 thumbnail.add(link);
                 ImageData data = getData(entry);
@@ -125,7 +148,7 @@ public class HomePage extends BasePage {
                 // TODO very inefficient. all data is loaded when generating the page.
                 link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
 
-                link.add(new Label("name", authorized.getEntry(ientry).getId()));
+                link.add(new Label("name", album.getEntry(ientry).getId()));
                 icolumn++;
                 ientry++;
             }
index 8339000e32d07158e1dd8e72ac180a485e90c7a7..a1859a70ac3e0d9bbb7d0276d77a52ee922863fb 100644 (file)
  */
 package org.wamblee.photos.wicket;
 
-import javax.servlet.http.HttpServletRequest;
-
 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.request.target.basic.RedirectRequestTarget;
+import org.apache.wicket.request.target.coding.MixedParamUrlCodingStrategy;
 import org.apache.wicket.settings.IApplicationSettings;
+import org.apache.wicket.settings.IExceptionSettings;
 import org.wamblee.wicket.inject.ComponentInstantiationInjector;
 import org.wamblee.wicket.transactions.OpenTransactionInViewRequestCycle;
 
 /**
  * Application object for your web application. If you want to run this
  * application without deploying, run the Start class.
- * 
+ *
  * @see org.wamblee.Start#main(String[])
  */
 public class WicketApplication extends WebApplication {
@@ -44,8 +43,7 @@ public class WicketApplication extends WebApplication {
 
     @Override
     public RequestCycle newRequestCycle(Request aRequest, Response aResponse) {
-        return new OpenTransactionInViewRequestCycle(this,
-            (WebRequest) aRequest, aResponse);
+        return new OpenTransactionInViewRequestCycle(this, (WebRequest) aRequest, aResponse);
     }
 
     @Override
@@ -56,6 +54,8 @@ public class WicketApplication extends WebApplication {
         IApplicationSettings settings = getApplicationSettings();
         settings.setInternalErrorPage(ErrorPage.class);
 
+        mount(new MixedParamUrlCodingStrategy("view", HomePage.class, new String[]{"path"}));
+
         // Use the lines below to get the internal error page also when in
         // development mode.
         // IExceptionSettings exs = getExceptionSettings();
@@ -68,5 +68,4 @@ public class WicketApplication extends WebApplication {
     public Class<HomePage> getHomePage() {
         return HomePage.class;
     }
-
 }
index eaa78ec393e945317d204d7e6d6c4322c779e71a..4c08befdd724f8ca741395e15c27fc9c4d861a38 100644 (file)
@@ -1,8 +1,6 @@
 /* general */
 body {
-    font-family: Arial, Helvetica, sans-serif;
-    list-style-type: square;
-    font-size: 20px; 
+    font-family: sans-serif;
 }
 /* menu styling */
 
@@ -79,32 +77,28 @@ ul.feedbackPanel {
     font-size: 24px;
 }
 
-#content table {
-    text-align: left;
-}
-
-#content th, #content td {
+#photos th, #content td {
     vertical-align: top;
     padding: 10px;
 }
 
-.entries a {
-    display: block;
-    float: left;
-    font-size: 24px;
-    margin-left: 10%;
-    margin-right: 24px;
-    line-height: 34px;
-    width: 100%;
+#photos table {
+    font-size: small;
+    text-align: left;
+    border-collapse: collapse;
+    border-spacing: 0px;
 }
 
-.entries .index {
-    display: block;
-    float: left;
-    font-size: 30px;
-    width: 100%;
-    margin-top: 10px;
+#photoentry {
+    padding: 0.2em;
+    border: 1px;
+    border: 1px;
+    border-style: solid;
+    border-bottom-color: gray;
 }
 
+#photos img {
+    display: block;
+}
 
-
+}
\ No newline at end of file
diff --git a/src/main/webapp/404.jsp b/src/main/webapp/404.jsp
new file mode 100644 (file)
index 0000000..5f2d8eb
--- /dev/null
@@ -0,0 +1,47 @@
+<%@ page language="java" pageEncoding="UTF-8" session="true"%>
+
+
+
+<%
+    String path = request.getContextPath();
+    String basePath = request.getScheme() + "://" +
+        request.getServerName() + ":" + request.getServerPort() + path +
+        "/";
+%>
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+
+<head>
+<title wicket:id="title">Oops</title>
+
+<%
+    String cssUrl = request.getContextPath() + "/resources/org.wamblee.photos.wicket.BasePage/photos.css";
+%>
+
+<link rel="stylesheet" type="text/css" href="<%= cssUrl %>" />
+</head>
+
+<body>
+
+       <div id="banner">
+           <%
+               String logoUrl = request.getContextPath() + "/resources/org.wamblee.photos.wicket.BasePage/wamblee_logo.png";
+           %>
+        <img src="<%= logoUrl %>" />
+               <span class="title">wamblee photos</span>
+       </div>
+
+
+    <br/>
+       Oops, the requested URL could not be found.
+       <%
+        String loginUrl = request.getContextPath() + "/login.jsp";
+    %>
+       Please <a href="<%= loginUrl %>">login again</a>
+
+       <%
+           session.invalidate();
+       %>
+</body>
+</html>
index 67624786057d68abeabca6b6ea4ca4906a596630..eaa1fd7eefb04c179d1b5472348079a9b777ef85 100644 (file)
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-       version="3.0">
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+         version="3.0">
 
-       <display-name>cdi</display-name>
+    <display-name>cdi</display-name>
 
-       <!--  
-             There are three means to configure Wickets configuration mode and they are
-             tested in the order given. 
-             1) A system property: -Dwicket.configuration
-             2) servlet specific <init-param>
-             3) context specific <context-param>
-             The value might be either "development" (reloading when templates change)
-             or "deployment". If no configuration is found, "development" is the default.
-       -->
-       
-       <filter>
-               <filter-name>authentication</filter-name>
-               <filter-class>org.wamblee.photos.security.AuthenticationFilter</filter-class>
-               <init-param>
-                       <param-name>loginpage</param-name>
-                       <param-value>/login.jsp</param-value>
-               </init-param>
-               <!-- each authenticated user is assigned to the gruop ALL in the security realm configuration --> 
-               <init-param>
-                       <param-name>role</param-name>
-                       <param-value>ALL</param-value>
-               </init-param>
-               <!-- defines the resource URLs for which no authentication is required --> 
-               <init-param>
-                       <param-name>resources</param-name>
-                       <param-value>/resources</param-value>
-               </init-param>
-       </filter>
+    <!--
+          There are three means to configure Wickets configuration mode and they are
+          tested in the order given.
+          1) A system property: -Dwicket.configuration
+          2) servlet specific <init-param>
+          3) context specific <context-param>
+          The value might be either "development" (reloading when templates change)
+          or "deployment". If no configuration is found, "development" is the default.
+    -->
 
-       <filter>
-               <filter-name>photos</filter-name>
-               <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
-               <init-param>
-                       <param-name>applicationClassName</param-name>
-                       <param-value>org.wamblee.photos.wicket.WicketApplication</param-value>
-               </init-param>
-               <init-param>
-                       <param-name>configuration</param-name>
-                       <param-value>development</param-value>
-               </init-param>
-       </filter>
+    <filter>
+        <filter-name>authentication</filter-name>
+        <filter-class>org.wamblee.photos.security.AuthenticationFilter</filter-class>
+        <init-param>
+            <param-name>loginpage</param-name>
+            <param-value>/login.jsp</param-value>
+        </init-param>
+        <!-- each authenticated user is assigned to the gruop ALL in the security realm configuration -->
+        <init-param>
+            <param-name>role</param-name>
+            <param-value>ALL</param-value>
+        </init-param>
+        <!-- defines the resource URLs for which no authentication is required -->
+        <init-param>
+            <param-name>resources</param-name>
+            <param-value>/resources</param-value>
+        </init-param>
+    </filter>
 
+    <filter>
+        <filter-name>photos</filter-name>
+        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
+        <init-param>
+            <param-name>applicationClassName</param-name>
+            <param-value>org.wamblee.photos.wicket.WicketApplication</param-value>
+        </init-param>
+        <init-param>
+            <param-name>configuration</param-name>
+            <param-value>development</param-value>
+        </init-param>
+    </filter>
+
+    <filter-mapping>
+        <filter-name>authentication</filter-name>
+        <url-pattern>/*</url-pattern>
+    </filter-mapping>
     <filter-mapping>
-       <filter-name>authentication</filter-name>
-       <url-pattern>/*</url-pattern>
+        <filter-name>photos</filter-name>
+        <url-pattern>/*</url-pattern>
     </filter-mapping>
-       <filter-mapping>
-               <filter-name>photos</filter-name>
-               <url-pattern>/*</url-pattern>
-       </filter-mapping>
-       
-       <security-constraint>
-               <web-resource-collection>
-                       <web-resource-name>resources</web-resource-name>
-                       <url-pattern>/resources/*</url-pattern>
-               </web-resource-collection>
-       </security-constraint>
-       <security-constraint>
-               <web-resource-collection>
-                       <web-resource-name>securedaccess</web-resource-name>
-                       <url-pattern>/*</url-pattern>
-               </web-resource-collection>
-               <auth-constraint>
-                       <role-name>ALL</role-name>
-                       <role-name>users</role-name>
-               </auth-constraint>
-       </security-constraint>
-       
-       <login-config>
-               <auth-method>FORM</auth-method>
-               <realm-name>PhotoXChangeRealm</realm-name>
-               <form-login-config>
-                       <form-login-page>/login.jsp</form-login-page>
-                       <form-error-page>/loginError.jsp</form-error-page>
-               </form-login-config>
-       </login-config>
-       
-       <session-config>
-               <session-timeout>10</session-timeout>
-       </session-config>
-       
-       <welcome-file-list>
-               <welcome-file>login.jsp</welcome-file>
-       </welcome-file-list>
-       
-       <security-role>
-               <role-name>ALL</role-name>
-       </security-role>
-       
+
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>resources</web-resource-name>
+            <url-pattern>/resources/*</url-pattern>
+        </web-resource-collection>
+    </security-constraint>
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>securedaccess</web-resource-name>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>ALL</role-name>
+            <role-name>users</role-name>
+        </auth-constraint>
+    </security-constraint>
+
+    <login-config>
+        <auth-method>FORM</auth-method>
+        <realm-name>PhotoXChangeRealm</realm-name>
+        <form-login-config>
+            <form-login-page>/login.jsp</form-login-page>
+            <form-error-page>/loginError.jsp</form-error-page>
+        </form-login-config>
+    </login-config>
+
+    <error-page>
+        <error-code>404</error-code>
+        <!-- view will redirect to login if the user is not logged in -->
+        <location>/404.jsp</location>
+    </error-page>
+
+    <session-config>
+        <session-timeout>10</session-timeout>
+    </session-config>
+
+    <welcome-file-list>
+        <welcome-file>login.jsp</welcome-file>
+    </welcome-file-list>
+
+    <security-role>
+        <role-name>ALL</role-name>
+    </security-role>
+
 </web-app>
index e3fbb1124b6c1e9356145ab265ad35cfbca0e541..88393b49750a9e793515592e3614b9d29ffef01b 100644 (file)
 <head>
 <title wicket:id="title">Home Page</title>
 
+<%
+    String cssUrl = request.getContextPath() + "/resources/org.wamblee.photos.wicket.BasePage/photos.css";
+%>
 
-<link rel="stylesheet" type="text/css"
-       href="resources/org.wamblee.photos.wicket.BasePage/photos.css" />
+<link rel="stylesheet" type="text/css" href="<%= cssUrl %>" />
 </head>
 
 <body>
 
        <div id="banner">
-               <wicket:link>
-                       <img
-                               src="resources/org.wamblee.photos.wicket.BasePage/wamblee_logo.png" />
-               </wicket:link>
+           <%
+               String logoUrl = request.getContextPath() + "/resources/org.wamblee.photos.wicket.BasePage/wamblee_logo.png";
+           %>
+        <img src="<%= logoUrl %>" />
                <span class="title">wamblee photos</span>
        </div>