KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > BindMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/BindMethod.java,v 1.5 2004/08/02 15:45:47 unico Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/02 15:45:47 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.methods;
25
26 import java.io.IOException JavaDoc;
27 import org.apache.commons.httpclient.HttpConnection;
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.HttpState;
30 import org.apache.webdav.lib.util.XMLPrinter;
31
32 /**
33  * The BIND method modifies the collection identified by the Request-URI,
34  * by adding a new binding from the segment specified in the BIND body
35  * to the resource identified in the BIND body.
36  *
37  * BIND Method Example:
38  * >> Request:
39  * BIND /CollY HTTP/1.1
40  * Host: www.example.com
41  * Content-Type: text/xml; charset="utf-8"
42  * Content-Length: xxx
43  * <?xml version="1.0" encoding="utf-8" ?>
44  * <D:bind xmlns:D="DAV:">
45  * <D:segment>bar.html</D:segment>
46  * <D:href>http://www.example.com/CollX/foo.html</D:href>
47  * </D:bind>
48  *
49  * >> Response:
50  * HTTP/1.1 201 Created
51  * The server added a new binding to the collection, "http://www.example.com/CollY",
52  * associating "bar.html" with the resource identified by the URI
53  * "http://www.example.com/CollX/foo.html". Clients can now use the URI
54  * "http://www.example.com/CollY/bar.html", to submit requests to that resource.
55  *
56  */

57 public class BindMethod
58     extends XMLResponseMethodBase {
59
60
61     public static final String JavaDoc NAME = "BIND";
62
63     private boolean overwrite = true;
64     private String JavaDoc segment = null;
65     private String JavaDoc href = null;
66
67     // ----------------------------------------------------------- Constructors
68

69
70     /**
71      * Method constructor.
72      */

73     public BindMethod() {
74     }
75
76     public BindMethod(String JavaDoc existingBinding, String JavaDoc newBinding) {
77         super(newBinding.substring(0, newBinding.lastIndexOf('/')));
78         this.href = existingBinding;
79         this.segment = newBinding.substring(newBinding.lastIndexOf('/') + 1);
80     }
81
82     public String JavaDoc getName() {
83         return NAME;
84     }
85
86     /**
87      * By default, if there already is a binding for the specified segment
88      * in the collection, the new binding replaces the existing binding.
89      * This default binding replacement behavior can be overridden using
90      * the Overwrite header.
91      *
92      * @param overwrite New overwrite value
93      */

94     public void setOverwrite(boolean overwrite) {
95         checkNotUsed();
96         this.overwrite = overwrite;
97     }
98
99
100     /**
101      * By default, if there already is a binding for the specified segment
102      * in the collection, the new binding replaces the existing binding.
103      * This default binding replacement behavior can be overridden using
104      * the Overwrite header.
105      *
106      * @return the current value of the overwrite flag
107      */

108     public boolean isOverwrite() {
109         return overwrite;
110     }
111
112
113     /**
114      * Generate additional headers needed by the request.
115      *
116      * @param state HttpState token
117      * @param conn The connection being used for the request.
118      */

119     public void addRequestHeaders(HttpState state, HttpConnection conn)
120     throws IOException JavaDoc, HttpException {
121
122         super.addRequestHeaders(state, conn);
123
124         if (!isOverwrite())
125             super.setRequestHeader("Overwrite", "F");
126
127     }
128
129     /**
130      * DAV requests that contain a body must override this function to
131      * generate that body.
132      *
133      * <p>The default behavior simply returns an empty body.</p>
134      */

135     protected String JavaDoc generateRequestBody() {
136
137         if (segment == null || href == null)
138             throw new IllegalStateException JavaDoc
139                 ("Segment and Href must be set before " +
140                  "calling this function.");
141
142         XMLPrinter printer = new XMLPrinter();
143
144         printer.writeXMLHeader();
145         printer.writeElement("D", "DAV:", "bind", XMLPrinter.OPENING);
146         printer.writeElement("D", "segment", XMLPrinter.OPENING);
147         printer.writeText(segment);
148         printer.writeElement("D", "segment", XMLPrinter.CLOSING);
149         printer.writeElement("D", "href", XMLPrinter.OPENING);
150         printer.writeText(href);
151         printer.writeElement("D", "href", XMLPrinter.CLOSING);
152         printer.writeElement("D", "bind", XMLPrinter.CLOSING);
153
154         return printer.toString();
155     }
156
157     /**
158      * @return path of the resource to be bound
159      */

160     public String JavaDoc getHref() {
161         return href;
162     }
163
164     /**
165      * @return new resource name
166      */

167     public String JavaDoc getSegment() {
168         return segment;
169     }
170
171     /**
172      * @param href path of the resource to be bound
173      */

174     public void setHref(String JavaDoc href) {
175         this.href = href;
176     }
177
178     /**
179      * @param segment new resource name
180      */

181     public void setSegment(String JavaDoc segment) {
182         this.segment = segment;
183     }
184
185 }
186
187
Popular Tags