001/*
002 *  Copyright 2018 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.core.minimize;
017
018import java.io.ByteArrayInputStream;
019import java.io.IOException;
020import java.io.InputStream;
021import java.net.URI;
022import java.net.URISyntaxException;
023import java.nio.charset.StandardCharsets;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.avalon.framework.component.Component;
028import org.apache.excalibur.source.Source;
029import org.apache.excalibur.source.SourceNotFoundException;
030import org.apache.excalibur.source.SourceValidity;
031import org.apache.excalibur.source.impl.validity.TimeStampValidity;
032
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * A simple cache for source maps from files
037 */
038public class SourceMapCache extends AbstractLogEnabled implements Component
039{
040    /** The avalon ROLE */
041    public static final String ROLE = SourceMapCache.class.getName();
042    
043    private static Map<String, SourceMapSource> _sourceMapCache = new HashMap<>();
044    
045    /**
046     * Put a source map into the cache
047     * @param uri The uri of the source map
048     * @param content The content of the source map
049     * @param lastModified The last modified date of the source map
050     */
051    public void put(String uri, String content, Long lastModified)
052    {
053        SourceMapSource sourceMapData = new SourceMapSource(uri, content, lastModified);
054        _sourceMapCache.put(uri, sourceMapData);
055    }
056    
057    /**
058     * Get a value from the cache
059     * @param uri The uri of the source map
060     * @return The data, or null if the uri is not in the cache
061     */
062    public Source get(String uri)
063    {
064        return _sourceMapCache.getOrDefault(uri, null);
065    }
066
067    
068    /**
069     * The Source of a source map
070     */
071    public static final class SourceMapSource implements Source
072    {
073        private String _content;
074        private Long _lastModified;
075        private String _location;
076        private TimeStampValidity _timeStampValidity;
077        
078        /**
079         * Default constructor for a memory source
080         * @param location The location of the source
081         * @param content The content of the source
082         * @param lastModified The last modified date
083         */
084        public SourceMapSource(String location, String content, Long lastModified)
085        {
086            _location = location;
087            _content = content;
088            _lastModified = lastModified;
089        }
090        
091        public boolean exists()
092        {
093            return true;
094        }
095
096        public InputStream getInputStream() throws IOException, SourceNotFoundException
097        {
098            return new ByteArrayInputStream(_content.getBytes(StandardCharsets.UTF_8));
099        }
100
101        public String getURI()
102        {
103            return _location;
104        }
105
106        public String getScheme()
107        {
108            try
109            {
110                return new URI(_location).getScheme();
111            }
112            catch (URISyntaxException e)
113            {
114                return null;
115            }
116        }
117
118        public SourceValidity getValidity()
119        {
120            if (_timeStampValidity == null)
121            {
122                _timeStampValidity = new TimeStampValidity(_lastModified);
123            }
124            return _timeStampValidity;
125        }
126
127        public void refresh()
128        {
129            // do nothing
130        }
131
132        public String getMimeType()
133        {
134            return "application/json";
135        }
136
137        public long getContentLength()
138        {
139            return _content.length();
140        }
141
142        public long getLastModified()
143        {
144            return _lastModified;
145        }
146    }
147}