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.http4;
018
019import java.io.IOException;
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.util.FileObjectUtils;
027import org.apache.http.Header;
028import org.apache.http.HttpResponse;
029import org.apache.http.entity.ContentType;
030import org.apache.http.protocol.HTTP;
031
032/**
033 * Creates {@code FileContentInfoFactory} instances for http4 provider.
034 *
035 * @since 2.3
036 * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
037 */
038@Deprecated
039public class Http4FileContentInfoFactory implements FileContentInfoFactory {
040
041    /**
042     * Constructs a new instance.
043     */
044    public Http4FileContentInfoFactory() {
045        // empty
046    }
047
048    @SuppressWarnings("unchecked")
049    @Override
050    public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
051        String contentMimeType = null;
052        String contentCharset = null;
053
054        try (Http4FileObject<Http4FileSystem> http4File = (Http4FileObject<Http4FileSystem>) FileObjectUtils
055                .getAbstractFileObject(fileContent.getFile())) {
056            final HttpResponse lastHeadResponse = http4File.getLastHeadResponse();
057
058            final Header header = lastHeadResponse.getFirstHeader(HTTP.CONTENT_TYPE);
059
060            if (header != null) {
061                final ContentType contentType = ContentType.parse(header.getValue());
062                contentMimeType = contentType.getMimeType();
063
064                if (contentType.getCharset() != null) {
065                    contentCharset = contentType.getCharset().name();
066                }
067            }
068
069            return new DefaultFileContentInfo(contentMimeType, contentCharset);
070        } catch (final IOException e) {
071            throw new FileSystemException(e);
072        }
073    }
074}