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;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.SequenceInputStream;
25 import java.io.Writer;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Vector;
31 * Implementation of the <code>ClientPutComplexDir</code> command. This
32 * command can be used to insert directories that do not exist on disk.
34 * @author David Roden <droden@gmail.com>
37 public class ClientPutComplexDir extends ClientPutDir {
39 /** The file entries of this directory. */
40 private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
42 /** Whether this request has payload. */
43 private boolean hasPayload = false;
45 /** The input streams for the payload. */
46 private List<InputStream> payloadInputStreams = new ArrayList<InputStream>();
48 /** The total number of bytes of the payload. */
49 private long payloadLength = 0;
52 * Creates a new <code>ClientPutComplexDir</code> command with the specified identifier and URI.
53 * @param identifier The identifier of the command
54 * @param uri The URI of the command
56 public ClientPutComplexDir(String identifier, String uri) {
57 super("ClientPutComplexDir", identifier, uri);
61 * Adds a file to the directory inserted by this request.
62 * @param fileEntry The file entry to add to the directory
64 public void addFileEntry(FileEntry fileEntry) {
65 fileEntries.add(fileEntry);
72 protected void write(Writer writer) throws IOException {
75 for (FileEntry fileEntry: fileEntries) {
76 writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
77 if (fileEntry.getContentType() != null) {
78 writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);
80 writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
81 if (fileEntry instanceof DirectFileEntry) {
83 writer.write("Files." + fileIndex + ".DataLength=" + ((DirectFileEntry) fileEntry).getDataLength() + LINEFEED);
84 payloadLength += ((DirectFileEntry) fileEntry).getDataLength();
85 payloadInputStreams.add(((DirectFileEntry) fileEntry).getDataInputStream());
86 } else if (fileEntry instanceof DiskFileEntry) {
87 writer.write("Files." + fileIndex + ".Filename=" + ((DiskFileEntry) fileEntry).getFilename() + LINEFEED);
88 } else if (fileEntry instanceof RedirectFileEntry) {
89 writer.write("Files." + fileIndex + ".TargetURI=" + ((RedirectFileEntry) fileEntry).getTargetURI() + LINEFEED);
99 protected boolean hasPayload() {
107 protected long getPayloadLength() {
108 return payloadLength;
115 protected InputStream getPayload() {
116 /* grr. use Vector here because it returns an Enumeration. */
117 Vector<InputStream> inputStreams = new Vector<InputStream>(payloadInputStreams);
118 return new SequenceInputStream(inputStreams.elements());