ib          2004/07/20 16:33:00

  Modified:    webdavclient/clientlib/src/java/org/apache/webdav/lib/methods
                        CopyMethod.java MoveMethod.java
  Log:
  Fix for bug #30125: MoveMethod, CopyMethod should allow absolute URLs for 
destination and avoid adding default port
  
  Revision  Changes    Path
  1.5       +4 -6      
jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/CopyMethod.java
  
  Index: CopyMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/CopyMethod.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CopyMethod.java   6 Jul 2004 19:07:50 -0000       1.4
  +++ CopyMethod.java   20 Jul 2004 23:33:00 -0000      1.5
  @@ -220,9 +220,7 @@
   
           super.addRequestHeaders(state, conn);
   
  -        String absoluteDestination =
  -            conn.getProtocol().getScheme() + "://" + conn.getHost() + ":"
  -            + conn.getPort() + destination;
  +        String absoluteDestination = MoveMethod.getAbsoluteDestination(conn, 
destination);
           super.setRequestHeader("Destination", absoluteDestination);
   
           if (!isOverwrite())
  
  
  
  1.5       +50 -6     
jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/MoveMethod.java
  
  Index: MoveMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/MoveMethod.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MoveMethod.java   6 Jul 2004 20:09:51 -0000       1.4
  +++ MoveMethod.java   20 Jul 2004 23:33:00 -0000      1.5
  @@ -27,6 +27,7 @@
   import org.apache.commons.httpclient.HttpConnection;
   import org.apache.commons.httpclient.HttpException;
   import org.apache.commons.httpclient.HttpState;
  +import org.apache.commons.httpclient.protocol.Protocol;
   
   
   /**
  @@ -185,14 +186,57 @@
   
           super.addRequestHeaders(state, conn);
   
  -        String absoluteDestination =
  -            conn.getProtocol().getScheme() + "://" + conn.getHost() + ":"
  -            + conn.getPort() + destination;
  +        String absoluteDestination = getAbsoluteDestination(conn, destination);
           super.setRequestHeader("Destination", absoluteDestination);
   
           if (!isOverwrite())
               super.setRequestHeader("Overwrite", "F");
   
  +    }
  +
  +    /**
  +     * A client of the [EMAIL PROTECTED] MoveMethod} can specify a destination as 
either an
  +     * absolute URL (possibly to a different server), or as a absolute path on
  +     * the same server, but this function makes sure that the path sent to the
  +     * server is always an absolute URL.
  +     *
  +     * <p>Note that this function will add server and port to the request -
  +     * however, port is not added if it is the default port for the scheme
  +     * in question. </p>
  +     *
  +     * <p>This function is static so that it can be reused by the [EMAIL PROTECTED] 
CopyMethod}.
  +     * </p>
  +     *
  +     * @param conn  The connection for the current request, in case the caller
  +     *  specifies an absolute path.
  +     *
  +     * @param absolutePathOrURL If an absolute URL, nothing done, but if an absolute
  +     *  path, it is converted into an absolute URL.
  +     *
  +     * @return An absolute URL
  +     */
  +    static String getAbsoluteDestination(HttpConnection conn, String 
absolutePathOrURL) {
  +
  +        String absoluteDestination = absolutePathOrURL;
  +
  +        // is this an absolute path?
  +        if (absolutePathOrURL.startsWith("/")) {
  +
  +            // yes - get the protocol to start the URL with the appropriate scheme.
  +            Protocol protocol = conn.getProtocol();
  +            StringBuffer bufDest = new StringBuffer(protocol.getScheme());
  +            bufDest.append("://").append(conn.getHost());
  +
  +            // only add in the port if it is not the default port.
  +            if (conn.getPort() != protocol.getDefaultPort()) {
  +                bufDest.append(':').append(conn.getPort());
  +            }
  +
  +            // append the path.
  +            bufDest.append(absolutePathOrURL);
  +            absoluteDestination = bufDest.toString();
  +        }
  +        return absoluteDestination;
       }
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to