001/* 002 * Copyright 2019 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.MalformedURLException; 022import java.nio.charset.StandardCharsets; 023import java.util.Map; 024 025import org.apache.excalibur.source.Source; 026import org.apache.excalibur.source.SourceFactory; 027import org.apache.excalibur.source.SourceNotFoundException; 028import org.apache.excalibur.source.SourceValidity; 029import org.apache.excalibur.source.impl.validity.TimeStampValidity; 030 031/** 032 * Fake factory for SourceMapCache 033 */ 034public class SourceMapSourceFactory implements SourceFactory 035{ 036 /** Parameter last modified */ 037 public static final String LAST_MODIFIED = "lastmodified"; 038 /** Parameter content*/ 039 public static final String CONTENT = "content"; 040 /** Declared protocol */ 041 public static final String SCHEME = "sourcemap"; 042 043 public Source getSource(String location, Map parameters) throws IOException, MalformedURLException 044 { 045 return new SourceMapSource(location, (String) parameters.get(CONTENT), (Long) parameters.get(LAST_MODIFIED)); 046 } 047 048 public void release(Source source) 049 { 050 // Nothing to do 051 } 052 053 /** 054 * The Source of a source map 055 */ 056 public static final class SourceMapSource implements Source 057 { 058 private String _content; 059 private Long _lastModified; 060 private String _location; 061 private TimeStampValidity _timeStampValidity; 062 063 /** 064 * Default constructor for a memory source 065 * @param location The location of the source 066 * @param content The content of the source 067 * @param lastModified The last modified date 068 */ 069 public SourceMapSource(String location, String content, Long lastModified) 070 { 071 _location = location; 072 _content = content; 073 _lastModified = lastModified; 074 } 075 076 public boolean exists() 077 { 078 return true; 079 } 080 081 public InputStream getInputStream() throws IOException, SourceNotFoundException 082 { 083 return new ByteArrayInputStream(_content.getBytes(StandardCharsets.UTF_8)); 084 } 085 086 public String getURI() 087 { 088 return _location; 089 } 090 091 public String getScheme() 092 { 093 return SCHEME; 094 } 095 096 public SourceValidity getValidity() 097 { 098 if (_timeStampValidity == null) 099 { 100 _timeStampValidity = new TimeStampValidity(_lastModified); 101 } 102 return _timeStampValidity; 103 } 104 105 public void refresh() 106 { 107 // do nothing 108 } 109 110 public String getMimeType() 111 { 112 return "application/json"; 113 } 114 115 public long getContentLength() 116 { 117 return _content.length(); 118 } 119 120 public long getLastModified() 121 { 122 return _lastModified; 123 } 124 } 125}