001/* 002 * (C) Copyright 2015-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Kevin Leturc 018 */ 019package org.nuxeo.ecm.liveconnect.core; 020 021import java.util.Objects; 022 023import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry; 024import org.nuxeo.ecm.platform.mimetype.service.MimetypeRegistryService; 025import org.nuxeo.runtime.api.Framework; 026 027import com.google.common.base.MoreObjects; 028import com.google.common.base.MoreObjects.ToStringHelper; 029 030/** 031 * Basic implementation of {@link LiveConnectFile}. 032 * 033 * @since 8.1 034 */ 035public abstract class AbstractLiveConnectFile implements LiveConnectFile { 036 037 private static final long serialVersionUID = 1L; 038 039 private final LiveConnectFileInfo info; 040 041 private String mimeType; 042 043 public AbstractLiveConnectFile(LiveConnectFileInfo info) { 044 this.info = Objects.requireNonNull(info); 045 } 046 047 @Override 048 public final LiveConnectFileInfo getInfo() { 049 return info; 050 } 051 052 /** 053 * Should be overriden by subclasses wanting to rely on a different field as mime type. 054 */ 055 @Override 056 public String getMimeType() { 057 if (mimeType == null) { 058 MimetypeRegistryService service = (MimetypeRegistryService) Framework.getService(MimetypeRegistry.class); 059 mimeType = service.getMimetypeFromFilename(getFilename()); 060 } 061 return mimeType; 062 } 063 064 /** 065 * Should be overriden by subclasses wanting to rely on a different field as encoding. 066 */ 067 @Override 068 public String getEncoding() { 069 // TODO extract from mimeType 070 return null; 071 } 072 073 @Override 074 public String toString() { 075 ToStringHelper helper = MoreObjects.toStringHelper(this); 076 helper.add("mimeType", getMimeType()); 077 helper.add("encoding", getEncoding()); 078 helper.add("filename", getFilename()); 079 helper.add("fileSize", getFileSize()); 080 helper.add("digest", getDigest()); 081 helper.add("info", getInfo()); 082 return helper.toString(); 083 } 084}