2 * jSite - ClientPutComplexDir.java - Copyright © 2006–2012 David Roden
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.
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.
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.
19 package de.todesbaum.util.freenet.fcp2;
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;
31 import net.pterodactylus.util.io.Closer;
34 * Implementation of the <code>ClientPutComplexDir</code> command. This command
35 * can be used to insert directories that do not exist on disk.
37 * @author David Roden <droden@gmail.com>
40 public class ClientPutComplexDir extends ClientPutDir<ClientPutComplexDir> {
42 /** The file entries of this directory. */
43 private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
45 /** Whether this request has payload. */
46 private boolean hasPayload = false;
48 /** The input streams for the payload. */
49 private File payloadFile;
51 /** The total number of bytes of the payload. */
52 private long payloadLength = 0;
54 /** The temp directory to use. */
55 private final String tempDirectory;
58 * Creates a new <code>ClientPutComplexDir</code> command with the specified
62 * The identifier of the command
64 * The URI of the command
66 public ClientPutComplexDir(String identifier, String uri) {
67 this(identifier, uri, null);
71 * Creates a new <code>ClientPutComplexDir</code> command with the specified
75 * The identifier of the command
77 * The URI of the command
78 * @param tempDirectory
79 * The temp directory to use, or {@code null} to use the default
82 public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
83 super("ClientPutComplexDir", identifier, uri);
84 this.tempDirectory = tempDirectory;
88 * Adds a file to the directory inserted by this request.
91 * The file entry to add to the directory
93 * if an I/O error occurs when creating the payload stream
95 public void addFileEntry(FileEntry fileEntry) throws IOException {
96 if (fileEntry instanceof DirectFileEntry) {
97 if (payloadFile == null) {
99 payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
100 payloadFile.deleteOnExit();
101 } catch (IOException e) {
105 if (payloadFile != null) {
106 InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
107 FileOutputStream payloadOutputStream = null;
109 payloadOutputStream = new FileOutputStream(payloadFile, true);
110 byte[] buffer = new byte[65536];
112 while ((read = payloadInputStream.read(buffer)) != -1) {
113 payloadOutputStream.write(buffer, 0, read);
115 payloadOutputStream.flush();
116 fileEntries.add(fileEntry);
117 } catch (IOException ioe1) {
118 payloadFile.delete();
121 Closer.close(payloadOutputStream);
122 Closer.close(payloadInputStream);
126 fileEntries.add(fileEntry);
134 protected void write(Writer writer) throws IOException {
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);
142 writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
143 if (fileEntry instanceof DirectFileEntry) {
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);
160 protected boolean hasPayload() {
168 protected long getPayloadLength() {
169 return payloadLength;
176 protected InputStream getPayload() {
177 if (payloadFile != null) {
179 return new FileInputStream(payloadFile);
180 } catch (FileNotFoundException e) {
181 /* shouldn't occur. */