Search in sources :

Example 1 with Conf

use of org.tuckey.web.filters.urlrewrite.Conf in project runwar by cfmlprojects.

the class UrlRewriteFilterTest method simpleApacheHConf.

@Test
public void simpleApacheHConf() throws IOException, ServletException, InvocationTargetException {
    Conf conf = new Conf(servletContext, new FileInputStream(new File("src/test/resources/urlrewrite.htaccess")), "urlrewrite.htaccess", null, true);
    assertTrue(conf.isEngineEnabled());
    assertTrue(conf.isOk());
    assertTrue(conf.getRules().size() > 0);
    NormalRewrittenUrl rewrittenRequest;
    MockRequest request;
    UrlRewriter urlRewriter = new UrlRewriter(conf);
    request = new MockRequest("/notthere");
    rewrittenRequest = (NormalRewrittenUrl) urlRewriter.processRequest(request, response);
    assertNotNull(rewrittenRequest);
    assertEquals("index.cfm/notthere", rewrittenRequest.getTarget());
}
Also used : Conf(org.tuckey.web.filters.urlrewrite.Conf) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) MockRequest(testutils.mock.MockRequest) File(java.io.File) FileInputStream(java.io.FileInputStream) NormalRewrittenUrl(org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl) Test(org.junit.jupiter.api.Test)

Example 2 with Conf

use of org.tuckey.web.filters.urlrewrite.Conf in project engine by craftercms.

the class SiteContextFactory method getUrlRewriter.

protected UrlRewriter getUrlRewriter(SiteContext siteContext, String[] urlRewriteConfPaths, ResourceLoader resourceLoader) {
    String siteName = siteContext.getSiteName();
    String confPath = null;
    Resource confResource = null;
    Conf conf = null;
    UrlRewriter urlRewriter = null;
    logger.info("--------------------------------------------------");
    logger.info("<Loading URL rewrite engine for site: " + siteName + ">");
    logger.info("--------------------------------------------------");
    try {
        for (int i = urlRewriteConfPaths.length - 1; i >= 0; i--) {
            Resource resource = resourceLoader.getResource(urlRewriteConfPaths[i]);
            if (resource.exists()) {
                confPath = urlRewriteConfPaths[i];
                confResource = resource;
                break;
            }
        }
        if (confResource != null) {
            // By convention, if it ends in .xml, it's an XML-style url rewrite config, else it's a mod_rewrite-style
            // url rewrite config
            boolean modRewriteStyleConf = !confPath.endsWith(".xml");
            try (InputStream is = confResource.getInputStream()) {
                conf = new Conf(servletContext, is, confPath, "", modRewriteStyleConf);
                logger.info("URL rewrite configuration loaded @ " + confResource);
            }
        }
        if (conf != null) {
            if (conf.isOk() && conf.isEngineEnabled()) {
                urlRewriter = new UrlRewriter(conf);
                logger.info("URL rewrite engine loaded for site " + siteName + " (conf ok)");
            } else {
                logger.error("URL rewrite engine not loaded, there might have been conf errors");
            }
        }
        return urlRewriter;
    } catch (Exception e) {
        throw new SiteContextCreationException("Unable to load URL rewrite conf for site '" + siteName + "'", e);
    } finally {
        logger.info("--------------------------------------------------");
        logger.info("</Loading URL rewrite engine for site: " + siteName + ">");
        logger.info("--------------------------------------------------");
    }
}
Also used : SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) Conf(org.tuckey.web.filters.urlrewrite.Conf) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException)

Example 3 with Conf

use of org.tuckey.web.filters.urlrewrite.Conf in project load-balancer by RestComm.

the class BalancerUrlRewriteFilter method init.

public void init(BalancerRunner balancerRunner) throws ServletException {
    log.info("filter init called");
    Conf conf = new BalancerConf(balancerRunner.getConfiguration().getHttpConfiguration().getUrlrewriteRule());
    checkConfLocal(conf);
}
Also used : Conf(org.tuckey.web.filters.urlrewrite.Conf)

Example 4 with Conf

use of org.tuckey.web.filters.urlrewrite.Conf in project camel by apache.

the class UrlRewriteFilter method doStart.

