fix #575 : Add user-agent matching rule to path mapping

This commit is contained in:
yfujita 2016-07-22 18:19:33 +09:00
parent 2ef04118b3
commit 21462149e2
5 changed files with 75 additions and 6 deletions

View file

@ -59,6 +59,9 @@ public class CreateForm {
@ValidateTypeFailure
public Long createdTime;
@Required
public String userAgent;
public void initialize() {
crudMode = CrudMode.CREATE;
sortOrder = 0;

View file

@ -18,6 +18,7 @@ package org.codelibs.fess.es.config.exentity;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.es.config.bsentity.BsPathMapping;
/**
@ -29,6 +30,8 @@ public class PathMapping extends BsPathMapping {
private Pattern regexPattern;
private Pattern userAgentPattern;
public String getId() {
return asDocMeta().id();
}
@ -52,10 +55,25 @@ public class PathMapping extends BsPathMapping {
return regexPattern.matcher(input);
}
public boolean hasUAMathcer() {
return StringUtil.isNotBlank(getUserAgent());
}
public Matcher getUAMatcher(final CharSequence input) {
if (!hasUAMathcer()) {
return null;
}
if (userAgentPattern == null) {
userAgentPattern = Pattern.compile(getUserAgent());
}
return userAgentPattern.matcher(input);
}
@Override
public String toString() {
return "PathMapping [regexPattern=" + regexPattern + ", createdBy=" + createdBy + ", createdTime=" + createdTime + ", processType="
+ processType + ", regex=" + regex + ", replacement=" + replacement + ", sortOrder=" + sortOrder + ", updatedBy="
+ updatedBy + ", updatedTime=" + updatedTime + ", docMeta=" + docMeta + "]";
+ processType + ", regex=" + regex + ", replacement=" + replacement + ", sortOrder=" + sortOrder + ", userAgent="
+ userAgent + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", docMeta=" + docMeta + "]";
}
}

View file

@ -22,11 +22,14 @@ import java.util.Map;
import java.util.regex.Matcher;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.es.config.exbhv.PathMappingBhv;
import org.codelibs.fess.es.config.exentity.PathMapping;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.util.LaRequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -95,7 +98,10 @@ public class PathMappingHelper {
}
String result = text;
for (final PathMapping pathMapping : cachedPathMappingList) {
result = result.replaceAll("(\"[^\"]*)" + pathMapping.getRegex() + "([^\"]*\")", "$1" + pathMapping.getReplacement() + "$2");
if (matchUserAgent(pathMapping)) {
result =
result.replaceAll("(\"[^\"]*)" + pathMapping.getRegex() + "([^\"]*\")", "$1" + pathMapping.getReplacement() + "$2");
}
}
return result;
}
@ -114,11 +120,42 @@ public class PathMappingHelper {
private String replaceUrl(final List<PathMapping> pathMappingList, final String url) {
String newUrl = url;
for (final PathMapping pathMapping : pathMappingList) {
final Matcher matcher = pathMapping.getMatcher(newUrl);
if (matcher.find()) {
newUrl = matcher.replaceAll(pathMapping.getReplacement());
if (matchUserAgent(pathMapping)) {
final Matcher matcher = pathMapping.getMatcher(newUrl);
if (matcher.find()) {
newUrl = matcher.replaceAll(pathMapping.getReplacement());
}
}
}
return newUrl;
}
private boolean matchUserAgent(final PathMapping pathMapping) {
if (!pathMapping.hasUAMathcer()) {
return true;
}
//TODO use OptionalThing
final HttpServletRequest request;
try {
request = LaRequestUtil.getRequest();
} catch (IllegalStateException e) {
//could not get request.
return false;
}
/*
final OptionalThing<HttpServletRequest> op = LaRequestUtil.getOptionalRequest();
if (!op.isPresent()) {
return false;
}
final HttpServletRequest request = op.get();
*/
final String userAgent = request.getHeader("user-agent");
if (StringUtil.isBlank(userAgent)) {
return false;
}
return pathMapping.getUAMatcher(userAgent).find();
}
}

View file

@ -72,6 +72,10 @@
<th><la:message key="labels.sortOrder" /></th>
<td>${f:h(sortOrder)}<la:hidden property="sortOrder" /></td>
</tr>
<tr>
<th class="col-xs-2"><la:message key="labels.userAgent" /></th>
<td>${f:h(userAgent)}<la:hidden property="userAgent" /></td>
</tr>
</tbody>
</table>
</div>

View file

@ -88,6 +88,13 @@
min="0" max="100000">
</div>
</div>
<div class="form-group">
<label for="userAgent" class="col-sm-3 control-label"><la:message key="labels.userAgent" /></label>
<div class="col-sm-9">
<la:errors property="userAgent" />
<la:text property="userAgent" styleClass="form-control" />
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">