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.Closer;
47 import de.todesbaum.util.io.ReplacingOutputStream;
48 import de.todesbaum.util.io.StreamCopier;
51 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
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();
63 public void addInsertListener(InsertListener insertListener) {
64 insertListeners.add(insertListener);
67 public void removeInsertListener(InsertListener insertListener) {
68 insertListeners.remove(insertListener);
71 protected void fireProjectInsertStarted() {
72 for (InsertListener insertListener: insertListeners) {
73 insertListener.projectInsertStarted(project);
77 protected void fireProjectURIGenerated(String uri) {
78 for (InsertListener insertListener: insertListeners) {
79 insertListener.projectURIGenerated(project, uri);
83 protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
84 for (InsertListener insertListener: insertListeners) {
85 insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
89 protected void fireProjectInsertFinished(boolean success, Throwable cause) {
90 for (InsertListener insertListener: insertListeners) {
91 insertListener.projectInsertFinished(project, success, cause);
99 public void setDebug(boolean debug) {
105 * The project to set.
107 public void setProject(Project project) {
108 this.project = project;
112 * @param freenetInterface
113 * The freenetInterface to set.
115 public void setFreenetInterface(Freenet7Interface freenetInterface) {
116 this.freenetInterface = freenetInterface;
119 public void start() {
120 fileScanner = new FileScanner(project);
121 fileScanner.addFileScannerListener(this);
122 new Thread(fileScanner).start();
125 private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
126 File file = new File(project.getLocalPath(), filename);
127 length[0] = file.length();
128 if (!fileOption.getReplaceEdition()) {
129 return new FileInputStream(file);
131 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
132 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
133 FileInputStream fileInput = new FileInputStream(file);
135 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
136 outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
137 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
138 outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
139 outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
141 StreamCopier.copy(fileInput, outputStream, length[0]);
143 Closer.close(fileInput);
144 Closer.close(outputStream);
145 Closer.close(filteredByteOutputStream);
147 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
148 length[0] = filteredBytes.length;
149 return new ByteArrayInputStream(filteredBytes);
152 private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
153 File tempFile = File.createTempFile("jsite", ".zip");
154 tempFile.deleteOnExit();
155 FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
156 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
158 for (String filename: containerFiles.get(containerName)) {
159 File dataFile = new File(project.getLocalPath(), filename);
160 if (dataFile.exists()) {
161 ZipEntry zipEntry = new ZipEntry(filename);
162 long[] fileLength = new long[1];
163 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
165 zipOutputStream.putNextEntry(zipEntry);
166 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
168 zipOutputStream.closeEntry();
169 wrappedInputStream.close();
174 zipOutputStream.closeEntry();
175 Closer.close(zipOutputStream);
176 Closer.close(fileOutputStream);
179 containerLength[0] = tempFile.length();
180 return new FileInputStream(tempFile);
183 private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
184 FileEntry fileEntry = null;
185 FileOption fileOption = project.getFileOption(filename);
186 if (filename.startsWith("/container/:")) {
187 String containerName = filename.substring("/container/:".length());
189 long[] containerLength = new long[1];
190 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
191 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
192 } catch (IOException ioe1) {
195 if (fileOption.isInsert()) {
197 long[] fileLength = new long[1];
198 InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
199 fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
200 } catch (IOException ioe1) {
203 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
209 private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
210 for (String filename: new ArrayList<String>(files)) {
211 FileOption fileOption = project.getFileOption(filename);
212 String containerName = fileOption.getContainer();
213 if (!containerName.equals("")) {
214 if (!containers.contains(containerName)) {
215 containers.add(containerName);
216 containerFiles.put(containerName, new ArrayList<String>());
217 /* hmm. looks like a hack to me. */
218 files.add("/container/:" + containerName);
220 containerFiles.get(containerName).add(filename);
221 files.remove(filename);
230 fireProjectInsertStarted();
231 List<String> files = fileScanner.getFiles();
233 /* create connection to node */
234 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
235 boolean connected = false;
236 Throwable cause = null;
238 connected = connection.connect();
239 } catch (IOException e1) {
244 fireProjectInsertFinished(false, cause);
248 Client client = new Client(connection);
250 /* create containers */
251 final List<String> containers = new ArrayList<String>();
252 final Map<String, List<String>> containerFiles = new HashMap<String, List<String>>();
253 createContainers(files, containers, containerFiles);
256 int edition = project.getEdition();
257 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
258 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
259 putDir.setDefaultName(project.getIndexFile());
260 putDir.setVerbosity(Verbosity.ALL);
261 putDir.setMaxRetries(-1);
262 for (String filename: files) {
263 FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
264 if (fileEntry != null) {
265 putDir.addFileEntry(fileEntry);
271 client.execute(putDir);
272 } catch (IOException ioe1) {
273 fireProjectInsertFinished(false, ioe1);
277 /* parse progress and success messages */
278 String finalURI = null;
279 boolean success = false;
280 boolean finished = false;
281 boolean disconnected = false;
283 Message message = client.readMessage();
284 finished = (message == null) || (disconnected = client.isDisconnected());
286 System.out.println(message);
289 String messageName = message.getName();
290 if ("URIGenerated".equals(messageName)) {
291 finalURI = message.get("URI");
292 fireProjectURIGenerated(finalURI);
294 if ("SimpleProgress".equals(messageName)) {
295 int total = Integer.parseInt(message.get("Total"));
296 int succeeded = Integer.parseInt(message.get("Succeeded"));
297 int fatal = Integer.parseInt(message.get("FatallyFailed"));
298 int failed = Integer.parseInt(message.get("Failed"));
299 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
300 fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
302 success = "PutSuccessful".equals(messageName);
303 finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
307 /* post-insert work */
308 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
310 String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
311 int newEdition = Integer.parseInt(editionPart);
312 project.setEdition(newEdition);
317 // INTERFACE FileScannerListener
323 public void fileScannerFinished(FileScanner fileScanner) {
324 if (!fileScanner.isError()) {
325 new Thread(this).start();
327 fireProjectInsertFinished(false, null);
329 fileScanner.removeFileScannerListener(this);