Hi,

 I've traced Client.java and try to write a simple program to connect to
my slide 'http://137.189.94.70:8080/slide'. The program has been attached.

 However, it fails to connect with the following WARNING and
NullPointerException shown as follows:

uri is http://137.189.94.70:8080/slide
Apr 1, 2003 12:23:17 PM org.apache.commons.httpclient.HttpMethodBase
processAuthenticationResponse
WARNING: No credentials available for the Basic authentication realm
'Slide DAV Server'
Exception in thread "main" java.lang.NullPointerException
        at org.apache.webdav.cmd.Spool$2.write(Spool.java:163)
        at java.io.PrintStream.write(PrintStream.java:258)
        at
sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:334)
        at
sun.nio.cs.StreamEncoder$CharsetSE.implWrite(StreamEncoder.java:393)
        at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:134)
        at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
        at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
        at java.io.PrintStream.write(PrintStream.java:304)
        at java.io.PrintStream.print(PrintStream.java:448)
        at java.io.PrintStream.println(PrintStream.java:585)
        at Testing.handleException(Testing.java:232)
        at Testing.connect(Testing.java:105)
        at Testing.main(Testing.java:242)

What's the problem?
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Stack;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.net.URL;
import java.net.MalformedURLException;

import org.apache.webdav.lib.WebdavResource;
import org.apache.util.HttpURL;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpException;
import org.apache.webdav.cmd.Spool;

public class Testing{

   static private Spool spool;
 
   static private HttpURL httpURL;

   static private WebdavResource webdavResource = null;

   static private File dir = new File(".");   

   static private int debugLevel = 0;

   static InputStream in;

   static PrintStream out;

   static private String commandPrompt =null;
   
   static private String path = "";

   public Testing(){

   }
 
   private static void init(){
        spool = new Spool(in,out);
        in  = in;
        out = new PrintStream(spool.getOutputStream());
   //     updatePrompt(getPath());
   }

   private static void connect(String uri){
        System.out.println("uri is " + uri);

        try {
            // Set up for processing WebDAV resources
            httpURL = new HttpURL(uri);
            if (webdavResource == null) {
                webdavResource = new WebdavResource(httpURL);
                webdavResource.setDebug(debugLevel);
            } else {
                webdavResource.close();
                webdavResource.setHttpURL(httpURL);
            }
            setPath(webdavResource.getPath());
        }
        catch (HttpException we) {
            if (we.getReasonCode() == HttpStatus.SC_UNAUTHORIZED) {
                try {
                    out.print("UserName: ");
                    BufferedReader in =
                        new BufferedReader(new InputStreamReader(System.in));
                    String userName = in.readLine();
                    if ((userName==null) || (userName.length()==0)) {
                        disconnect();
                        return;
                    }
                    userName = userName.trim();
                    System.out.print("Password: ");
                    String password = in.readLine();
                    if (password != null)
                        password= password.trim();
                    try {
                        if (webdavResource != null)
                            webdavResource.close();
                    } catch (IOException e) {
                    } finally {
                        httpURL = null;
                        webdavResource = null;
                    }
                    httpURL = new HttpURL(uri);
                    // It should be used like this way.
                    httpURL.setUserInfo(userName, password);
                    webdavResource = new WebdavResource(httpURL);
                    webdavResource.setDebug(debugLevel);
                    setPath(webdavResource.getPath());
                }
                catch (Exception ex) {
                    handleException(ex);
                    httpURL = null;
                    webdavResource = null;
                }
            }
            else  {
                handleException(we);
                httpURL = null;
                webdavResource = null;
            }
        }
        catch (Exception ex) {
            handleException(ex);
            webdavResource = null;
            httpURL = null;
        }
        updatePrompt(getPath());
   }

    private static String getPath()
    {
        return path;
    }


    static void disconnect()
    {
        out.println("disconnect");
        try {
            webdavResource.close();
        } catch (IOException e) {
        } finally {
            // Make sure the connection closed.
            httpURL = null;
            webdavResource = null;
        }
        updatePrompt(getPath());
    }

    private static void updatePrompt(String path)
    {
        StringBuffer buff = new StringBuffer();
        try {
            buff.append("[" + httpURL.getHost().toUpperCase() + "] ");
            buff.append(path);
        } catch (Exception e) {
            buff.append("[ Slide ]");
        }
        buff.append(" $ ");
        commandPrompt = buff.toString();
    }

    private static void setPath(String path1)
    {
        if (!path1.endsWith("/")) {
            path1 = path1 + "/";
        }

        path = normalize(path1);
    }
   
    private static String normalize(String path)
    {
        if (path == null)
            return null;

        String normalized = path;

        // Normalize the slashes and add leading slash if necessary
        if (normalized.indexOf('\\') >= 0)
            normalized = normalized.replace('\\', '/');
        if (!normalized.startsWith("/"))
            normalized = "/" + normalized;

        // Resolve occurrences of "/./" in the normalized path
        while (true) {
            int index = normalized.indexOf("/./");
            if (index < 0)
            break;
            normalized = normalized.substring(0, index) +
            normalized.substring(index + 2);
        }

        // Resolve occurrences of "/../" in the normalized path


      while (true) {
            int index = normalized.indexOf("/../");
            if (index < 0)
            break;
            if (index == 0)
            return ("/");  // The only left path is the root.
            int index2 = normalized.lastIndexOf('/', index - 1);
            normalized = normalized.substring(0, index2) +
            normalized.substring(index + 3);
        }

        // Resolve occurrences of "//" in the normalized path
        while (true) {
            int index = normalized.indexOf("//");
            if (index < 0)
            break;
            normalized = normalized.substring(0, index) +
            normalized.substring(index + 1);
        }

        // Return the normalized path that we have completed
        return (normalized);
    }

    private static void handleException(Exception ex)
    {
        if (ex instanceof HttpException) {
            if (((HttpException) ex).getReasonCode() == 
HttpStatus.SC_METHOD_NOT_ALLOWED) {
                out.println("Warning: Not WebDAV-enabled?");
            }
            else if (((HttpException) ex).getReasonCode() == 
HttpStatus.SC_UNAUTHORIZED) {
                out.println("Warning: Unauthorized");
            }
            else {
                out.println("Warning: " + ex.getMessage());
            }
        }
        else if (ex instanceof IOException) {
            out.println("Error: " + ex.getMessage());
        }
        else {
            out.println("Fatal Error: " + ex.getMessage());
            ex.printStackTrace(out);
            out.println("Please, email to [EMAIL PROTECTED]");
            System.exit(-1);
        }
   }


    public static void main(String[] args){
        init();
        connect("http://137.189.94.70:8080/slide";);
        disconnect();
   }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to