knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [25/64] [partial] knox git commit: KNOX-998 - Refactoring save 1
Date Fri, 01 Sep 2017 13:17:23 GMT
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepDescriptorFactory.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepDescriptorFactory.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepDescriptorFactory.java
new file mode 100644
index 0000000..df05cf4
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepDescriptorFactory.java
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.api;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.Set;
+
+public abstract class UrlRewriteStepDescriptorFactory {
+
+  private static Map<String,Class<? extends UrlRewriteStepDescriptor>> MAP = loadStepDescriptors();
+
+  private UrlRewriteStepDescriptorFactory() {
+  }
+
+  @SuppressWarnings("unchecked")
+  public static <T extends UrlRewriteStepDescriptor<?>> T create( String type ) {
+    try {
+      Class<? extends UrlRewriteStepDescriptor> descriptorClass = MAP.get( type );
+      return (T)descriptorClass.newInstance();
+    } catch( InstantiationException e ) {
+      throw new IllegalArgumentException( type );
+    } catch( IllegalAccessException e ) {
+      throw new IllegalArgumentException( type );
+    }
+  }
+
+  private static Map<String,Class<? extends UrlRewriteStepDescriptor>> loadStepDescriptors() {
+    Map<String,Class<? extends UrlRewriteStepDescriptor>> map
+        = new HashMap<>();
+    ServiceLoader<? extends UrlRewriteStepDescriptor> descriptors
+        = ServiceLoader.load( UrlRewriteStepDescriptor.class );
+    for( UrlRewriteStepDescriptor descriptor : descriptors ) {
+      String descriptorType = descriptor.type();
+      Class<? extends UrlRewriteStepDescriptor> descriptorClass = descriptor.getClass() ;
+      map.put( descriptorType, descriptorClass );
+    }
+    return map;
+  }
+
+  public static Set<String> getTypes() {
+    return MAP.keySet();
+  }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepFlow.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepFlow.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepFlow.java
new file mode 100644
index 0000000..ace4536
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStepFlow.java
@@ -0,0 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.api;
+
+public enum UrlRewriteStepFlow { ALL, AND, OR }

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
new file mode 100644
index 0000000..5a742b7
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.api;
+
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter;
+import org.apache.knox.gateway.util.MimeTypes;
+import org.apache.knox.gateway.util.urltemplate.Resolver;
+
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.ServiceLoader;
+
+public abstract class UrlRewriteStreamFilterFactory {
+
+  private static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
+
+  private static final Map<String,Map<String,UrlRewriteStreamFilter>> MAP = loadFactories();
+
+  private UrlRewriteStreamFilterFactory() {
+  }
+
+  public static InputStream create(
+      MimeType type,
+      String name,
+      InputStream stream,
+      UrlRewriter rewriter,
+      Resolver resolver,
+      UrlRewriter.Direction direction,
+      UrlRewriteFilterContentDescriptor config )
+          throws IOException {
+    InputStream filteredStream = null;
+    Map<String,UrlRewriteStreamFilter> nameMap = getNameMap( type );
+    UrlRewriteStreamFilter filter = getFilter( nameMap, name );
+    String charset = MimeTypes.getCharset( type, DEFAULT_CHARACTER_ENCODING );
+    if( filter != null ) {
+      filteredStream = filter.filter( stream, charset, rewriter, resolver, direction, config );
+    }
+    return filteredStream;
+  }
+
+  private static Map<String,Map<String,UrlRewriteStreamFilter>> loadFactories() {
+    Map<String,Map<String,UrlRewriteStreamFilter>> typeMap = new HashMap<>();
+    ServiceLoader<UrlRewriteStreamFilter> filters = ServiceLoader.load( UrlRewriteStreamFilter.class );
+    for( UrlRewriteStreamFilter filter : filters ) {
+      String[] types = filter.getTypes();
+      for( String type: types ) {
+        Map<String,UrlRewriteStreamFilter> nameMap = typeMap.get( type );
+        if( nameMap == null ) {
+          nameMap = new LinkedHashMap<String,UrlRewriteStreamFilter>();
+          typeMap.put( type, nameMap );
+        }
+        for( String name: filter.getNames() ) {
+          nameMap.put( name, filter );
+        }
+      }
+    }
+    return typeMap;
+  }
+
+  private static Map<String,UrlRewriteStreamFilter> getNameMap( MimeType type ) {
+    if( type == null ) {
+      type = new MimeType();
+    }
+    Map<String,UrlRewriteStreamFilter> nameMap = MAP.get( type.getBaseType() );
+    try {
+      if( nameMap == null ) {
+        type.setPrimaryType( "*" );
+        nameMap = MAP.get( type.getBaseType() );
+        if( nameMap == null ) {
+          type.setSubType( "*" );
+          nameMap = MAP.get( type.getBaseType() );
+          if( nameMap == null ) {
+            nameMap = MAP.get( null );
+          }
+        }
+      }
+    } catch( MimeTypeParseException e ) {
+      throw new IllegalArgumentException( type.toString(), e );
+    }
+    return nameMap;
+  }
+
+  private static UrlRewriteStreamFilter getFilter( Map<String,UrlRewriteStreamFilter> map, String name ) {
+    UrlRewriteStreamFilter filter = null;
+    if( map != null ) {
+      if( name == null && !map.isEmpty() ) {
+        filter = map.values().iterator().next();
+      } else {
+        filter = map.get( name );
+      }
+    }
+    return filter;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriter.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriter.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriter.java
new file mode 100644
index 0000000..bfee565
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriter.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.api;
+
+import org.apache.knox.gateway.util.urltemplate.Resolver;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+public interface UrlRewriter {
+
+  enum Direction { IN, OUT }
+
+  UrlRewriteRulesDescriptor getConfig();
+
+  Template rewrite( Resolver resolver, Template uri, Direction direction, String ruleName );
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
new file mode 100644
index 0000000..08efde4
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
@@ -0,0 +1,129 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRuleProcessorHolder;
+import org.apache.knox.gateway.util.urltemplate.Matcher;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A simple extension to the matcher that takes into account scopes for rules along with the templates themselves.
+ * This matcher maintains a list of matchers and delegates to an appropriate matcher based on scope information for the
+ * associated rules.
+ */
+public class ScopedMatcher extends Matcher<UrlRewriteRuleProcessorHolder> {
+
+  public static final String GLOBAL_SCOPE = "GLOBAL";
+
+  private List<Matcher<UrlRewriteRuleProcessorHolder>> matchers;
+
+  public ScopedMatcher() {
+    super();
+    matchers = new ArrayList<>();
+    matchers.add(new Matcher<UrlRewriteRuleProcessorHolder>());
+  }
+
+  @Override
+  public UrlRewriteRuleProcessorHolder get(Template template) {
+    return super.get(template);
+  }
+
+  @Override
+  public void add(Template template, UrlRewriteRuleProcessorHolder value) {
+    Matcher<UrlRewriteRuleProcessorHolder> matcher = getMatcher(template, value);
+    matcher.add( template, value );
+  }
+
+  @Override
+  public Match match(Template input) {
+    return match(input, null);
+  }
+
+  public Match match(Template input, String scope) {
+    List<Match> matches = new ArrayList<>();
+    for (Matcher<UrlRewriteRuleProcessorHolder> matcher : matchers) {
+      Match match = matcher.match(input);
+      if (match != null) {
+        matches.add(match);
+      }
+    }
+    if (matches.size() == 0) {
+      return null;
+    }
+    if (matches.size() == 1) {
+      return getMatch(matches, scope);
+    }
+    return findBestMatch(matches, scope);
+  }
+
+  private Match findBestMatch(List<Match> matches, String scope) {
+    if (scope != null) {
+      //when multiple matches are found, find the first one that matches in scope
+      for ( Match match : matches ) {
+        String matchedScope = match.getValue().getScope();
+        if ( matchedScope != null && matchedScope.equals(scope) ) {
+          return match;
+        }
+      }
+    }
+    //since no scope match was found return the first global scopeed match
+    for ( Match match : matches ) {
+      String matchedScope = match.getValue().getScope();
+      if ( matchedScope != null && matchedScope.equals(GLOBAL_SCOPE) ) {
+        return match;
+      }
+    }
+    //return the first match from the list
+    return getMatch(matches, scope);
+  }
+
+  private Match getMatch(List<Match> matches, String scope) {
+    Match match = matches.get(0);
+    String matchedScope = match.getValue().getScope();
+    if (matchedScope != null && scope != null && !matchedScope.equals(scope) && !matchedScope.equals(GLOBAL_SCOPE)) {
+      return null;
+    }
+    return match;
+  }
+
+  /**
+   * Returns a matcher for a given template and processor holder. This method takes into account different scopes in
+   * addition to template values. If a matcher exists for a template but the scope is different, a new matcher is
+   * created and returned.
+   * @param template the template for which a matcher is needed
+   * @param holder the rule holder that goes along with the template.
+   * @return a matcher
+   */
+  private Matcher<UrlRewriteRuleProcessorHolder> getMatcher(Template template, UrlRewriteRuleProcessorHolder holder) {
+    for (Matcher<UrlRewriteRuleProcessorHolder> matcher : matchers) {
+      UrlRewriteRuleProcessorHolder matchersHolder = matcher.get(template);
+      if (matchersHolder == null) {
+        return matcher;
+      } else if (holder.getScope() == null && matchersHolder.getScope() == null) {
+        return matcher;
+      }
+    }
+    Matcher<UrlRewriteRuleProcessorHolder> matcher = new Matcher<>();
+    matchers.add(matcher);
+    return matcher;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionDescriptor.java
new file mode 100644
index 0000000..6c4cf24
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionDescriptor.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStepDescriptor;
+
+public interface UrlRewriteActionDescriptor extends UrlRewriteStepDescriptor<UrlRewriteActionDescriptor> {
+
+  String operation();
+
+  UrlRewriteActionDescriptor operation( String operation );
+
+  String parameter();
+
+  UrlRewriteActionDescriptor parameter( String parameter );
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteDescriptorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteDescriptorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteDescriptorExt.java
new file mode 100644
index 0000000..40edd14
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteDescriptorExt.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
+
+public class UrlRewriteActionRewriteDescriptorExt
+    extends UrlRewriteActionDescriptorBase
+    implements UrlRewriteActionDescriptor {
+
+  public UrlRewriteActionRewriteDescriptorExt() {
+    super( "rewrite" );
+  }
+
+  public String template() {
+    return parameter();
+  }
+
+  public UrlRewriteActionRewriteDescriptorExt template( String template ) {
+    parameter( template );
+    return this;
+  }
+
+  public void setTemplate( String template ) {
+    parameter( template );
+  }
+
+  @Override
+  public String getParam() {
+    return null;
+  }
+
+  public String getTemplate() {
+    return parameter();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteProcessorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteProcessorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteProcessorExt.java
new file mode 100644
index 0000000..9b6eb89
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteActionRewriteProcessorExt.java
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.knox.gateway.util.urltemplate.Expander;
+import org.apache.knox.gateway.util.urltemplate.Parser;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+public class UrlRewriteActionRewriteProcessorExt
+    implements UrlRewriteStepProcessor<UrlRewriteActionRewriteDescriptorExt> {
+
+  private Template template;
+  private Expander expander;
+
+  @Override
+  public String getType() {
+    return "rewrite";
+  }
+
+  @Override
+  public void initialize( UrlRewriteEnvironment environment, UrlRewriteActionRewriteDescriptorExt descriptor ) throws Exception {
+    this.expander = new Expander();
+    if ( descriptor.parameter() != null ) {
+      this.template = Parser.parseTemplate( descriptor.parameter() );
+    } else {
+      this.template = Parser.parseTemplate( "" );
+    }
+  }
+
+  @Override
+  public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+    Template rewritten = expander.expandToTemplate( template, context.getParameters(), context.getEvaluator() );
+    context.setCurrentUrl( rewritten );
+    return UrlRewriteStepStatus.SUCCESS;
+  }
+
+  @Override
+  public void destroy() {
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptor.java
new file mode 100644
index 0000000..ec8696b
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptor.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor;
+
+public interface UrlRewriteCheckDescriptor extends UrlRewriteFlowDescriptor<UrlRewriteCheckDescriptor> {
+
+  String operation();
+
+  UrlRewriteCheckDescriptor operation( String operation );
+
+  String input();
+
+  UrlRewriteCheckDescriptor input( String input );
+
+  String value();
+
+  UrlRewriteCheckDescriptor value( String value );
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptorExt.java
new file mode 100644
index 0000000..e123929
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckDescriptorExt.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFlowDescriptorBase;
+
+public class UrlRewriteCheckDescriptorExt
+    extends UrlRewriteFlowDescriptorBase<UrlRewriteCheckDescriptor>
+    implements UrlRewriteCheckDescriptor {
+
+  private String operation;
+  private String input;
+  private String value;
+
+  public UrlRewriteCheckDescriptorExt() {
+    super( "check" );
+  }
+
+  @Override
+  public String operation() {
+    return operation;
+  }
+
+  @Override
+  public UrlRewriteCheckDescriptor operation( String operation ) {
+    this.operation = operation;
+    return this;
+  }
+
+  public void setOperation( String operation ) {
+    operation( operation );
+  }
+
+  public void setOper( String operation ) {
+    operation( operation );
+  }
+
+  public void setOp( String operation ) {
+    operation( operation );
+  }
+
+  public String getOper() {
+    return operation();
+  }
+
+  @Override
+  public String input() {
+    return input;
+  }
+
+  @Override
+  public UrlRewriteCheckDescriptor input( String input ) {
+    this.input = input;
+    return this;
+  }
+
+  public void setInput( String input ) {
+    input( input );
+  }
+
+  public String getInput() {
+    return input();
+  }
+
+  @Override
+  public String value() {
+    return value;
+  }
+
+  @Override
+  public UrlRewriteCheckDescriptor value( String value ) {
+    this.value = value;
+    return this;
+  }
+
+  public void setValue( String value ) {
+    value( value );
+  }
+
+  public String getValue() {
+    return value();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckProcessorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckProcessorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckProcessorExt.java
new file mode 100644
index 0000000..f21aa2f
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteCheckProcessorExt.java
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+
+public class UrlRewriteCheckProcessorExt implements
+    UrlRewriteStepProcessor<UrlRewriteCheckDescriptor> {
+
+  @Override
+  public String getType() {
+    return "check";
+  }
+
+  @Override
+  public void initialize( UrlRewriteEnvironment environment, UrlRewriteCheckDescriptor descriptor ) throws Exception {
+  }
+
+  @Override
+  public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+    return UrlRewriteStepStatus.FAILURE;
+  }
+
+  @Override
+  public void destroy() {
+  }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptor.java
new file mode 100644
index 0000000..3146cff
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptor.java
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor;
+
+public interface UrlRewriteControlDescriptor extends UrlRewriteFlowDescriptor<UrlRewriteControlDescriptor> {
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptorExt.java
new file mode 100644
index 0000000..39a77cf
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlDescriptorExt.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFlowDescriptorBase;
+
+public class UrlRewriteControlDescriptorExt
+    extends UrlRewriteFlowDescriptorBase<UrlRewriteControlDescriptor>
+    implements UrlRewriteControlDescriptor {
+
+  public UrlRewriteControlDescriptorExt() {
+    super( "control" );
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlProcessorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlProcessorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlProcessorExt.java
new file mode 100644
index 0000000..6e11082
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteControlProcessorExt.java
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+
+public class UrlRewriteControlProcessorExt implements
+    UrlRewriteStepProcessor<UrlRewriteControlDescriptor> {
+
+  @Override
+  public String getType() {
+    return "control";
+  }
+
+  @Override
+  public void initialize( UrlRewriteEnvironment environment, UrlRewriteControlDescriptor descriptor ) throws Exception {
+  }
+
+  @Override
+  public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+    return UrlRewriteStepStatus.FAILURE;
+  }
+
+  @Override
+  public void destroy() {
+  }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptor.java
new file mode 100644
index 0000000..6396516
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptor.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+import java.net.URISyntaxException;
+
+public interface UrlRewriteMatchDescriptor extends
+    UrlRewriteFlowDescriptor<UrlRewriteMatchDescriptor> {
+
+  String operation();
+
+  UrlRewriteMatchDescriptor operation( String operation );
+
+  String pattern();
+
+  UrlRewriteMatchDescriptor pattern( String pattern ) throws URISyntaxException;
+
+  Template template();
+
+  UrlRewriteMatchDescriptor template( Template pattern );
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
new file mode 100644
index 0000000..51a08d5
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFlowDescriptorBase;
+import org.apache.knox.gateway.util.urltemplate.Parser;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+import java.net.URISyntaxException;
+
+public class UrlRewriteMatchDescriptorExt
+    extends UrlRewriteFlowDescriptorBase<UrlRewriteMatchDescriptor>
+    implements UrlRewriteMatchDescriptor {
+
+  private String operation;
+  private String pattern;
+  private Template template;
+
+  public UrlRewriteMatchDescriptorExt() {
+    super( "match" );
+  }
+
+  @Override
+  public String operation() {
+    return operation;
+  }
+
+  public String getOperation() {
+    return operation;
+  }
+
+  @Override
+  public UrlRewriteMatchDescriptor operation( String operation ) {
+    this.operation = operation;
+    return this;
+  }
+
+  public void setOperation( String operation ) {
+    operation( operation );
+  }
+
+  public void setOper( String operation ) {
+    operation( operation );
+  }
+
+  public void setOp( String operation ) {
+    operation( operation );
+  }
+
+  public String getOper() {
+    return operation();
+  }
+
+  @Override
+  public String pattern() {
+    return pattern;
+  }
+
+  @Override
+  public UrlRewriteMatchDescriptor pattern( String pattern ) throws URISyntaxException {
+    this.pattern = pattern;
+    this.template = Parser.parseTemplate( pattern );
+    return this;
+  }
+
+  public void setUrl( String pattern ) throws URISyntaxException {
+    pattern( pattern );
+  }
+
+  public void setPattern( String pattern ) throws URISyntaxException {
+    pattern( pattern );
+  }
+
+  public String getPattern() {
+    return pattern;
+  }
+
+  @Override
+  public Template template() {
+    return template;
+  }
+
+  @Override
+  public UrlRewriteMatchDescriptor template( Template template ) {
+    this.template = template;
+    // The template is now optional for rules.
+    if( template == null ) {
+      this.pattern = null;
+    } else {
+      this.pattern = template.toString();
+    }
+    return this;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
new file mode 100644
index 0000000..45c9155
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.ext;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.knox.gateway.util.urltemplate.Matcher;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+public class UrlRewriteMatchProcessorExt implements
+    UrlRewriteStepProcessor<UrlRewriteMatchDescriptor> {
+
+  //private UrlRewriteMatchDescriptor descriptor;
+  private Matcher<Void> matcher;
+
+  @Override
+  public String getType() {
+    return "match";
+  }
+
+  @Override
+  public void initialize( UrlRewriteEnvironment environment, UrlRewriteMatchDescriptor descriptor ) throws Exception {
+    Template template = descriptor.template();
+    if( template == null ) {
+      this.matcher = null;
+    } else {
+      this.matcher = new Matcher<Void>( descriptor.template(), null );
+    }
+  }
+
+  @Override
+  public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+    UrlRewriteStepStatus status = UrlRewriteStepStatus.SUCCESS;
+    if( matcher != null ) {
+      status = UrlRewriteStepStatus.FAILURE;
+      Matcher.Match match = matcher.match( context.getCurrentUrl() );
+      if( match != null ) {
+        context.addParameters( match.getParams() );
+        status = UrlRewriteStepStatus.SUCCESS;
+      }
+    }
+    return status;
+  }
+
+  @Override
+  public void destroy() {
+    matcher = null;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteMessages.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteMessages.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteMessages.java
new file mode 100644
index 0000000..35247c9
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteMessages.java
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.i18n;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.knox.gateway.i18n.messages.Message;
+import org.apache.knox.gateway.i18n.messages.MessageLevel;
+import org.apache.knox.gateway.i18n.messages.Messages;
+import org.apache.knox.gateway.i18n.messages.StackTrace;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+@Messages(logger="org.apache.hadoop.gateway")
+public interface UrlRewriteMessages {
+
+  @Message( level = MessageLevel.DEBUG, text = "Failed to parse value as URL: {0}" )
+  void failedToParseValueForUrlRewrite( String value );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to write the rules descriptor: {0}" )
+  void failedToWriteRulesDescriptor( @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.DEBUG, text = "Failed to filter attribute {0}: {1}" )
+  void failedToFilterAttribute( String attributeName, @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to load rewrite rules descriptor: {0}" )
+  void failedToLoadRewriteRulesDescriptor( @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to initialize rewrite rules: {0}" )
+  void failedToInitializeRewriteRules( @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to initialize rewrite functions: {0}" )
+  void failedToInitializeRewriteFunctions( @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to destroy rewrite rule processor: {0}" )
+  void failedToDestroyRewriteStepProcessor( @StackTrace(level = MessageLevel.DEBUG) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to destroy rewrite function processor: {0}" )
+  void failedToDestroyRewriteFunctionProcessor( @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to filter value {0}, rule {1}" )
+  void failedToFilterValue( String value, String rule );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to filter value {0}, rule {1}: {2}" )
+  void failedToFilterValue( String value, String rule, @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to filter field name {0}: {1}" )
+  void failedToFilterFieldName( String fieldName, @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Rewrite function {0} failed: {1}" )
+  void failedToInvokeRewriteFunction( String functionName, @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to find values by parameter name {0}: {1}" )
+  void failedToFindValuesByParameter( String parameterName, @StackTrace( level = MessageLevel.DEBUG ) Exception e );
+
+  @Message( level = MessageLevel.DEBUG, text = "Rewrote URL: {0}, direction: {1} via implicit rule: {2} to URL: {3}" )
+  void rewroteUrlViaImplicitRule( Template inputUri, UrlRewriter.Direction direction, String ruleName, Template outputUri );
+
+  @Message( level = MessageLevel.DEBUG, text = "Rewrote URL: {0}, direction: {1} via explicit rule: {2} to URL: {3}" )
+  void rewroteUrlViaExplicitRule( Template inputUri, UrlRewriter.Direction direction, String ruleName, Template outputUri );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to rewrite URL: {0}, direction: {1} via rule: {2}, status: {3}" )
+  void failedToRewriteUrl( Template inputUri, UrlRewriter.Direction direction, String ruleName, UrlRewriteStepStatus stepStatus );
+
+  @Message( level = MessageLevel.ERROR, text = "Failed to rewrite URL: {0}, direction: {1}, rule: {2}" )
+  void failedToRewriteUrlDueToException( Template inputUri, UrlRewriter.Direction direction, String ruleName, @StackTrace(level = MessageLevel.DEBUG) Exception exception );
+
+  @Message( level = MessageLevel.TRACE, text = "No rule matching URL: {0}, direction: {1}" )
+  void noRuleMatchingUrl( Template inputUri, UrlRewriter.Direction direction );
+
+  @Message( level = MessageLevel.TRACE, text = "Failed to decode query string: {0}" )
+  void failedToDecodeQueryString( String queryString, @StackTrace(level = MessageLevel.TRACE) Exception exception );
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteResources.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteResources.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteResources.java
new file mode 100644
index 0000000..8b4c7b5
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/i18n/UrlRewriteResources.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.i18n;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor;
+import org.apache.knox.gateway.i18n.resources.Resource;
+import org.apache.knox.gateway.i18n.resources.Resources;
+
+@Resources
+public interface UrlRewriteResources {
+
+  @Resource( text="No importer for descriptor format {0}" )
+  String noImporterForFormat( String format );
+
+  @Resource( text="No exporter for descriptor format {0}" )
+  String noExporterForFormat( String format );
+
+  @Resource( text="Unexpected selector type {0}" )
+  String unexpectedRewritePathSelector( UrlRewriteFilterPathDescriptor selector );
+
+  @Resource( text="Unexpected selected node type {0}" )
+  String unexpectedSelectedNodeType( Object node );
+
+  @Resource( text="Invalid frontend rewrite function parameter {0}" )
+  String invalidFrontendFunctionParameter( String parameter );
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/CookieScopeResponseWrapper.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/CookieScopeResponseWrapper.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/CookieScopeResponseWrapper.java
new file mode 100644
index 0000000..8fbc3a7
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/CookieScopeResponseWrapper.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.GatewayResponseWrapper;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class CookieScopeResponseWrapper extends GatewayResponseWrapper {
+
+    private static final String SET_COOKIE = "Set-Cookie";
+
+    private static final String COOKIE_PATH = "Path=/";
+
+    private final String scopePath;
+
+    public CookieScopeResponseWrapper(HttpServletResponse response, String gatewayPath) {
+        super(response);
+        this.scopePath = COOKIE_PATH + gatewayPath + "/";
+    }
+
+    @Override
+    public void addHeader(String name, String value) {
+        if (SET_COOKIE.equals(name)) {
+            String updatedCookie;
+            if (value.contains(COOKIE_PATH)) {
+                updatedCookie = value.replace(COOKIE_PATH, scopePath);
+            } else {
+                // append the scope path
+                updatedCookie = String.format("%s %s;", value, scopePath);
+            }
+            super.addHeader(name, updatedCookie);
+        } else {
+            super.addHeader(name, value);
+        }
+    }
+
+    @Override
+    public OutputStream getRawOutputStream() throws IOException {
+        return getResponse().getOutputStream();
+    }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/FrontendFunctionProcessor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/FrontendFunctionProcessor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/FrontendFunctionProcessor.java
new file mode 100644
index 0000000..5964510
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/FrontendFunctionProcessor.java
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.FrontendFunctionDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteResources;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteResolver;
+import org.apache.knox.gateway.i18n.resources.ResourcesFactory;
+import org.apache.knox.gateway.services.GatewayServices;
+
+import java.net.URI;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class FrontendFunctionProcessor implements UrlRewriteFunctionProcessor<FrontendFunctionDescriptor> {
+
+  private static UrlRewriteResources RES = ResourcesFactory.get( UrlRewriteResources.class );
+
+  private Map<String,UrlRewriteResolver> resolvers;
+
+  @Override
+  public String name() {
+    return FrontendFunctionDescriptor.FUNCTION_NAME;
+  }
+
+  @Override
+  public void initialize( UrlRewriteEnvironment environment, FrontendFunctionDescriptor descriptor ) throws Exception {
+    if( environment == null ) {
+      throw new IllegalArgumentException( "environment==null" );
+    }
+    URI frontend = environment.getAttribute( FrontendFunctionDescriptor.FRONTEND_URI_ATTRIBUTE );
+    resolvers = new HashMap<>();
+    if( frontend == null ) {
+      resolvers.put( "url", new ParamResolver( "gateway.url" ) );
+      resolvers.put( "addr", new ParamResolver( "gateway.addr" ) );
+      resolvers.put( "scheme", new ParamResolver( "gateway.scheme" ) );
+      resolvers.put( "host", new ParamResolver( "gateway.host" ) );
+      resolvers.put( "port", new ParamResolver( "gateway.port" ) );
+      resolvers.put( "path", new ParamResolver( "gateway.path" ) );
+    } else {
+      resolvers.put( "url", new FixedResolver( frontend.toString() ) );
+      resolvers.put( "addr", new FixedResolver( frontend.getHost() + ":" + frontend.getPort() ) );
+      resolvers.put( "scheme", new FixedResolver( frontend.getScheme() ) );
+      resolvers.put( "host", new FixedResolver( frontend.getHost() ) );
+      resolvers.put( "port", new FixedResolver( Integer.toString( frontend.getPort() ) ) );
+      resolvers.put( "path", new FixedResolver( frontend.getPath() ) );
+    }
+    resolvers.put( "topology", new FixedResolver( (String)environment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE) ) );
+    resolvers.put( "address", resolvers.get( "addr" ) );
+  }
+
+  @Override
+  public void destroy() throws Exception {
+    resolvers.clear();
+  }
+
+  @Override
+  public List<String> resolve( UrlRewriteContext context, List<String> parameters ) throws Exception {
+    String parameter = "url";
+    if( parameters != null && parameters.size() > 0 ) {
+      String first = parameters.get( 0 );
+      if( first != null ) {
+        parameter = first;
+      }
+    }
+    parameter = parameter.trim().toLowerCase();
+    UrlRewriteResolver resolver = resolvers.get( parameter );
+    if( resolver == null ) {
+      throw new IllegalArgumentException( RES.invalidFrontendFunctionParameter( parameter ) );
+    }
+    List<String> results = resolver.resolve( context, parameters );
+    return results;
+  }
+
+  private static class ParamResolver implements UrlRewriteResolver {
+
+    private String paramName;
+
+    private ParamResolver( String paramName ) {
+      this.paramName = paramName;
+    }
+
+    @Override
+    public List<String> resolve( UrlRewriteContext context, List<String> parameter ) throws Exception {
+      return context.getParameters().resolve( paramName );
+    }
+
+  }
+
+  private static class FixedResolver implements UrlRewriteResolver {
+
+    private List<String> fixedValues;
+
+    private FixedResolver( String... fixedValues ) {
+      this.fixedValues = Arrays.asList( fixedValues );
+    }
+
+    @Override
+    public List<String> resolve( UrlRewriteContext context, List<String> parameter ) throws Exception {
+      return fixedValues;
+    }
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteContextImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteContextImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteContextImpl.java
new file mode 100644
index 0000000..8a7e2bc
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteContextImpl.java
@@ -0,0 +1,152 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter;
+import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteMessages;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.util.urltemplate.Evaluator;
+import org.apache.knox.gateway.util.urltemplate.Params;
+import org.apache.knox.gateway.util.urltemplate.Resolver;
+import org.apache.knox.gateway.util.urltemplate.Template;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class UrlRewriteContextImpl implements UrlRewriteContext {
+
+  private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class );
+
+  private UrlRewriteEnvironment environment;
+  private Resolver resolver;
+  private Evaluator evaluator;
+  private Map<String,UrlRewriteFunctionProcessor> functions;
+  private ContextParameters params;
+  private UrlRewriter.Direction direction;
+  private Template originalUrl;
+  private Template currentUrl;
+
+  public UrlRewriteContextImpl(
+      UrlRewriteEnvironment environment,
+      Resolver resolver,
+      Map<String,UrlRewriteFunctionProcessor> functions,
+      UrlRewriter.Direction direction,
+      Template url ) {
+    this.environment = environment;
+    this.resolver = resolver;
+    this.functions = functions;
+    this.params = new ContextParameters();
+    this.evaluator = new ContextEvaluator();
+    this.direction = direction;
+    this.originalUrl = url;
+    this.currentUrl = url;
+  }
+
+  @Override
+  public UrlRewriter.Direction getDirection() {
+    return direction;
+  }
+
+  @Override
+  public Template getOriginalUrl() {
+    return originalUrl;
+  }
+
+  @Override
+  public Template getCurrentUrl() {
+    return currentUrl;
+  }
+
+  @Override
+  public void setCurrentUrl( Template url ) {
+    currentUrl = url;
+  }
+
+  @Override
+  public void addParameters( Params parameters ) {
+    params.add( parameters );
+  }
+
+  @Override
+  public Params getParameters() {
+    return params;
+  }
+
+  @Override
+  public Evaluator getEvaluator() {
+    return evaluator;
+  }
+
+  private class ContextParameters implements Params {
+
+    Map<String,List<String>> map = new HashMap<>();
+
+    @Override
+    public Set<String> getNames() {
+      return map.keySet();
+    }
+
+    @Override
+    public List<String> resolve( String name ) {
+      List<String> values = map.get( name ); // Try to find the name in the context map.
+      if( values == null ) {
+        try {
+          values = resolver.resolve( name );
+          if( values == null ) {
+            values = environment.resolve( name ); // Try to find the name in the environment.
+          }
+        } catch( Exception e ) {
+          LOG.failedToFindValuesByParameter( name, e );
+          // Ignore it and return null.
+        }
+      }
+      return values;
+    }
+
+    public void add( Params params ) {
+      for( String name : params.getNames() ) {
+        map.put( name, params.resolve( name ) );
+      }
+    }
+
+  }
+
+  private class ContextEvaluator implements Evaluator {
+
+    @Override
+    public List<String> evaluate( String function, List<String> parameters ) {
+      List<String> results = null;
+      UrlRewriteFunctionProcessor processor = functions.get( function );
+      if( processor != null ) {
+        try {
+          results = processor.resolve( UrlRewriteContextImpl.this, parameters );
+        } catch( Exception e ) {
+          LOG.failedToInvokeRewriteFunction( function, e );
+          results = null;
+        }
+      }
+      return results;
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteDeploymentContributor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteDeploymentContributor.java
new file mode 100644
index 0000000..e3213e8
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteDeploymentContributor.java
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.deploy.DeploymentContext;
+import org.apache.knox.gateway.deploy.ProviderDeploymentContributorBase;
+import org.apache.knox.gateway.descriptor.FilterDescriptor;
+import org.apache.knox.gateway.descriptor.FilterParamDescriptor;
+import org.apache.knox.gateway.descriptor.ResourceDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptorFactory;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter;
+import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteMessages;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.topology.Provider;
+import org.apache.knox.gateway.topology.Service;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.List;
+
+public class UrlRewriteDeploymentContributor extends
+    ProviderDeploymentContributorBase {
+
+  private static final String PROVIDER_ROLE_NAME = "rewrite";
+  private static final String PROVIDER_IMPL_NAME = "url-rewrite";
+  private static final String PARAM_SERVICE_ROLE = "service.role";
+  private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class );
+
+  @Override
+  public String getRole() {
+    return PROVIDER_ROLE_NAME;
+  }
+
+  @Override
+  public String getName() {
+    return PROVIDER_IMPL_NAME;
+  }
+
+  public void initializeContribution( DeploymentContext context ) {
+    context.addDescriptor( getRole(), UrlRewriteRulesDescriptorFactory.create() );
+  }
+
+  public void contributeProvider( DeploymentContext context, Provider provider ) {
+  }
+
+  public void finalizeContribution( DeploymentContext context ) {
+    // Write the descriptor into the archive.
+    UrlRewriteRulesDescriptor descriptor = context.getDescriptor( getRole() );
+    StringWriter writer = new StringWriter();
+    try {
+      UrlRewriteRulesDescriptorFactory.store( descriptor, "xml", writer );
+    } catch( IOException e ) {
+      LOG.failedToWriteRulesDescriptor( e );
+    }
+    String asset = writer.toString();
+    context.getWebArchive().addAsWebInfResource(
+        new StringAsset( asset ),
+        UrlRewriteServletContextListener.DESCRIPTOR_DEFAULT_FILE_NAME );
+
+    // Tell the provider the location of the descriptor.
+    context.getWebAppDescriptor().createListener().listenerClass( UrlRewriteServletContextListener.class.getName() );
+    context.getWebAppDescriptor().createContextParam()
+        .paramName( UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME )
+        .paramValue( UrlRewriteServletContextListener.DESCRIPTOR_DEFAULT_LOCATION );
+//    ServletType<WebAppDescriptor> servlet = findServlet( context, context.getTopology().getName() );
+//    servlet.createInitParam()
+//        .paramName( UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME )
+//        .paramValue( DESCRIPTOR_FILE_NAME );
+  }
+
+  @Override
+  public void contributeFilter(
+      DeploymentContext context,
+      Provider provider,
+      Service service,
+      ResourceDescriptor resource,
+      List<FilterParamDescriptor> params ) {
+    FilterDescriptor filterDescriptor = resource.addFilter();
+    filterDescriptor.role( getRole() ).name( getName() ).impl( UrlRewriteServletFilter.class ).params( params );
+    filterDescriptor.param().name(PARAM_SERVICE_ROLE).value(service.getRole());
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterApplyDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterApplyDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterApplyDescriptorImpl.java
new file mode 100644
index 0000000..a11999d
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterApplyDescriptorImpl.java
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor;
+
+public class UrlRewriteFilterApplyDescriptorImpl
+    extends UrlRewriteFilterSelectorDescriptorBase<UrlRewriteFilterApplyDescriptor>
+    implements UrlRewriteFilterApplyDescriptor {
+
+  private String rule;
+
+  @Override
+  public String rule() {
+    return rule;
+  }
+
+  @Override
+  public UrlRewriteFilterApplyDescriptor rule( String rule ) {
+    this.rule = rule;
+    return this;
+  }
+
+  public void setRule( String rule ) {
+    this.rule = rule;
+  }
+
+  public String getRule() {
+    return rule;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterBufferDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterBufferDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterBufferDescriptorImpl.java
new file mode 100644
index 0000000..3079f2d
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterBufferDescriptorImpl.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor;
+
+public class UrlRewriteFilterBufferDescriptorImpl
+    extends UrlRewriteFilterGroupDescriptorBase
+    implements UrlRewriteFilterBufferDescriptor {
+
+  @Override
+  public UrlRewriteFilterDetectDescriptor addDetect( String path, String value ) {
+    UrlRewriteFilterDetectDescriptor detect = new UrlRewriteFilterDetectDescriptorImpl();
+    detect.path( path );
+    detect.value( value );
+    addSelector( detect );
+    return detect;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java
new file mode 100644
index 0000000..8176507
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
+
+public class UrlRewriteFilterContentDescriptorImpl
+    extends UrlRewriteFilterGroupDescriptorBase
+    implements UrlRewriteFilterContentDescriptor {
+
+  private String type;
+
+  private String asType;
+
+  public UrlRewriteFilterContentDescriptorImpl() {
+  }
+
+  @Override
+  public String type() {
+    return this.type;
+  }
+
+  @Override
+  public String asType() {
+    return asType;
+  }
+
+  @Override
+  public UrlRewriteFilterContentDescriptor type( String type ) {
+    this.type = type;
+    return this;
+  }
+
+  @Override
+  public UrlRewriteFilterContentDescriptor asType( String type ) {
+    asType = type;
+    return this;
+  }
+
+  public void setType(String type ) {
+    type( type );
+  }
+
+  public String getType() {
+    return type;
+  }
+
+  public String getAsType() {
+    return asType;
+  }
+
+  public void setAsType(String asType) {
+    this.asType = asType;
+  }
+
+  @Override
+  public UrlRewriteFilterBufferDescriptor addBuffer( String path ) {
+    UrlRewriteFilterBufferDescriptor descriptor = new UrlRewriteFilterBufferDescriptorImpl();
+    descriptor.path( path );
+    addSelector( descriptor );
+    return descriptor;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDescriptorImpl.java
new file mode 100644
index 0000000..70425ba
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDescriptorImpl.java
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor;
+import org.apache.knox.gateway.util.MimeTypeMap;
+
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class UrlRewriteFilterDescriptorImpl implements
+    UrlRewriteFilterDescriptor {
+
+  private String name;
+  private List<UrlRewriteFilterContentDescriptor> contentList = new ArrayList<UrlRewriteFilterContentDescriptor>();
+  private MimeTypeMap<UrlRewriteFilterContentDescriptor> contentMap = new MimeTypeMap<UrlRewriteFilterContentDescriptor>();
+
+  public UrlRewriteFilterDescriptorImpl() {
+  }
+
+  @Override
+  public String name() {
+    return this.name;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  @Override
+  public UrlRewriteFilterDescriptor name( String name ) {
+    this.name = name;
+    return this;
+  }
+
+  public void setName( String name ) {
+    this.name = name;
+  }
+
+  @Override
+  public List<UrlRewriteFilterContentDescriptor> getContents() {
+    return contentList;
+  }
+
+  @Override
+  public UrlRewriteFilterContentDescriptor getContent( String type ) {
+    return contentMap.get( type );
+  }
+
+  @Override
+  public UrlRewriteFilterContentDescriptor getContent( MimeType type ) {
+    return contentMap.get( type );
+  }
+
+  @Override
+  public UrlRewriteFilterContentDescriptor addContent( String type ) {
+    UrlRewriteFilterContentDescriptorImpl impl = new UrlRewriteFilterContentDescriptorImpl();
+    impl.type( type );
+    contentList.add( impl );
+    try {
+      contentMap.put( new MimeType( type ), impl );
+    } catch( MimeTypeParseException e ) {
+      throw new IllegalArgumentException( type, e );
+    }
+    return impl;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDetectDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDetectDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDetectDescriptorImpl.java
new file mode 100644
index 0000000..b78cef8
--- /dev/null
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteFilterDetectDescriptorImpl.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.knox.gateway.filter.rewrite.impl;
+
+import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor;
+
+public class UrlRewriteFilterDetectDescriptorImpl
+    extends UrlRewriteFilterGroupDescriptorBase
+    implements UrlRewriteFilterDetectDescriptor {
+
+  private String value;
+  private Object compiledValue;
+
+  @Override
+  public String value() {
+    return value;
+  }
+
+  @Override
+  public UrlRewriteFilterDetectDescriptor value( String value ) {
+    this.value = value;
+    return this;
+  }
+
+  public void setValue( String value ) {
+    this.value = value;
+  }
+
+  public String getValue() {
+    return value;
+  }
+
+  @Override
+  public <C> C compiledValue() {
+    return (C)compiledValue;
+  }
+
+  @Override
+  public UrlRewriteFilterDetectDescriptor compiledValue( String compiledValue ) {
+    this.compiledValue = compiledValue;
+    return this;
+  }
+
+  @Override
+  public <C> C compiledValue( Compiler<C> compiler ) {
+    compiledValue = compiler.compile( value, (C)compiledValue );
+    return (C)compiledValue;
+  }
+
+}


Mime
View raw message