001/*
002 *  Copyright 2020 Anyware Services
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 */
016package org.ametys.cms.data;
017
018import java.io.IOException;
019import java.io.InputStream;
020
021import org.apache.excalibur.source.Source;
022import org.apache.excalibur.source.SourceNotFoundException;
023import org.apache.excalibur.source.SourceValidity;
024import org.apache.excalibur.source.impl.validity.TimeStampValidity;
025
026import org.ametys.core.cocoon.source.NamedSource;
027import org.ametys.core.util.DateUtils;
028import org.ametys.core.util.POIHolder;
029
030/**
031 * {@link Source} representing a binary.
032 */
033public class BinarySource implements NamedSource, POIHolder
034{
035    private Binary _binary;
036    private String _filename;
037    private String _uri;
038    private String _scheme;
039    
040    /**
041     * Constructor.
042     * @param binary the {@link Binary}.
043     * @param filename the filename.
044     * @param uri this source's uri.
045     * @param scheme this source's uri's scheme
046     */
047    public BinarySource(Binary binary, String filename, String uri, String scheme)
048    {
049        _binary = binary;
050        _filename = filename;
051        _uri = uri;
052        _scheme = scheme;
053    }
054    
055    public boolean exists()
056    {
057        return _binary != null && _binary.getFilename().equals(_filename);
058    }
059
060    public InputStream getInputStream() throws IOException, SourceNotFoundException
061    {
062        return _binary.getInputStream();
063    }
064
065    public String getURI()
066    {
067        return _uri;
068    }
069
070    public String getScheme()
071    {
072        return _scheme;
073    }
074
075    public SourceValidity getValidity()
076    {
077        return new TimeStampValidity(getLastModified());
078    }
079    
080    public String getName()
081    {
082        return _binary.getFilename();
083    }
084
085    public void refresh()
086    {
087        // does nothing
088    }
089
090    public String getMimeType()
091    {
092        return _binary.getMimeType();
093    }
094
095    public long getContentLength()
096    {
097        return _binary.getLength();
098    }
099
100    public long getLastModified()
101    {
102        return _binary != null ? DateUtils.asDate(_binary.getLastModificationDate()).getTime() : 0;
103    }
104    
105    public Point getPOI()
106    {
107        return _binary.getPOI();
108    }
109}