1 package org.jahia.services.urlrewriting; 2 3 import org.jahia.services.JahiaInitializableService; 4 import org.jahia.settings.SettingsBean; 5 import org.jahia.exceptions.JahiaInitializationException; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import org.apache.commons.digester.Digester; 9 import org.apache.commons.digester.Rule; 10 import java.util.Properties ; 11 import java.io.IOException ; 12 import org.xml.sax.SAXException ; 13 import java.io.File ; 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.InvocationTargetException ; 16 17 25 26 public class URLRewritingService extends JahiaInitializableService { 27 28 private static org.apache.log4j.Logger logger = 29 org.apache.log4j.Logger.getLogger(URLRewritingService.class); 30 31 private static URLRewritingService singletonInstance = null; 32 33 private Digester digester; 34 private ArrayList rewriteRules = new ArrayList (); 35 36 private URLRewritingService() { 37 } 38 39 45 public synchronized static URLRewritingService getInstance() { 46 if (singletonInstance == null) { 47 singletonInstance = new URLRewritingService(); 48 } 49 return singletonInstance; 50 } 51 52 53 62 public void init (SettingsBean jSettings) 63 throws JahiaInitializationException { 64 try { 65 load(jSettings.getJahiaEtcDiskPath() + File.separator + "config" + 66 File.separator + "url-rewriting.xml"); 67 } catch (Throwable t) { 68 throw new JahiaInitializationException("Error while loading URL rewriting configuration file", t); 69 } 70 } 71 72 public String getOriginalFromRewritten(String rewrittenURL) { 73 Iterator ruleIter = rewriteRules.iterator(); 74 while (ruleIter.hasNext()) { 75 RewriteRule curRule = (RewriteRule) ruleIter.next(); 76 String originalURL = curRule.getOriginalFromRewritten(rewrittenURL); 77 if (originalURL != null) { 78 return originalURL; 79 } 80 } 81 return null; 82 } 83 84 public String getRewrittenFromOriginal(String originalURL) { 85 Iterator ruleIter = rewriteRules.iterator(); 86 while (ruleIter.hasNext()) { 87 RewriteRule curRule = (RewriteRule) ruleIter.next(); 88 String rewrittenURL = curRule.getRewrittenFromOriginal(originalURL); 89 if (rewrittenURL != null) { 90 return rewrittenURL; 91 } 92 } 93 return null; 94 95 } 96 97 private void load(String urlRewritingConfigFileName) 98 throws IOException , SAXException { 99 initDigester(); 100 File urlRewritingConfigFile = new File (urlRewritingConfigFileName); 101 this.digester.parse(urlRewritingConfigFile); 102 } 103 104 private void initDigester() { 105 this.digester = new Digester(); 106 107 AddRewriteRuleRule addRewriteRuleRule = 108 new AddRewriteRuleRule(); 109 digester.addRule( 110 "rewrite-rules/rule", 111 addRewriteRuleRule); 112 digester.addRule( 113 "rewrite-rules/rule/original-from-rewritten/property", 114 addRewriteRuleRule.addOriginalFromRewrittenPropsRule); 115 digester.addRule( 116 "rewrite-rules/rule/rewritten-from-original/property", 117 addRewriteRuleRule.addRewrittenFromOriginalPropsRule); 118 } 119 120 final class AddRewriteRuleRule extends Rule { 121 122 private Properties originalFromRewrittenProps = new Properties (); 123 private Properties rewrittenFromOriginalProps = new Properties (); 124 125 AddPropertyRule addOriginalFromRewrittenPropsRule = new AddPropertyRule(originalFromRewrittenProps); 126 AddPropertyRule addRewrittenFromOriginalPropsRule = new AddPropertyRule(rewrittenFromOriginalProps); 127 128 private Properties properties = new Properties (); 129 130 public void begin( 131 String namespace, 132 String name, 133 org.xml.sax.Attributes attributes) 134 throws Exception { 135 for (int i = 0; i < attributes.getLength(); i++) { 136 properties.setProperty( 137 attributes.getQName(i), 138 attributes.getValue(i)); 139 } 140 } 141 142 public void end(String namespace, String name) throws Exception { 143 String className = properties.getProperty("class"); 144 try { 145 Class rewriteRuleClass = Class.forName(className); 146 Class [] parameterTypes = new Class [2]; 147 parameterTypes[0] = Properties .class; 148 parameterTypes[1] = Properties .class; 149 Constructor rewriteRuleConstructor = rewriteRuleClass.getConstructor(parameterTypes); 150 Object [] initArgs = new Object [2]; 151 initArgs[0] = originalFromRewrittenProps; 152 initArgs[1] = rewrittenFromOriginalProps; 153 RewriteRule rewriteRule = (RewriteRule) rewriteRuleConstructor.newInstance(initArgs); 154 rewriteRules.add(rewriteRule); 155 } catch (ClassNotFoundException cnfe) { 156 logger.error("Couldn't find validator class " + className, cnfe); 157 } catch (IllegalAccessException iae) { 158 logger.error("Error accessing validator class " + className, iae); 159 } catch (InstantiationException ie) { 160 logger.error("Validation class "+className+" instantiation exception", ie); 161 } catch (InvocationTargetException ite) { 162 logger.error("Invocation target exception while invoking validator class " + className, ite); 163 } catch (NoSuchMethodException nsme) { 164 logger.error("No such method exception while creating instance of validator class "+className, nsme); 165 } 166 originalFromRewrittenProps = new Properties (); 167 rewrittenFromOriginalProps = new Properties (); 168 properties = new Properties (); 169 } 170 171 172 final class AddPropertyRule extends Rule { 173 174 Properties properties = new Properties (); 175 Properties targetProperties; 176 177 public AddPropertyRule(Properties targetProperties) { 178 this.targetProperties = targetProperties; 179 } 180 181 182 public void begin( 183 String namespace, 184 String name, 185 org.xml.sax.Attributes attributes) 186 throws Exception { 187 for (int i = 0; i < attributes.getLength(); i++) { 188 properties.setProperty( 189 attributes.getQName(i), 190 attributes.getValue(i)); 191 } 192 } 193 public void end(String namespace, String name) 194 throws Exception { 195 targetProperties.setProperty(properties.getProperty("name"), properties.getProperty("value")); 196 properties = new Properties (); 197 } 198 199 } 200 } 201 202 } | Popular Tags |