001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.webdav;
018
019import org.apache.commons.vfs2.FileContent;
020import org.apache.commons.vfs2.FileContentInfo;
021import org.apache.commons.vfs2.FileContentInfoFactory;
022import org.apache.commons.vfs2.FileSystemException;
023import org.apache.commons.vfs2.impl.DefaultFileContentInfo;
024import org.apache.commons.vfs2.provider.URLFileName;
025import org.apache.commons.vfs2.util.FileObjectUtils;
026import org.apache.jackrabbit.webdav.property.DavProperty;
027import org.apache.jackrabbit.webdav.property.DavPropertyName;
028import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
029import org.apache.jackrabbit.webdav.property.DavPropertySet;
030
031/**
032 * Determines the content information for files accessed via WebDAV.
033 *
034 * @since 2.0
035 */
036public class WebdavFileContentInfoFactory implements FileContentInfoFactory {
037
038    /**
039     * Constructs a new instance.
040     */
041    public WebdavFileContentInfoFactory() {
042        // empty
043    }
044
045    @Override
046    public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
047        final WebdavFileObject file = (WebdavFileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile());
048
049        String contentType = null;
050        String contentEncoding = null;
051
052        final DavPropertyNameSet nameSet = new DavPropertyNameSet();
053        nameSet.add(DavPropertyName.GETCONTENTTYPE);
054        final DavPropertySet propertySet = file.getProperties((URLFileName) file.getName(), nameSet, true);
055
056        DavProperty property = propertySet.get(DavPropertyName.GETCONTENTTYPE);
057        if (property != null) {
058            contentType = (String) property.getValue();
059        }
060        property = propertySet.get(WebdavFileObject.RESPONSE_CHARSET);
061        if (property != null) {
062            contentEncoding = (String) property.getValue();
063        }
064
065        return new DefaultFileContentInfo(contentType, contentEncoding);
066    }
067}