KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > SlideUri


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/SlideUri.java,v 1.10.2.3 2004/10/18 09:11:42 luetzkendorf Exp $
3  * $Revision: 1.10.2.3 $
4  * $Date: 2004/10/18 09:11:42 $
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.slide.search;
25
26 /**
27  * A SlideUri contains two parts the <em>context path</em> and the <em>slide uri</em>.
28  *
29  * <p>The context path is determined by the path of the webapp and the path
30  * to which the WebDavServlet is mapped. Samples: <code>/slide</code>,
31  * <code>/webapp/webdav</code> and <code>/</code>.
32  *
33  * <p>The slide uri is an URI as represented by {@link org.apache.slide.common.Uri}.
34  *
35  *
36  */

37 public class SlideUri {
38     
39     private String JavaDoc context;
40     private String JavaDoc path;
41     
42     /**
43      * Creates a SlideUri.
44      * @param slideContextPath the Context path of the WebDAVServlet
45      * @param requestUri the URI from an WebDAV(HTTP) request
46      *
47      * @return the created SlideUri
48      */

49     public static SlideUri createWithRequestUri(String JavaDoc slideContextPath, String JavaDoc requestUri) {
50        if (slideContextPath.length() <= 1) {
51            // this.slideContextPath == "" or this.slideContextPath = "/"
52
return new SlideUri("/", requestUri);
53        } else {
54            return new SlideUri(slideContextPath,
55                 requestUri.substring(slideContextPath.length()));
56        }
57     }
58     
59     /**
60      * Constructs a SlideUri.
61      *
62      * @param slideContextPath Context and ServletPath
63      * @param slideUri uri
64      */

65     private SlideUri (String JavaDoc slideContextPath, String JavaDoc slideUri) {
66         if (slideContextPath == null || slideUri == null) {
67             throw new NullPointerException JavaDoc();
68         }
69         if (slideUri.length() == 0) {
70             slideUri = "/";
71         }
72         if (!slideContextPath.startsWith("/")) {
73             throw new IllegalArgumentException JavaDoc("slideContextPath must be absolute");
74         }
75         if (!slideUri.startsWith("/")) {
76             throw new IllegalArgumentException JavaDoc("slideUri must be absolute");
77         }
78         this.context = slideContextPath;
79         this.path = slideUri;
80
81         // normalize
82
if (this.context.endsWith("/") && this.context.length() > 1) {
83             this.context = this.context.substring(0, this.context.length()-1);
84         }
85         if (this.path.endsWith("/") && this.context.length() > 1) {
86             this.path = this.path.substring(0, this.path.length()-1);
87         }
88     }
89     
90     /**
91      * Determines the slidePath from an webdav path.
92      *
93      * @param path relative or absolute webdav path
94      *
95      * @throws InvalidScopeException if the given path is absolute but not
96      * in the scope given by the current slice context path.
97      *
98      */

99     public String JavaDoc getSlidePath (String JavaDoc davPath) throws InvalidScopeException {
100
101         if (davPath.startsWith("/")) {
102             // not a relative path
103
if (!(davPath.startsWith(this.context))) {
104                 throw new InvalidScopeException (
105                         "Uri \"" + davPath + "\" does not refer to " + context
106                         + ". If an absolute scope is used, it must start with \""
107                         + context + "\"");
108             }
109             if (davPath.length() == this.context.length()) {
110                 return "/";
111             }
112             if (this.context.length() > 1 && davPath.charAt(this.context.length()) != '/') {
113                 throw new InvalidScopeException (
114                         "Uri \"" + davPath + "\" does not refer to " + context
115                         + ". If an absolute scope is used, it must start with \""
116                         + context + "\"");
117             }
118
119             if (this.context.length() > 1) {
120                 return davPath.substring(this.context.length());
121             } else {
122                 // context == "/"
123
return davPath;
124             }
125         } else {
126             // relative path
127
if (path.length() > 1) {
128                 return this.path + "/" + davPath;
129             } else {
130                 // this.path == "/"
131
return this.path + davPath;
132             }
133         }
134     }
135     
136     /**
137      * Translates a slide uri to an absolute webdav path.
138      *
139      * @param slidePath slide internal uri
140      */

141     public String JavaDoc getContextPath (String JavaDoc slidePath) {
142         if (slidePath.startsWith("/")) {
143             if (context.length() > 1) {
144                 return context + slidePath;
145             } else {
146                 // this.context == "/"
147
return slidePath;
148             }
149         } else {
150             if (context.length() > 1) {
151                 return context + "/" + slidePath;
152             } else {
153                 // this.context == "/"
154
return "/" + slidePath;
155             }
156         }
157     }
158 }
159
160
Popular Tags