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 javax.inject.Inject;
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.WebMarkupContainer;
26 import org.apache.wicket.markup.html.basic.Label;
27 import org.apache.wicket.markup.html.image.Image;
28 import org.apache.wicket.markup.html.link.Link;
29 import org.apache.wicket.markup.repeater.RepeatingView;
30 import org.apache.wicket.resource.ByteArrayResource;
31 import org.wamblee.photos.model.Album;
32 import org.wamblee.photos.model.Photo;
33 import org.wamblee.photos.model.PhotoEntry;
34 import org.wamblee.photos.model.plumbing.AllPhotos;
35 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
36 import org.wamblee.security.authentication.User;
37 import org.wamblee.security.authentication.UserAdministration;
42 public class HomePage extends BasePage {
44 private static final long serialVersionUID = 1L;
47 private transient User user;
50 private transient UserAdministration userAdmin;
52 // TODO the state should be stored as a path.
56 private transient Album album;
60 private transient Album authorized;
62 private class SerializableEntryLink extends Link {
66 public SerializableEntryLink(String aId, String aPath) {
72 public void onClick() {
73 System.out.println("Entry " + path + " was clicked");
78 * Constructor that is invoked when page is invoked without a session.
80 * @param parameters Page parameters
82 public HomePage(final PageParameters parameters) throws Exception {
84 add(new Label("message", "Hello world!"));
86 System.out.println("Currently logged in user: " + user);
88 List<String> usernames = userAdmin.getUsers();
89 System.out.println("All user names: " + usernames);
91 usernames = userAdmin.getUsers("public");
92 System.out.println("Users in group public: " + usernames);
94 System.out.println("Entries: " + album.size());
95 for (int i = 0; i < album.size(); i++) {
96 PhotoEntry entry = album.getEntry(i);
97 System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
100 System.out.println("Authorized Entries: " + authorized.size());
101 for (int i = 0; i < authorized.size(); i++) {
102 PhotoEntry entry = authorized.getEntry(i);
103 System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
108 RepeatingView row = new RepeatingView("row");
110 while (irow < 5 && ientry < authorized.size()) {
112 WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
114 RepeatingView column = new RepeatingView("column");
116 while (icolumn < 5 && ientry < authorized.size()) {
117 WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
118 column.add(thumbnail);
120 final PhotoEntry entry = authorized.getEntry(ientry);
121 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
123 ImageData data = getData(entry);
125 // TODO very inefficient. all data is loaded when generating the page.
126 link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
128 link.add(new Label("name", authorized.getEntry(ientry).getId()));
136 public static final class ImageData {
137 private String contentType;
140 public ImageData(String aContentType, byte[] aData) {
141 contentType = aContentType;
145 public String getContentType() {
149 public byte[] getData() {
154 private ImageData getData(PhotoEntry aEntry) {
155 if (aEntry instanceof Photo) {
156 return getData((Photo) aEntry);
157 } else if (aEntry instanceof Album) {
158 return getData((Album) aEntry);
160 throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
164 private ImageData getData(Photo aPhoto) {
165 try (InputStream is = aPhoto.getThumbNail()) {
166 return new ImageData("image/jpeg", getBytes(is));
168 catch (IOException e) {
170 throw new RuntimeException("Cannot read photo", e);
174 private byte[] getBytes(InputStream is) throws IOException {
175 ByteArrayOutputStream bos = new ByteArrayOutputStream();
176 byte[] block = new byte[1024];
177 int n = is.read(block);
179 bos.write(block, 0, n);
182 return bos.toByteArray();
185 private ImageData getData(Album aAlbum) {
186 try (InputStream is = getClass().getResourceAsStream("folder.png")) {
187 return new ImageData("image/png", getBytes(is));
189 catch (IOException e) {
190 throw new RuntimeException("Cannot read album jpg", e);