1f6a43b7db6e51fcb0e32d7001b9acc49bef8245
[jSite.git] / src / de / todesbaum / util / freenet / fcp2 / ClientPutComplexDir.java
1 /*
2  * todesbaum-lib -
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.util.freenet.fcp2;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.Writer;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import de.todesbaum.util.io.Closer;
33
34 /**
35  * Implementation of the <code>ClientPutComplexDir</code> command. This
36  * command can be used to insert directories that do not exist on disk.
37  *
38  * @author David Roden &lt;droden@gmail.com&gt;
39  * @version $Id$
40  */
41 public class ClientPutComplexDir extends ClientPutDir {
42
43         /** The file entries of this directory. */
44         private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
45
46         /** Whether this request has payload. */
47         private boolean hasPayload = false;
48
49         /** The input streams for the payload. */
50         private File payloadFile;
51
52         /** The total number of bytes of the payload. */
53         private long payloadLength = 0;
54
55         /** The temp directory to use. */
56         private final String tempDirectory;
57
58         /**
59          * Creates a new <code>ClientPutComplexDir</code> command with the specified identifier and URI.
60          * @param identifier The identifier of the command
61          * @param uri The URI of the command
62          */
63         public ClientPutComplexDir(String identifier, String uri) {
64                 this(identifier, uri, null);
65         }
66
67         /**
68          * Creates a new <code>ClientPutComplexDir</code> command with the specified
69          * identifier and URI.
70          *
71          * @param identifier
72          *            The identifier of the command
73          * @param uri
74          *            The URI of the command
75          * @param tempDirectory
76          *            The temp directory to use, or {@code null} to use the default
77          *            temp directory
78          */
79         public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
80                 super("ClientPutComplexDir", identifier, uri);
81                 this.tempDirectory = tempDirectory;
82         }
83
84         /**
85          * Adds a file to the directory inserted by this request.
86          * @param fileEntry The file entry to add to the directory
87          */
88         public void addFileEntry(FileEntry fileEntry) {
89                 if (fileEntry instanceof DirectFileEntry) {
90                         if (payloadFile == null){
91                                 try {
92                                         payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
93                                         payloadFile.deleteOnExit();
94                                 } catch (IOException e) {
95                                 }
96                         }
97                         if (payloadFile != null) {
98                                 InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
99                                 FileOutputStream payloadOutputStream = null;
100                                 try {
101                                         payloadOutputStream = new FileOutputStream(payloadFile, true);
102                                         byte[] buffer = new byte[65536];
103                                         int read = 0;
104                                         while ((read = payloadInputStream.read(buffer)) != -1) {
105                                                 payloadOutputStream.write(buffer, 0, read);
106                                         }
107                                         payloadOutputStream.flush();
108                                         fileEntries.add(fileEntry);
109                                 } catch (IOException ioe1) {
110                                         /* hmm, ignore? */
111                                 } finally {
112                                         Closer.close(payloadOutputStream);
113                                         Closer.close(payloadInputStream);
114                                 }
115                         }
116                 } else {
117                         fileEntries.add(fileEntry);
118                 }
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         protected void write(Writer writer) throws IOException {
126                 super.write(writer);
127                 int fileIndex = 0;
128                 for (FileEntry fileEntry: fileEntries) {
129                         writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
130                         if (fileEntry.getContentType() != null) {
131                                 writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);
132                         }
133                         writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
134                         if (fileEntry instanceof DirectFileEntry) {
135                                 hasPayload = true;
136                                 writer.write("Files." + fileIndex + ".DataLength=" + ((DirectFileEntry) fileEntry).getDataLength() + LINEFEED);
137                                 payloadLength += ((DirectFileEntry) fileEntry).getDataLength();
138                         } else if (fileEntry instanceof DiskFileEntry) {
139                                 writer.write("Files." + fileIndex + ".Filename=" + ((DiskFileEntry) fileEntry).getFilename() + LINEFEED);
140                         } else if (fileEntry instanceof RedirectFileEntry) {
141                                 writer.write("Files." + fileIndex + ".TargetURI=" + ((RedirectFileEntry) fileEntry).getTargetURI() + LINEFEED);
142                         }
143                         fileIndex++;
144                 }
145         }
146
147         /**
148          * {@inheritDoc}
149          */
150         @Override
151         protected boolean hasPayload() {
152                 return hasPayload;
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         @Override
159         protected long getPayloadLength() {
160                 return payloadLength;
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         @Override
167         protected InputStream getPayload() {
168                 if (payloadFile != null) {
169                         try {
170                                 return new FileInputStream(payloadFile);
171                         } catch (FileNotFoundException e) {
172                                 /* shouldn't occur. */
173                         }
174                 }
175                 return null;
176         }
177
178 }