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 Roden <droden@gmail.com>
54 public class ProjectInserter implements FileScannerListener, Runnable {
56 private static int counter = 0;
57 private boolean debug = false;
58 private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
59 protected Freenet7Interface freenetInterface;
60 protected Project project;
61 private FileScanner fileScanner;
62 protected final Object lockObject = new Object();
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;
120 public void start() {
121 fileScanner = new FileScanner(project);
122 fileScanner.addFileScannerListener(this);
123 new Thread(fileScanner).start();
126 private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
127 File file = new File(project.getLocalPath(), filename);
128 length[0] = file.length();
129 if (!fileOption.getReplaceEdition()) {
130 return new FileInputStream(file);
132 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
133 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
134 FileInputStream fileInput = new FileInputStream(file);
136 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
137 outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
138 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
139 outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
140 outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
142 StreamCopier.copy(fileInput, outputStream, length[0]);
144 Closer.close(fileInput);
145 Closer.close(outputStream);
146 Closer.close(filteredByteOutputStream);
148 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
149 length[0] = filteredBytes.length;
150 return new ByteArrayInputStream(filteredBytes);
153 private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
154 File tempFile = File.createTempFile("jsite", ".zip");
155 tempFile.deleteOnExit();
156 FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
157 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
159 for (String filename: containerFiles.get(containerName)) {
160 File dataFile = new File(project.getLocalPath(), filename);
161 if (dataFile.exists()) {
162 ZipEntry zipEntry = new ZipEntry(filename);
163 long[] fileLength = new long[1];
164 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
166 zipOutputStream.putNextEntry(zipEntry);
167 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
169 zipOutputStream.closeEntry();
170 wrappedInputStream.close();
175 zipOutputStream.closeEntry();
176 Closer.close(zipOutputStream);
177 Closer.close(fileOutputStream);
180 containerLength[0] = tempFile.length();
181 return new FileInputStream(tempFile);
184 private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
185 FileEntry fileEntry = null;
186 FileOption fileOption = project.getFileOption(filename);
187 if (filename.startsWith("/container/:")) {
188 String containerName = filename.substring("/container/:".length());
190 long[] containerLength = new long[1];
191 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
192 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
193 } catch (IOException ioe1) {
196 if (fileOption.isInsert()) {
198 long[] fileLength = new long[1];
199 InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
200 fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
201 } catch (IOException ioe1) {
204 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
210 private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
211 for (String filename: new ArrayList<String>(files)) {
212 FileOption fileOption = project.getFileOption(filename);
213 String containerName = fileOption.getContainer();
214 if (!containerName.equals("")) {
215 if (!containers.contains(containerName)) {
216 containers.add(containerName);
217 containerFiles.put(containerName, new ArrayList<String>());
218 /* hmm. looks like a hack to me. */
219 files.add("/container/:" + containerName);
221 containerFiles.get(containerName).add(filename);
222 files.remove(filename);
231 fireProjectInsertStarted();
232 List<String> files = fileScanner.getFiles();
234 /* create connection to node */
235 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
236 boolean connected = false;
237 Throwable cause = null;
239 connected = connection.connect();
240 } catch (IOException e1) {
245 fireProjectInsertFinished(false, cause);
249 Client client = new Client(connection);
251 /* create containers */
252 final List<String> containers = new ArrayList<String>();
253 final Map<String, List<String>> containerFiles = new HashMap<String, List<String>>();
254 createContainers(files, containers, containerFiles);
257 int edition = project.getEdition();
258 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
259 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
260 putDir.setDefaultName(project.getIndexFile());
261 putDir.setVerbosity(Verbosity.ALL);
262 putDir.setMaxRetries(-1);
263 for (String filename: files) {
264 FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
265 if (fileEntry != null) {
266 putDir.addFileEntry(fileEntry);
272 client.execute(putDir);
273 } catch (IOException ioe1) {
274 fireProjectInsertFinished(false, ioe1);
278 /* parse progress and success messages */
279 String finalURI = null;
280 boolean success = false;
281 boolean finished = false;
282 boolean disconnected = false;
284 Message message = client.readMessage();
285 finished = (message == null) || (disconnected = client.isDisconnected());
287 System.out.println(message);
290 String messageName = message.getName();
291 if ("URIGenerated".equals(messageName)) {
292 finalURI = message.get("URI");
293 fireProjectURIGenerated(finalURI);
295 if ("SimpleProgress".equals(messageName)) {
296 int total = Integer.parseInt(message.get("Total"));
297 int succeeded = Integer.parseInt(message.get("Succeeded"));
298 int fatal = Integer.parseInt(message.get("FatallyFailed"));
299 int failed = Integer.parseInt(message.get("Failed"));
300 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
301 fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
303 success = "PutSuccessful".equals(messageName);
304 finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
308 /* post-insert work */
309 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
311 String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
312 int newEdition = Integer.parseInt(editionPart);
313 project.setEdition(newEdition);
318 // INTERFACE FileScannerListener
324 public void fileScannerFinished(FileScanner fileScanner) {
325 if (!fileScanner.isError()) {
326 new Thread(this).start();
328 fireProjectInsertFinished(false, null);
330 fileScanner.removeFileScannerListener(this);