remove subversion $Id$ tags
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.application;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
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;
32 import java.util.Map;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
35
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;
49
50 /**
51  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52  */
53 public class ProjectInserter implements FileScannerListener, Runnable {
54
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
63         public void addInsertListener(InsertListener insertListener) {
64                 insertListeners.add(insertListener);
65         }
66
67         public void removeInsertListener(InsertListener insertListener) {
68                 insertListeners.remove(insertListener);
69         }
70
71         protected void fireProjectInsertStarted() {
72                 for (InsertListener insertListener: insertListeners) {
73                         insertListener.projectInsertStarted(project);
74                 }
75         }
76         
77         protected void fireProjectURIGenerated(String uri) {
78                 for (InsertListener insertListener: insertListeners) {
79                         insertListener.projectURIGenerated(project, uri);
80                 }
81         }
82
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);
86                 }
87         }
88
89         protected void fireProjectInsertFinished(boolean success, Throwable cause) {
90                 for (InsertListener insertListener: insertListeners) {
91                         insertListener.projectInsertFinished(project, success, cause);
92                 }
93         }
94
95         /**
96          * @param debug
97          *            The debug to set.
98          */
99         public void setDebug(boolean debug) {
100                 this.debug = debug;
101         }
102
103         /**
104          * @param project
105          *            The project to set.
106          */
107         public void setProject(Project project) {
108                 this.project = project;
109         }
110
111         /**
112          * @param freenetInterface
113          *            The freenetInterface to set.
114          */
115         public void setFreenetInterface(Freenet7Interface freenetInterface) {
116                 this.freenetInterface = freenetInterface;
117         }
118
119         public void start() {
120                 fileScanner = new FileScanner(project);
121                 fileScanner.addFileScannerListener(this);
122                 new Thread(fileScanner).start();
123         }
124
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);
130                 }
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);
134                 try {
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));
140                         }
141                         StreamCopier.copy(fileInput, outputStream, length[0]);
142                 } finally {
143                         Closer.close(fileInput);
144                         Closer.close(outputStream);
145                         Closer.close(filteredByteOutputStream);
146                 }
147                 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
148                 length[0] = filteredBytes.length;
149                 return new ByteArrayInputStream(filteredBytes);
150         }
151
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);
157                 try {
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);
164                                         try {
165                                                 zipOutputStream.putNextEntry(zipEntry);
166                                                 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
167                                         } finally {
168                                                 zipOutputStream.closeEntry();
169                                                 wrappedInputStream.close();
170                                         }
171                                 }
172                         }
173                 } finally {     
174                         zipOutputStream.closeEntry();
175                         Closer.close(zipOutputStream);
176                         Closer.close(fileOutputStream);
177                 }
178
179                 containerLength[0] = tempFile.length();
180                 return new FileInputStream(tempFile);
181         }
182
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());
188                         try {
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) {
193                         }
194                 } else {
195                         if (fileOption.isInsert()) {
196                                 try {
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) {
201                                 }
202                         } else {
203                                 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
204                         }
205                 }
206                 return fileEntry;
207         }
208
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);
219                                 }
220                                 containerFiles.get(containerName).add(filename);
221                                 files.remove(filename);
222                         }
223                 }
224         }
225
226         /**
227          * {@inheritDoc}
228          */
229         public void run() {
230                 fireProjectInsertStarted();
231                 List<String> files = fileScanner.getFiles();
232
233                 /* create connection to node */
234                 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
235                 boolean connected = false;
236                 Throwable cause = null;
237                 try {
238                         connected = connection.connect();
239                 } catch (IOException e1) {
240                         cause = e1;
241                 }
242                 
243                 if (!connected) {
244                         fireProjectInsertFinished(false, cause);
245                         return;
246                 }
247                 
248                 Client client = new Client(connection);
249
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);
254
255                 /* collect files */
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);
266                         }
267                 }
268
269                 /* start request */
270                 try {
271                         client.execute(putDir);
272                 } catch (IOException ioe1) {
273                         fireProjectInsertFinished(false, ioe1);
274                         return;
275                 }
276
277                 /* parse progress and success messages */
278                 String finalURI = null;
279                 boolean success = false;
280                 boolean finished = false;
281                 boolean disconnected = false;
282                 while (!finished) {
283                         Message message = client.readMessage();
284                         finished = (message == null) || (disconnected = client.isDisconnected());
285                         if (debug) {
286                                 System.out.println(message);
287                         }
288                         if (!finished) {
289                                 String messageName = message.getName();
290                                 if ("URIGenerated".equals(messageName)) {
291                                         finalURI = message.get("URI");
292                                         fireProjectURIGenerated(finalURI);
293                                 }
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);
301                                 }
302                                 success = "PutSuccessful".equals(messageName);
303                                 finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
304                         }
305                 }
306
307                 /* post-insert work */
308                 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
309                 if (success) {
310                         String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
311                         int newEdition = Integer.parseInt(editionPart);
312                         project.setEdition(newEdition);
313                 }
314         }
315
316         //
317         // INTERFACE FileScannerListener
318         //
319
320         /**
321          * {@inheritDoc}
322          */
323         public void fileScannerFinished(FileScanner fileScanner) {
324                 if (!fileScanner.isError()) {
325                         new Thread(this).start();
326                 } else {
327                         fireProjectInsertFinished(false, null);
328                 }
329                 fileScanner.removeFileScannerListener(this);
330         }
331
332 }