|
| 1 | +/* |
| 2 | + * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.ocpsoft.rewrite.config.tuckey; |
| 17 | + |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +import javax.servlet.ServletContext; |
| 23 | + |
| 24 | +import org.jboss.logging.Logger; |
| 25 | +import org.tuckey.web.filters.urlrewrite.Conf; |
| 26 | +import org.tuckey.web.filters.urlrewrite.UrlRewriter; |
| 27 | + |
| 28 | +import com.ocpsoft.rewrite.config.Configuration; |
| 29 | +import com.ocpsoft.rewrite.config.ConfigurationBuilder; |
| 30 | +import com.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 34 | + * |
| 35 | + */ |
| 36 | +public class TuckeyConfigurationProvider extends HttpConfigurationProvider |
| 37 | +{ |
| 38 | + Logger log = Logger.getLogger(TuckeyConfigurationProvider.class); |
| 39 | + private UrlRewriter urlRewriter; |
| 40 | + |
| 41 | + @Override |
| 42 | + public Configuration getConfiguration(final ServletContext context) |
| 43 | + { |
| 44 | + loadUrlRewriteConfig(context); |
| 45 | + ConfigurationBuilder config = ConfigurationBuilder.begin(); |
| 46 | + if (urlRewriter != null) |
| 47 | + { |
| 48 | + config.addRule(new TuckeyRewriteRuleAdaptor(urlRewriter)); |
| 49 | + } |
| 50 | + return config; |
| 51 | + } |
| 52 | + |
| 53 | + private void loadUrlRewriteConfig(final ServletContext context) |
| 54 | + { |
| 55 | + String confPath = "/WEB-INF/urlrewrite.xml"; |
| 56 | + InputStream inputStream = context.getResourceAsStream(confPath); |
| 57 | + // attempt to retrieve from location other than local WEB-INF |
| 58 | + if (inputStream == null) { |
| 59 | + inputStream = ClassLoader.getSystemResourceAsStream(confPath); |
| 60 | + } |
| 61 | + URL confUrl = null; |
| 62 | + try { |
| 63 | + confUrl = context.getResource(confPath); |
| 64 | + } |
| 65 | + catch (MalformedURLException e) { |
| 66 | + log.debug(e); |
| 67 | + } |
| 68 | + String confUrlStr = null; |
| 69 | + if (confUrl != null) { |
| 70 | + confUrlStr = confUrl.toString(); |
| 71 | + } |
| 72 | + if (inputStream == null) { |
| 73 | + log.error("unable to find urlrewrite conf file at " + confPath); |
| 74 | + // set the writer back to null |
| 75 | + if (urlRewriter != null) { |
| 76 | + log.error("unloading existing conf"); |
| 77 | + urlRewriter = null; |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + else { |
| 82 | + Conf conf = new Conf(context, inputStream, confPath, confUrlStr, false); |
| 83 | + checkConfLocal(conf); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void checkConfLocal(final Conf conf) |
| 88 | + { |
| 89 | + if (log.isDebugEnabled()) { |
| 90 | + if (conf.getRules() != null) { |
| 91 | + log.debug("inited with " + conf.getRules().size() + " rules"); |
| 92 | + } |
| 93 | + log.debug("conf is " + (conf.isOk() ? "ok" : "NOT ok")); |
| 94 | + } |
| 95 | + if (conf.isOk() && conf.isEngineEnabled()) { |
| 96 | + urlRewriter = new UrlRewriter(conf); |
| 97 | + log.info("loaded (conf ok)"); |
| 98 | + |
| 99 | + } |
| 100 | + else { |
| 101 | + if (!conf.isOk()) { |
| 102 | + log.error("Conf failed to load"); |
| 103 | + } |
| 104 | + if (!conf.isEngineEnabled()) { |
| 105 | + log.error("Engine explicitly disabled in conf"); // not really an error but we want ot to show in logs |
| 106 | + } |
| 107 | + if (urlRewriter != null) { |
| 108 | + log.error("unloading existing conf"); |
| 109 | + urlRewriter = null; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int priority() |
| 116 | + { |
| 117 | + return 100; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments