2c23f878e057ef8a97eed9d62909e580af9366d3
[jSite.git] / src / main / java / de / todesbaum / util / freenet / fcp2 / ClientPutComplexDir.java
1 /*
2  * jSite - ClientPutComplexDir.java - Copyright © 2006–2012 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.util.freenet.fcp2;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import net.pterodactylus.util.io.Closer;
32
33 /**
34  * Implementation of the <code>ClientPutComplexDir</code> command. This command
35  * can be used to insert directories that do not exist on disk.
36  *
37  * @author David Roden &lt;droden@gmail.com&gt;
38  * @version $Id$
39  */
40 public class ClientPutComplexDir extends ClientPutDir<ClientPutComplexDir> {
41
42         /** The file entries of this directory. */
43         private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
44
45         /** Whether this request has payload. */
46         private boolean hasPayload = false;
47
48         /** The input streams for the payload. */
49         private File payloadFile;
50
51         /** The total number of bytes of the payload. */
52         private long payloadLength = 0;
53
54         /** The temp directory to use. */
55         private final String tempDirectory;
56
57         /**
58          * Creates a new <code>ClientPutComplexDir</code> command with the specified
59          * identifier and URI.
60          *
61          * @param identifier
62          *            The identifier of the command
63          * @param uri
64          *            The URI of the command
65          */
66         public ClientPutComplexDir(String identifier, String uri) {
67                 this(identifier, uri, null);
68         }
69
70         /**
71          * Creates a new <code>ClientPutComplexDir</code> command with the specified
72          * identifier and URI.
73          *
74          * @param identifier
75          *            The identifier of the command
76          * @param uri
77          *            The URI of the command
78          * @param tempDirectory
79          *            The temp directory to use, or {@code null} to use the default
80          *            temp directory
81          */
82         public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
83                 super("ClientPutComplexDir", identifier, uri);
84                 this.tempDirectory = tempDirectory;
85         }
86
87         /**
88          * Adds a file to the directory inserted by this request.
89          *
90          * @param fileEntry
91          *            The file entry to add to the directory
92          * @throws IOException
93          *             if an I/O error occurs when creating the payload stream
94          */
95         public void addFileEntry(FileEntry fileEntry) throws IOException {
96                 if (fileEntry instanceof DirectFileEntry) {
97                         if (payloadFile == null) {
98                                 try {
99                                         payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
100                                         payloadFile.deleteOnExit();
101                                 } catch (IOException e) {
102                                         /* ignore. */
103                                 }
104                         }
105                         if (payloadFile != null) {
106                                 InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
107                                 FileOutputStream payloadOutputStream = null;
108                                 try {
109                                         payloadOutputStream = new FileOutputStream(payloadFile, true);
110                                         byte[] buffer = new byte[65536];
111                                         int read = 0;
112                                         while ((read = payloadInputStream.read(buffer)) != -1) {
113                                                 payloadOutputStream.write(buffer, 0, read);
114                                         }
115                                         payloadOutputStream.flush();
116                                         fileEntries.add(fileEntry);
117                                 } catch (IOException ioe1) {
118                                         payloadFile.delete();
119                                         throw ioe1;
120                                 } finally {
121                                         Closer.close(payloadOutputStream);
122                                         Closer.close(payloadInputStream);
123                                 }
124                         }
125                 } else {
126                         fileEntries.add(fileEntry);
127                 }
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         protected void write(Writer writer) throws IOException {
135                 super.write(writer);
136                 int fileIndex = 0;
137                 for (FileEntry fileEntry : fileEntries) {
138                         writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
139                         if (fileEntry.getContentType() != null) {
140                                 writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);
141                         }
142                         writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
143                         if (fileEntry instanceof DirectFileEntry) {
144                                 hasPayload = true;
145                                 writer.write("Files." + fileIndex + ".DataLength=" + ((DirectFileEntry) fileEntry).getDataLength() + LINEFEED);
146                                 payloadLength += ((DirectFileEntry) fileEntry).getDataLength();
147                         } else if (fileEntry instanceof DiskFileEntry) {
148                                 writer.write("Files." + fileIndex + ".Filename=" + ((DiskFileEntry) fileEntry).getFilename() + LINEFEED);
149                         } else if (fileEntry instanceof RedirectFileEntry) {
150                                 writer.write("Files." + fileIndex + ".TargetURI=" + ((RedirectFileEntry) fileEntry).getTargetURI() + LINEFEED);
151                         }
152                         fileIndex++;
153                 }
154         }
155
156         /**
157          * {@inheritDoc}
158          */
159         @Override
160         protected boolean hasPayload() {
161                 return hasPayload;
162         }
163
164         /**
165          * {@inheritDoc}
166          */
167         @Override
168         protected long getPayloadLength() {
169                 return payloadLength;
170         }
171
172         /**
173          * {@inheritDoc}
174          */
175         @Override
176         protected InputStream getPayload() {
177                 if (payloadFile != null) {
178                         try {
179                                 return new FileInputStream(payloadFile);
180                         } catch (FileNotFoundException e) {
181                                 /* shouldn't occur. */
182                         }
183                 }
184                 return null;
185         }
186
187 }