2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.wicket;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.List;
22 import java.util.logging.Logger;
23 import javax.inject.Inject;
25 import org.apache.wicket.PageParameters;
26 import org.apache.wicket.markup.html.WebMarkupContainer;
27 import org.apache.wicket.markup.html.basic.Label;
28 import org.apache.wicket.markup.html.image.Image;
29 import org.apache.wicket.markup.html.link.Link;
30 import org.apache.wicket.markup.repeater.RepeatingView;
31 import org.apache.wicket.resource.ByteArrayResource;
32 import org.wamblee.photos.model.Album;
33 import org.wamblee.photos.model.Photo;
34 import org.wamblee.photos.model.PhotoEntry;
35 import org.wamblee.photos.model.plumbing.AllPhotos;
36 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
37 import org.wamblee.security.authentication.User;
38 import org.wamblee.security.authentication.UserAdministration;
43 public class HomePage extends BasePage {
45 private static final Logger LOGGER = Logger.getLogger(HomePage.class.getName());
47 private static final long serialVersionUID = 1L;
50 private transient User user;
53 private transient UserAdministration userAdmin;
55 // TODO the state should be stored as a path.
59 private transient Album album;
63 private transient Album authorized;
65 private class SerializableEntryLink extends Link {
69 public SerializableEntryLink(String aId, String aPath) {
75 public void onClick() {
76 System.out.println("Entry " + path + " was clicked");
77 PageParameters pars = new PageParameters();
78 pars.put("path", path);
79 setResponsePage(HomePage.class, pars);
86 * Constructor that is invoked when page is invoked without a session.
88 * @param parameters Page parameters
90 public HomePage(final PageParameters parameters) throws Exception {
93 path = parameters.getString("path", "/");
94 if (!path.startsWith("/")) {
95 info("Invalid album '" + path + "', showing root album instead");
99 add(new Label("message", "Hello world!"));
101 System.out.println("Currently logged in user: " + user);
103 List<String> usernames = userAdmin.getUsers();
104 System.out.println("All user names: " + usernames);
106 usernames = userAdmin.getUsers("public");
107 System.out.println("Users in group public: " + usernames);
109 System.out.println("Entries: " + album.size());
110 for (int i = 0; i < album.size(); i++) {
111 PhotoEntry entry = album.getEntry(i);
112 System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
115 System.out.println("Authorized Entries: " + authorized.size());
116 for (int i = 0; i < authorized.size(); i++) {
117 PhotoEntry entry = authorized.getEntry(i);
118 System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
121 PhotoEntry current = authorized.getEntry(path);
123 if (current instanceof Photo) {
124 throw new RuntimeException("Photo entry viewing not yet implemented");
127 Album album = (Album) current;
131 RepeatingView row = new RepeatingView("row");
133 while (irow < 5 && ientry < album.size()) {
135 WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
137 RepeatingView column = new RepeatingView("column");
139 while (icolumn < 5 && ientry < album.size()) {
140 WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
141 column.add(thumbnail);
143 final PhotoEntry entry = album.getEntry(ientry);
144 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
146 ImageData data = getData(entry);
148 // TODO very inefficient. all data is loaded when generating the page.
149 link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
151 link.add(new Label("name", album.getEntry(ientry).getId()));
159 public static final class ImageData {
160 private String contentType;
163 public ImageData(String aContentType, byte[] aData) {
164 contentType = aContentType;
168 public String getContentType() {
172 public byte[] getData() {
177 private ImageData getData(PhotoEntry aEntry) {
178 if (aEntry instanceof Photo) {
179 return getData((Photo) aEntry);
180 } else if (aEntry instanceof Album) {
181 return getData((Album) aEntry);
183 throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
187 private ImageData getData(Photo aPhoto) {
188 try (InputStream is = aPhoto.getThumbNail()) {
189 return new ImageData("image/jpeg", getBytes(is));
191 catch (IOException e) {
193 throw new RuntimeException("Cannot read photo", e);
197 private byte[] getBytes(InputStream is) throws IOException {
198 ByteArrayOutputStream bos = new ByteArrayOutputStream();
199 byte[] block = new byte[1024];
200 int n = is.read(block);
202 bos.write(block, 0, n);
205 return bos.toByteArray();
208 private ImageData getData(Album aAlbum) {
209 try (InputStream is = getClass().getResourceAsStream("folder.png")) {
210 return new ImageData("image/png", getBytes(is));
212 catch (IOException e) {
213 throw new RuntimeException("Cannot read album jpg", e);