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.webdav4;
018
019import java.util.Objects;
020
021import org.apache.commons.vfs2.FileContent;
022import org.apache.commons.vfs2.FileContentInfo;
023import org.apache.commons.vfs2.FileContentInfoFactory;
024import org.apache.commons.vfs2.FileSystemException;
025import org.apache.commons.vfs2.impl.DefaultFileContentInfo;
026import org.apache.commons.vfs2.provider.GenericURLFileName;
027import org.apache.commons.vfs2.util.FileObjectUtils;
028import org.apache.jackrabbit.webdav.property.DavProperty;
029import org.apache.jackrabbit.webdav.property.DavPropertyName;
030import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
031import org.apache.jackrabbit.webdav.property.DavPropertySet;
032
033/**
034 * Determines the content information for files accessed via WebDAV.
035 *
036 * @since 2.5.0
037 */
038public class Webdav4FileContentInfoFactory implements FileContentInfoFactory {
039
040    /**
041     * Constructs a new instance.
042     */
043    public Webdav4FileContentInfoFactory() {
044        // empty
045    }
046
047    @Override
048    public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
049        final Webdav4FileObject file = (Webdav4FileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile());
050
051        String contentType = null;
052        String contentEncoding = null;
053
054        final DavPropertyNameSet nameSet = new DavPropertyNameSet();
055        nameSet.add(DavPropertyName.GETCONTENTTYPE);
056        final DavPropertySet propertySet = file.getProperties((GenericURLFileName) file.getName(), nameSet, true);
057
058        DavProperty<?> property = propertySet.get(DavPropertyName.GETCONTENTTYPE);
059        if (property != null) {
060            contentType = Objects.toString(property.getValue(), null);
061        }
062        property = propertySet.get(Webdav4FileObject.RESPONSE_CHARSET);
063        if (property != null) {
064            contentEncoding = Objects.toString(property.getValue(), null);
065        }
066
067        return new DefaultFileContentInfo(contentType, contentEncoding);
068    }
069}