KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webdav > HierarchicalMethod


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webdav;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 /**
25  * Abstract base class for the hierarchical methods COPY and MOVE
26  *
27  * @author gavinc
28  */

29 public abstract class HierarchicalMethod extends WebDAVMethod
30 {
31     // Request parameters
32

33     protected String JavaDoc m_strDestinationPath;
34     protected boolean m_overwrite = false;
35
36     /**
37      * Default constructor
38      */

39     public HierarchicalMethod()
40     {
41     }
42
43     /**
44      * Return the destination path
45      *
46      * @return String
47      */

48     public final String JavaDoc getDestinationPath()
49     {
50         return m_strDestinationPath;
51     }
52
53     /**
54      * Return the overwrite setting
55      *
56      * @return boolean
57      */

58     public final boolean hasOverWrite()
59     {
60         return m_overwrite;
61     }
62
63     /**
64      * Parse the request headers
65      *
66      * @exception WebDAVServerException
67      */

68     protected void parseRequestHeaders() throws WebDAVServerException
69     {
70         // Get the destination path for the copy
71

72         String JavaDoc strDestination = m_request.getHeader(WebDAV.HEADER_DESTINATION);
73
74         if (logger.isDebugEnabled())
75             logger.debug("Parsing Destination header: " + strDestination);
76
77         if (strDestination != null && strDestination.length() > 0)
78         {
79             int offset = -1;
80
81             if (strDestination.startsWith("http://"))
82             {
83                 // Check that the URL is on this server and refers to the WebDAV
84
// path, if not then return an error
85

86                 checkDestinationPath(strDestination);
87
88                 // Set the offset to the start of the
89

90                 offset = 7;
91             }
92             else if (strDestination.startsWith("https://"))
93             {
94                 // Check that the URL is on this server and refers to the WebDAV
95
// path, if not then return an error
96

97                 checkDestinationPath(strDestination);
98
99                 // Set the offset to the start of the
100

101                 offset = 8;
102             }
103
104             // Strip the start of the path if not a relative path
105

106             if (offset != -1)
107             {
108                 offset = strDestination.indexOf(WebDAV.PathSeperator, offset);
109                 if (offset != -1)
110                 {
111                     String JavaDoc strPath = strDestination.substring(offset);
112                     String JavaDoc servletPath = m_request.getServletPath();
113
114                     offset = strPath.indexOf(servletPath);
115                     if (offset != -1)
116                         strPath = strPath.substring(offset + servletPath.length());
117
118                     m_strDestinationPath = WebDAV.decodeURL(strPath);
119                 }
120             }
121         }
122
123         // Failed to fix the destination path, return an error
124

125         if (m_strDestinationPath == null)
126         {
127             logger.warn("Failed to parse the Destination header: " + strDestination);
128             throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
129         }
130
131         // Check if the copy should overwrite an existing file
132

133         String JavaDoc strOverwrite = m_request.getHeader(WebDAV.HEADER_OVERWRITE);
134         if (strOverwrite != null && strOverwrite.equals(WebDAV.T))
135         {
136             m_overwrite = true;
137         }
138     }
139
140     /**
141      * Parse the request body
142      *
143      * @exception WebDAVServerException
144      */

145     protected void parseRequestBody() throws WebDAVServerException
146     {
147         // NOTE: Hierarchical methods do have a body to define what should
148
// happen
149
// to the properties when they are moved or copied, however, this
150
// feature is not implemented by many servers, including ours!!
151
}
152
153     /**
154      * Check that the destination path is on this server and is a valid WebDAV
155      * path for this server
156      *
157      * @param path String
158      * @exception WebDAVServerException
159      */

160     protected final void checkDestinationPath(String JavaDoc path) throws WebDAVServerException
161     {
162         try
163         {
164             // Parse the URL
165

166             URL JavaDoc url = new URL JavaDoc(path);
167
168             // Check if the path is on this WebDAV server
169

170             boolean localPath = true;
171
172             if (url.getPort() != -1 && url.getPort() != m_request.getLocalPort())
173             {
174                 // Debug
175

176                 if (logger.isDebugEnabled())
177                     logger.debug("Destination path, different server port");
178
179                 localPath = false;
180             }
181             else if (url.getHost().equals(m_request.getLocalName()) == false
182                     && url.getHost().equals(m_request.getLocalAddr()) == false)
183             {
184                 // Debug
185

186                 if (logger.isDebugEnabled())
187                     logger.debug("Destination path, different server name/address");
188
189                 localPath = false;
190             }
191             else if (url.getPath().indexOf(m_request.getServletPath()) == -1)
192             {
193                 // Debug
194

195                 if (logger.isDebugEnabled())
196                     logger.debug("Destination path, different serlet path");
197
198                 localPath = false;
199             }
200
201             // If the URL does not refer to this WebDAV server throw an
202
// exception
203

204             if (localPath != true)
205                 throw new WebDAVServerException(HttpServletResponse.SC_BAD_GATEWAY);
206         }
207         catch (MalformedURLException JavaDoc ex)
208         {
209             // Debug
210

211             if (logger.isDebugEnabled())
212                 logger.debug("Bad destination path, " + path);
213
214             throw new WebDAVServerException(HttpServletResponse.SC_BAD_GATEWAY);
215         }
216     }
217 }
218
Popular Tags