any kind of error will abort the insert
[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 Roden <droden@gmail.com>
52  * @version $Id$
53  */
54 public class ProjectInserter implements FileScannerListener, Runnable {
55
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();
63
64         public void addInsertListener(InsertListener insertListener) {
65                 insertListeners.add(insertListener);
66         }
67
68         public void removeInsertListener(InsertListener insertListener) {
69                 insertListeners.remove(insertListener);
70         }
71
72         protected void fireProjectInsertStarted() {
73                 for (InsertListener insertListener: insertListeners) {
74                         insertListener.projectInsertStarted(project);
75                 }
76         }
77         
78         protected void fireProjectURIGenerated(String uri) {
79                 for (InsertListener insertListener: insertListeners) {
80                         insertListener.projectURIGenerated(project, uri);
81                 }
82         }
83
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);
87                 }
88         }
89
90         protected void fireProjectInsertFinished(boolean success, Throwable cause) {
91                 for (InsertListener insertListener: insertListeners) {
92                         insertListener.projectInsertFinished(project, success, cause);
93                 }
94         }
95
96         /**
97          * @param debug
98          *            The debug to set.
99          */
100         public void setDebug(boolean debug) {
101                 this.debug = debug;
102         }
103
104         /**
105          * @param project
106          *            The project to set.
107          */
108         public void setProject(Project project) {
109                 this.project = project;
110         }
111
112         /**
113          * @param freenetInterface
114          *            The freenetInterface to set.
115          */
116         public void setFreenetInterface(Freenet7Interface freenetInterface) {
117                 this.freenetInterface = freenetInterface;
118         }
119
120         public void start() {
121                 fileScanner = new FileScanner(project);
122                 fileScanner.addFileScannerListener(this);
123                 new Thread(fileScanner).start();
124         }
125
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);
131                 }
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);
135                 try {
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));
141                         }
142                         StreamCopier.copy(fileInput, outputStream, length[0]);
143                 } finally {
144                         Closer.close(fileInput);
145                         Closer.close(outputStream);
146                         Closer.close(filteredByteOutputStream);
147                 }
148                 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
149                 length[0] = filteredBytes.length;
150                 return new ByteArrayInputStream(filteredBytes);
151         }
152
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);
158                 try {
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);
165                                         try {
166                                                 zipOutputStream.putNextEntry(zipEntry);
167                                                 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
168                                         } finally {
169                                                 zipOutputStream.closeEntry();
170                                                 wrappedInputStream.close();
171                                         }
172                                 }
173                         }
174                 } finally {     
175                         zipOutputStream.closeEntry();
176                         Closer.close(zipOutputStream);
177                         Closer.close(fileOutputStream);
178                 }
179
180                 containerLength[0] = tempFile.length();
181                 return new FileInputStream(tempFile);
182         }
183
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());
189                         try {
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) {
194                         }
195                 } else {
196                         if (fileOption.isInsert()) {
197                                 try {
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) {
202                                 }
203                         } else {
204                                 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
205                         }
206                 }
207                 return fileEntry;
208         }
209
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);
220                                 }
221                                 containerFiles.get(containerName).add(filename);
222                                 files.remove(filename);
223                         }
224                 }
225         }
226
227         /**
228          * {@inheritDoc}
229          */
230         public void run() {
231                 fireProjectInsertStarted();
232                 List<String> files = fileScanner.getFiles();
233
234                 /* create connection to node */
235                 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
236                 boolean connected = false;
237                 Throwable cause = null;
238                 try {
239                         connected = connection.connect();
240                 } catch (IOException e1) {
241                         cause = e1;
242                 }
243                 
244                 if (!connected) {
245                         fireProjectInsertFinished(false, cause);
246                         return;
247                 }
248                 
249                 Client client = new Client(connection);
250
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);
255
256                 /* collect files */
257                 int edition = project.getEdition();
258                 String dirURI = "freenet: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);
267                         }
268                 }
269
270                 /* start request */
271                 try {
272                         client.execute(putDir);
273                 } catch (IOException ioe1) {
274                         fireProjectInsertFinished(false, ioe1);
275                         return;
276                 }
277
278                 /* parse progress and success messages */
279                 String finalURI = null;
280                 boolean success = false;
281                 boolean finished = false;
282                 boolean disconnected = false;
283                 while (!finished) {
284                         Message message = client.readMessage();
285                         finished = (message == null) || (disconnected = client.isDisconnected());
286                         if (debug) {
287                                 System.out.println(message);
288                         }
289                         if (!finished) {
290                                 String messageName = message.getName();
291                                 if ("URIGenerated".equals(messageName)) {
292                                         finalURI = message.get("URI");
293                                         fireProjectURIGenerated(finalURI);
294                                 }
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);
302                                 }
303                                 success = "PutSuccessful".equals(messageName);
304                                 finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
305                         }
306                 }
307
308                 /* post-insert work */
309                 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
310                 if (success) {
311                         String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
312                         int newEdition = Integer.parseInt(editionPart);
313                         project.setEdition(newEdition);
314                 }
315         }
316
317         //
318         // INTERFACE FileScannerListener
319         //
320
321         /**
322          * {@inheritDoc}
323          */
324         public void fileScannerFinished(FileScanner fileScanner) {
325                 if (!fileScanner.isError()) {
326                         new Thread(this).start();
327                 } else {
328                         fireProjectInsertFinished(false, null);
329                 }
330                 fileScanner.removeFileScannerListener(this);
331         }
332
333 }