Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Fix(ZNTA-1293): remove cid param (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Aug 30, 2016
1 parent 6f84fa9 commit 3a78fe5
Showing 1 changed file with 45 additions and 1 deletion.
@@ -1,17 +1,28 @@
package org.zanata.servlet;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.Direction;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.ocpsoft.rewrite.servlet.config.Forward;
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
import org.ocpsoft.rewrite.servlet.config.HttpOperation;
import org.ocpsoft.rewrite.servlet.config.Path;
import org.ocpsoft.rewrite.servlet.config.Query;
import org.ocpsoft.rewrite.servlet.config.Redirect;
import org.ocpsoft.rewrite.servlet.config.bind.RequestBinding;
import org.ocpsoft.rewrite.servlet.config.rule.Join;
import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
import org.ocpsoft.urlbuilder.Address;
import org.ocpsoft.urlbuilder.AddressBuilder;

import javax.servlet.ServletContext;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

/*
* This class replaces urlrewrite.xml, with simpler bidirectional mappings for external/internal URLs.
Expand All @@ -25,6 +36,11 @@ public Configuration getConfiguration(final ServletContext context) {
// NB: inbound rules are processed in order, outbound rules in reverse order (as of Rewrite 3.0.0.Alpha1)
return ConfigurationBuilder.begin()

// strip cid params to avoid NonexistentConversationException
.addRule()
.when(Query.parameterExists("cid"))
.perform(new RedirectWithoutParam("cid"))

.addRule()
.when(Direction.isInbound().and(Path.matches("/seam/resource/restv1/{path}")))
.perform(Forward.to("/rest/{path}"))
Expand Down Expand Up @@ -130,4 +146,32 @@ public Configuration getConfiguration(final ServletContext context) {
public int priority() {
return 0;
}

static class RedirectWithoutParam extends HttpOperation {
private final String paramName;

RedirectWithoutParam(String paramName) {
this.paramName = paramName;
}

@Override
public void performHttp(HttpServletRewrite event,
EvaluationContext context) {
// Remove param from address query
Address address = event.getAddress();
String query = address.getQuery();
List<NameValuePair> nameValuePairs =
URLEncodedUtils.parse(query, UTF_8);
nameValuePairs.removeIf(nvp -> nvp.getName().equals(paramName));

String newAddress;
if (nameValuePairs.isEmpty()) {
newAddress = address.getPath();
} else {
newAddress = address.getPath() + "?" + URLEncodedUtils.format(nameValuePairs, UTF_8);
}
((HttpInboundServletRewrite) event).redirectTemporary(AddressBuilder.create(newAddress).toString());
}
}

}

0 comments on commit 3a78fe5

Please sign in to comment.