⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multistatusresponse.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
package com.maverick.http;

import net.n3.nanoxml.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author Lee David Painter
 * @version $Revision: 1.7 $
 */
public class MultiStatusResponse {


    String href;
    int status;
    String version;
    String reason;
    Properties properties;
    boolean collection = false;
    
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    MultiStatusResponse(IXMLElement element) throws IOException {
        if(element.getFirstChildNamed("href", element.getNamespace())==null)
            throw new IOException("Expected href element in multistatus response!");

        href = URLDecoder.decode(element.getFirstChildNamed("href", element.getNamespace()).getContent());

        IXMLElement props = element.getFirstChildNamed("propstat", element.getNamespace());

        if(props==null)
           throw new IOException("Multistatus response returned no property elements!");

        if(props.getFirstChildNamed("status", element.getNamespace())==null)
            throw new IOException("Expected status element in multistatus response!");


        String status = props.getFirstChildNamed("status", element.getNamespace()).getContent();

        StringTokenizer tokens = new StringTokenizer(status, " ", false);
        reason = "";

        try {
          version = tokens.nextToken();
          this.status = Integer.parseInt(tokens.nextToken());

          while(tokens.hasMoreTokens()) {
            reason += tokens.nextToken() + " ";
          }
          reason = URLDecoder.decode(reason.trim());
        }
        catch (NoSuchElementException e) {
          throw new IOException("Failed to read HTTP repsonse header");
        }
        catch (NumberFormatException e) {
          throw new IOException("Failed to read HTTP resposne header");
        }

        // Create a new set of properties
        properties = new Properties();
        
        // Check the status, if its not found then return
        if(this.status==404)
        	return; 
        
        props = props.getFirstChildNamed("prop", props.getNamespace());

        if(props==null)
            throw new IOException("No prop elements returned in propstat!");

        IXMLElement child;


        for(Enumeration e = props.getChildren().elements(); e.hasMoreElements();) {
          child = (IXMLElement) e.nextElement();
          
          if(child.getName().equalsIgnoreCase("resourcetype")) {
        	  if(child.getChildrenNamed("collection")!=null)
        		  collection = true;
          } else {
              properties.put(child.getName().toLowerCase(),
                         child.getContent()==null ? "" : child.getContent());
          }
        }


    }


    public long getContentLength() {
        if(properties.containsKey("getcontentlength")) {
            return Long.parseLong(properties.getProperty("getcontentlength"));
        } else
            return 0;
    }

    private long processDate(String date) {

        long retval = 0;
        try {
            retval = Date.parse(date);
        } catch(Throwable t) {

            try {
                /*org.joda.time.DateTime dt = new org.joda.time.DateTime(properties.getProperty("creationdate"));
                retval = dt.getMillis();*/
                return 0; // TODO change
            } catch(Throwable t2) { }
        }

        return retval;
    }

    public long getCreationDate() {

           if (properties.containsKey("creationdate")) {
                return processDate(properties.getProperty("creationdate"));
            }
            else
                return 0;
    }

    public long getLastModified() {

        if (properties.containsKey("getlastmodified")) {
            return processDate(properties.getProperty("getlastmodified"));
        }
        else
                return 0;
    }

    public String getContentType() {
        return getProperty("getcontenttype");
    }

    public String getDisplayName() {
        if(properties.containsKey("displayname"))
            return getProperty("displayname");
        else {
           int idx = href.lastIndexOf('/');
           if(idx>-1) {
               return href.substring(idx+1);
           } else
               return href;
        }
    }

    public int getStatus() {
        return status;
    }

    public String getHref() {
        return href;
    }

    public boolean isCollection() {
        return collection;
    }

    public String getProperty(String property) {
        return properties.getProperty(property.toLowerCase());
    }



    public static MultiStatusResponse[] createResponse(HttpResponse response) throws IOException {

        if(response.getStatus()!=207) {
            throw new IOException("Http status is not Multistatus/207");
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int read;
        byte[] buf = new byte[4096];
        while((read = response.getInputStream().read(buf)) > -1) {
            out.write(buf, 0, read);
        }

        try {
            IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
            IXMLReader reader = StdXMLReader.stringReader(new String(out.toByteArray(), "UTF8"));
            parser.setReader(reader);
            IXMLElement rootElement = (IXMLElement) parser.parse();

            if(!rootElement.getName().equalsIgnoreCase("multistatus"))
                throw new IOException("Invalid DAV root element for multistatus response: " + rootElement.getName());

            // Now process the responses
            Vector children = rootElement.getChildrenNamed("response", rootElement.getNamespace());
            Vector responses = new Vector();

            for(Enumeration e = children.elements(); e.hasMoreElements();) {
                responses.addElement(new MultiStatusResponse((IXMLElement) e.nextElement()));
            }

            MultiStatusResponse[] array = new MultiStatusResponse[responses.size()];
            responses.copyInto(array);
            return array;

        } catch (Exception ex) {
        	ex.printStackTrace();
            throw new IOException("");
        }

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -