com.ocpsoft.rewrite.servlet.config.QueryString Maven / Gradle / Ivy
/*
* Copyright 2011 Lincoln Baxter, III
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ocpsoft.rewrite.servlet.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import com.ocpsoft.common.util.Assert;
import com.ocpsoft.rewrite.bind.Bindable;
import com.ocpsoft.rewrite.bind.Binding;
import com.ocpsoft.rewrite.bind.Bindings;
import com.ocpsoft.rewrite.bind.DefaultBindable;
import com.ocpsoft.rewrite.bind.Evaluation;
import com.ocpsoft.rewrite.bind.util.Maps;
import com.ocpsoft.rewrite.config.Condition;
import com.ocpsoft.rewrite.context.EvaluationContext;
import com.ocpsoft.rewrite.event.InboundRewrite;
import com.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
import com.ocpsoft.rewrite.servlet.util.QueryStringBuilder;
/**
* A {@link Condition} that inspects the value of {@link HttpServletRewrite#getRequestQueryString()}
*
* @author Lincoln Baxter, III
*/
public abstract class QueryString extends HttpCondition implements Bindable
{
@SuppressWarnings({ "rawtypes" })
protected final DefaultBindable> bindable = new DefaultBindable();
/**
* Bind the values of this {@link QueryString} query to the given {@link Binding}.
*/
@Override
public QueryString bindsTo(final Binding binding)
{
this.bindable.bindsTo(binding);
return this;
}
@Override
public List getBindings()
{
return bindable.getBindings();
}
/**
* Return a new {@link Condition} matching against the entire {@link HttpServletRequest#getQueryString()}
*
* This value may be bound.
*
* See also: {@link #bindsTo(Binding)}
*/
public static QueryString matches(final String pattern)
{
Assert.notNull(pattern, "URL pattern must not be null.");
return new QueryString() {
@Override
public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationContext context)
{
String queryString = null;
if (event instanceof InboundRewrite)
queryString = event.getRequestQueryString();
else if (event instanceof HttpOutboundServletRewrite)
queryString = QueryStringBuilder.build(event.getURL()).toQueryString();
if (Pattern.compile(pattern).matcher(queryString == null ? "" : queryString).matches())
{
List values = new ArrayList();
values.add(queryString);
Bindings.enqueueSubmission(event, context, bindable, values.toArray(new String[] {}));
return true;
}
return false;
}
};
}
/**
* Return a new {@link Condition} matching against the existence of specific parameters within
* {@link HttpServletRequest#getQueryString()}
*
* The values of all matching parameters may be bound. By default, matching values are bound to the
* {@link EvaluationContext}.
*
* See also: {@link #bindsTo(Binding)}
*/
public static QueryString parameterExists(final String nameRegex)
{
Assert.notNull(nameRegex, "Parameter name pattern must not be null.");
return new QueryString() {
@Override
@SuppressWarnings({ "rawtypes" })
public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationContext context)
{
Pattern pattern = Pattern.compile(nameRegex);
QueryStringBuilder queryString = null;
queryString = QueryStringBuilder.build(event.getURL());
List values = new ArrayList();
Map map = new LinkedHashMap();
for (String name : queryString.getParameterNames()) {
if (pattern.matcher(name).matches())
{
String[] temp = queryString.getParameterValues(name);
DefaultBindable tempBindable = new DefaultBindable();
tempBindable.bindsTo(Evaluation.property(name));
map.put(tempBindable, temp);
values.addAll(Arrays.asList(temp));
}
}
map.put(bindable, values.toArray(new String[] {}));
if (!values.isEmpty())
return Bindings.enqueuePreOperationSubmissions(event, context, map);
return false;
}
};
}
/**
* Return a new {@link Condition} matching against the existence of a parameter values within
* {@link HttpServletRequest#getQueryString()}
*
* The values of all matching parameter values may be bound. By default, matching values are bound to the
* {@link EvaluationContext}.
*
* See also: {@link #bindsTo(Binding)}
*/
public static QueryString valueExists(final String valueRegex)
{
Assert.notNull(valueRegex, "Parameter value pattern must not be null.");
return new QueryString() {
@Override
@SuppressWarnings({ "rawtypes" })
public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationContext context)
{
Pattern pattern = Pattern.compile(valueRegex);
QueryStringBuilder queryString = QueryStringBuilder.build(event.getRequestQueryString());
List values = new ArrayList();
Map map = new LinkedHashMap();
for (String name : queryString.getParameterNames()) {
List paramValues = Arrays.asList(queryString.getParameterValues(name));
DefaultBindable tempBindable = new DefaultBindable();
tempBindable.bindsTo(Evaluation.property(name));
for (String value : paramValues) {
if (pattern.matcher(value).matches())
{
Maps.addArrayValue(map, tempBindable, value);
values.add(value);
}
}
}
map.put(bindable, values.toArray(new String[] {}));
if (!values.isEmpty())
return Bindings.enqueuePreOperationSubmissions(event, context, map);
return false;
}
};
}
}