All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ocpsoft.rewrite.servlet.impl.DefaultHttpRewriteProvider Maven / Gradle / Ivy

There is a newer version: 10.0.2.Final
Show newest version
/*
 * Copyright 2013 Lincoln Baxter, III
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ocpsoft.rewrite.servlet.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.ServletContext;

import org.ocpsoft.common.services.NonEnriching;
import org.ocpsoft.common.services.ServiceLoader;
import org.ocpsoft.common.util.Iterators;
import org.ocpsoft.logging.Logger;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationLoader;
import org.ocpsoft.rewrite.config.Operation;
import org.ocpsoft.rewrite.config.Rule;
import org.ocpsoft.rewrite.context.RewriteState;
import org.ocpsoft.rewrite.event.InboundRewrite;
import org.ocpsoft.rewrite.event.OutboundRewrite;
import org.ocpsoft.rewrite.exception.RewriteException;
import org.ocpsoft.rewrite.param.DefaultParameterValueStore;
import org.ocpsoft.rewrite.param.Parameter;
import org.ocpsoft.rewrite.param.ParameterStore;
import org.ocpsoft.rewrite.param.ParameterValueStore;
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.ServletRewriteFlow;
import org.ocpsoft.rewrite.servlet.http.HttpRewriteProvider;
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
import org.ocpsoft.rewrite.spi.RuleCacheProvider;
import org.ocpsoft.rewrite.util.ParameterUtils;
import org.ocpsoft.rewrite.util.ServiceLogger;

/**
 * @author Lincoln Baxter, III
 * 
 */
/**
 * @author Lincoln Baxter, III
 * 
 */
public class DefaultHttpRewriteProvider extends HttpRewriteProvider implements NonEnriching
{
   private static Logger log = Logger.getLogger(DefaultHttpRewriteProvider.class);
   private volatile ConfigurationLoader loader;
   private volatile List ruleCacheProviders;

   @Override
   @SuppressWarnings("unchecked")
   public void init(ServletContext context)
   {
      if (loader == null)
         synchronized (this)
         {
            if (loader == null)
               loader = ConfigurationLoader.create(context);
         }

      if (ruleCacheProviders == null)
         synchronized (this)
         {
            ruleCacheProviders = Iterators
                     .asList(ServiceLoader.load(RuleCacheProvider.class));

            ServiceLogger.logLoadedServices(log, RuleCacheProvider.class, ruleCacheProviders);
         }

      loader.loadConfiguration(context);

   }

   @Override
   public void rewriteHttp(final HttpServletRewrite event)
   {
      if (event instanceof InboundRewrite)
         rewriteInbound(event);

      else if (event instanceof OutboundRewrite)
         rewriteOutbound(event);

      else
         throw new RewriteException("Unknown Rewrite event type [" + event.getClass().getName()
                  + "] - was neither an inbound nor outbound Rewrite.");

   }

