Whitespace and formatting fixes.
[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 command
36  * 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
60          * identifier and URI.
61          *
62          * @param identifier
63          *            The identifier of the command
64          * @param uri
65          *            The URI of the command
66          */
67         public ClientPutComplexDir(String identifier, String uri) {
68                 this(identifier, uri, null);
69         }
70
71         /**
72          * Creates a new <code>ClientPutComplexDir</code> command with the specified
73          * identifier and URI.
74          *
75          * @param identifier
76          *            The identifier of the command
77          * @param uri
78          *            The URI of the command
79          * @param tempDirectory
80          *            The temp directory to use, or {@code null} to use the default
81          *            temp directory
82          */
83         public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
84                 super("ClientPutComplexDir", identifier, uri);
85                 this.tempDirectory = tempDirectory;
86         }
87
88         /**
89          * Adds a file to the directory inserted by this request.
90          *
91          * @param fileEntry
92          *            The file entry to add to the directory
93          */
94         public void addFileEntry(FileEntry fileEntry) {
95                 if (fileEntry instanceof DirectFileEntry) {
96                         if (payloadFile == null) {
97                                 try {
98                                         payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
99                                         payloadFile.deleteOnExit();
100                                 } catch (IOException e) {
101                                         /* ignore. */
102                                 }
103                         }
104                         if (payloadFile != null) {
105                                 InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
106                                 FileOutputStream payloadOutputStream = null;
107                                 try {
108                                         payloadOutputStream = new FileOutputStream(payloadFile, true);
109                                         byte[] buffer = new byte[65536];
110                                         int read = 0;
111                                         while ((read = payloadInputStream.read(buffer)) != -1) {
112                                                 payloadOutputStream.write(buffer, 0, read);
113                                         }
114                                         payloadOutputStream.flush();
115                                         fileEntries.add(fileEntry);
116                                 } catch (IOException ioe1) {
117                                         /* hmm, ignore? */
118                                 } finally {
119                                         Closer.close(payloadOutputStream);
120                                         Closer.close(payloadInputStream);
121                                 }
122                         }
123                 } else {
124                         fileEntries.add(fileEntry);
125                 }
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         @Override
132         protected void write(Writer writer) throws IOException {
133                 super.write(writer);
134                 int fileIndex = 0;
135                 for (FileEntry fileEntry : fileEntries) {
136                         writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
137                         if (fileEntry.getContentType() != null) {
138                                 writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);
139                         }
140                         writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
141                         if (fileEntry instanceof DirectFileEntry) {
142                                 hasPayload = true;
143                                 writer.write("Files." + fileIndex + ".DataLength=" + ((DirectFileEntry) fileEntry).getDataLength() + LINEFEED);
144                                 payloadLength += ((DirectFileEntry) fileEntry).getDataLength();
145                         } else if (fileEntry instanceof DiskFileEntry) {
146                                 writer.write("Files." + fileIndex + ".Filename=" + ((DiskFileEntry) fileEntry).getFilename() + LINEFEED);
147                         } else if (fileEntry instanceof RedirectFileEntry) {
148                                 writer.write("Files." + fileIndex + ".TargetURI=" + ((RedirectFileEntry) fileEntry).getTargetURI() + LINEFEED);
149                         }
150                         fileIndex++;
151                 }
152         }
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         protected boolean hasPayload() {
159                 return hasPayload;
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         protected long getPayloadLength() {
167                 return payloadLength;
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         protected InputStream getPayload() {
175                 if (payloadFile != null) {
176                         try {
177                                 return new FileInputStream(payloadFile);
178                         } catch (FileNotFoundException e) {
179                                 /* shouldn't occur. */
180                         }
181                 }
182                 return null;
183         }
184
185 }