You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/09/25 22:25:41 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/search SlideUri.java

luetzkendorf    2004/09/25 13:25:40

  Modified:    src/share/org/apache/slide/search Tag:
                        SLIDE_2_1_RELEASE_BRANCH SlideUri.java
  Log:
  fix for bug 28140; SlideUri ctor now requires the slide context path because it
  can't determined from the request uri
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.10.2.1  +104 -110  jakarta-slide/src/share/org/apache/slide/search/SlideUri.java
  
  Index: SlideUri.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/SlideUri.java,v
  retrieving revision 1.10
  retrieving revision 1.10.2.1
  diff -u -r1.10 -r1.10.2.1
  --- SlideUri.java	14 Jun 2004 12:40:05 -0000	1.10
  +++ SlideUri.java	25 Sep 2004 20:25:40 -0000	1.10.2.1
  @@ -24,10 +24,14 @@
   package org.apache.slide.search;
   
   /**
  - * The root of an href in slide is the slide servlet. For an external href the
  - * root is the server. Example: the host is localhost, the context is slide,
  - * the slidePath is /mycoll/myfile.xml
  - * http://localhost/slide/mycoll/myfile.xml
  + * A SlideUri contains two parts the <em>context path</em> and the <em>slide uri</em>. 
  + * 
  + * <p>The context path is determined by the path of the webapp and the path 
  + * to which the WebDavServlet is mapped. Samples: <code>/slide</code>, 
  + * <code>/webapp/webdav</code> and <code>/</code>.
  + * 
  + * <p>The slide uri is an URI as represented by {@link org.apache.slide.common.Uri}.  
  + * 
    *
    */
   public class SlideUri {
  @@ -36,125 +40,115 @@
       private String path;
       
       /**
  -     * Constructs a SlideUri. If the requestUri is null, paths are regarded
  -     * as being always slidePaths
  +     * Creates a SlideUri.
  +     * @param slideContextPath the Context path of the WebDAVServlet
  +     * @param requestUri the URI from an WebDAV(HTTP) request
  +     * 
  +     * @return the created SlideUri
        */
  -    public SlideUri (String requestUri) {
  -        if (requestUri == null) {
  -            this.context = null;
  -            this.path = "";
  -        }
  -        else {
  -            this.context = getContextOfRelPath(requestUri);
  -            this.path = getPathOfRelPath(requestUri);
  -        }
  -    }
  -    
  -    /**
  -     * Method getSlideUri. If the requestUri is null, relpath is regarded to be
  -     * already a slidePath, it must start with a "/"
  -     *
  -     * @param    relPath             a  String
  -     *
  -     * @return   a String
  -     *
  -     * @throws   InvalidScopeException
  -     *
  -     */
  -    public String getSlidePath (String relPath) throws InvalidScopeException {
  -        
  -        if (context == null) {
  -            if (!relPath.startsWith("/"))
  -                throw new InvalidScopeException ("absolute scope is required");
  -            return relPath;
  -        }
  -        
  -        StringBuffer sb = new StringBuffer ();
  -        if (relPath.startsWith("/")) {
  -            
  -            if (!context.equals ("")) {
  -                
  -                String relContext = getContextOfRelPath (relPath);
  -                if (!relContext.equals (context))
  -                    throw new InvalidScopeException ("Uri \"" + relPath + "\" does not refer to " + context
  -                                                         + ". If an absolute scope is used, it must start with \""
  -                                                         + context + "\"");
  -                
  -                relPath = getPathOfRelPath (relPath);
  -            }
  -        }
  -            
  -        else {
  -            sb.append (path);
  -            if (!path.endsWith("/"))
  -                sb.append ("/");
  -        }
  -        
  -        sb.append (relPath);
  -        
  -        return sb.toString();
  +    public static SlideUri createWithRequestUri(String slideContextPath, String requestUri) {
  +       if (slideContextPath.length() == 1) {
  +           // this.slideContextPath == "/"
  +            return new SlideUri(slideContextPath, requestUri);
  +       } else {
  +           return new SlideUri(slideContextPath, 
  +                requestUri.substring(slideContextPath.length()));
  +       }
       }
       
       /**
  -     * Method getContextPath
  -     *
  -     * @param    internalHref        a  String
  -     *
  -     * @return   a String
  -     * @deprecated
  +     * Constructs a SlideUri. 
  +     * 
  +     * @param slideContextPath Context and ServletPath
  +     * @param slideUri uri
        */
  -    public String getContextPath (String slidePath) {
  -        if (context == null) {
  -            throw new RuntimeException ("getContextPath not allowed in this context");
  +    private SlideUri (String slideContextPath, String slideUri) {
  +        if (slideContextPath == null || slideUri == null) {
  +            throw new NullPointerException();
  +        }
  +        if (slideUri.length() == 0) {
  +            slideUri = "/";
  +        }
  +        if (!slideContextPath.startsWith("/")) {
  +            throw new IllegalArgumentException("slideContextPath must be absolute");
  +        }
  +        if (!slideUri.startsWith("/")) {
  +            throw new IllegalArgumentException("slideUri must be absolute");
  +        }
  +        this.context = slideContextPath;
  +        this.path = slideUri;
  +
  +        // normalize
  +        if (this.context.endsWith("/") && this.context.length() > 1) {
  +            this.context = this.context.substring(0, this.context.length()-1);
  +        }
  +        if (this.path.endsWith("/") && this.context.length() > 1) {
  +            this.path = this.path.substring(0, this.path.length()-1);
           }
  -        return context + slidePath;
       }
       
  -    
       /**
  -     * Method getContextOfRelPath
  +     * Determines the slidePath from an webdav path.
  +     * 
  +     * @param   path relative or absolute webdav path 
        *
  -     * @param    relPath             a  String
  -     *
  -     * @return   a String
  +     * @throws  InvalidScopeException if the given path is absolute but not 
  +     *             in the scope given by the current slice context path. 
        *
        */
  -    private String getContextOfRelPath (String relPath) {
  -        // FIXME the context may contain slashes too 
  -        // this does not work when the servlet in not the default, e.g. /slide/abc/
  -        String result = null;
  -        int posSlash = relPath.indexOf ('/', 1);
  -        if (posSlash == -1)
  -            result = relPath.substring (0);
  -        else
  -            result = relPath.substring (0, posSlash);
  -        
  -        return result;
  +    public String getSlidePath (String davPath) throws InvalidScopeException {
  +
  +        if (davPath.startsWith("/")) {
  +            // not a relative path
  +            if (!(davPath.startsWith(this.context))) {
  +                throw new InvalidScopeException (
  +                        "Uri \"" + davPath + "\" does not refer to " + context
  +                        + ". If an absolute scope is used, it must start with \""
  +                        + context + "\"");
  +            }
  +            if (davPath.length() == this.context.length()) {
  +                return "/";
  +            }
  +            if (davPath.charAt(this.context.length()) != '/') {
  +                throw new InvalidScopeException (
  +                        "Uri \"" + davPath + "\" does not refer to " + context
  +                        + ". If an absolute scope is used, it must start with \""
  +                        + context + "\"");                
  +            }
  +
  +            return davPath.substring(this.context.length());
  +        } else {
  +            // relative path
  +            if (path.length() > 1) {
  +                return this.path + "/" + davPath;
  +            } else {
  +                // this.path == "/"
  +                return this.path + davPath;
  +            }
  +        }
       }
       
       /**
  -     * Method getPathOfRelPath
  -     *
  -     * @param    relPath             a  String
  -     *
  -     * @return   a String
  -     *
  +     * Translates a slide uri to an absolute webdav path.
  +     * 
  +     * @param  slidePath slide internal uri
        */
  -    private String getPathOfRelPath (String relPath) {
  -        // FIXME the context may contain slashes too 
  -        String result = null;
  -        int posSlash = relPath.indexOf ('/', 1);
  -        if (posSlash == -1)
  -            result = "";
  -        else
  -            result = relPath.substring (posSlash);
  -        
  -        return result;
  +    public String getContextPath (String slidePath) {
  +        if (slidePath.startsWith("/")) {
  +            if (context.length() > 1) {
  +                return context + slidePath;
  +            } else {
  +                // this.context == "/"
  +                return slidePath;
  +            }
  +        } else {
  +            if (context.length() > 1) {
  +                return context + "/" + slidePath;
  +            } else {
  +                // this.context == "/"
  +                return "/" + slidePath;
  +            }
  +        }
       }
  -    
  -    
  -    
  -    
  -    
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org