   private void rewriteInbound(final HttpServletRewrite event)
   {
      ServletContext servletContext = event.getServletContext();
      if (loader == null)
      {
         synchronized (servletContext)
         {
            if (loader == null)
               loader = ConfigurationLoader.create(servletContext);
         }
      }

      Configuration compiledConfiguration = loader.loadConfiguration(servletContext);
      List rules = compiledConfiguration.getRules();

      final EvaluationContextImpl context = new EvaluationContextImpl();

      Object cacheKey = null;
      for (int i = 0; i < ruleCacheProviders.size(); i++)
      {
         RuleCacheProvider provider = ruleCacheProviders.get(i);

         cacheKey = provider.createKey(event, context);
         final List list = provider.get(cacheKey);
         if (list != null && !list.isEmpty())
         {
            if (log.isDebugEnabled())
               log.debug("Using cached ruleset for event [" + event + "] from provider [" + provider + "].");
            for (int j = 0; j < list.size(); j++)
            {
               Rule rule = list.get(j);
               try {
                  event.getEvaluatedRules().add(rule);

                  context.clear();
                  DefaultParameterValueStore values = new DefaultParameterValueStore();
                  context.put(ParameterValueStore.class, values);
                  context.setState(RewriteState.EVALUATING);

                  if (rule.evaluate(event, context))
                  {
                     if (handleBindings(event, context, values))
                     {
                        context.setState(RewriteState.PERFORMING);
                        if (log.isDebugEnabled())
                           log.debug("Rule [" + rule + "] matched and will be performed.");

                        List preOperations = context.getPreOperations();
                        for (int k = 0; k < preOperations.size(); k++)
                        {
                           preOperations.get(k).perform(event, context);
                        }

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }

                        rule.perform(event, context);

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }

                        List postOperations = context.getPostOperations();
                        for (int k = 0; k < postOperations.size(); k++)
                        {
                           postOperations.get(k).perform(event, context);
                        }

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }
                     }
                  }
                  else
                  {
                     event.getEvaluatedRules().remove(rule);
                     break;
                  }
               }
               catch (Exception e) {
                  throw new RewriteException("Error during [" + event + "] while executing rule [" + rule + "]");
               }
            }
         }
      }

      /*
       * Highly optimized loop - for performance reasons. Think before you change this!
       */
      List cacheable = new ArrayList();
      for (int i = 0; i < rules.size(); i++)
      {
         Rule rule = rules.get(i);
         try {
            event.getEvaluatedRules().add(rule);

            context.clear();
            DefaultParameterValueStore values = new DefaultParameterValueStore();
            context.put(ParameterValueStore.class, values);

            context.setState(RewriteState.EVALUATING);
            if (rule.evaluate(event, context))
            {
               if (handleBindings(event, context, values))
               {
                  context.setState(RewriteState.PERFORMING);
                  if (log.isDebugEnabled())
                     log.debug("Rule [" + rule + "] matched and will be performed.");
                  cacheable.add(rule);
                  List preOperations = context.getPreOperations();
                  for (int k = 0; k < preOperations.size(); k++)
                  {
                     preOperations.get(k).perform(event, context);
                  }

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }

                  rule.perform(event, context);

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }

                  List postOperations = context.getPostOperations();
                  for (int k = 0; k < postOperations.size(); k++)
                  {
                     postOperations.get(k).perform(event, context);
                  }

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }
               }
            }
            else
            {
               event.getEvaluatedRules().remove(rule);
            }
         }
         catch (Exception e) {
            throw new RewriteException("Error during [" + event + "] while executing rule [" + rule + "]", e);
         }
      }

      if (!cacheable.isEmpty())
      {
         for (int i = 0; i < ruleCacheProviders.size(); i++)
         {
            ruleCacheProviders.get(i).put(cacheKey, cacheable);
         }
      }
   }

   private void rewriteOutbound(HttpServletRewrite event)
   {
      ServletContext servletContext = event.getServletContext();
      if (loader == null)
      {
         synchronized (servletContext)
         {
            if (loader == null)
               loader = ConfigurationLoader.create(servletContext);
         }
      }

      Configuration compiledConfiguration = loader.loadConfiguration(servletContext);
      List rules = compiledConfiguration.getRules();

      final EvaluationContextImpl context = new EvaluationContextImpl();

      Object cacheKey = null;
      for (int i = ruleCacheProviders.size() - 1; i >= 0; i--)
      {
         RuleCacheProvider provider = ruleCacheProviders.get(i);

         cacheKey = provider.createKey(event, context);
         final List list = provider.get(cacheKey);
         if (list != null && !list.isEmpty())
         {
            if (log.isDebugEnabled())
               log.debug("Using cached ruleset for event [" + event + "] from provider [" + provider + "].");
            for (int j = list.size() - 1; j >= 0; j--)
            {
               Rule rule = list.get(j);
               try {
                  event.getEvaluatedRules().add(rule);

                  context.clear();
                  DefaultParameterValueStore values = new DefaultParameterValueStore();
                  context.put(ParameterValueStore.class, values);
                  context.setState(RewriteState.EVALUATING);

                  if (rule.evaluate(event, context))
                  {
                     if (handleBindings(event, context, values))
                     {
                        context.setState(RewriteState.PERFORMING);
                        if (log.isDebugEnabled())
                           log.debug("Rule [" + rule + "] matched and will be performed.");

                        List preOperations = context.getPreOperations();
                        for (int k = 0; k < preOperations.size(); k++)
                        {
                           preOperations.get(k).perform(event, context);
                        }

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }

                        rule.perform(event, context);

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }

                        List postOperations = context.getPostOperations();
                        for (int k = 0; k < postOperations.size(); k++)
                        {
                           postOperations.get(k).perform(event, context);
                        }

                        if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                        {
                           return;
                        }
                     }
                  }
                  else
                  {
                     event.getEvaluatedRules().remove(rule);
                     break;
                  }
               }
               catch (Exception e) {
                  throw new RewriteException("Error during [" + event + "] while executing rule [" + rule + "]");
               }
            }
         }
      }

      /*
       * Highly optimized loop - for performance reasons. Think before you change this!
       */
      List cacheable = new ArrayList();
      for (int i = rules.size() - 1; i >= 0; i--)
      {
         Rule rule = rules.get(i);
         try {
            event.getEvaluatedRules().add(rule);

            context.clear();
            DefaultParameterValueStore values = new DefaultParameterValueStore();
            context.put(ParameterValueStore.class, values);

            context.setState(RewriteState.EVALUATING);
            if (rule.evaluate(event, context))
            {
               if (handleBindings(event, context, values))
               {
                  context.setState(RewriteState.PERFORMING);
                  if (log.isDebugEnabled())
                     log.debug("Rule [" + rule + "] matched and will be performed.");
                  cacheable.add(rule);
                  List preOperations = context.getPreOperations();
                  for (int k = 0; k < preOperations.size(); k++)
                  {
                     preOperations.get(k).perform(event, context);
                  }

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }

                  rule.perform(event, context);

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }

                  List postOperations = context.getPostOperations();
                  for (int k = 0; k < postOperations.size(); k++)
                  {
                     postOperations.get(k).perform(event, context);
                  }

                  if (event.getFlow().is(ServletRewriteFlow.HANDLED))
                  {
                     break;
                  }
               }
            }
            else {
               event.getEvaluatedRules().remove(rule);
            }
         }
         catch (Exception e) {
            throw new RewriteException("Error during [" + event + "] while executing rule [" + rule + "]", e);
         }
      }

      if (!cacheable.isEmpty())
      {
         for (int i = ruleCacheProviders.size() - 1; i >= 0; i--)
         {
            ruleCacheProviders.get(i).put(cacheKey, cacheable);
         }
      }
   }

   private boolean handleBindings(final HttpServletRewrite event, final EvaluationContextImpl context,
            DefaultParameterValueStore values)
   {
      boolean result = true;
      ParameterStore store = (ParameterStore) context.get(ParameterStore.class);

      for (Entry> entry : store)
      {
         Parameter parameter = entry.getValue();
         String value = values.retrieve(parameter);

         if (!ParameterUtils.enqueueSubmission(event, context, parameter, value))
         {
            result = false;
            break;
         }
      }
      return result;
   }

   @Override
   public void shutdown(ServletContext context)
   {}

   @Override
   public int priority()
   {
      return 0;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy