DAV ServerObjects updates

thobaben_serialize
Harald Wolff 2014-08-24 13:07:27 +02:00
parent 0fa6e912da
commit e271583f3e
9 changed files with 555 additions and 0 deletions

View File

@ -0,0 +1,116 @@
package org.hwo.net.serverobjects;
import java.io.IOException;
import javax.xml.ws.http.HTTPException;
import org.hwo.net.http.HttpException;
import org.hwo.net.serverobjects.dav.DAVCollection;
import org.hwo.net.serverobjects.dav.DAVPropertyName;
import org.hwo.net.serverobjects.dav.DAVPropertyNameList;
import org.hwo.net.serverobjects.dav.DAVResource;
import org.hwo.xml.NodeListIterator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class DAVCollectionSO extends AbstractServerObject{
class RootCollection extends DAVCollection
{
public RootCollection(String name) {
super(name);
}
@Override
protected String constructURI() {
return "";
}
@Override
public DAVResource getParent() {
return null;
}
}
private DAVCollection rootCollection;
public DAVCollectionSO()
{
rootCollection = new RootCollection("");
}
@Override
public void request(ServerObjectRequest request) throws IOException, HttpException
{
request.getHttpRequest().addResponseHeader("DAV", "1");
String method = request.getHttpRequest().getRequestMethod();
System.err.println("DAV Request: " + method);
if (method.equals("GET"))
{
request.getHttpRequest().setResponseHeader("Content-Type", "text/plain");
request.getHttpRequest().getResponseWriter().write("This is a DAV Container.");
} else if (method.equals("PROPFIND"))
{
doPROPFIND(request);
} else if (method.equals("PROPPATCH"))
{
doPROPPATCH(request);
} else if (method.equals("MKCOL"))
{
doMKCOL(request);
} else if (method.equals("DELETE"))
{
doDELETE(request);
} else if (method.equals("OPTIONS"))
{
doOPTIONS(request);
}
}
protected void doPROPFIND(ServerObjectRequest request) throws HttpException
{
System.err.println("PROPFIND:");
System.err.println(new String(request.getHttpRequest().getRequestContent()));
Document d = request.getHttpRequest().getXMLRequest();
if (d == null)
throw new HttpException(403);
Element propfind = d.getDocumentElement();
if (propfind.getLocalName().equals("propfind") && propfind.getNamespaceURI().equals("DAV:"))
{
for (Element child:NodeListIterator.create(propfind.getChildNodes()))
{
if (child.getLocalName().equals("prop"))
{
DAVPropertyNameList names = DAVPropertyNameList.fromXML(child);
for (DAVPropertyName name:names)
{
System.err.println("DAVProp: " + name.getNamespaceURI() + " : " + name.getPropertyName());
}
}
}
}
}
protected void doPROPPATCH(ServerObjectRequest request) throws HttpException
{
}
protected void doMKCOL(ServerObjectRequest request) throws HttpException
{
}
protected void doDELETE(ServerObjectRequest request) throws HttpException
{
}
protected void doOPTIONS(ServerObjectRequest request) throws HttpException
{
request.getHttpRequest().setResultCode(200);
}
}

View File

@ -0,0 +1,53 @@
package org.hwo.net.serverobjects.dav;
import java.util.LinkedList;
import java.util.List;
public class DAVCollection extends DAVResource{
List<DAVResource> children;
public DAVCollection(String name) {
super(name);
children = new LinkedList<DAVResource>();
}
@Override
public boolean isCollection() {
return true;
}
@Override
public DAVResource[] getChildren() {
return children.toArray(new DAVResource[0]);
}
public void appendResource(DAVResource child)
{
DAVResource resource = getChildByName(child.getName());
if (resource != null)
{
removeChild(resource);
}
children.add(child);
child.setParent(this);
}
public DAVResource getChildByName(String name)
{
for (DAVResource resource: children)
{
if (resource.getName().equals(name))
return resource;
}
return null;
}
public void removeChild(DAVResource child)
{
children.remove(child);
child.setParent(null);
}
}

View File

