3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.util.freenet.fcp2;
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;
32 import de.todesbaum.util.io.Closer;
35 * Implementation of the <code>ClientPutComplexDir</code> command. This command
36 * can be used to insert directories that do not exist on disk.
38 * @author David Roden <droden@gmail.com>
41 public class ClientPutComplexDir extends ClientPutDir {
43 /** The file entries of this directory. */
44 private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
46 /** Whether this request has payload. */
47 private boolean hasPayload = false;
49 /** The input streams for the payload. */
50 private File payloadFile;
52 /** The total number of bytes of the payload. */
53 private long payloadLength = 0;
55 /** The temp directory to use. */
56 private final String tempDirectory;
59 * Creates a new <code>ClientPutComplexDir</code> command with the specified
63 * The identifier of the command
65 * The URI of the command
67 public ClientPutComplexDir(String identifier, String uri) {
68 this(identifier, uri, null);
72 * Creates a new <code>ClientPutComplexDir</code> command with the specified
76 * The identifier of the command
78 * The URI of the command
79 * @param tempDirectory
80 * The temp directory to use, or {@code null} to use the default
83 public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
84 super("ClientPutComplexDir", identifier, uri);
85 this.tempDirectory = tempDirectory;
89 * Adds a file to the directory inserted by this request.
92 * The file entry to add to the directory
94 * if an I/O error occurs when creating the payload stream
96 public void addFileEntry(FileEntry fileEntry) throws IOException {
97 if (fileEntry instanceof DirectFileEntry) {
98 if (payloadFile == null) {
100 payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
101 payloadFile.deleteOnExit();
102 } catch (IOException e) {
106 if (payloadFile != null) {
107 InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
108 FileOutputStream payloadOutputStream = null;
110 payloadOutputStream = new FileOutputStream(payloadFile, true);
111 byte[] buffer = new byte[65536];
113 while ((read = payloadInputStream.read(buffer)) != -1) {
114 payloadOutputStream.write(buffer, 0, read);
116 payloadOutputStream.flush();
117 fileEntries.add(fileEntry);
118 } catch (IOException ioe1) {
119 payloadFile.delete();
122 Closer.close(payloadOutputStream);
123 Closer.close(payloadInputStream);
127 fileEntries.add(fileEntry);
135 protected void write(Writer writer) throws IOException {
138 for (FileEntry fileEntry : fileEntries) {
139 writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
140 if (fileEntry.getContentType() != null) {
141 writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);
143 writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
144 if (fileEntry instanceof DirectFileEntry) {
146 writer.write("Files." + fileIndex + ".DataLength=" + ((DirectFileEntry) fileEntry).getDataLength() + LINEFEED);
147 payloadLength += ((DirectFileEntry) fileEntry).getDataLength();
148 } else if (fileEntry instanceof DiskFileEntry) {
149 writer.write("Files." + fileIndex + ".Filename=" + ((DiskFileEntry) fileEntry).getFilename() + LINEFEED);
150 } else if (fileEntry instanceof RedirectFileEntry) {
151 writer.write("Files." + fileIndex + ".TargetURI=" + ((RedirectFileEntry) fileEntry).getTargetURI() + LINEFEED);
161 protected boolean hasPayload() {
169 protected long getPayloadLength() {
170 return payloadLength;
177 protected InputStream getPayload() {
178 if (payloadFile != null) {
180 return new FileInputStream(payloadFile);
181 } catch (FileNotFoundException e) {
182 /* shouldn't occur. */