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