@ -0,0 +1,46 @@
package org.hwo.net.serverobjects.dav;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class DAVDeadPropertyNS extends DAVPropertyNamespace {
DocumentBuilder builder;
public DAVDeadPropertyNS(String namespaceURI)
{
super(namespaceURI);
try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
builder = f.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
@Override
public Element getPropertyValue(DAVResource resource, DAVProperty property) {
return resource.getProperty(property.getNamespace().getNamespaceURI(), property.getPropertyName());
}
@Override
public void setPropertyValue(DAVResource resource, DAVProperty property,Element value) {
if (property.isValue(value))
{
Document d = builder.newDocument();
Node n = d.importNode(value, true);
d.appendChild(n);
System.err.println("setPropertyValue: " + d.toString());
}
}
}

View File

@ -0,0 +1,43 @@
package org.hwo.net.serverobjects.dav;
import org.w3c.dom.Element;
public abstract class DAVProperty {
private DAVPropertyNamespace namespace;
private String propertyName;
public DAVProperty(DAVPropertyNamespace namespace,String propertyName)
{
this.namespace = namespace;
this.propertyName = propertyName;
}
public DAVPropertyNamespace getNamespace() {
return namespace;
}
public String getPropertyName() {
return propertyName;
}
public Element getValue(DAVResource resource)
{
return namespace.getPropertyValue(resource, this);
}
public void setValue(DAVResource resource,Element value)
{
this.namespace.setPropertyValue(resource, this, value);
}
public boolean isValue(Element value)
{
if (value.getNamespaceURI().equals(namespace.getNamespaceURI()) && value.getLocalName().equals(propertyName))
return true;
return false;
}
}

View File

@ -0,0 +1,29 @@
package org.hwo.net.serverobjects.dav;
import org.w3c.dom.Element;
public class DAVPropertyDAVNS extends DAVPropertyNamespace {
public DAVPropertyDAVNS(String namespaceURI) {
super(namespaceURI);
}
@Override
public Element getPropertyValue(DAVResource resource, DAVProperty property) {
if (property.getPropertyName().equals(""))
{
}
return null;
}
@Override
public void setPropertyValue(DAVResource resource, DAVProperty property,
Element value) {
}
}

View File

@ -0,0 +1,52 @@
package org.hwo.net.serverobjects.dav;
import org.w3c.dom.Element;
public class DAVPropertyName {
private String namespaceURI;
private String propertyName;
public static DAVPropertyName fromXML(Element property)
{
DAVPropertyName dpn = new DAVPropertyName();
dpn.setNamespaceURI(property.getNamespaceURI());
dpn.setPropertyName(property.getLocalName());
return dpn;
}
public DAVPropertyName()
{
setNamespaceURI(null);
setPropertyName(null);
}
public DAVPropertyName(String propertyName)
{
setNamespaceURI(propertyName);
setPropertyName(null);
}
public DAVPropertyName(String propertyName,String namespaceURI)
{
setNamespaceURI(propertyName);
setPropertyName(namespaceURI);
}
public String getNamespaceURI() {
return namespaceURI;
}
public void setNamespaceURI(String namespaceURI) {
this.namespaceURI = namespaceURI;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
}

View File

@ -0,0 +1,23 @@
package org.hwo.net.serverobjects.dav;
import java.util.LinkedList;
import org.hwo.xml.NodeListIterator;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DAVPropertyNameList extends LinkedList<DAVPropertyName>{
public static DAVPropertyNameList fromXML(Element prop)
{
DAVPropertyNameList l = new DAVPropertyNameList();
NodeList props = prop.getChildNodes();
for (Element p:new NodeListIterator(props))
l.add(DAVPropertyName.fromXML(p));
return l;
}
}

View File

@ -0,0 +1,40 @@
package org.hwo.net.serverobjects.dav;
import java.util.Hashtable;
import org.w3c.dom.Element;
public abstract class DAVPropertyNamespace {
private static Hashtable<String,DAVPropertyNamespace>
namespaces;
public static DAVPropertyNamespace getNameSpace(String namespaceURI)
{
if (!namespaces.containsKey(namespaceURI))
namespaces.put(namespaceURI, new DAVDeadPropertyNS(namespaceURI));
return namespaces.get(namespaceURI);
}
private String namespaceURI;
public DAVPropertyNamespace(String namespaceURI)
{
this.namespaceURI = namespaceURI;
}
public String getNamespaceURI() {
return namespaceURI;
}
abstract public Element getPropertyValue(DAVResource resource,DAVProperty property);
abstract public void setPropertyValue(DAVResource resource,DAVProperty property,Element value);
static {
namespaces = new Hashtable<String, DAVPropertyNamespace>();
}
}

View File

@ -0,0 +1,153 @@
package org.hwo.net.serverobjects.dav;
import java.util.Hashtable;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.hwo.xml.NodeListIterator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
public class DAVResource {
private byte[] content;
private String name;
private String mimetype;
private DAVResource
parent;
private String uri;
private Hashtable<String, Document>
properties;
private DocumentBuilder
builder;
public DAVResource(String name)
{
this.setContent(new byte[0]);
this.setName(name);
this.setMimetype("application/octet-stream");
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
try {
builder = f.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
protected void setParent(DAVResource resource)
{
this.parent = resource;
this.setURI(constructURI());
}
public DAVResource getParent()
{
return this.parent;
}
protected String constructURI()
{
if (parent != null)
return String.format("%s/%s",parent.constructURI(),name);
return String.format("/%s", name);
}
public boolean isCollection()
{
return false;
}
public DAVResource[] getChildren()
{
return new DAVResource[0];
}
public int getContentLength()
{
return content.length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getURI()
{
return this.uri;
}
private void setURI(String uri)
{
this.uri = uri;
}
public String getMimetype() {
return mimetype;
}
public void setMimetype(String mimetype) {
this.mimetype = mimetype;
}
public Document getPropertyDocument(String namespaceURI)
{
if (!properties.containsKey(namespaceURI))
{
Document d = builder.newDocument();
d.appendChild(d.createElementNS(namespaceURI, "properties"));
properties.put(namespaceURI, d);
}
return properties.get(namespaceURI);
}
public Element getProperty(String namespaceURI,String propertyName)
{
Document pdoc = getPropertyDocument(namespaceURI);
NodeList nl = pdoc.getElementsByTagNameNS(namespaceURI, propertyName);
if (nl.getLength()>0)
return (Element)nl.item(0);
return null;
}
public void setProperty(String namespaceURI,String propertyName,Element value)
{
Document pdoc = getPropertyDocument(namespaceURI);
Element propertyElement = getProperty(namespaceURI, propertyName);
if (propertyElement == null)
{
propertyElement = pdoc.createElementNS(namespaceURI, propertyName);
pdoc.getDocumentElement().appendChild( propertyElement );
}
while (propertyElement.hasChildNodes())
propertyElement.removeChild(propertyElement.getFirstChild());
Element imp = (Element)pdoc.importNode(propertyElement, true);
for (Element c: NodeListIterator.create(imp.getChildNodes()))
propertyElement.appendChild( c );
}
}