/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: Head.java 541515 2007-05-25 02:45:06Z vgritsenko $
*/
package org.apache.xindice.webadmin.webdav.components;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.data.Entry;
import org.apache.xindice.webadmin.util.MimeTable;
import org.apache.xindice.webadmin.webdav.DAVRequest;
import org.apache.xindice.webadmin.webdav.DAVResponse;
import org.apache.xindice.webadmin.Location;
import org.apache.xindice.xml.TextWriter;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.w3c.dom.Document;
/**
* This class implements the Head HTTP command for WebDAV operations on
* Xindice.
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
* @version $Revision: 541515 $, $Date: 2007-05-24 22:45:06 -0400 (Thu, 24 May 2007) $
*/
public class Head implements DAVComponent {
private static final Log log = LogFactory.getLog(Head.class);
public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
Collection col = target.getCollection();
String name = target.getName();
if (target.isRoot() || (col != null && name == null)) {
if (log.isDebugEnabled()) {
log.debug("Method Head not allowed on collections!");
}
res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
if (col == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
try {
Entry resource = col.getEntry(name);
if (resource != null) {
byte[] resultBytes;
if (resource.getEntryType() == Entry.DOCUMENT) {
resultBytes = TextWriter.toString((Document) resource.getValue()).getBytes();
res.setContentType(MimeTable.XML_MIME_TYPE);
} else {
resultBytes = (byte[]) resource.getValue();
res.setContentType(MimeTable.getMimeType(name));
}
if (resource.getModificationTime() != 0) {
res.addDateHeader("Last-Modified", resource.getModificationTime());
}
res.setContentLength(resultBytes.length);
} else {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
} catch (DBException e) {
log.error("Could not get resource " + name + " from collection " + col.getCanonicalName(), e);
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}