Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers.builder;
import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.attribute.ExchangeAttribute;
import io.undertow.attribute.ExchangeAttributeParser;
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.PredicateBuilder;
import io.undertow.predicate.Predicates;
import io.undertow.predicate.PredicatesHandler;
import io.undertow.server.HandlerWrapper;
import io.undertow.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
/**
* Parser for the undertow-handlers.conf file.
*
* This file has a line by line syntax, specifying predicate -> handler. If no predicate is specified then
* the line is assumed to just contain a handler.
*
* @author Stuart Douglas
*/
public class PredicatedHandlersParser {
public static final String ELSE = "else";
public static final String ARROW = "->";
public static final String NOT = "not";
public static final String OR = "or";
public static final String AND = "and";
public static final String TRUE = "true";
public static final String FALSE = "false";
public static List parse(final File file, final ClassLoader classLoader) {
return parse(file.toPath(), classLoader);
}
public static List parse(final Path file, final ClassLoader classLoader) {
try {
return parse(new String(Files.readAllBytes(file), StandardCharsets.UTF_8), classLoader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List parse(final InputStream inputStream, final ClassLoader classLoader) {
return parse(FileUtils.readFile(inputStream), classLoader);
}
public static List parse(final String contents, final ClassLoader classLoader) {
Deque tokens = tokenize(contents);
Node node = parse(contents, tokens);
Map predicateBuilders = loadPredicateBuilders(classLoader);
Map handlerBuilders = loadHandlerBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handleNode(contents, node, predicateBuilders, handlerBuilders, attributeParser);
}
public static Predicate parsePredicate(String string, ClassLoader classLoader) {
Deque tokens = tokenize(string);
Node node = parse(string, tokens);
Map predicateBuilders = loadPredicateBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handlePredicateNode(string, node, predicateBuilders, attributeParser);
}
public static HandlerWrapper parseHandler(String string, ClassLoader classLoader) {
Deque tokens = tokenize(string);
Node node = parse(string, tokens);
Map handlerBuilders = loadHandlerBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handleHandlerNode(string, (ExpressionNode)node, handlerBuilders, attributeParser);
}
private static List handleNode(String contents, Node node, Map predicateBuilders, Map handlerBuilders, ExchangeAttributeParser attributeParser) {
if(node instanceof BlockNode) {
return handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, attributeParser);
} else if(node instanceof ExpressionNode) {
HandlerWrapper handler = handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, attributeParser);
return Collections.singletonList(new PredicatedHandler(Predicates.truePredicate(), handler));
} else if(node instanceof PredicateOperatorNode) {
return Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, attributeParser));
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
private static PredicatedHandler handlePredicateOperatorNode(String contents, PredicateOperatorNode node, Map predicateBuilders, Map handlerBuilders, ExchangeAttributeParser parser) {
Predicate predicate = handlePredicateNode(contents, node.getLeft(), predicateBuilders, parser);
HandlerWrapper ret = handlePredicatedAction(contents, node.getRight(), predicateBuilders, handlerBuilders, parser);
HandlerWrapper elseBranch = null;
if(node.getElseBranch() != null) {
elseBranch = handlePredicatedAction(contents, node.getElseBranch(), predicateBuilders, handlerBuilders, parser);
}
return new PredicatedHandler(predicate, ret, elseBranch);
}
private static HandlerWrapper handlePredicatedAction(String contents, Node node, Map predicateBuilders, Map handlerBuilders, ExchangeAttributeParser parser) {
if(node instanceof ExpressionNode) {
return handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, parser);
} else if(node instanceof BlockNode) {
List handlers = handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, parser);
return new PredicatesHandler.Wrapper(handlers, false);
} else if(node instanceof PredicateOperatorNode) {
List handlers = Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, parser));
return new PredicatesHandler.Wrapper(handlers, false);
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
private static List handleBlockNode(String contents, BlockNode node, Map predicateBuilders, Map handlerBuilders, ExchangeAttributeParser parser) {
List ret = new ArrayList<>();
for(Node line : node.getBlock()) {
ret.addAll(handleNode(contents, line, predicateBuilders, handlerBuilders, parser));
}
return ret;
}
private static HandlerWrapper handleHandlerNode(String contents, ExpressionNode node, Map handlerBuilders, ExchangeAttributeParser parser) {
Token token = node.getToken();
HandlerBuilder builder = handlerBuilders.get(token.getToken());
if (builder == null) {
throw error(contents, token.getPosition(), "no handler named " + token.getToken() + " known handlers are " + handlerBuilders.keySet());
}
Map parameters = new HashMap<>();
Set requiredParameters = builder.requiredParameters();
requiredParameters = requiredParameters == null? Collections.emptySet() : new HashSet<>(requiredParameters);
for(Map.Entry val : node.getValues().entrySet()) {
String name = val.getKey();
if(name == null) {
if(builder.defaultParameter() == null) {
throw error(contents, token.getPosition(), "default parameter not supported");
}
name = builder.defaultParameter();
}
Class> type = builder.parameters().get(name);
if(type == null) {
throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
}
if(val.getValue() instanceof ValueNode) {
parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
} else if(val.getValue() instanceof ArrayNode) {
parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
} else {
throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
}
requiredParameters.remove(name);
}
if (!requiredParameters.isEmpty()) {
throw error(contents, token.getPosition(), "required parameters " + requiredParameters + " not provided for handler " + builder.name());
}
return builder.build(parameters);
}
private static Predicate handlePredicateNode(String contents, Node node, Map handlerBuilders, ExchangeAttributeParser parser) {
if(node instanceof AndNode) {
AndNode andNode = (AndNode)node;
return Predicates.and(handlePredicateNode(contents, andNode.getLeft(), handlerBuilders, parser), handlePredicateNode(contents, andNode.getRight(), handlerBuilders, parser));
} else if(node instanceof OrNode) {
OrNode orNode = (OrNode)node;
return Predicates.or(handlePredicateNode(contents, orNode.getLeft(), handlerBuilders, parser), handlePredicateNode(contents, orNode.getRight(), handlerBuilders, parser));
} else if(node instanceof NotNode) {
NotNode orNode = (NotNode)node;
return Predicates.not(handlePredicateNode(contents, orNode.getNode(), handlerBuilders, parser));
} else if(node instanceof ExpressionNode) {
return handlePredicateExpressionNode(contents, (ExpressionNode) node, handlerBuilders, parser);
}else if(node instanceof OperatorNode) {
switch (node.getToken().getToken()) {
case TRUE: {
return Predicates.truePredicate();
}
case FALSE: {
return Predicates.falsePredicate();
}
}
}
throw error(contents, node.getToken().getPosition(), "unexpected node " + node);
}
private static Predicate handlePredicateExpressionNode(String contents, ExpressionNode node, Map handlerBuilders, ExchangeAttributeParser parser) {
Token token = node.getToken();
PredicateBuilder builder = handlerBuilders.get(token.getToken());
if (builder == null) {
throw error(contents, token.getPosition(), "no predicate named " + token.getToken() + " known predicates are " + handlerBuilders.keySet());
}
Map parameters = new HashMap<>();
Set requiredParameters = builder.requiredParameters();
requiredParameters = requiredParameters == null? Collections.emptySet() : new HashSet<>(requiredParameters);
for(Map.Entry val : node.getValues().entrySet()) {
String name = val.getKey();
if(name == null) {
if(builder.defaultParameter() == null) {
throw error(contents, token.getPosition(), "default parameter not supported");
}
name = builder.defaultParameter();
}
Class> type = builder.parameters().get(name);
if(type == null) {
throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
}
if(val.getValue() instanceof ValueNode) {
parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
} else if(val.getValue() instanceof ArrayNode) {
parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
} else {
throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
}
requiredParameters.remove(name);
}
if (!requiredParameters.isEmpty()) {
throw error(contents, token.getPosition(), "required parameters " + requiredParameters + " not provided for predicate " + builder.name());
}
return builder.build(parameters);
}
private static Object readArrayType(final String string, String paramName, ArrayNode value, ExchangeAttributeParser parser, Class type) {
if (!type.isArray()) {
throw error(string, value.getToken().getPosition(), "parameter is not an array type " + paramName);
}
Class> componentType = type.getComponentType();
final List