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.logging.Logger;
22 import javax.inject.Inject;
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.basic.Label;
26 import org.apache.wicket.markup.html.image.Image;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.Panel;
29 import org.apache.wicket.resource.ByteArrayResource;
30 import org.wamblee.photos.model.Album;
31 import org.wamblee.photos.model.Photo;
32 import org.wamblee.photos.model.PhotoEntry;
33 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
38 public class PhotoPanel extends Panel {
40 private static final Logger LOGGER = Logger.getLogger(PhotoPanel.class.getName());
42 private static final long serialVersionUID = 1L;
43 public static final int MAX_ROWS = 5;
44 public static final int MAX_COLUMNS = 5;
48 private transient Album authorized;
53 * Constructor that is invoked when page is invoked without a session.
55 * @param parameters Page parameters
57 public PhotoPanel(String aId, final PageParameters parameters) throws Exception {
60 path = parameters.getString("path", "/");
61 if (!path.startsWith("/")) {
62 info("Invalid album '" + path + "', showing root album instead");
65 add(new Label("path", path));
67 PhotoEntry current = authorized.getEntry(path);
68 if (current instanceof Album) {
69 throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
71 final Photo photo = (Photo) current;
73 String parentPath_ = path.substring(0, path.lastIndexOf("/"));
74 if (parentPath_.length() == 0) {
77 final String parentPath = parentPath_;
78 final Album parent = (Album) authorized.getEntry(parentPath);
79 final Photo before = parent.findPhotoBefore(photo.getId());
80 final Photo after = parent.findPhotoAfter(photo.getId());
82 Link prevLink = new Link("prevLink") {
90 public void onClick() {
94 PageParameters pars = new PageParameters();
95 pars.put("path", before.getPath());
96 setResponsePage(HomePage.class, pars);
102 Link nextLink = new Link("nextLink") {
110 public void onClick() {
114 PageParameters pars = new PageParameters();
115 pars.put("path", after.getPath());
116 setResponsePage(HomePage.class, pars);
122 Link parentLink = new Link("parentLink") {
124 if ("/".equals(path)) {
130 public void onClick() {
131 PageParameters pars = new PageParameters();
133 pars.put("path", parentPath);
134 pars.put("index", 0);
135 setResponsePage(HomePage.class, pars);
141 Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(photo)));
145 private byte[] getData(Photo aPhoto) {
146 try (InputStream is = aPhoto.getPhoto()) {
149 catch (IOException e) {
151 throw new RuntimeException("Cannot read photo", e);
155 private byte[] getBytes(InputStream is) throws IOException {
156 ByteArrayOutputStream bos = new ByteArrayOutputStream();
157 byte[] block = new byte[1024];
158 int n = is.read(block);
160 bos.write(block, 0, n);
163 return bos.toByteArray();