ced300820a461372644e51e8ae84495eedba7403
[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$
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 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         /**
121          * @param maxRetries
122          *            The maxRetries to set.
123          */
124         public void setMaxRetries(int maxRetries) {
125                 this.maxRetries = maxRetries;
126         }
127
128         public void start() {
129                 fileScanner = new FileScanner(project);
130                 fileScanner.addFileScannerListener(this);
131                 new Thread(fileScanner).start();
132         }
133
134         private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
135                 File file = new File(project.getLocalPath(), filename);
136                 length[0] = file.length();
137                 if (!fileOption.getReplaceEdition()) {
138                         return new FileInputStream(file);
139                 }
140                 ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
141                 ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
142                 FileInputStream fileInput = new FileInputStream(file);
143                 outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
144                 outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
145                 for (int index = 1; index <= fileOption.getEditionRange(); index++) {
146                         outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
147                         outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
148                 }
149                 StreamCopier.copy(fileInput, outputStream, length[0]);
150                 outputStream.close();
151                 filteredByteOutputStream.close();
152                 byte[] filteredBytes = filteredByteOutputStream.toByteArray();
153                 length[0] = filteredBytes.length;
154                 return new ByteArrayInputStream(filteredBytes);
155         }
156
157         private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
158                 File tempFile = File.createTempFile("jsite", ".zip");
159                 tempFile.deleteOnExit();
160                 FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
161                 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
162                 for (String filename: containerFiles.get(containerName)) {
163                         File dataFile = new File(project.getLocalPath(), filename);
164                         if (dataFile.exists()) {
165                                 ZipEntry zipEntry = new ZipEntry(filename);
166                                 long[] fileLength = new long[1];
167                                 InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
168                                 zipOutputStream.putNextEntry(zipEntry);
169                                 StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
170                                 zipOutputStream.closeEntry();
171                                 wrappedInputStream.close();
172                         }
173                 }
174                 zipOutputStream.closeEntry();
175
176                 /* FIXME - create metadata */
177                 // ZipEntry metadataEntry = new ZipEntry("metadata");
178                 // zipOutputStream.putNextEntry(metadataEntry);
179                 // Metadata zipMetadata = new Metadata();
180                 // for (String filename: containerFiles.get(containerName)) {
181                 // if (new File(project.getLocalPath(), filename).exists()) {
182                 // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
183                 // zipEntryMetadata.setName(filename);
184                 // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
185                 // zipMetadata.addDocument(zipEntryMetadata);
186                 // }
187                 // }
188                 // zipOutputStream.write(zipMetadata.toByteArray());
189                 // zipOutputStream.closeEntry();
190                 zipOutputStream.close();
191
192                 containerLength[0] = tempFile.length();
193                 return new FileInputStream(tempFile);
194         }
195
196         private FileEntry createFileEntry(String filename, int edition, Map<String, List<String>> containerFiles) {
197                 FileEntry fileEntry = null;
198                 FileOption fileOption = project.getFileOption(filename);
199                 if (filename.startsWith("/container/:")) {
200                         String containerName = filename.substring("/container/:".length());
201                         try {
202                                 long[] containerLength = new long[1];
203                                 InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength);
204                                 fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]);
205                         } catch (IOException ioe1) {
206                         }
207                 } else {
208                         if (fileOption.isInsert()) {
209                                 try {
210                                         long[] fileLength = new long[1];
211                                         InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
212                                         fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]);
213                                 } catch (IOException ioe1) {
214                                 }
215                         } else {
216                                 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
217                         }
218                 }
219                 return fileEntry;
220         }
221
222         private void createContainers(List<String> files, List<String> containers, Map<String, List<String>> containerFiles) {
223                 for (String filename: new ArrayList<String>(files)) {
224                         FileOption fileOption = project.getFileOption(filename);
225                         String containerName = fileOption.getContainer();
226                         if (!containerName.equals("")) {
227                                 if (!containers.contains(containerName)) {
228                                         containers.add(containerName);
229                                         containerFiles.put(containerName, new ArrayList<String>());
230                                         /* hmm. looks like a hack to me. */
231                                         files.add("/container/:" + containerName);
232                                 }
233                                 containerFiles.get(containerName).add(filename);
234                                 files.remove(filename);
235                         }
236                 }
237         }
238
239         /**
240          * {@inheritDoc}
241          */
242         public void run() {
243                 fireProjectInsertStarted();
244                 List<String> files = fileScanner.getFiles();
245
246                 /* create connection to node */
247                 Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
248                 boolean connected = false;
249                 Throwable cause = null;
250                 try {
251                         connected = connection.connect();
252                 } catch (IOException e1) {
253                         cause = e1;
254                 }
255                 
256                 if (!connected) {
257                         fireProjectInsertFinished(false, cause);
258                         return;
259                 }
260                 
261                 Client client = new Client(connection);
262
263                 /* create containers */
264                 final List<String> containers = new ArrayList<String>();
265                 final Map<String, List<String>> containerFiles = new HashMap<String, List<String>>();
266                 createContainers(files, containers, containerFiles);
267
268                 /* collect files */
269                 int edition = ((EditionProject) project).getEdition();
270                 String dirURI = "freenet:USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
271                 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
272                 putDir.setDefaultName(project.getIndexFile());
273                 putDir.setVerbosity(Verbosity.ALL);
274                 putDir.setMaxRetries(maxRetries);
275                 for (String filename: files) {
276                         FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
277                         if (fileEntry != null) {
278                                 putDir.addFileEntry(fileEntry);
279                         }
280                 }
281
282                 /* start request */
283                 try {
284                         client.execute(putDir);
285                 } catch (IOException ioe1) {
286                         fireProjectInsertFinished(false, ioe1);
287                         return;
288                 }
289
290                 /* parse progress and success messages */
291                 boolean success = false;
292                 boolean finished = false;
293                 boolean disconnected = false;
294                 while (!finished) {
295                         Message message = client.readMessage();
296                         finished = (message == null) || (disconnected = client.isDisconnected());
297                         if (debug) {
298                                 System.out.println(message);
299                         }
300                         if (!finished) {
301                                 String messageName = message.getName();
302                                 if ("URIGenerated".equals(messageName)) {
303                                         fireProjectURIGenerated(message.get("URI"));
304                                 }
305                                 if ("SimpleProgress".equals(messageName)) {
306                                         int total = Integer.parseInt(message.get("Total"));
307                                         int succeeded = Integer.parseInt(message.get("Succeeded"));
308                                         int fatal = Integer.parseInt(message.get("FatallyFailed"));
309                                         int failed = Integer.parseInt(message.get("Failed"));
310                                         boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
311                                         fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
312                                 }
313                                 success = "PutSuccessful".equals(messageName);
314                                 finished = success || "PutFailed".equals(messageName);
315                         }
316                 }
317
318                 /* post-insert work */
319                 fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
320                 if (success) {
321                         if (project instanceof EditionProject) {
322                                 ((EditionProject) project).setEdition(edition + 1);
323                         }
324                 }
325         }
326
327         //
328         // INTERFACE FileScannerListener
329         //
330
331         /**
332          * {@inheritDoc}
333          */
334         public void fileScannerFinished(FileScanner fileScanner) {
335                 if (!fileScanner.isError()) {
336                         new Thread(this).start();
337                 } else {
338                         fireProjectInsertFinished(false, null);
339                 }
340                 fileScanner.removeFileScannerListener(this);
341         }
342
343 }