version 0.4.3, fix forward edition numbers
[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.ReplacingOutputStream;
47 import de.todesbaum.util.io.StreamCopier;
48
49 /**
50  * @author David Roden <droden@gmail.com>
51  * @version $Id: ProjectInserter.java 440 2006-03-30 09:31:25Z bombe $
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         private int maxRetries = 99999;
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 fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
79                 for (InsertListener insertListener: insertListeners) {
80                         insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
81                 }
82         }
83
84         protected void fireProjectInsertFinished(boolean success, Throwable cause) {
85                 for (InsertListener insertListener: insertListeners) {
86                         insertListener.projectInsertFinished(project, success, cause);
87                 }
88         }
89
90         /**
91          * @param debug
92          *            The debug to set.
93          */
94         public void setDebug(boolean debug) {
95                 this.debug = debug;
96         }
97
98         /**
99          * @param project
100          *            The project to set.
101          */
102         public void setProject(Project project) {
103                 this.project = project;
104         }
105
106         /**
107          * @param freenetInterface
108          *            The freenetInterface to set.
109          */
110         public void setFreenetInterface(Freenet7Interface freenetInterface) {
111                 this.freenetInterface = freenetInterface;
112         }
113
114         /**
115          * @param maxRetries
116          *            The maxRetries to set.
117          */
118         public void setMaxRetries(int maxRetries) {
119                 this.maxRetries = maxRetries;
120         }
121
122         public void start() {
123                 fileScanner = new FileScanner(project);
124                 fileScanner.addFileScannerListener(this);
125                 new Thread(fileScanner).start();
126         }
127
128         private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
129                 File file = new File(project.getLocalPath(), filename);
130                 length[0] = file.length();
131                 if (!fileOption.getReplaceEdition()) {
132                         return new FileInputStream(file);
133                 }
134                 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
135                 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
136                 FileInputStream fileInput = new FileInputStream(file);
137                 outputStream.addReplacement("$[CONTAINER]", "/");
138                 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
139                 outputStream.addReplacement("$[URI]", project.getFinalURI(0));
140                 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
141                         outputStream.addReplacement("$[URI+" + index + "]", project.getFinalURI(index));
142                         outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
143                 }
144                 StreamCopier.copy(fileInput, outputStream, length[0]);
145                 outputStream.close();
146                 filteredByteOutputStream.close();
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                 for (String filename: containerFiles.get(containerName)) {
158                         File dataFile = new File(project.getLocalPath(), filename);
159                         if (dataFile.exists()) {
160                                 ZipEntry zipEntry = new ZipEntry(filename);
161                                 long[] fileLength = new long[1];
162                                 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
163                                 zipOutputStream.putNextEntry(zipEntry);
164                                 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
165                                 zipOutputStream.closeEntry();
166                                 wrappedInputStream.close();
167                         }
168                 }
169                 zipOutputStream.closeEntry();
170
171                 /* FIXME - create metadata */
172                 // ZipEntry metadataEntry = new ZipEntry("metadata");
173                 // zipOutputStream.putNextEntry(metadataEntry);
174                 // Metadata zipMetadata = new Metadata();
175                 // for (String filename: containerFiles.get(containerName)) {
176                 // if (new File(project.getLocalPath(), filename).exists()) {
177                 // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
178                 // zipEntryMetadata.setName(filename);
179                 // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
180                 // zipMetadata.addDocument(zipEntryMetadata);
181                 // }
182                 // }
183                 // zipOutputStream.write(zipMetadata.toByteArray());
184                 // zipOutputStream.closeEntry();
185                 zipOutputStream.close();
186
187                 containerLength[0] = tempFile.length();
188                 return new FileInputStream(tempFile);
189         }
190
191         private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
192                 FileEntry fileEntry = null;
193                 FileOption fileOption = project.getFileOption(filename);
194                 if (filename.startsWith("/container/:")) {
195                         String containerName = filename.substring("/container/:".length());
196                         try {
197                                 long[] containerLength = new long[1];
198                                 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
199                                 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
200                         } catch (IOException ioe1) {
201                         }
202                 } else {
203                         if (fileOption.isInsert()) {
204                                 try {
205                                         long[] fileLength = new long[1];
206                                         InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
207                                         fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
208                                 } catch (IOException ioe1) {
209                                 }
210                         } else {
211                                 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
212                         }
213                 }
214                 return fileEntry;
215         }
216
217         private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
218                 for (String filename: new ArrayList<String>(files)) {
219                         FileOption fileOption = project.getFileOption(filename);
220                         String containerName = fileOption.getContainer();
221                         if (!containerName.equals("")) {
222                                 if (!containers.contains(containerName)) {
223                                         containers.add(containerName);
224                                         containerFiles.put(containerName, new ArrayList<String>());
225                                         /* hmm. looks like a hack to me. */
226                                         files.add("/container/:" + containerName);
227                                 }
228                                 containerFiles.get(containerName).add(filename);
229                                 files.remove(filename);
230                         }
231                 }
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         public void run() {
238                 fireProjectInsertStarted();
239                 List<String> files = fileScanner.getFiles();
240
241                 /* create connection to node */
242                 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
243                 try {
244                         connection.connect();
245                 } catch (IOException e1) {
246                         fireProjectInsertFinished(false, e1);
247                         return;
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 = ((EditionProject) project).getEdition();
258                 String dirURI = 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(maxRetries);
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                 boolean success = true;
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 ("SimpleProgress".equals(messageName)) {
291                                         int total = Integer.parseInt(message.get("Total"));
292                                         int succeeded = Integer.parseInt(message.get("Succeeded"));
293                                         int fatal = Integer.parseInt(message.get("FatallyFailed"));
294                                         int failed = Integer.parseInt(message.get("Failed"));
295                                         boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
296                                         fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
297                                 }
298                                 success = "PutSuccessful".equals(messageName);
299                                 finished = success || "PutFailed".equals(messageName);
300                         }
301                 }
302
303                 /* post-insert work */
304                 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
305                 if (success) {
306                         if (project instanceof EditionProject) {
307                                 ((EditionProject) project).setEdition(edition + 1);
308                         }
309                 }
310         }
311
312         //
313         // INTERFACE FileScannerListener
314         //
315
316         /**
317          * {@inheritDoc}
318          */
319         public void fileScannerFinished(FileScanner fileScanner) {
320                 if (!fileScanner.isError()) {
321                         new Thread(this).start();
322                 } else {
323                         fireProjectInsertFinished(false, null);
324                 }
325                 fileScanner.removeFileScannerListener(this);
326         }
327
328 }