@Override
protected void doStart() throws Exception {
    ObjectHelper.notNull(camelContext, "camelContext");
    if (conf == null) {
        if (modRewriteConfFile != null) {
            LOG.debug("Using mod rewrite config file: {} as config for urlRewrite", modRewriteConfFile);
            InputStream is = camelContext.getClassResolver().loadResourceAsStream(modRewriteConfFile);
            if (is == null) {
                throw new IOException("Cannot load mod rewrite config file: " + modRewriteConfFile);
            }
            try {
                String text = camelContext.getTypeConverter().mandatoryConvertTo(String.class, is);
                ModRewriteConfLoader loader = new ModRewriteConfLoader();
                conf = new Conf();
                loader.process(text, conf);
            } finally {
                IOHelper.close(is);
            }
        } else if (modRewriteConfText != null) {
            LOG.debug("Using modRewriteConfText: {} as config for urlRewrite", modRewriteConfText);
            ModRewriteConfLoader loader = new ModRewriteConfLoader();
            conf = new Conf();
            loader.process(modRewriteConfText, conf);
        } else if (configFile != null) {
            LOG.debug("Using config file: {} as config for urlRewrite", configFile);
            InputStream is = camelContext.getClassResolver().loadResourceAsStream(configFile);
            if (is == null) {
                throw new IOException("Cannot load config file: " + configFile);
            }
            try {
                conf = new Conf(is, configFile);
            } finally {
                IOHelper.close(is);
            }
        }
        if (conf != null) {
            // set options before initializing
            conf.setUseQueryString(isUseQueryString());
            conf.setUseContext(isUseContext());
            if (getDefaultMatchType() != null) {
                conf.setDefaultMatchType(getDefaultMatchType());
            }
            if (getDecodeUsing() != null) {
                conf.setDecodeUsing(getDecodeUsing());
            }
            conf.initialise();
        }
        if (conf == null || !conf.isOk()) {
            throw new IllegalStateException("Error configuring config file: " + configFile);
        }
    }
    if (urlRewriter == null) {
        urlRewriter = new UrlRewriter(conf);
    }
}
Also used : Conf(org.tuckey.web.filters.urlrewrite.Conf) ModRewriteConfLoader(org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 5 with Conf

use of org.tuckey.web.filters.urlrewrite.Conf in project sling by apache.

the class SlingUrlRewriteFilter method configure.

private void configure(final ComponentContext context) {
    logger.info("configuring URL rewriter");
    final Dictionary properties = context.getProperties();
    final String rules = PropertiesUtil.toString(properties.get(REWRITE_RULES_PARAMETER), DEFAULT_REWRITE_RULES);
    final Document document = createDocument(rules);
    if (document == null) {
        logger.error("creating rules document failed");
        return;
    }
    final Conf conf = new DocumentConf(document);
    conf.initialise();
    clearRewriter();
    if (conf.isOk()) {
        logger.info("rewrite configuration is ok");
    } else {
        logger.error("rewrite configuration is NOT ok");
        return;
    }
    rewriter = new UrlRewriter(conf);
    logger.info("rewrite engine is enabled: {}", conf.isEngineEnabled());
    if (conf.getRules() != null) {
        logger.info("number of rewrite rules: {}", conf.getRules().size());
    } else {
        logger.info("no rewrite rules");
    }
}
Also used : Dictionary(java.util.Dictionary) Conf(org.tuckey.web.filters.urlrewrite.Conf) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) Document(org.w3c.dom.Document)

Aggregations

Conf (org.tuckey.web.filters.urlrewrite.Conf)5 UrlRewriter (org.tuckey.web.filters.urlrewrite.UrlRewriter)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Dictionary (java.util.Dictionary)1 ConfigurationException (org.craftercms.commons.config.ConfigurationException)1 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)1 Test (org.junit.jupiter.api.Test)1 BeansException (org.springframework.beans.BeansException)1 Resource (org.springframework.core.io.Resource)1 NormalRewrittenUrl (org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl)1 ModRewriteConfLoader (org.tuckey.web.filters.urlrewrite.utils.ModRewriteConfLoader)1 Document (org.w3c.dom.Document)1 MockRequest (testutils.mock.MockRequest)1