001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.webdav.resource; 023 024import static javax.ws.rs.core.Response.Status.OK; 025 026import java.io.IOException; 027import java.net.URI; 028import java.net.URISyntaxException; 029import java.util.ArrayList; 030import java.util.List; 031 032import javax.servlet.http.HttpServletRequest; 033import javax.ws.rs.GET; 034import javax.ws.rs.HeaderParam; 035import javax.ws.rs.Produces; 036import javax.ws.rs.core.Context; 037import javax.ws.rs.core.Response; 038import javax.ws.rs.core.UriBuilder; 039import javax.ws.rs.core.UriInfo; 040import javax.xml.bind.JAXBException; 041import javax.xml.bind.Unmarshaller; 042 043import net.java.dev.webdav.jaxrs.methods.PROPFIND; 044import net.java.dev.webdav.jaxrs.xml.elements.HRef; 045import net.java.dev.webdav.jaxrs.xml.elements.MultiStatus; 046import net.java.dev.webdav.jaxrs.xml.elements.Prop; 047import net.java.dev.webdav.jaxrs.xml.elements.PropFind; 048import net.java.dev.webdav.jaxrs.xml.elements.PropStat; 049import net.java.dev.webdav.jaxrs.xml.elements.Status; 050import net.java.dev.webdav.jaxrs.xml.properties.LockDiscovery; 051import net.java.dev.webdav.jaxrs.xml.properties.SupportedLock; 052 053import org.apache.commons.text.StringEscapeUtils; 054import org.apache.commons.logging.Log; 055import org.apache.commons.logging.LogFactory; 056import org.nuxeo.ecm.core.api.DocumentModel; 057import org.nuxeo.ecm.webdav.backend.Backend; 058import org.nuxeo.ecm.webdav.jaxrs.IsFolder; 059import org.nuxeo.ecm.webdav.jaxrs.Util; 060 061/** 062 * A resource for folder-like objects in the repository. 063 */ 064public class FolderResource extends ExistingResource { 065 066 private static final Log log = LogFactory.getLog(FolderResource.class); 067 068 public FolderResource(String path, DocumentModel doc, HttpServletRequest request, Backend backend) { 069 super(path, doc, request, backend); 070 } 071 072 @GET 073 @Produces("text/html") 074 public String get() { 075 StringBuilder sb = new StringBuilder(); 076 sb.append("<html><body><p>"); 077 sb.append("Folder listing for "); 078 sb.append(path); 079 sb.append("/"); 080 sb.append("</p>\n<ul>\n"); 081 List<DocumentModel> children = backend.getChildren(doc.getRef()); 082 for (DocumentModel child : children) { 083 String name = backend.getDisplayName(child); 084 String qname = StringEscapeUtils.escapeHtml4(name); 085 sb.append("<li><a href=\""); 086 sb.append(qname); 087 if (child.isFolder()) { 088 sb.append("/"); 089 } 090 sb.append("\">"); 091 sb.append(qname); 092 sb.append("</a></li>\n"); 093 } 094 sb.append("</ul></body>\n"); 095 return sb.toString(); 096 } 097 098 @PROPFIND 099 @Produces({ "application/xml", "text/xml" }) 100 public Response propfind(@Context UriInfo uriInfo, @HeaderParam("depth") String depth) 101 throws IOException, JAXBException, URISyntaxException { 102 103 if (depth == null) { 104 depth = "1"; 105 } 106 107 Unmarshaller u = Util.getUnmarshaller(); 108 109 Prop prop = null; 110 if (request.getInputStream() != null && request.getContentLength() > 0) { 111 PropFind propFind; 112 try { 113 propFind = (PropFind) u.unmarshal(request.getInputStream()); 114 } catch (JAXBException e) { 115 log.error(e); 116 // FIXME: check this is the right response code 117 return Response.status(400).build(); 118 } 119 prop = propFind.getProp(); 120 // Util.printAsXml(prop); 121 } 122 123 final net.java.dev.webdav.jaxrs.xml.elements.Response response; 124 response = createResponse(doc, uriInfo, prop, false); 125 126 if (!doc.isFolder() || depth.equals("0")) { 127 return Response.status(207).entity(new MultiStatus(response)).build(); 128 } 129 130 List<net.java.dev.webdav.jaxrs.xml.elements.Response> responses = new ArrayList<net.java.dev.webdav.jaxrs.xml.elements.Response>(); 131 responses.add(response); 132 133 List<DocumentModel> children = backend.getChildren(doc.getRef()); 134 for (DocumentModel child : children) { 135 net.java.dev.webdav.jaxrs.xml.elements.Response childResponse; 136 childResponse = createResponse(child, uriInfo, prop); 137 138 responses.add(childResponse); 139 } 140 141 MultiStatus st = new MultiStatus( 142 responses.toArray(new net.java.dev.webdav.jaxrs.xml.elements.Response[responses.size()])); 143 // printXml(st); 144 return Response.status(207).entity(st).build(); 145 } 146 147 protected net.java.dev.webdav.jaxrs.xml.elements.Response createResponse(DocumentModel doc, UriInfo uriInfo, 148 Prop prop) throws URISyntaxException { 149 return createResponse(doc, uriInfo, prop, true); 150 } 151 152 protected net.java.dev.webdav.jaxrs.xml.elements.Response createResponse(DocumentModel doc, UriInfo uriInfo, 153 Prop prop, boolean append) throws URISyntaxException { 154 PropStatBuilderExt props = getPropStatBuilderExt(doc, uriInfo); 155 PropStat propStatFound = props.build(); 156 PropStat propStatNotFound = null; 157 if (prop != null) { 158 propStatNotFound = props.notFound(prop); 159 } 160 161 net.java.dev.webdav.jaxrs.xml.elements.Response response; 162 UriBuilder uriBuilder = uriInfo.getRequestUriBuilder(); 163 if (append) { 164 String path = new URI(null, backend.getDisplayName(doc), null).toASCIIString(); 165 uriBuilder.path(path); 166 } 167 URI uri = uriBuilder.build(); 168 if (doc.isFolder()) { 169 PropStat folderPropStat = new PropStat( 170 new Prop(new LockDiscovery(), new SupportedLock(), new IsFolder("t")), new Status(OK)); 171 if (propStatNotFound != null) { 172 response = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(uri), null, null, null, 173 propStatFound, propStatNotFound, folderPropStat); 174 } else { 175 response = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(uri), null, null, null, 176 propStatFound, folderPropStat); 177 } 178 } else { 179 if (propStatNotFound != null) { 180 response = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(uri), null, null, null, 181 propStatFound, propStatNotFound); 182 } else { 183 response = new net.java.dev.webdav.jaxrs.xml.elements.Response(new HRef(uri), null, null, null, 184 propStatFound); 185 } 186 } 187 return response; 188 } 189 190}