2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.application;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
36 import de.todesbaum.jsite.gui.FileScanner;
37 import de.todesbaum.jsite.gui.FileScannerListener;
38 import de.todesbaum.util.freenet.fcp2.Client;
39 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
42 import de.todesbaum.util.freenet.fcp2.FileEntry;
43 import de.todesbaum.util.freenet.fcp2.Message;
44 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
45 import de.todesbaum.util.freenet.fcp2.Verbosity;
46 import de.todesbaum.util.io.ReplacingOutputStream;
47 import de.todesbaum.util.io.StreamCopier;
50 * @author David Roden <droden@gmail.com>
53 public class ProjectInserter implements FileScannerListener, Runnable {
55 private static int counter = 0;
56 private boolean debug = false;
57 private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
58 protected Freenet7Interface freenetInterface;
59 protected Project project;
60 private FileScanner fileScanner;
61 protected final Object lockObject = new Object();
62 private int maxRetries = 99999;
64 public void addInsertListener(InsertListener insertListener) {
65 insertListeners.add(insertListener);
68 public void removeInsertListener(InsertListener insertListener) {
69 insertListeners.remove(insertListener);
72 protected void fireProjectInsertStarted() {
73 for (InsertListener insertListener: insertListeners) {
74 insertListener.projectInsertStarted(project);
78 protected void fireProjectURIGenerated(String uri) {
79 for (InsertListener insertListener: insertListeners) {
80 insertListener.projectURIGenerated(project, uri);
84 protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
85 for (InsertListener insertListener: insertListeners) {
86 insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
90 protected void fireProjectInsertFinished(boolean success, Throwable cause) {
91 for (InsertListener insertListener: insertListeners) {
92 insertListener.projectInsertFinished(project, success, cause);
100 public void setDebug(boolean debug) {
106 * The project to set.
108 public void setProject(Project project) {
109 this.project = project;
113 * @param freenetInterface
114 * The freenetInterface to set.
116 public void setFreenetInterface(Freenet7Interface freenetInterface) {
117 this.freenetInterface = freenetInterface;
122 * The maxRetries to set.
124 public void setMaxRetries(int maxRetries) {
125 this.maxRetries = maxRetries;
128 public void start() {
129 fileScanner = new FileScanner(project);
130 fileScanner.addFileScannerListener(this);
131 new Thread(fileScanner).start();
134 private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
135 File file = new File(project.getLocalPath(), filename);
136 length[0] = file.length();
137 if (!fileOption.getReplaceEdition()) {
138 return new FileInputStream(file);
140 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
141 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
142 FileInputStream fileInput = new FileInputStream(file);
143 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
144 outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
145 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
146 outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
147 outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
149 StreamCopier.copy(fileInput, outputStream, length[0]);
150 outputStream.close();
151 filteredByteOutputStream.close();
152 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
153 length[0] = filteredBytes.length;
154 return new ByteArrayInputStream(filteredBytes);
157 private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
158 File tempFile = File.createTempFile("jsite", ".zip");
159 tempFile.deleteOnExit();
160 FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
161 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
162 for (String filename: containerFiles.get(containerName)) {
163 File dataFile = new File(project.getLocalPath(), filename);
164 if (dataFile.exists()) {
165 ZipEntry zipEntry = new ZipEntry(filename);
166 long[] fileLength = new long[1];
167 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
168 zipOutputStream.putNextEntry(zipEntry);
169 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
170 zipOutputStream.closeEntry();
171 wrappedInputStream.close();
174 zipOutputStream.closeEntry();
176 /* FIXME - create metadata */
177 // ZipEntry metadataEntry = new ZipEntry("metadata");
178 // zipOutputStream.putNextEntry(metadataEntry);
179 // Metadata zipMetadata = new Metadata();
180 // for (String filename: containerFiles.get(containerName)) {
181 // if (new File(project.getLocalPath(), filename).exists()) {
182 // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
183 // zipEntryMetadata.setName(filename);
184 // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
185 // zipMetadata.addDocument(zipEntryMetadata);
188 // zipOutputStream.write(zipMetadata.toByteArray());
189 // zipOutputStream.closeEntry();
190 zipOutputStream.close();
192 containerLength[0] = tempFile.length();
193 return new FileInputStream(tempFile);
196 private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
197 FileEntry fileEntry = null;
198 FileOption fileOption = project.getFileOption(filename);
199 if (filename.startsWith("/container/:")) {
200 String containerName = filename.substring("/container/:".length());
202 long[] containerLength = new long[1];
203 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
204 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
205 } catch (IOException ioe1) {
208 if (fileOption.isInsert()) {
210 long[] fileLength = new long[1];
211 InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
212 fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
213 } catch (IOException ioe1) {
216 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
222 private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
223 for (String filename: new ArrayList<String>(files)) {
224 FileOption fileOption = project.getFileOption(filename);
225 String containerName = fileOption.getContainer();
226 if (!containerName.equals("")) {
227 if (!containers.contains(containerName)) {
228 containers.add(containerName);
229 containerFiles.put(containerName, new ArrayList<String>());
230 /* hmm. looks like a hack to me. */
231 files.add("/container/:" + containerName);
233 containerFiles.get(containerName).add(filename);
234 files.remove(filename);
243 fireProjectInsertStarted();
244 List<String> files = fileScanner.getFiles();
246 /* create connection to node */
247 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
248 boolean connected = false;
249 Throwable cause = null;
251 connected = connection.connect();
252 } catch (IOException e1) {
257 fireProjectInsertFinished(false, cause);
261 Client client = new Client(connection);
263 /* create containers */
264 final List<String> containers = new ArrayList<String>();
265 final Map<String, List<String>> containerFiles = new HashMap<String, List<String>>();
266 createContainers(files, containers, containerFiles);
269 int edition = ((EditionProject) project).getEdition();
270 String dirURI = "freenet:USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
271 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
272 putDir.setDefaultName(project.getIndexFile());
273 putDir.setVerbosity(Verbosity.ALL);
274 putDir.setMaxRetries(maxRetries);
275 for (String filename: files) {
276 FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
277 if (fileEntry != null) {
278 putDir.addFileEntry(fileEntry);
284 client.execute(putDir);
285 } catch (IOException ioe1) {
286 fireProjectInsertFinished(false, ioe1);
290 /* parse progress and success messages */
291 boolean success = false;
292 boolean finished = false;
293 boolean disconnected = false;
295 Message message = client.readMessage();
296 finished = (message == null) || (disconnected = client.isDisconnected());
298 System.out.println(message);
301 String messageName = message.getName();
302 if ("URIGenerated".equals(messageName)) {
303 fireProjectURIGenerated(message.get("URI"));
305 if ("SimpleProgress".equals(messageName)) {
306 int total = Integer.parseInt(message.get("Total"));
307 int succeeded = Integer.parseInt(message.get("Succeeded"));
308 int fatal = Integer.parseInt(message.get("FatallyFailed"));
309 int failed = Integer.parseInt(message.get("Failed"));
310 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
311 fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
313 success = "PutSuccessful".equals(messageName);
314 finished = success || "PutFailed".equals(messageName);
318 /* post-insert work */
319 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
321 if (project instanceof EditionProject) {
322 ((EditionProject) project).setEdition(edition + 1);
328 // INTERFACE FileScannerListener
334 public void fileScannerFinished(FileScanner fileScanner) {
335 if (!fileScanner.isError()) {
336 new Thread(this).start();
338 fireProjectInsertFinished(false, null);
340 fileScanner.removeFileScannerListener(this);