Skip to content

Commit

Permalink
Fix [HHQ-6019]: Add validation for save resources name to ignore from…
Browse files Browse the repository at this point in the history
… illegal characters
  • Loading branch information
tgoldman committed Jun 17, 2015
1 parent da993fb commit 8e97b94
Showing 1 changed file with 28 additions and 1 deletion.
Expand Up @@ -64,7 +64,8 @@ public Resource(ResourceType type, Resource prototype, String name,

@Override
public void setName(String name) {
super.setName(name);
// Clear any illegal character in resource name before saving it
super.setName(clearNonValidCharacters(name));
}
protected Collection<ResourceGroup> getGroupBag() {
return _groupBag;
Expand Down Expand Up @@ -234,4 +235,30 @@ public String toString() {
.toString();
}

/**
* This method was written for Rest API <b>getResources</b> call that returns resources data.
* Any illegal character fails the API call.
* The method validates that the resource name will not contain any illegal character before saving it in DB.
* Characters with ASCII value greater than 127 or lower than 33 will be removed from resource name.
*
* @param name the resource name
* @return name without any illegal character
*/
private String clearNonValidCharacters(String name){
if (name != null && !name.matches("\\A\\p{ASCII}*\\z")){
if (_log.isDebugEnabled()){
_log.debug("Found illegal characters in resource name [" + name + "]\nThese characters won't be saved");
}
// Ignore from characters with Ascii >= 128 or ASCII =< 32
StringBuilder cleanName = new StringBuilder();

for (int i = 0; i < name.length(); i++) {
if (32 < name.charAt(i) && name.charAt(i) < 128) {
cleanName.append(name.charAt(i));
}
}
return cleanName.toString();
}
return name;
}
}

0 comments on commit 8e97b94

Please sign in to comment.