if (illegalCharactersPattern.matcher(tag).matches()) { //"[~!@#$;%^*()+={}[]|\\<>\"\']"
throw new RegistryException("The tag '" + tag + "' contains one or more illegal " +
"characters (~!@#$;%^*()+={}|\\<>\"\')");
}
boolean transactionSucceeded = false;
RequestContext context = new RequestContext(this, repository, versionRepository);
try {
// start the transaction
beginTransaction();
ResourcePath processedPath = new ResourcePath(resourcePath);
context.setResourcePath(processedPath);
context.setTag(tag);
context.setOldTags(getTags(processedPath.getPath()));
registryContext.getHandlerManager().applyTag(context);
if (!context.isSimulation()) {
if (!context.isProcessingComplete()) {
if (!processedPath.isCurrentVersion()) {
String msg = "Failed to apply tag to the resource " + processedPath +
". Given path refers to an archived version of the resource.";
log.error(msg);
throw new RegistryException(msg);
}
resourcePath = processedPath.getPath();
// break the comma separated words into multiple tags
String[] tags = tag.split(",");
ResourceImpl resource = tagsDAO.getResourceWithMinimumData(resourcePath);
if (resource == null) {
String msg = "Failed to apply tag " + tag + " on resource " + resourcePath +
". Resource " + resourcePath + " does not exist.";
log.error(msg);
throw new RegistryException(msg);
}
String userName = CurrentSession.getUser();
if (!AuthorizationUtils.authorize(resource.getPath(), ActionConstants.GET)) {
String msg = "Failed to apply tag " + tag + " on resource " + resourcePath +
". User " + userName + " is not authorized to read the resource.";
log.error(msg);
throw new RegistryException(msg);
}
for (int i = 0; i < tags.length; i++) {
tags[i] = tags[i].trim();
if (tags[i].length() == 0 || tags[i].equals(" ")) {
continue;
}
if (tagsDAO.taggingExists(tags[i], resource, userName)) {
// Already there, don't bother doing it again.
continue;
}
tagsDAO.addTagging(tags[i], resource, userName);
if (context.isLoggingActivity()) {
registryContext.getLogWriter().addLog(
resourcePath, userName, LogEntry.TAG, tags[i]);
}
}
}