Skip to content

Commit

Permalink
Started work on a rule that will use regular expressions to rewite th…
Browse files Browse the repository at this point in the history
…e URL
  • Loading branch information
anders_n committed Aug 1, 2005
1 parent 94a9ef4 commit 532602f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .settings/.cvsignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.eclipse.jdt.core.prefs
org.eclipse.jdt.ui.prefs
2 changes: 1 addition & 1 deletion src/net/sf/j2ep/RewriteFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private String getURI(HttpServletRequest httpRequest) {
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
log = LogFactory.getLog("net.sf.j2ep");
log = LogFactory.getLog(RewriteFilter.class);

String data = filterConfig.getInitParameter("dataUrl");
if (data == null) {
Expand Down
7 changes: 0 additions & 7 deletions src/net/sf/j2ep/rules/BaseRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package net.sf.j2ep.rules;

import javax.servlet.http.HttpServletRequest;

import net.sf.j2ep.Rule;
import net.sf.j2ep.Server;

Expand All @@ -42,11 +40,6 @@ public abstract class BaseRule implements Rule {
* The servers id, used for mapping the correct server.
*/
private String serverId;

/**
* @see Rule#matches(HttpServletRequest)
*/
public abstract boolean matches(HttpServletRequest request);

/**
* @see net.sf.j2ep.Rule#process(java.lang.String)
Expand Down
102 changes: 102 additions & 0 deletions src/net/sf/j2ep/rules/RewriteRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2005 Anders Nyman.
*
* 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 net.sf.j2ep.rules;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RewriteRule extends BaseRule {

private Pattern matchPattern;
private String rewriteTo;
private Pattern revertPattern;
private boolean isRewriting;
private boolean isReverting;

/**
* Logging element supplied by commons-logging.
*/
private static Log log;

public RewriteRule() {
isRewriting = false;
log = LogFactory.getLog(RewriteRule.class);
}

public boolean matches(HttpServletRequest request) {
String uri = getURI(request);
Matcher matcher = matchPattern.matcher(uri);
return matcher.matches();
}

public String process(String uri) {
Matcher matcher = matchPattern.matcher(uri);
String replaced = matcher.replaceAll(rewriteTo);
log.debug("Rewriting URI \n" + uri + " >> " + replaced);

return replaced;
}


public void setFrom(String regex) {
if (regex == null) {
throw new IllegalArgumentException("From pattern cannot be null.");
} else {
matchPattern = Pattern.compile(regex);
}
}

public void setTo(String to) {
if (to == null) {
throw new IllegalArgumentException("To string cannot be null.");
} else {
rewriteTo = to;
isRewriting = true;
}
}

public void setRevert(String regex) {
if (regex == null) {
throw new IllegalArgumentException("Revert pattern cannot be null.");
} else {
revertPattern = Pattern.compile(regex);
isReverting = true;
}
}

/**
* Will build a URI but including the Query String. That means that it really
* isn't a URI, but quite near.
*
* @param httpRequest Request to get the URI and query string from
* @return The URI for this request including the query string
*/
private String getURI(HttpServletRequest httpRequest) {
String contextPath = httpRequest.getContextPath();
String uri = httpRequest.getRequestURI().substring(contextPath.length());
if (httpRequest.getQueryString() != null) {
uri += "?" + httpRequest.getQueryString();
}
return uri;
}

}

0 comments on commit 532602f

Please sign in to comment.