com.ocpsoft.rewrite.servlet.config.Substitute 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 com.ocpsoft.common.util.Assert;
import com.ocpsoft.rewrite.bind.Binding;
import com.ocpsoft.rewrite.bind.Evaluation;
import com.ocpsoft.rewrite.bind.ParameterizedPattern;
import com.ocpsoft.rewrite.bind.RegexOperationParameterBuilder;
import com.ocpsoft.rewrite.config.Operation;
import com.ocpsoft.rewrite.context.EvaluationContext;
import com.ocpsoft.rewrite.event.InboundRewrite;
import com.ocpsoft.rewrite.event.OutboundRewrite;
import com.ocpsoft.rewrite.param.OperationParameterBuilder;
import com.ocpsoft.rewrite.param.Parameter;
import com.ocpsoft.rewrite.param.ParameterizedOperation;
import com.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
import com.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
/**
* Responsible for substituting inbound/outbound URLs with a replacement. For {@link InboundRewrite} events, this
* {@link Operation} calls {@link HttpInboundServletRewrite#forward(String)}, and for {@link OutboundRewrite} events,
* this method calls {@link HttpOutboundServletRewrite#setOutboundURL(String)}
*
* @author Lincoln Baxter, III
*/
public class Substitute extends HttpOperation implements
ParameterizedOperation, String>
{
private final ParameterizedPattern location;
private Substitute(final String location)
{
Assert.notNull(location, "Location must not be null.");
this.location = new ParameterizedPattern("[^/]+", location);
for (Parameter parameter : this.location.getParameters().values()) {
parameter.bindsTo(Evaluation.property(parameter.getName()));
}
}
/**
* Substitute the current URL with the given location.
*
* The given location may be parameterized using the following format:
*
*
* /example/{param}
* /example/{value}/sub/{value2}
* ... and so on
*
*
* Parameters may be bound. By default, matching parameter values are extracted from bindings in the
* {@link EvaluationContext}.
*
* See also {@link #where(String)}
*/
public static Substitute with(final String location)
{
return new Substitute(location);
}
@Override
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
{
if (event instanceof HttpInboundServletRewrite)
{
String target = location.build(event, context);
((HttpInboundServletRewrite) event).forward(target);
}
else if (event instanceof HttpOutboundServletRewrite)
{
String target = location.build(event, context);
if (((HttpOutboundServletRewrite) event).getOutboundURL().startsWith(event.getContextPath())
&& target.startsWith("/")
&& !target.startsWith(event.getContextPath()))
{
target = event.getContextPath() + target;
}
((HttpOutboundServletRewrite) event).setOutboundURL(target);
}
}
@Override
public RegexOperationParameterBuilder where(final String param)
{
return new RegexOperationParameterBuilder(this, location.getParameter(param));
}
@Override
public RegexOperationParameterBuilder where(final String param, final String pattern)
{
return where(param).matches(pattern);
}
@Override
public RegexOperationParameterBuilder where(final String param, final String pattern,
final Binding binding)
{
return where(param, pattern).bindsTo(binding);
}
@Override
public RegexOperationParameterBuilder where(final String param, final Binding binding)
{
return where(param).bindsTo(binding);
}
}