Regex Pattern Mapping
Dear All,
I had a class to parse HttpResponses. It simply uses validPattern to check whether the response is in valid format. If not it considered to be an error. Additionally, if the matchGroup of the validResponse is having the acceptedPattern response considered to be ACCEPTED otherwise REJECTED.
Ex:
validPattern = ".*(OK|FAILED).*";
matchGroup = 1;
acceptedPattern = "OK"
Relavent class implementation similar as follows:
publicclass RegexResponseParserimplements ResponseParser{
privatestatic Logger cat = Logger.getLogger(RegexResponseParser.class);
Pattern validPattern;
int matchGroup = 0;
Pattern acceptedPattern;
/**
* Create a response parser.
*
* @param validPattern a String containing a regular expression that defines a valid response
* text. If the response text does not match this pattern then the response is considered an
* error. The validPattern should contain embeded match groups (see responseGroup param).
* @param matchGroup The match group within the validPattern that is used to extract
* the portion of the response to be tested with the acceptedPattern.
* @param acceptedPattern a String containing a regular expression that defines an accepted
* response. The match group indicated by matchGroup is tested against the accepted pattern.
* If there is a match then the response is considered accepted, otherwise rejected.
**/
public RegexResponseParser(String validPattern,int matchGroup, String acceptedPattern){
this.validPattern = Pattern.compile(validPattern, Pattern.DOTALL);
this.matchGroup = matchGroup;
this.acceptedPattern = Pattern.compile(acceptedPattern, Pattern.DOTALL);
}
public TransferResponse parse(String response){
Matcher validMatcher = validPattern.matcher(response);
if (validMatcher.matches() ==false){
returnnew ErrorTransferResponse(response);
}
String matchText = validMatcher.group(matchGroup);
Matcher acceptedMatcher = acceptedPattern.matcher(matchText);
if (acceptedMatcher.matches() ==false){
returnnew RejectedTransferResponse(response);
}
else{
returnnew AcceptedTransferResponse(response);
}
}
}
In the new version of the application release this class was replaced with following class. Here the class is generalized to have a (pattern, code) mapping such as:
- (".*OK.*", ACCEPTED)
- (".*FAILED.*", REJECTED)
Here is the new Parser implementation:
publicclass RegExResponseParserextends ResponseParser{
private Map patternToCodeMap =new HashMap();
publicvoid setPatternToCodeMap(Map patternToCodeMap){
this.patternToCodeMap = patternToCodeMap;
}
public LeadStatusCode parse(Map parseEnv)throws ParseException{
String body = getResponseBody(parseEnv);
Set matchedCodes =new HashSet();
for (Iterator it = patternToCodeMap.keySet().iterator(); it.hasNext();){
String key = (String) it.next();
if (Pattern.compile(key, Pattern.DOTALL).matcher(body).matches())
matchedCodes.add(patternToCodeMap.get(key));
}
if (matchedCodes.size() > 0){
StringBuffer msg =new StringBuffer("Multiple status codes were parsed from the response body: ");
for (Iterator it = matchedCodes.iterator(); it.hasNext();){
msg.append(((LeadStatusCode) it.next()).getName());
if (it.hasNext()) msg.append(", ");
}
thrownew ParseException(msg.toString());
}elseif (rejectingNoMatch && matchedCodes.size() == 0){
return LeadStatusCode.REJECTED;
}elseif (!rejectingNoMatch && matchedCodes.size() == 0){
returnnull;
}else{
return (LeadStatusCode) matchedCodes.iterator().next();
}
}
We have many configurations passed to created old ResponseParser beans though Spring framwork as follows:
<bean id="responseParser" class="x.y.z.RegexResponseParser">
<property name="validPatternString">
<value>.*(OK|FAILED).*</value>
</property>
<property name="matchGroup">
<value>1</value>
</property>
<property name="acceptedPatternString">
<value>OK</value>
</property>
</bean>
<bean id="responseParser2" class="x.y.z.RegexResponseParser">
<property name="validPatternString">
<value>.*(SUCCESS|FAILED|QUEUED|REJECTED).*</value>
</property>
<property name="matchGroup">
<value>1</value>
</property>
<property name="acceptedPatternString">
<value>SUCCESS|QUEUED</value>
</property>
</bean>
Now we need to have a method in the new ResponseParser implementation to convert old ResponseParser class to new ones. I would really appriciate if you can suggest a senario for the following method implementation:
publicclass RegExResponseParserextends ResponseParser{
public ResponseParser convert(x.y.z.ResponseParser oldResponseParser){
// Mapping old parser patterns to new parser pattern
return newResponseParser;
}
}

