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