jSite: First commit : verion 4.0 (written by Bombe)
[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.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;
29
30 /**
31  * Implementation of the <code>ClientPutComplexDir</code> command. This
32  * command can be used to insert directories that do not exist on disk.
33  * 
34  * @author David Roden &lt;droden@gmail.com&gt;
35  * @version $Id: ClientPutComplexDir.java 356 2006-03-24 15:13:38Z bombe $
36  */
37 public class ClientPutComplexDir extends ClientPutDir {
38
39         /** The file entries of this directory. */
40         private List<FileEntry> fileEntries = new ArrayList<FileEntry>();
41         
42         /** Whether this request has payload. */
43         private boolean hasPayload = false;
44         
45         /** The input streams for the payload. */
46         private List<InputStream> payloadInputStreams = new ArrayList<InputStream>();
47         
48         /** The total number of bytes of the payload. */
49         private long payloadLength = 0;
50
51         /**
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
55          */
56         public ClientPutComplexDir(String identifier, String uri) {
57                 super("ClientPutComplexDir", identifier, uri);
58         }
59
60         /**
61          * Adds a file to the directory inserted by this request.
62          * @param fileEntry The file entry to add to the directory
63          */
64         public void addFileEntry(FileEntry fileEntry) {
65                 fileEntries.add(fileEntry);
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         protected void write(Writer writer) throws IOException {
73                 super.write(writer);
74                 int fileIndex = 0;
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);
79                         }
80                         writer.write("Files." + fileIndex + ".UploadFrom=" + fileEntry.getName() + LINEFEED);
81                         if (fileEntry instanceof DirectFileEntry) {
82                                 hasPayload = true;
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);
90                         }
91                         fileIndex++;
92                 }
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         protected boolean hasPayload() {
100                 return hasPayload;
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         protected long getPayloadLength() {
108                 return payloadLength;
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
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());
119         }
120
121 }