001/* 002 * Copyright 2016 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 */ 016 017package org.ametys.plugins.core.ui.resources.css.sass; 018 019import java.io.IOException; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.ResourceNotFoundException; 025import org.apache.excalibur.source.Source; 026 027import org.ametys.core.resources.SimpleResourceHandler; 028 029/** 030 * Reader for uncompiled SASS files. 031 */ 032public class UncompiledSassResourceHandler extends SimpleResourceHandler 033{ 034 @Override 035 public Source setup(String location, Map objectModel, Parameters par, boolean readForDownload) throws ProcessingException, IOException 036 { 037 Source source = null; 038 // If the file exists at the original location, return it without any processing 039 040 source = _getSource(location); 041 if (source == null) 042 { 043 int lastSlashIndex = location.lastIndexOf("/"); 044 if (lastSlashIndex + 2 < location.length()) 045 { 046 // Appends underscore to the beginning of the file name 047 String underscorelocation = location.substring(0, lastSlashIndex + 1) + "_" + location.substring(lastSlashIndex + 1); 048 source = _getSource(underscorelocation); 049 } 050 } 051 052 if (source == null) 053 { 054 throw new ResourceNotFoundException("Resource not found for URI : " + location); 055 } 056 057 _source = source; 058 return _source; 059 } 060 061 private Source _getSource(String location) 062 { 063 Source source = null; 064 try 065 { 066 source = _resolver.resolveURI(location); 067 } 068 catch (IOException e) 069 { 070 // Nothing 071 } 072 073 if (source == null || !source.exists()) 074 { 075 _resolver.release(source); 076 return null; 077 } 078 079 return source; 080 } 081 082 @Override 083 public String getMimeType() 084 { 085 return "text/css"; 086 } 087}