001/*
002 * Copyright 2008, 2009 Daniel MANZKE
003 *
004 * This file is part of webdav-jaxrs.
005 *
006 * webdav-jaxrs is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * webdav-jaxrs is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with webdav-jaxrs.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.nuxeo.ecm.webdav.resource;
020
021import net.java.dev.webdav.jaxrs.xml.elements.Prop;
022import net.java.dev.webdav.jaxrs.xml.elements.PropStat;
023import net.java.dev.webdav.jaxrs.xml.properties.*;
024
025import org.nuxeo.ecm.webdav.jaxrs.IsHidden;
026import org.w3c.dom.Element;
027
028import javax.ws.rs.core.Response.Status;
029
030import java.util.*;
031
032/**
033 * Copy-pasted then modified from jaxrs-webdav.
034 */
035public class PropStatBuilderExt {
036    private List<Object> properties;
037
038    private Status status;
039
040    private Set<String> names;
041
042    public PropStatBuilderExt() {
043        properties = new LinkedList<Object>();
044        names = new HashSet<String>();
045    }
046
047    public PropStatBuilderExt creationDate(Date dateTime) {
048        if (!names.contains("creationdate")) {
049            CreationDate create = new CreationDate(dateTime);
050            properties.add(create);
051            names.add("creationdate");
052        }
053
054        return this;
055    }
056
057    public PropStatBuilderExt lastModified(Date dateTime) {
058        if (!names.contains("getlastmodified")) {
059            GetLastModified lastModified = new GetLastModified(dateTime);
060            properties.add(lastModified);
061            names.add("getlastmodified");
062        }
063
064        return this;
065    }
066
067    public PropStatBuilderExt contentLength(long length) {
068        if (!names.contains("getcontentlength")) {
069            GetContentLength contentLength = new GetContentLength(length);
070            properties.add(contentLength);
071            names.add("getcontentlength");
072        }
073
074        return this;
075    }
076
077    public PropStatBuilderExt isResource(long length, String mime) {
078        if (!names.contains("getcontenttype")) {
079            GetContentType type = new GetContentType(mime);
080            properties.add(type);
081            names.add("getcontenttype");
082            GetContentLength contentLength = new GetContentLength(length);
083            properties.add(contentLength);
084            names.add("getcontentlength");
085        }
086
087        return this;
088    }
089
090    public PropStatBuilderExt isCollection() {
091        if (!names.contains("resourcetype")) {
092            ResourceType type = ResourceType.COLLECTION;
093            properties.add(type);
094            names.add("resourcetype");
095        }
096
097        return this;
098    }
099
100    public PropStatBuilderExt displayName(String displayName) {
101        if (!names.contains("displayname")) {
102            DisplayName name = new DisplayName(displayName);
103            properties.add(name);
104            names.add("displayname");
105        }
106
107        return this;
108    }
109
110    public PropStatBuilderExt isHidden(boolean hide) {
111        if (!names.contains("ishidden")) {
112            IsHidden hidden = new IsHidden(hide ? 1 : 0);
113            properties.add(hidden);
114            names.add("ishidden");
115        }
116
117        return this;
118    }
119
120    public PropStat notFound(Prop allprops) {
121        boolean empty = true;
122        List<Object> notFound = new ArrayList<Object>();
123        for (Object prop : allprops.getProperties()) {
124            if (prop instanceof Element) {
125                Element element = (Element) prop;
126                String name = element.getLocalName();
127                if (!names.contains(name)) {
128                    notFound.add(prop);
129                    empty = false;
130                }
131            }
132        }
133
134        PropStat stat = null;
135        if (!empty) {
136            Object[] objects = notFound.toArray(new Object[properties.size()]);
137            Prop prop = new Prop(objects);
138            stat = new PropStat(prop, new net.java.dev.webdav.jaxrs.xml.elements.Status(Status.NOT_FOUND));
139        }
140
141        return stat;
142    }
143
144    public PropStatBuilderExt status(Status status) {
145        this.status = status;
146
147        return this;
148    }
149
150    public PropStat build() {
151        Object[] objects = properties.toArray(new Object[properties.size()]);
152        Prop prop = new Prop(objects);
153        PropStat stat = new PropStat(prop, new net.java.dev.webdav.jaxrs.xml.elements.Status(status));
154
155        return stat;
156    }
157}