Fix up all file headers.
[jSite.git] / src / de / todesbaum / util / freenet / fcp2 / ClientPutDir.java
1 /*
2  * jSite - ClientPutDir.java - Copyright © 2006–2012 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.util.freenet.fcp2;
20
21 import java.io.IOException;
22 import java.io.Writer;
23
24 /**
25  * Abstract base class for all put requests that insert a directory.
26  *
27  * @param <C>
28  *            The type of the “ClientPutDir” command
29  * @author David Roden &lt;droden@gmail.com&gt;
30  */
31 public class ClientPutDir<C extends ClientPutDir<?>> extends ClientPut {
32
33         /**
34          * All possible manifest putters. Manifest putters are used to distribute
35          * files of a directory insert to different containers, depending on size,
36          * type, and other factors.
37          *
38          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
39          */
40         public enum ManifestPutter {
41
42                 /**
43                  * Use the “simple” manifest putter. Despite its name this is currently
44                  * the default manifest putter.
45                  */
46                 SIMPLE("simple"),
47
48                 /** Use the “default” manifest putter. */
49                 DEFAULT("default");
50
51                 /** The name of the manifest putter. */
52                 private final String name;
53
54                 /**
55                  * Creates a new manifest putter.
56                  *
57                  * @param name
58                  *            The name of the manifest putter
59                  */
60                 private ManifestPutter(String name) {
61                         this.name = name;
62                 }
63
64                 /**
65                  * Returns the name of the manifest putter.
66                  *
67                  * @return The name of the manifest putter
68                  */
69                 public String getName() {
70                         return name;
71                 }
72
73         }
74
75         /** The default file of the directory. */
76         protected String defaultName;
77
78         /** The manifest putter to use. */
79         private ManifestPutter manifestPutter;
80
81         /**
82          * Creates a new request with the specified name, identifier, and URI.
83          *
84          * @param name
85          *            The name of the request
86          * @param identifier
87          *            The identifier of the request
88          * @param uri
89          *            The URI of the request
90          */
91         public ClientPutDir(String name, String identifier, String uri) {
92                 super(name, identifier, uri);
93         }
94
95         /**
96          * Returns the default name of the directory.
97          *
98          * @return The default name of the directory
99          */
100         public String getDefaultName() {
101                 return defaultName;
102         }
103
104         /**
105          * Sets the default name of the directory. The default name of a directory
106          * is the name of the file that will be delivered if the directory was
107          * requested without a filename. It's about the same as the
108          * <code>index.html</code> file that gets delivered if you only request a
109          * directory from a webserver.
110          *
111          * @param defaultName
112          *            The default name of the directory
113          */
114         public void setDefaultName(String defaultName) {
115                 this.defaultName = defaultName;
116         }
117
118         /**
119          * Returns the current manifest putter.
120          *
121          * @return The current manifest putter (may be {@code null})
122          */
123         public ManifestPutter getManifestPutter() {
124                 return manifestPutter;
125         }
126
127         /**
128          * Sets the manifest putter for the “ClientPutDir” command. If {@code null}
129          * is given the node will choose a manifest putter.
130          *
131          * @param manifestPutter
132          *            The manifest putter to use for the command (may be
133          *            {@code null})
134          * @return This ClientPutDir command
135          */
136         @SuppressWarnings("unchecked")
137         public C setManifestPutter(ManifestPutter manifestPutter) {
138                 this.manifestPutter = manifestPutter;
139                 return (C) this;
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         protected void write(Writer writer) throws IOException {
147                 super.write(writer);
148                 if (defaultName != null)
149                         writer.write("DefaultName=" + defaultName + LINEFEED);
150                 if (manifestPutter != null) {
151                         writer.write("ManifestPutter=" + manifestPutter.getName() + LINEFEED);
152                 }
153         }
154
155 }