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