http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/javascript/JavaScriptUrlRewriteStreamFilter.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/javascript/JavaScriptUrlRewriteStreamFilter.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/javascript/JavaScriptUrlRewriteStreamFilter.java deleted file mode 100644 index 41f141b..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/javascript/JavaScriptUrlRewriteStreamFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.javascript; - -import org.apache.commons.io.input.ReaderInputStream; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; -import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStreamFilter; -import org.apache.hadoop.gateway.util.urltemplate.Resolver; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -public class JavaScriptUrlRewriteStreamFilter implements UrlRewriteStreamFilter { - - private static String[] TYPES = new String[]{ "application/javascript", "text/javascript", "*/javascript", - "application/x-javascript", "text/x-javascript", "*/x-javascript" }; - private static String[] NAMES = new String[]{ null }; - - @Override - public String[] getTypes() { - return TYPES; - } - - @Override - public String[] getNames() { - return NAMES; - } - - @Override - public InputStream filter( - InputStream stream, - String encoding, - UrlRewriter rewriter, - Resolver resolver, - UrlRewriter.Direction direction, - UrlRewriteFilterContentDescriptor config ) - throws IOException { - - if ( config != null ) { - return new ReaderInputStream( - new JavaScriptUrlRewriteFilterReader( - new InputStreamReader( stream, encoding ), rewriter, resolver, direction, config ), encoding ); - } else { - return stream; - } - } -}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonFilterReader.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonFilterReader.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonFilterReader.java deleted file mode 100644 index 10fc9b8..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonFilterReader.java +++ /dev/null @@ -1,644 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.json; - -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.databind.node.TextNode; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; -import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; -import org.apache.hadoop.gateway.util.JsonPath; - -import java.io.IOException; -import java.io.Reader; -import java.io.StringWriter; -import java.util.List; -import java.util.Stack; -import java.util.regex.Pattern; - -class JsonFilterReader extends Reader { - - private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class ); - - private static final UrlRewriteFilterPathDescriptor.Compiler<JsonPath.Expression> JPATH_COMPILER = new JsonPathCompiler(); - private static final UrlRewriteFilterPathDescriptor.Compiler<Pattern> REGEX_COMPILER = new RegexCompiler(); - - private JsonFactory factory; - private JsonParser parser; - private JsonGenerator generator; - private ObjectMapper mapper; - - private Reader reader; - private int offset; - private StringWriter writer; - private StringBuffer buffer; - private Stack<Level> stack; - private Level bufferingLevel; - private UrlRewriteFilterBufferDescriptor bufferingConfig; - private UrlRewriteFilterGroupDescriptor config; - - - public JsonFilterReader( Reader reader, UrlRewriteFilterContentDescriptor config ) throws IOException { - this.reader = reader; - factory = new JsonFactory(); - mapper = new ObjectMapper(); - parser = factory.createParser( reader ); - writer = new StringWriter(); - buffer = writer.getBuffer(); - offset = 0; - generator = factory.createGenerator( writer ); - stack = new Stack<Level>(); - bufferingLevel = null; - bufferingConfig = null; - this.config = config; - } - - @Override - public int read( char[] destBuffer, int destOffset, int destCount ) throws IOException { - int count = 0; - int available = buffer.length() - offset; - - if( available == 0 ) { - JsonToken token = parser.nextToken(); - if( token == null ) { - count = -1; - } else { - processCurrentToken(); - available = buffer.length() - offset; - } - } - - if( available > 0 ) { - count = Math.min( destCount, available ); - buffer.getChars( offset, offset+count, destBuffer, destOffset ); - offset += count; - if( offset == buffer.length() ) { - offset = 0; - buffer.setLength( 0 ); - } - } - - return count; - } - - private void processCurrentToken() throws IOException { - switch( parser.getCurrentToken() ) { - case START_OBJECT: - processStartObject(); - break; - case END_OBJECT: - processEndObject(); - break; - case START_ARRAY: - processStartArray(); - break; - case END_ARRAY: - processEndArray(); - break; - case FIELD_NAME: - processFieldName(); // Could be the name of an object, array or value. - break; - case VALUE_STRING: - processValueString(); - break; - case VALUE_NUMBER_INT: - case VALUE_NUMBER_FLOAT: - processValueNumber(); - break; - case VALUE_TRUE: - case VALUE_FALSE: - processValueBoolean(); - break; - case VALUE_NULL: - processValueNull(); - break; - case NOT_AVAILABLE: - // Ignore it. - break; - } - generator.flush(); - } - - private Level pushLevel( String field, JsonNode node, JsonNode scopeNode, UrlRewriteFilterGroupDescriptor scopeConfig ) { - if( !stack.isEmpty() ) { - Level top = stack.peek(); - if( scopeNode == null ) { - scopeNode = top.scopeNode; - scopeConfig = top.scopeConfig; - } - } - Level level = new Level( field, node, scopeNode, scopeConfig ); - stack.push( level ); - return level; - } - - private void processStartObject() throws IOException { - JsonNode node; - Level child; - Level parent; - if( stack.isEmpty() ) { - node = mapper.createObjectNode(); - child = pushLevel( null, node, node, config ); - } else { - child = stack.peek(); - if( child.node == null ) { - child.node = mapper.createObjectNode(); - parent = stack.get( stack.size()-2 ); - switch( parent.node.asToken() ) { - case START_ARRAY: - ((ArrayNode)parent.node ).add( child.node ); - break; - case START_OBJECT: - ((ObjectNode)parent.node ).put( child.field, child.node ); - break; - default: - throw new IllegalStateException(); - } - } else if( child.isArray() ) { - parent = child; - node = mapper.createObjectNode(); - child = pushLevel( null, node, null, null ); - ((ArrayNode)parent.node ).add( child.node ); - } else { - throw new IllegalStateException(); - } - } - if( bufferingLevel == null ) { - if( !startBuffering( child ) ) { - generator.writeStartObject(); - } - } - } - - private void processEndObject() throws IOException { - Level child; - Level parent; - child = stack.pop(); - if( bufferingLevel == child ) { - filterBufferedNode( child ); - mapper.writeTree( generator, child.node ); - bufferingLevel = null; - bufferingConfig = null; - } else if( bufferingLevel == null ) { - generator.writeEndObject(); - if( !stack.isEmpty() ) { - parent = stack.peek(); - switch( parent.node.asToken() ) { - case START_ARRAY: - ((ArrayNode)parent.node ).removeAll(); - break; - case START_OBJECT: - ((ObjectNode)parent.node ).removeAll(); - break; - default: - throw new IllegalStateException(); - } - } - } - } - - private void processStartArray() throws IOException { - JsonNode node; - Level child; - Level parent; - if( stack.isEmpty() ) { - node = mapper.createArrayNode(); - child = pushLevel( null, node, node, config ); - } else { - child = stack.peek(); - if( child.node == null ) { - child.node = mapper.createArrayNode(); - parent = stack.get( stack.size() - 2 ); - switch( parent.node.asToken() ) { - case START_ARRAY: - ((ArrayNode)parent.node ).add( child.node ); - break; - case START_OBJECT: - ((ObjectNode)parent.node ).put( child.field, child.node ); - break; - default: - throw new IllegalStateException(); - } - } else if( child.isArray() ) { - parent = child; - child = pushLevel( null, mapper.createArrayNode(), null, null ); - ((ArrayNode)parent.node ).add( child.node ); - } else { - throw new IllegalStateException(); - } - } - if( bufferingLevel == null ) { - if( !startBuffering( child ) ) { - generator.writeStartArray(); - } - } - } - - private void processEndArray() throws IOException { - Level child; - Level parent; - child = stack.pop(); - if( bufferingLevel == child ) { - filterBufferedNode( child ); - mapper.writeTree( generator, child.node ); - bufferingLevel = null; - bufferingConfig = null; - } else if( bufferingLevel == null ) { - generator.writeEndArray(); - if( !stack.isEmpty() ) { - parent = stack.peek(); - switch( parent.node.asToken() ) { - case START_ARRAY: - ((ArrayNode)parent.node ).removeAll(); - break; - case START_OBJECT: - ((ObjectNode)parent.node ).removeAll(); - break; - default: - throw new IllegalStateException(); - } - } - } - } - - private void processFieldName() throws IOException { - Level child = pushLevel( parser.getCurrentName(), null, null, null ); - try { - child.field = filterFieldName( child.field ); - } catch( Exception e ) { - LOG.failedToFilterFieldName( child.field, e ); - // Write original name. - } - if( bufferingLevel == null ) { - generator.writeFieldName( child.field ); - } - } - - private void processValueString() throws IOException { - Level child; - Level parent; - String value = null; - parent = stack.peek(); - if( parent.isArray() ) { - ArrayNode array = (ArrayNode)parent.node; - array.add( parser.getText() ); - if( bufferingLevel == null ) { - value = filterStreamValue( parent ); - array.set( array.size()-1, new TextNode( value ) ); - } else { - array.removeAll(); - } - } else { - child = stack.pop(); - parent = stack.peek(); - ((ObjectNode)parent.node ).put( child.field, parser.getText() ); - if( bufferingLevel == null ) { - child.node = parent.node; // Populate the JsonNode of the child for filtering. - value = filterStreamValue( child ); - } - } - if( bufferingLevel == null ) { - if( parent.node.isArray() ) { - ((ArrayNode)parent.node).removeAll(); - } else { - ((ObjectNode)parent.node).removeAll(); - } - generator.writeString( value ); - } - } - - private void processValueNumber() throws IOException { - Level child; - Level parent; - parent = stack.peek(); - if( parent.isArray() ) { - if( bufferingLevel != null ) { - ArrayNode array = (ArrayNode)parent.node; - processBufferedArrayValueNumber( array ); - } - } else { - child = stack.pop(); - if( bufferingLevel != null ) { - parent = stack.peek(); - ObjectNode object = (ObjectNode)parent.node; - processBufferedFieldValueNumber( child, object ); - } - } - if( bufferingLevel == null ) { - processedUnbufferedValueNumber(); - } - } - - private void processedUnbufferedValueNumber() throws IOException { - switch( parser.getNumberType() ) { - case INT: - generator.writeNumber( parser.getIntValue() ); - break; - case LONG: - generator.writeNumber( parser.getLongValue() ); - break; - case BIG_INTEGER: - generator.writeNumber( parser.getBigIntegerValue() ); - break; - case FLOAT: - generator.writeNumber( parser.getFloatValue() ); - break; - case DOUBLE: - generator.writeNumber( parser.getDoubleValue() ); - break; - case BIG_DECIMAL: - generator.writeNumber( parser.getDecimalValue() ); - break; - } - } - - private void processBufferedFieldValueNumber( Level child, ObjectNode object ) throws IOException { - //object.put( child.field, parser.getDecimalValue() ); - switch( parser.getNumberType() ) { - case INT: - object.put( child.field, parser.getIntValue() ); - break; - case LONG: - object.put( child.field, parser.getLongValue() ); - break; - case BIG_INTEGER: - object.put( child.field, parser.getDecimalValue() ); - break; - case FLOAT: - object.put( child.field, parser.getFloatValue() ); - break; - case DOUBLE: - object.put( child.field, parser.getDoubleValue() ); - break; - case BIG_DECIMAL: - object.put( child.field, parser.getDecimalValue() ); - break; - } - } - - private void processBufferedArrayValueNumber( ArrayNode array ) throws IOException { - //array.add( parser.getDecimalValue() ); - switch( parser.getNumberType() ) { - case INT: - array.add( parser.getIntValue() ); - break; - case LONG: - array.add( parser.getLongValue() ); - break; - case BIG_INTEGER: - array.add( parser.getDecimalValue() ); - break; - case FLOAT: - array.add( parser.getFloatValue() ); - break; - case DOUBLE: - array.add( parser.getDoubleValue() ); - break; - case BIG_DECIMAL: - array.add( parser.getDecimalValue() ); - break; - } - } - - private void processValueBoolean() throws IOException { - Level child; - Level parent; - parent = stack.peek(); - if( parent.isArray() ) { - ((ArrayNode)parent.node ).add( parser.getBooleanValue() ); - //dump(); - if( bufferingLevel == null ) { - ((ArrayNode)parent.node ).removeAll(); - } - } else { - child = stack.pop(); - parent = stack.peek(); - ((ObjectNode)parent.node ).put( child.field, parser.getBooleanValue() ); - //dump(); - if( bufferingLevel == null ) { - ((ObjectNode)parent.node ).remove( child.field ); - } - } - if( bufferingLevel == null ) { - generator.writeBoolean( parser.getBooleanValue() ); - } - } - - private void processValueNull() throws IOException { - Level child; - Level parent = stack.peek(); - if( parent.isArray() ) { - ((ArrayNode)parent.node ).addNull(); - //dump(); - if( bufferingLevel == null ) { - ((ArrayNode)parent.node ).removeAll(); - } - } else { - child = stack.pop(); - parent = stack.peek(); - ((ObjectNode)parent.node ).putNull( child.field ); - //dump(); - if( bufferingLevel == null ) { - ((ObjectNode)parent.node ).remove( child.field ); - } - } - if( bufferingLevel == null ) { - generator.writeNull(); - } - } - - protected boolean startBuffering( Level node ) { - boolean buffered = false; - UrlRewriteFilterGroupDescriptor scope = node.scopeConfig; - if( scope != null ) { - for( UrlRewriteFilterPathDescriptor selector : scope.getSelectors() ) { - JsonPath.Expression path = (JsonPath.Expression)selector.compiledPath( JPATH_COMPILER ); - List<JsonPath.Match> matches = path.evaluate( node.scopeNode ); - if( matches != null && matches.size() > 0 ) { - if( selector instanceof UrlRewriteFilterBufferDescriptor ) { - bufferingLevel = node; - bufferingConfig = (UrlRewriteFilterBufferDescriptor)selector; - buffered = true; - } - break; - } - } - } - return buffered; - } - - protected String filterStreamValue( Level node ) { - String value; - if( node.isArray() ) { - value = node.node.get( 0 ).asText(); - } else { - value = node.node.get( node.field ).asText(); - } - String rule = null; - UrlRewriteFilterGroupDescriptor scope = node.scopeConfig; - //TODO: Scan the top level apply rules for the first match. - if( scope != null ) { - for( UrlRewriteFilterPathDescriptor selector : scope.getSelectors() ) { - JsonPath.Expression path = (JsonPath.Expression)selector.compiledPath( JPATH_COMPILER ); - List<JsonPath.Match> matches = path.evaluate( node.scopeNode ); - if( matches != null && matches.size() > 0 ) { - JsonPath.Match match = matches.get( 0 ); - if( match.getNode().isTextual() ) { - if( selector instanceof UrlRewriteFilterApplyDescriptor ) { - UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor)selector; - rule = apply.rule(); - break; - } - } - } - } - } - try { - value = filterValueString( node.field, value, rule ); - if( node.isArray() ) { - ((ArrayNode)node.node).set( 0, new TextNode( value ) ); - } else { - ((ObjectNode)node.node).put( node.field, value ); - } - } catch( Exception e ) { - LOG.failedToFilterValue( value, rule, e ); - } - return value; - } - - private void filterBufferedNode( Level node ) { - for( UrlRewriteFilterPathDescriptor selector : bufferingConfig.getSelectors() ) { - JsonPath.Expression path = (JsonPath.Expression)selector.compiledPath( JPATH_COMPILER ); - List<JsonPath.Match> matches = path.evaluate( node.node ); - for( JsonPath.Match match : matches ) { - if( selector instanceof UrlRewriteFilterApplyDescriptor ) { - if( match.getNode().isTextual() ) { - filterBufferedValue( match, (UrlRewriteFilterApplyDescriptor)selector ); - } - } else if( selector instanceof UrlRewriteFilterDetectDescriptor ) { - UrlRewriteFilterDetectDescriptor detectConfig = (UrlRewriteFilterDetectDescriptor)selector; - JsonPath.Expression detectPath = (JsonPath.Expression)detectConfig.compiledPath( JPATH_COMPILER ); - List<JsonPath.Match> detectMatches = detectPath.evaluate( node.node ); - for( JsonPath.Match detectMatch : detectMatches ) { - if( detectMatch.getNode().isTextual() ) { - String detectValue = detectMatch.getNode().asText(); - Pattern detectPattern = detectConfig.compiledValue( REGEX_COMPILER ); - if( detectPattern.matcher( detectValue ).matches() ) { - filterBufferedValues( node, detectConfig.getSelectors() ); - } - } - } - } - } - } - } - - private void filterBufferedValues( Level node, List<UrlRewriteFilterPathDescriptor> selectors ) { - for( UrlRewriteFilterPathDescriptor selector : selectors ) { - JsonPath.Expression path = (JsonPath.Expression)selector.compiledPath( JPATH_COMPILER ); - List<JsonPath.Match> matches = path.evaluate( node.node ); - for( JsonPath.Match match : matches ) { - if( match.getNode().isTextual() ) { - if( selector instanceof UrlRewriteFilterApplyDescriptor ) { - filterBufferedValue( match, (UrlRewriteFilterApplyDescriptor)selector ); - } - } - } - } - } - - private void filterBufferedValue( JsonPath.Match match, UrlRewriteFilterApplyDescriptor apply ) { - String field = match.getField(); - String value = match.getNode().asText(); - try { - value = filterValueString( field, value, apply.rule() ); - ((ObjectNode)match.getParent().getNode()).put( field, value ); - } catch( Exception e ) { - LOG.failedToFilterValue( value, apply.rule(), e ); - } - } - - protected String filterFieldName( String field ) { - return field; - } - - protected String filterValueString( String name, String value, String rule ) { - return value; - } - - @Override - public void close() throws IOException { - generator.close(); - writer.close(); - parser.close(); - reader.close(); - } - - private static class Level { - String field; - JsonNode node; - JsonNode scopeNode; - UrlRewriteFilterGroupDescriptor scopeConfig; - private Level( String field, JsonNode node, JsonNode scopeNode, UrlRewriteFilterGroupDescriptor scopeConfig ) { - this.field = field; - this.node = node; - this.scopeNode = scopeNode; - this.scopeConfig = scopeConfig; - } - public boolean isArray() { - return node != null && node.isArray(); - } - } - - private static class JsonPathCompiler implements UrlRewriteFilterPathDescriptor.Compiler<JsonPath.Expression> { - @Override - public JsonPath.Expression compile( String expression, JsonPath.Expression compiled ) { - return JsonPath.compile( expression ); - } - } - - private static class RegexCompiler implements UrlRewriteFilterPathDescriptor.Compiler<Pattern> { - @Override - public Pattern compile( String expression, Pattern compiled ) { - if( compiled != null ) { - return compiled; - } else { - return Pattern.compile( expression ); - } - } - } - -// private void dump() throws IOException { -// mapper.writeTree( factory.createGenerator( System.out ), stack.get( 0 ).node ); -// System.out.println(); -// } - -} - http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteFilterReader.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteFilterReader.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteFilterReader.java deleted file mode 100644 index a5bbc82..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteFilterReader.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.json; - -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; -import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; -import org.apache.hadoop.gateway.util.urltemplate.Parser; -import org.apache.hadoop.gateway.util.urltemplate.Resolver; -import org.apache.hadoop.gateway.util.urltemplate.Template; - -import java.io.IOException; -import java.io.Reader; -import java.net.URISyntaxException; - -public class JsonUrlRewriteFilterReader extends JsonFilterReader { - - private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class ); - - private Resolver resolver; - private UrlRewriter rewriter; - private UrlRewriter.Direction direction; - - public JsonUrlRewriteFilterReader( - Reader reader, - UrlRewriter rewriter, - Resolver resolver, - UrlRewriter.Direction direction, - UrlRewriteFilterContentDescriptor config ) - throws IOException { - super( reader, config ); - this.resolver = resolver; - this.rewriter = rewriter; - this.direction = direction; - } - - protected String filterValueString( String name, String value, String rule ) { - try { - Template input = Parser.parseLiteral( value ); - Template output = rewriter.rewrite( resolver, input, direction, rule ); - value = output.getPattern(); - } catch( URISyntaxException e ) { - LOG.failedToParseValueForUrlRewrite( value ); - } - return value; - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteStreamFilter.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteStreamFilter.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteStreamFilter.java deleted file mode 100644 index 9599f40..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/json/JsonUrlRewriteStreamFilter.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.json; - -import org.apache.commons.io.input.ReaderInputStream; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; -import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStreamFilter; -import org.apache.hadoop.gateway.util.urltemplate.Resolver; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -public class JsonUrlRewriteStreamFilter implements UrlRewriteStreamFilter { - - private static String[] TYPES = new String[]{ "application/json", "text/json", "*/json" }; - private static String[] NAMES = new String[]{ null }; - - @Override - public String[] getTypes() { - return TYPES; - } - - @Override - public String[] getNames() { - return NAMES; - } - - @Override - public InputStream filter( - InputStream stream, - String encoding, - UrlRewriter rewriter, - Resolver resolver, - UrlRewriter.Direction direction, - UrlRewriteFilterContentDescriptor config ) - throws IOException { - return new ReaderInputStream( - new JsonUrlRewriteFilterReader( - new InputStreamReader( stream, encoding ), rewriter, resolver, direction, config ), encoding ); - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java deleted file mode 100644 index 00a9027..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.noop; - -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; -import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStreamFilter; -import org.apache.hadoop.gateway.util.urltemplate.Resolver; - -import java.io.IOException; -import java.io.InputStream; - -public class NoOpUrlRewriteStreamFilter implements UrlRewriteStreamFilter { - - private static String[] TYPES = new String[]{ null }; - private static String[] NAMES = new String[]{ null }; - - @Override - public String[] getTypes() { - return TYPES; - } - - @Override - public String[] getNames() { - return NAMES; - } - - @Override - public InputStream filter( - InputStream stream, - String encoding, - UrlRewriter rewriter, - Resolver resolver, - UrlRewriter.Direction direction, - UrlRewriteFilterContentDescriptor config ) - throws IOException { - return stream; - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java deleted file mode 100644 index 2dd6519..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java +++ /dev/null @@ -1,643 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterScopeDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteResources; -import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; -import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory; -import org.apache.hadoop.gateway.util.XmlUtils; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.Text; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.Attribute; -import javax.xml.stream.events.Characters; -import javax.xml.stream.events.Comment; -import javax.xml.stream.events.EndElement; -import javax.xml.stream.events.Namespace; -import javax.xml.stream.events.StartDocument; -import javax.xml.stream.events.StartElement; -import javax.xml.stream.events.XMLEvent; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import java.io.IOException; -import java.io.Reader; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Iterator; -import java.util.Stack; -import java.util.regex.Pattern; - -public abstract class XmlFilterReader extends Reader { - - private static final UrlRewriteResources RES = ResourcesFactory.get( UrlRewriteResources.class ); - - private static final String DEFAULT_XML_VERSION = "1.0"; - - private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class ); - private static final UrlRewriteFilterPathDescriptor.Compiler<XPathExpression> XPATH_COMPILER = new XmlPathCompiler(); - private static final UrlRewriteFilterPathDescriptor.Compiler<Pattern> REGEX_COMPILER = new RegexCompiler(); - - private Reader reader; - private UrlRewriteFilterContentDescriptor config; - private int offset; - private StringWriter writer; - private StringBuffer buffer; - private XMLInputFactory factory; - private XMLEventReader parser; - private Document document; - private Stack<Level> stack; - private boolean isEmptyElement; - - protected XmlFilterReader( Reader reader, UrlRewriteFilterContentDescriptor config ) throws IOException, XMLStreamException { - this.reader = reader; - this.config = config; - writer = new StringWriter(); - buffer = writer.getBuffer(); - offset = 0; - document = null; - stack = new Stack<Level>(); - isEmptyElement = false; - factory = XMLInputFactory.newFactory(); - //KNOX-620 factory.setProperty( XMLConstants.ACCESS_EXTERNAL_DTD, "false" ); - //KNOX-620 factory.setProperty( XMLConstants.ACCESS_EXTERNAL_SCHEMA, "false" ); - factory.setProperty( "javax.xml.stream.isReplacingEntityReferences", Boolean.FALSE ); - factory.setProperty("http://java.sun.com/xml/stream/" - + "properties/report-cdata-event", Boolean.TRUE); - parser = factory.createXMLEventReader( reader ); - } - - protected abstract String filterAttribute( QName elementName, QName attributeName, String attributeValue, String ruleName ); - - protected abstract String filterText( QName elementName, String text, String ruleName ); - - @Override - public int read( char[] destBuffer, int destOffset, int destCount ) throws IOException { - int count = 0; - int available = buffer.length() - offset; - - if( available == 0 ) { - if( parser.hasNext() ) { - try { - XMLEvent event = parser.nextEvent(); - processEvent( event ); - } catch( IOException e ) { - throw e; - } catch( RuntimeException e ) { - throw e; - } catch ( Exception e ) { - throw new RuntimeException( e ); - } - available = buffer.length() - offset; - } else { - count = -1; - } - } - - if( available > 0 ) { - count = Math.min( destCount, available ); - buffer.getChars( offset, offset + count, destBuffer, destOffset ); - offset += count; - if( offset == buffer.length() ) { - offset = 0; - buffer.setLength( 0 ); - } - } - return count; - } - - private void processEvent( XMLEvent event ) throws ParserConfigurationException, XPathExpressionException, IOException, XMLStreamException { - int type = event.getEventType(); - switch( type ) { - case XMLEvent.START_DOCUMENT: - processStartDocument( (StartDocument)event ); - break; - case XMLEvent.END_DOCUMENT: - processEndDocument(); - break; - case XMLEvent.START_ELEMENT: - if( parser.peek().getEventType() == XMLEvent.END_ELEMENT ) - isEmptyElement = true; - processStartElement( event.asStartElement()); - break; - case XMLEvent.END_ELEMENT: - processEndElement( event.asEndElement() ); - isEmptyElement = false; - break; - case XMLEvent.CHARACTERS: - case XMLEvent.CDATA: - case XMLEvent.SPACE: - processCharacters( event.asCharacters() ); - break; - case XMLEvent.COMMENT: - processComment( (Comment)event ); - break; - case XMLEvent.DTD: - case XMLEvent.NAMESPACE: - case XMLEvent.ATTRIBUTE: - case XMLEvent.ENTITY_REFERENCE: - case XMLEvent.ENTITY_DECLARATION: - case XMLEvent.NOTATION_DECLARATION: - case XMLEvent.PROCESSING_INSTRUCTION: - default: - // Fail if we run into any of these for now. - throw new IllegalStateException( Integer.toString( type ) ); - } - } - - private void processStartDocument( StartDocument event ) throws ParserConfigurationException { - //System.out.println( "SD=" + event ); - String s; - - document = XmlUtils.createDocument( false ); - pushLevel( null, event, document, document, config ); - - writer.write( "<?xml" ); - - s = event.getVersion(); - if( s == null ) { - s = DEFAULT_XML_VERSION; - } - writer.write( " version=\""); - writer.write( s ); - writer.write( "\"" ); - - s = event.getCharacterEncodingScheme(); - if( s != null ) { - writer.write( " encoding=\""); - writer.write( s ); - writer.write( "\"" ); - } - - writer.write( " standalone=\""); - writer.write( event.isStandalone() ? "yes" : "no" ); - writer.write( "\"" ); - - writer.write( "?>" ); - } - - private void processEndDocument() { - stack.clear(); - document = null; - } - - private void processStartElement( StartElement event ) throws XPathExpressionException { - //System.out.println( "SE=" + event ); - - // Create a new "empty" element and add it to the document. - Element element = bufferElement( event ); - Level parent = stack.peek(); - parent.node.appendChild( element ); - - // If already buffering just continue to do so. - // Note: Don't currently support nested buffer or scope descriptors. - if( currentlyBuffering() ) { - pushLevel( parent, event, element, parent.scopeNode, parent.scopeConfig ); - bufferAttributes( event, element ); - // Else not currently buffering - } else { - // See if there is a matching path descriptor in the current scope. - UrlRewriteFilterPathDescriptor descriptor = pickFirstMatchingPath( parent ); - if( descriptor != null ) { - // If this is a buffer descriptor then switch to buffering and buffer the attributes. - if( descriptor instanceof UrlRewriteFilterBufferDescriptor ) { - pushLevel( parent, event, element, element, (UrlRewriteFilterBufferDescriptor)descriptor ); - bufferAttributes( event, element ); - // Otherwise if this is a scope descriptor then change the scope and stream the attributes. - } else if( descriptor instanceof UrlRewriteFilterScopeDescriptor ) { - pushLevel( parent, event, element, element, (UrlRewriteFilterScopeDescriptor)descriptor ); - streamElement( event, element ); - // Else found an unexpected matching path. - } else { - // This is likely because there is an <apply> targeted at the text of an element. - // That "convenience" config will be taken care of in the streamElement() processing. - pushLevel( parent, event, element, parent.scopeNode, parent.scopeConfig ); - streamElement( event, element ); - } - // If there is no matching path descriptor then continue streaming. - } else { - pushLevel( parent, event, element, parent.scopeNode, parent.scopeConfig ); - streamElement( event, element ); - } - } - } - - private void processEndElement( EndElement event ) throws XPathExpressionException, IOException { - //System.out.println( "EE=" + event ); - boolean buffering = currentlyBuffering(); - Level child = stack.pop(); - if( buffering ) { - if( child.node == child.scopeNode ) { - processBufferedElement( child ); - } - } else { - if( ! isEmptyElement ) { - QName n = event.getName(); - writer.write( "</" ); - String p = n.getPrefix(); - if( p != null && !p.isEmpty() ) { - writer.write( p ); - writer.write( ":" ); - } - writer.write( n.getLocalPart() ); - writer.write( ">" ); - } - child.node.getParentNode().removeChild( child.node ); - } - } - - private Element bufferElement( StartElement event ) { - QName qname = event.getName(); - String prefix = qname.getPrefix(); - String uri = qname.getNamespaceURI(); - Element element; - if( uri == null || uri.isEmpty() ) { - element = document.createElement( qname.getLocalPart() ); - } else { - element = document.createElementNS( qname.getNamespaceURI(), qname.getLocalPart() ); - if( prefix != null && !prefix.isEmpty() ) { - element.setPrefix( prefix ); - } - } - // Always need to buffer the namespaces regardless of what else happens so that XPath will work on attributes - // namespace qualified attributes. - bufferNamespaces( event, element ); - return element; - } - - private void bufferNamespaces( StartElement event, Element element ) { - Iterator namespaces = event.getNamespaces(); - while( namespaces.hasNext() ) { - Namespace namespace = (Namespace)namespaces.next(); - if( namespace.isDefaultNamespaceDeclaration() ) { - element.setAttribute( "xmlns", namespace.getNamespaceURI() ); - } else { - element.setAttribute( "xmlns:" + namespace.getPrefix(), namespace.getNamespaceURI() ); - } - } - } - - private void streamElement( StartElement event, Element element ) throws XPathExpressionException { - writer.write( "<" ); - QName qname = event.getName(); - String prefix = event.getName().getPrefix(); - if( prefix != null && !prefix.isEmpty() ) { - writer.write( prefix ); - writer.write( ":" ); - } - writer.write( qname.getLocalPart() ); - streamNamespaces( event ); - streamAttributes( event, element ); - if( isEmptyElement ) { - writer.write("/>"); - } else { - writer.write(">"); - } - } - - private void processBufferedElement( Level level, UrlRewriteFilterGroupDescriptor config ) throws XPathExpressionException { - for( UrlRewriteFilterPathDescriptor selector : config.getSelectors() ) { - if( selector instanceof UrlRewriteFilterApplyDescriptor ) { - XPathExpression path = (XPathExpression)selector.compiledPath( XPATH_COMPILER ); - Object node = path.evaluate( level.scopeNode, XPathConstants.NODE ); - if( node != null ) { - UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor)selector; - if( node instanceof Element ) { - Element element = (Element)node; - String value = element.getTextContent(); - value = filterText( extractQName( element ), value, apply.rule() ); - element.setTextContent( value ); - } else if( node instanceof Text ) { - Text text = (Text)node; - String value = text.getWholeText(); - value = filterText( extractQName( text.getParentNode() ), value, apply.rule() ); - text.replaceWholeText( value ); - } else if( node instanceof Attr ) { - Attr attr = (Attr)node; - String value = attr.getValue(); - value = filterAttribute( extractQName( attr.getOwnerElement() ), extractQName( attr ), value, apply.rule() ); - attr.setValue( value ); - } else { - throw new IllegalArgumentException( RES.unexpectedSelectedNodeType( node ) ); - } - } - } else if( selector instanceof UrlRewriteFilterDetectDescriptor ) { - XPathExpression path = (XPathExpression)selector.compiledPath( XPATH_COMPILER ); - Object node = path.evaluate( level.scopeNode, XPathConstants.NODE ); - if( node != null ) { - UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor)selector; - String value = null; - if( node instanceof Element ) { - Element element = (Element)node; - value = element.getTextContent(); - } else if( node instanceof Text ) { - Text text = (Text)node; - value = text.getWholeText(); - } else if( node instanceof Attr ) { - Attr attr = (Attr)node; - value = attr.getValue(); - } else { - throw new IllegalArgumentException( RES.unexpectedSelectedNodeType( node ) ); - } - if( detect.compiledValue( REGEX_COMPILER ).matcher( value ).matches() ) { - processBufferedElement( level, detect ); - } - } - } else { - throw new IllegalArgumentException( RES.unexpectedRewritePathSelector( selector ) ); - } - } - } - - private void processBufferedElement( Level level ) throws XPathExpressionException, IOException { - processBufferedElement( level, level.scopeConfig ); - writeBufferedElement( level.node, writer ); - } - - private QName extractQName( Node node ) { - QName qname; - String localName = node.getLocalName(); - if( localName == null ) { - qname = new QName( node.getNodeName() ); - } else { - if ( node.getPrefix() == null ) { - qname = new QName( node.getNamespaceURI(), localName ); - } else { - qname = new QName( node.getNamespaceURI(), localName, node.getPrefix() ); - } - - } - return qname; - } - - private void bufferAttributes( StartElement event, Element element ) { - Iterator attributes = event.getAttributes(); - while( attributes.hasNext() ) { - Attribute attribute = (Attribute)attributes.next(); - bufferAttribute( element, attribute ); - } - } - - private Attr bufferAttribute( Element element, Attribute attribute ) { - QName name = attribute.getName(); - String prefix = name.getPrefix(); - String uri = name.getNamespaceURI(); - Attr node; - if( uri == null || uri.isEmpty() ) { - node = document.createAttribute( name.getLocalPart() ); - element.setAttributeNode( node ); - } else { - node = document.createAttributeNS( uri, name.getLocalPart() ); - if( prefix != null && !prefix.isEmpty() ) { - node.setPrefix( prefix ); - } - element.setAttributeNodeNS( node ); - } - node.setTextContent( attribute.getValue() ); - return node; - } - - private void streamNamespaces( StartElement event ) { - Iterator i = event.getNamespaces(); - while( i.hasNext() ) { - Namespace ns = (Namespace)i.next(); - writer.write( " xmlns" ); - if( !ns.isDefaultNamespaceDeclaration() ) { - writer.write( ":" ); - writer.write( ns.getPrefix() ); - } - writer.write( "=\"" ); - writer.write( ns.getNamespaceURI() ); - writer.write( "\"" ); - } - } - - private void streamAttributes( StartElement event, Element element ) throws XPathExpressionException { - Iterator i = event.getAttributes(); - while( i.hasNext() ) { - Attribute attribute = (Attribute)i.next(); - streamAttribute( element, attribute ); - } - } - - private void streamAttribute( Element element, Attribute attribute ) throws XPathExpressionException { - Attr node; - QName name = attribute.getName(); - String prefix = name.getPrefix(); - String uri = name.getNamespaceURI(); - if( uri == null || uri.isEmpty() ) { - node = document.createAttribute( name.getLocalPart() ); - element.setAttributeNode( node ); - } else { - node = document.createAttributeNS( uri, name.getLocalPart() ); - if( prefix != null && !prefix.isEmpty() ) { - node.setPrefix( prefix ); - } - element.setAttributeNodeNS( node ); - } - - String value = attribute.getValue(); - Level level = stack.peek(); - if( ( level.scopeConfig ) == null || ( level.scopeConfig.getSelectors().isEmpty() ) ) { - value = filterAttribute( null, attribute.getName(), value, null ); - node.setValue( value ); - } else { - UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath( level ); - if( path instanceof UrlRewriteFilterApplyDescriptor ) { - String rule = ((UrlRewriteFilterApplyDescriptor)path).rule(); - value = filterAttribute( null, attribute.getName(), value, rule ); - node.setValue( value ); - } - } - - //dump( document ); - - if( prefix == null || prefix.isEmpty() ) { - writer.write( " " ); - writer.write( name.getLocalPart() ); - } else { - writer.write( " " ); - writer.write( prefix ); - writer.write( ":" ); - writer.write( name.getLocalPart() ); - } - writer.write( "=\"" ); - writer.write( value ); - writer.write( "\"" ); - element.removeAttributeNode( node ); - } - - private void processCharacters( Characters event ) throws XPathExpressionException { - //System.out.println( "T[" + event.isCData() + "," + event.isWhiteSpace() + "," + event.isIgnorableWhiteSpace() + "]=" + event ); - Level level = stack.peek(); - Node node = stack.peek().node; - if( event.isCData() ) { - node.appendChild( document.createCDATASection( event.getData() ) ); - } else { - node.appendChild( document.createTextNode( event.getData() ) ); - } - if( !currentlyBuffering() ) { - String value = event.getData(); - if( !event.isWhiteSpace() ) { - if( level.scopeConfig == null || level.scopeConfig.getSelectors().isEmpty() ) { - value = filterText( extractQName( node ), value, null ); - } else { - UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath( level ); - if( path instanceof UrlRewriteFilterApplyDescriptor ) { - String rule = ((UrlRewriteFilterApplyDescriptor)path).rule(); - value = filterText( extractQName( node ), value, rule ); - } - } - } - if( event.isCData() ) { - writer.write( "<![CDATA[" ); - writer.write( value ); - writer.write( "]]>" ); - } else { - writer.write( StringEscapeUtils.escapeXml( value ) ); - } - } - } - - private void processComment( Comment event ) { - //System.out.println( "C=" + event ); - if( currentlyBuffering() ) { - stack.peek().node.appendChild( document.createComment( event.getText() ) ); - } else { - writer.write( "<!--" ); - writer.write( event.getText() ); - writer.write( "-->" ); - } - } - - @Override - public void close() throws IOException { - try { - parser.close(); - } catch( XMLStreamException e ) { - throw new IOException( e ); - } - reader.close(); - writer.close(); - stack.clear(); - } - - protected UrlRewriteFilterPathDescriptor pickFirstMatchingPath( Level level ) { - UrlRewriteFilterPathDescriptor match = null; - if( level.scopeConfig != null ) { - for( UrlRewriteFilterPathDescriptor selector : level.scopeConfig.getSelectors() ) { - try { - XPathExpression path = (XPathExpression)selector.compiledPath( XPATH_COMPILER ); - Object node = path.evaluate( level.scopeNode, XPathConstants.NODE ); - if( node != null ) { - match = selector; - break; - } - } catch( XPathExpressionException e ) { - throw new IllegalArgumentException( selector.path(), e ); - } - } - } - return match; - } - - private boolean currentlyBuffering() { - return stack.peek().buffered; - } - - private Level pushLevel( Level parent, XMLEvent event, Node node, Node scopeNode, UrlRewriteFilterGroupDescriptor scopeConfig ) { - Level level = new Level( parent, event, node, scopeNode, scopeConfig ); - stack.push( level ); - return level; - } - - private static class Level { -// private Level parent; -// private XMLEvent event; - private Node node; - private UrlRewriteFilterGroupDescriptor scopeConfig; - private Node scopeNode; - private boolean buffered; - - private Level( Level parent, XMLEvent event, Node node, Node scopeNode, UrlRewriteFilterGroupDescriptor scopeConfig ) { -// this.parent = parent; -// this.event = event; - this.node = node; - this.scopeConfig = scopeConfig; - this.scopeNode = scopeNode; - this.buffered = ( ( parent != null ) && parent.buffered ) || - ( ( scopeConfig != null ) && ( scopeConfig instanceof UrlRewriteFilterBufferDescriptor ) ); - } - } - - private static class XmlPathCompiler implements UrlRewriteFilterPathDescriptor.Compiler<XPathExpression> { - private static XPath XPATH = XPathFactory.newInstance().newXPath(); - @Override - public XPathExpression compile( String expression, XPathExpression compiled ) { - try { - return XPATH.compile( expression ); - } catch( XPathExpressionException e ) { - throw new IllegalArgumentException( e ); - } - } - } - - private static class RegexCompiler implements UrlRewriteFilterPathDescriptor.Compiler<Pattern> { - @Override - public Pattern compile( String expression, Pattern compiled ) { - if( compiled != null ) { - return compiled; - } else { - return Pattern.compile( expression ); - } - } - } - - private static final void writeBufferedElement( Node node, Writer writer ) throws IOException { - try { - Transformer t = XmlUtils.getTransformer( false, false, 0, true ); - t.transform( new DOMSource( node ), new StreamResult( writer ) ); - } catch( TransformerException e ) { - throw new IOException( e ); - } - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java deleted file mode 100644 index 640d731..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java +++ /dev/null @@ -1,239 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -import org.apache.commons.digester3.Digester; -import org.apache.commons.digester3.Rule; -import org.apache.commons.digester3.SetPropertiesRule; -import org.apache.commons.digester3.binder.AbstractRulesModule; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptorFactory; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptorFactory; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteStepDescriptorFactory; -import org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteFilterApplyDescriptorImpl; -import org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteFilterBufferDescriptorImpl; -import org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteFilterDetectDescriptorImpl; -import org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteFilterScopeDescriptorImpl; -import org.xml.sax.Attributes; - -public class XmlRewriteRulesDigester extends AbstractRulesModule implements XmlRewriteRulesTags { - - @Override - protected void configure() { - forPattern( ROOT ).addRule( new RulesFactory() ); - forPattern( ROOT ).addRule( new SetPropertiesRule() ); - - for( String name : UrlRewriteFunctionDescriptorFactory.getNames() ) { - forPattern( ROOT + "/" + FUNCTIONS + "/" + name ).addRule( new FunctionFactory() ); - forPattern( ROOT + "/" + FUNCTIONS + "/" + name ).addRule( new SetPropertiesRule() ); - } - - forPattern( ROOT + "/" + RULE ).addRule( new RuleFactory() ); - forPattern( ROOT + "/" + RULE ).addRule( new SetPropertiesRule() ); - for( String type : UrlRewriteStepDescriptorFactory.getTypes() ) { - forPattern( "*/" + type ).addRule( new StepFactory() ); - forPattern( "*/" + type ).addRule( new SetPropertiesRule() ); - } - - forPattern( ROOT + "/" + FILTER ).addRule( new FilterFactory() ); - forPattern( ROOT + "/" + FILTER ).addRule( new SetPropertiesRule() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT ).addRule( new FilterContentFactory() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT ).addRule( new SetPropertiesRule() ); - - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/*/" + APPLY ).addRule( new FilterApplyFactory() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/*/" + APPLY ).addRule( new SetPropertiesRule() ); - - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + SCOPE ).addRule( new FilterScopeFactory() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + SCOPE ).addRule( new SetPropertiesRule() ); - - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + BUFFER ).addRule( new FilterBufferFactory() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + BUFFER ).addRule( new SetPropertiesRule() ); - - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + BUFFER + "/" + DETECT ).addRule( new FilterDetectFactory() ); - forPattern( ROOT + "/" + FILTER + "/" + CONTENT + "/" + BUFFER + "/" + DETECT ).addRule( new SetPropertiesRule() ); - -// forPattern( "*/" + MATCH ).addRule( new MatchFactory() ); -// forPattern( "*/" + MATCH ).addRule( new SetPropertiesRule() ); -// forPattern( "*/" + CHECK ).addRule( new CheckFactory() ); -// forPattern( "*/" + CHECK ).addRule( new SetPropertiesRule() ); -// forPattern( "*/" + CONTROL ).addRule( new ControlFactory() ); -// forPattern( "*/" + CONTROL ).addRule( new SetPropertiesRule() ); -// forPattern( "*/" + ACTION ).addRule( new ActionFactory() ); -// forPattern( "*/" + ACTION ).addRule( new SetPropertiesRule() ); - } - - private static class RulesFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - return UrlRewriteRulesDescriptorFactory.create(); - } - } - - private static class RuleFactory extends Rule { - @Override - public void begin( String namespace, String name, Attributes attributes ) throws Exception { - Digester digester = getDigester(); - UrlRewriteRulesDescriptor rules = digester.peek(); - UrlRewriteRuleDescriptor rule = rules.newRule(); - getDigester().push( rule ); - } - - @Override - public void end( String namespace, String name ) throws Exception { - Digester digester = getDigester(); - UrlRewriteRuleDescriptor rule = digester.pop(); - UrlRewriteRulesDescriptor rules = digester.peek(); - rules.addRule( rule ); - } - } - - private static class StepFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFlowDescriptor flow = getDigester().peek(); - return flow.addStep( name ); - } - } - - private static class FunctionFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteRulesDescriptor rules = getDigester().peek(); - return rules.addFunction( name ); - } - } - - private static class FilterFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteRulesDescriptor parent = getDigester().peek(); - return parent.addFilter( attributes.getValue( "name" ) ); - } - } - - private static class FilterContentFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFilterDescriptor parent = getDigester().peek(); - UrlRewriteFilterContentDescriptor descriptor = parent.addContent( attributes.getValue( "type" ) ); - if (attributes.getValue( "asType" ) != null) { - descriptor = descriptor.asType(attributes.getValue( "asType" )); - } - return descriptor; - } - } - - private static class FilterApplyFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFilterGroupDescriptor parent = getDigester().peek(); - UrlRewriteFilterPathDescriptor child = new UrlRewriteFilterApplyDescriptorImpl(); - child.path( attributes.getValue( "path" ) ); - parent.addSelector( child ); - return child; - } - } - - private static class FilterScopeFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFilterGroupDescriptor parent = getDigester().peek(); - UrlRewriteFilterPathDescriptor child = new UrlRewriteFilterScopeDescriptorImpl(); - child.path( attributes.getValue( "path" ) ); - parent.addSelector( child ); - return child; - } - } - - private static class FilterBufferFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFilterGroupDescriptor parent = getDigester().peek(); - UrlRewriteFilterPathDescriptor child = new UrlRewriteFilterBufferDescriptorImpl(); - child.path( attributes.getValue( "path" ) ); - parent.addSelector( child ); - return child; - } - } - - private static class FilterDetectFactory extends FactoryRule { - @Override - public Object create( String namespace, String name, Attributes attributes ) { - UrlRewriteFilterGroupDescriptor parent = getDigester().peek(); - UrlRewriteFilterPathDescriptor child = new UrlRewriteFilterDetectDescriptorImpl(); - child.path( attributes.getValue( "path" ) ); - parent.addSelector( child ); - return child; - } - } - -// private static class MatchFactory extends FactoryRule { -// @Override -// public Object create( String namespace, String name, Attributes attributes ) { -// UrlRewriteRuleDescriptor rule = getDigester().peek(); -// return rule.addMatch(); -// } -// } -// -// private static class CheckFactory extends FactoryRule { -// @Override -// public Object create( String namespace, String name, Attributes attributes ) { -// UrlRewriteRuleDescriptor rule = getDigester().peek(); -// return rule.addCheck(); -// } -// } -// -// private static class ActionFactory extends FactoryRule { -// @Override -// public Object create( String namespace, String name, Attributes attributes ) { -// UrlRewriteRuleDescriptor rule = getDigester().peek(); -// return rule.addAction(); -// } -// } -// -// private static class ControlFactory extends FactoryRule { -// @Override -// public Object create( String namespace, String name, Attributes attributes ) { -// UrlRewriteRuleDescriptor rule = getDigester().peek(); -// return rule.addControl(); -// } -// } - - private static abstract class FactoryRule extends Rule { - - protected abstract Object create( String namespace, String name, Attributes attributes ); - - @Override - public void begin( String namespace, String name, Attributes attributes ) throws Exception { - getDigester().push( create( namespace, name, attributes ) ); - } - - @Override - public void end( String namespace, String name ) throws Exception { - getDigester().pop(); - } - - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesTags.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesTags.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesTags.java deleted file mode 100644 index 6af7301..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesTags.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -/** - * <rules> - * <filter name=""> - * <content type="json"> == <scope path="$"/> - * <apply/> - * <select> - * <choice> - * <apply/> - * </choice> - * </select> - * </content> - * </filter> - * </rules> - */ -public interface XmlRewriteRulesTags { - - static final String ROOT = "rules"; - - static final String FUNCTIONS = "functions"; - - static final String RULE = "rule"; - -// static final String MATCH = "match"; -// static final String CHECK = "check"; -// static final String CONTROL = "control"; -// static final String ACTION = "action"; - - static final String FILTER = "filter"; - static final String CONTENT = "content"; - static final String SCOPE = "scope"; - static final String BUFFER = "buffer"; - static final String DETECT = "detect"; - static final String APPLY = "apply"; - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteFilterReader.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteFilterReader.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteFilterReader.java deleted file mode 100644 index f9b39e5..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteFilterReader.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; -import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; -import org.apache.hadoop.gateway.util.urltemplate.Parser; -import org.apache.hadoop.gateway.util.urltemplate.Resolver; -import org.apache.hadoop.gateway.util.urltemplate.Template; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLStreamException; -import java.io.IOException; -import java.io.Reader; -import java.net.URISyntaxException; - -import static org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter.Direction; - -public class XmlUrlRewriteFilterReader extends XmlFilterReader { - - private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class ); - - private Resolver resolver; - private UrlRewriter rewriter; - private Direction direction; - - public XmlUrlRewriteFilterReader( Reader reader, UrlRewriter rewriter, Resolver resolver, Direction direction, UrlRewriteFilterContentDescriptor config ) - throws IOException, ParserConfigurationException, XMLStreamException { - super( reader, config ); - this.resolver = resolver; - this.rewriter = rewriter; - this.direction = direction; - } - - //TODO: Need to limit which values are attempted to be filtered by the name. - private String filterValueString( String name, String value, String rule ) { - try { - Template input = Parser.parseLiteral( value ); - if( input != null ) { - Template output = rewriter.rewrite( resolver, input, direction, rule ); - if( output != null ) { - value = output.getPattern(); - } else { - LOG.failedToFilterValue( value, rule ); - } - } else { - LOG.failedToParseValueForUrlRewrite( value ); - } - } catch( URISyntaxException e ) { - LOG.failedToParseValueForUrlRewrite( value ); - } - return value; - } - - @Override - protected String filterAttribute( QName elementName, QName attributeName, String attributeValue, String ruleName ) { - return filterValueString( attributeName.getLocalPart(), attributeValue, ruleName ); - } - - @Override - protected String filterText( QName elementName, String text, String ruleName ) { - return filterValueString( elementName.getLocalPart(), text, ruleName ); - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporter.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporter.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporter.java deleted file mode 100644 index 1cc10a3..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesExporter.java +++ /dev/null @@ -1,188 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -import org.apache.commons.beanutils.BeanUtils; -import org.apache.hadoop.gateway.filter.rewrite.api.*; -import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; -import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteRulesExporter; -import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; -import org.apache.hadoop.gateway.util.XmlUtils; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.io.IOException; -import java.io.Writer; -import java.lang.reflect.InvocationTargetException; - -public class XmlUrlRewriteRulesExporter implements UrlRewriteRulesExporter, XmlRewriteRulesTags { - - private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class ); - - @Override - public String getFormat() { - return "xml"; - } - - @Override - public void store( UrlRewriteRulesDescriptor descriptor, Writer writer ) throws IOException { - try { - Document document = XmlUtils.createDocument(); - - Element root = document.createElement( ROOT ); - document.appendChild( root ); - - if( !descriptor.getFunctions().isEmpty() ) { - Element functionsElement = document.createElement( FUNCTIONS ); - root.appendChild( functionsElement ); - for( UrlRewriteFunctionDescriptor function : descriptor.getFunctions() ) { - Element functionElement = createElement( document, function.name(), function ); - functionsElement.appendChild( functionElement ); - } - } - - if( !descriptor.getRules().isEmpty() ) { - for( UrlRewriteRuleDescriptor rule : descriptor.getRules() ) { - Element ruleElement = createRule( document, rule ); - root.appendChild( ruleElement ); - } - } - - if( !descriptor.getFilters().isEmpty() ) { - for( UrlRewriteFilterDescriptor filter : descriptor.getFilters() ) { - Element filterElement = createFilter( document, filter ); - root.appendChild( filterElement ); - } - } - - XmlUtils.writeXml( document, writer ); - - } catch( ParserConfigurationException e ) { - throw new IOException( e ); - } catch( TransformerException e ) { - throw new IOException( e ); - } catch( InvocationTargetException e ) { - LOG.failedToWriteRulesDescriptor( e ); - } catch( NoSuchMethodException e ) { - LOG.failedToWriteRulesDescriptor( e ); - } catch( IntrospectionException e ) { - LOG.failedToWriteRulesDescriptor( e ); - } catch( IllegalAccessException e ) { - LOG.failedToWriteRulesDescriptor( e ); - } - } - - private Element createFilter( Document document, UrlRewriteFilterDescriptor parent ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element parentElement = createElement( document, FILTER, parent ); - for( UrlRewriteFilterContentDescriptor child: parent.getContents() ) { - Element childElement = createFilterContent( document, child ); - parentElement.appendChild( childElement ); - } - return parentElement; - } - - private Element createFilterContent( Document document, UrlRewriteFilterContentDescriptor parent ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element parentElement = createElement( document, CONTENT, parent ); - for( UrlRewriteFilterPathDescriptor child: parent.getSelectors() ) { - Element childElement = createFilterSelector( document, child ); - parentElement.appendChild( childElement ); - } - return parentElement; - } - - private Element createFilterSelector( Document document, UrlRewriteFilterPathDescriptor parent ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element parentElement = createElement( document, toTagName( parent ), parent ); - if( parent instanceof UrlRewriteFilterGroupDescriptor ) { - for( UrlRewriteFilterPathDescriptor child: ((UrlRewriteFilterGroupDescriptor)parent).getSelectors() ) { - Element childElement = createFilterSelector( document, child ); - parentElement.appendChild( childElement ); - } - } - return parentElement; - } - - private Element createRule( Document document, UrlRewriteRuleDescriptor rule ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element ruleElement = createElement( document, RULE, rule ); - for( UrlRewriteStepDescriptor step: rule.steps() ) { - Element childElement = createStep( document, step ); - ruleElement.appendChild( childElement ); - } - return ruleElement; - } - - private Element createStep( Document document, UrlRewriteStepDescriptor step ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element parentElement = createElement( document, step.type(), step ); - if( step instanceof UrlRewriteFlowDescriptor ) { - UrlRewriteFlowDescriptor flow = (UrlRewriteFlowDescriptor)step; - for( Object child: flow.steps() ) { - UrlRewriteStepDescriptor childStep = (UrlRewriteStepDescriptor)child; - Element childElement = createStep( document, childStep ); - parentElement.appendChild( childElement ); - } - - } - return parentElement; - } - - private Element createElement( Document document, String name, Object bean ) - throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Element element = document.createElement( name ); - BeanInfo beanInfo = Introspector.getBeanInfo( bean.getClass(), Object.class ); - for( PropertyDescriptor propInfo: beanInfo.getPropertyDescriptors() ) { - String propName = propInfo.getName(); - if( propInfo.getReadMethod() != null && String.class.isAssignableFrom( propInfo.getPropertyType() ) ) { - String propValue = BeanUtils.getProperty( bean, propName ); - if( propValue != null && !propValue.isEmpty() ) { - // Doing it the hard way to avoid having the &'s in the query string escaped at & - Attr attr = document.createAttribute( propName ); - attr.setValue( propValue ); - element.setAttributeNode( attr ); - //element.setAttribute( propName, propValue ); - } - } - } - return element; - } - - private static String toTagName( final UrlRewriteFilterPathDescriptor descriptor ) { - if( descriptor instanceof UrlRewriteFilterApplyDescriptor ) { - return APPLY; - } else if( descriptor instanceof UrlRewriteFilterDetectDescriptor ) { - return DETECT; - } else if( descriptor instanceof UrlRewriteFilterBufferDescriptor ) { - return BUFFER; - } else if( descriptor instanceof UrlRewriteFilterScopeDescriptor ) { - return SCOPE; - } else { - throw new IllegalArgumentException(); - } - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java deleted file mode 100644 index ec3c2c2..0000000 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlUrlRewriteRulesImporter.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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.hadoop.gateway.filter.rewrite.impl.xml; - -import org.apache.commons.digester3.Digester; -import org.apache.commons.digester3.ExtendedBaseRules; -import org.apache.commons.digester3.binder.DigesterLoader; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor; -import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteRulesImporter; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.io.Reader; - -import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; - -public class XmlUrlRewriteRulesImporter implements UrlRewriteRulesImporter { - - private static DigesterLoader loader = newLoader( new XmlRewriteRulesDigester() ); - - @Override - public String getFormat() { - return "xml"; - } - - @Override - public UrlRewriteRulesDescriptor load( Reader reader ) throws IOException { - Digester digester = loader.newDigester( new ExtendedBaseRules() ); - digester.setValidating( false ); - try { - UrlRewriteRulesDescriptor rules = digester.parse( reader ); - return rules; - } catch( SAXException e ) { - throw new IOException( e ); - } - } -}