@Test public void testResolve() { WebHookContentObjectSerialiser webHookContentObjectSerialiser = new WebHookPayloadContent.SimpleSerialiser(); JavaBean javaBean = new JavaBean("bt123", "project01"); ExtraParameters extraParameters = new ExtraParameters(); VariableResolver variableResolver = new WebHooksBeanUtilsVariableResolver(null, webHookContentObjectSerialiser, javaBean, extraParameters, null); assertEquals("bt123", variableResolver.resolve("buildId")); assertEquals("project01", variableResolver.resolve("projectId")); }
@Override public String resolve(String variableName) { String value = "UNRESOLVED"; if (variableName.startsWith(NOW) && variableName.endsWith(SUFFIX)){ try { String datePattern = variableName.substring(NOW.length(), variableName.length() - SUFFIX.length()); SimpleDateFormat format = new SimpleDateFormat(datePattern); return format.format(new Date()); } catch (NullPointerException | IllegalArgumentException e){ } } if (variableName.startsWith(ESCAPEJSON) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(ESCAPEJSON.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringEscapeUtils.escapeJson(extraParameters.get(dirtyString)); } return StringEscapeUtils.escapeJson((String) getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(CAPITALISE)|| variableName.startsWith(CAPITALIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseAllWords(extraParameters.get(dirtyString)); } return StringUtils.capitaliseAllWords((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SUBSTR) && variableName.endsWith(SUFFIX)){ try { String[] subStringOptions = variableName.substring(SUBSTR.length(), variableName.length() - SUFFIX.length()).split(","); String varName = subStringOptions[0]; if (extraParameters.containsKey(varName)){ return StringUtils.subString( this.extraParameters.get(varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } return StringUtils.subString( getProperty(bean, varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith("capitaliseFirst(")|| variableName.startsWith("capitalizeFirst(")) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseFirstWord(extraParameters.get(dirtyString)); } return StringUtils.capitaliseFirstWord((String) PropertyUtils.getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(SANITISE) || variableName.startsWith(SANITIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(SANITISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringSanitiser.sanitise(extraParameters.get(dirtyString)); } return StringSanitiser.sanitise((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SECURE) && variableName.endsWith(SUFFIX)){ String tokenString = variableName.substring(SECURE.length(), variableName.length() - SUFFIX.length()); String resolvedString = this.webHookSecretResolver.getSecret(sProject, tokenString); if (resolvedString != null){ return resolvedString; } } try { if (extraParameters.containsKey(variableName)){ value = extraParameters.get(variableName); } try { value = getProperty(bean, variableName).toString(); } catch (NullPointerException npe){ value = (String) getProperty(bean, variableName); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: " + e.getClass() + " thrown when trying to resolve value for " + variableName); } return value; }
WebHooksBeanUtilsVariableResolver implements VariableResolver { @Override public String resolve(String variableName) { String value = "UNRESOLVED"; if (variableName.startsWith(NOW) && variableName.endsWith(SUFFIX)){ try { String datePattern = variableName.substring(NOW.length(), variableName.length() - SUFFIX.length()); SimpleDateFormat format = new SimpleDateFormat(datePattern); return format.format(new Date()); } catch (NullPointerException | IllegalArgumentException e){ } } if (variableName.startsWith(ESCAPEJSON) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(ESCAPEJSON.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringEscapeUtils.escapeJson(extraParameters.get(dirtyString)); } return StringEscapeUtils.escapeJson((String) getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(CAPITALISE)|| variableName.startsWith(CAPITALIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseAllWords(extraParameters.get(dirtyString)); } return StringUtils.capitaliseAllWords((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SUBSTR) && variableName.endsWith(SUFFIX)){ try { String[] subStringOptions = variableName.substring(SUBSTR.length(), variableName.length() - SUFFIX.length()).split(","); String varName = subStringOptions[0]; if (extraParameters.containsKey(varName)){ return StringUtils.subString( this.extraParameters.get(varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } return StringUtils.subString( getProperty(bean, varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith("capitaliseFirst(")|| variableName.startsWith("capitalizeFirst(")) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseFirstWord(extraParameters.get(dirtyString)); } return StringUtils.capitaliseFirstWord((String) PropertyUtils.getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(SANITISE) || variableName.startsWith(SANITIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(SANITISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringSanitiser.sanitise(extraParameters.get(dirtyString)); } return StringSanitiser.sanitise((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SECURE) && variableName.endsWith(SUFFIX)){ String tokenString = variableName.substring(SECURE.length(), variableName.length() - SUFFIX.length()); String resolvedString = this.webHookSecretResolver.getSecret(sProject, tokenString); if (resolvedString != null){ return resolvedString; } } try { if (extraParameters.containsKey(variableName)){ value = extraParameters.get(variableName); } try { value = getProperty(bean, variableName).toString(); } catch (NullPointerException npe){ value = (String) getProperty(bean, variableName); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: " + e.getClass() + " thrown when trying to resolve value for " + variableName); } return value; } }
WebHooksBeanUtilsVariableResolver implements VariableResolver { @Override public String resolve(String variableName) { String value = "UNRESOLVED"; if (variableName.startsWith(NOW) && variableName.endsWith(SUFFIX)){ try { String datePattern = variableName.substring(NOW.length(), variableName.length() - SUFFIX.length()); SimpleDateFormat format = new SimpleDateFormat(datePattern); return format.format(new Date()); } catch (NullPointerException | IllegalArgumentException e){ } } if (variableName.startsWith(ESCAPEJSON) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(ESCAPEJSON.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringEscapeUtils.escapeJson(extraParameters.get(dirtyString)); } return StringEscapeUtils.escapeJson((String) getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(CAPITALISE)|| variableName.startsWith(CAPITALIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseAllWords(extraParameters.get(dirtyString)); } return StringUtils.capitaliseAllWords((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SUBSTR) && variableName.endsWith(SUFFIX)){ try { String[] subStringOptions = variableName.substring(SUBSTR.length(), variableName.length() - SUFFIX.length()).split(","); String varName = subStringOptions[0]; if (extraParameters.containsKey(varName)){ return StringUtils.subString( this.extraParameters.get(varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } return StringUtils.subString( getProperty(bean, varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith("capitaliseFirst(")|| variableName.startsWith("capitalizeFirst(")) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseFirstWord(extraParameters.get(dirtyString)); } return StringUtils.capitaliseFirstWord((String) PropertyUtils.getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(SANITISE) || variableName.startsWith(SANITIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(SANITISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringSanitiser.sanitise(extraParameters.get(dirtyString)); } return StringSanitiser.sanitise((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SECURE) && variableName.endsWith(SUFFIX)){ String tokenString = variableName.substring(SECURE.length(), variableName.length() - SUFFIX.length()); String resolvedString = this.webHookSecretResolver.getSecret(sProject, tokenString); if (resolvedString != null){ return resolvedString; } } try { if (extraParameters.containsKey(variableName)){ value = extraParameters.get(variableName); } try { value = getProperty(bean, variableName).toString(); } catch (NullPointerException npe){ value = (String) getProperty(bean, variableName); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: " + e.getClass() + " thrown when trying to resolve value for " + variableName); } return value; } WebHooksBeanUtilsVariableResolver(
			SProject sProject,
			WebHookContentObjectSerialiser webhookPayload, 
			Object javaBean, 
			ExtraParameters extraAndTeamCityProperties,
			WebHookSecretResolver webHookSecretResolver); }
WebHooksBeanUtilsVariableResolver implements VariableResolver { @Override public String resolve(String variableName) { String value = "UNRESOLVED"; if (variableName.startsWith(NOW) && variableName.endsWith(SUFFIX)){ try { String datePattern = variableName.substring(NOW.length(), variableName.length() - SUFFIX.length()); SimpleDateFormat format = new SimpleDateFormat(datePattern); return format.format(new Date()); } catch (NullPointerException | IllegalArgumentException e){ } } if (variableName.startsWith(ESCAPEJSON) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(ESCAPEJSON.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringEscapeUtils.escapeJson(extraParameters.get(dirtyString)); } return StringEscapeUtils.escapeJson((String) getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(CAPITALISE)|| variableName.startsWith(CAPITALIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseAllWords(extraParameters.get(dirtyString)); } return StringUtils.capitaliseAllWords((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SUBSTR) && variableName.endsWith(SUFFIX)){ try { String[] subStringOptions = variableName.substring(SUBSTR.length(), variableName.length() - SUFFIX.length()).split(","); String varName = subStringOptions[0]; if (extraParameters.containsKey(varName)){ return StringUtils.subString( this.extraParameters.get(varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } return StringUtils.subString( getProperty(bean, varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith("capitaliseFirst(")|| variableName.startsWith("capitalizeFirst(")) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseFirstWord(extraParameters.get(dirtyString)); } return StringUtils.capitaliseFirstWord((String) PropertyUtils.getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(SANITISE) || variableName.startsWith(SANITIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(SANITISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringSanitiser.sanitise(extraParameters.get(dirtyString)); } return StringSanitiser.sanitise((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SECURE) && variableName.endsWith(SUFFIX)){ String tokenString = variableName.substring(SECURE.length(), variableName.length() - SUFFIX.length()); String resolvedString = this.webHookSecretResolver.getSecret(sProject, tokenString); if (resolvedString != null){ return resolvedString; } } try { if (extraParameters.containsKey(variableName)){ value = extraParameters.get(variableName); } try { value = getProperty(bean, variableName).toString(); } catch (NullPointerException npe){ value = (String) getProperty(bean, variableName); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: " + e.getClass() + " thrown when trying to resolve value for " + variableName); } return value; } WebHooksBeanUtilsVariableResolver(
			SProject sProject,
			WebHookContentObjectSerialiser webhookPayload, 
			Object javaBean, 
			ExtraParameters extraAndTeamCityProperties,
			WebHookSecretResolver webHookSecretResolver); @Override String resolve(String variableName); }
WebHooksBeanUtilsVariableResolver implements VariableResolver { @Override public String resolve(String variableName) { String value = "UNRESOLVED"; if (variableName.startsWith(NOW) && variableName.endsWith(SUFFIX)){ try { String datePattern = variableName.substring(NOW.length(), variableName.length() - SUFFIX.length()); SimpleDateFormat format = new SimpleDateFormat(datePattern); return format.format(new Date()); } catch (NullPointerException | IllegalArgumentException e){ } } if (variableName.startsWith(ESCAPEJSON) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(ESCAPEJSON.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringEscapeUtils.escapeJson(extraParameters.get(dirtyString)); } return StringEscapeUtils.escapeJson((String) getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(CAPITALISE)|| variableName.startsWith(CAPITALIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseAllWords(extraParameters.get(dirtyString)); } return StringUtils.capitaliseAllWords((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SUBSTR) && variableName.endsWith(SUFFIX)){ try { String[] subStringOptions = variableName.substring(SUBSTR.length(), variableName.length() - SUFFIX.length()).split(","); String varName = subStringOptions[0]; if (extraParameters.containsKey(varName)){ return StringUtils.subString( this.extraParameters.get(varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } return StringUtils.subString( getProperty(bean, varName), Integer.valueOf(subStringOptions[1]), Integer.valueOf(subStringOptions[2]), Integer.valueOf(subStringOptions[3]) ); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith("capitaliseFirst(")|| variableName.startsWith("capitalizeFirst(")) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(CAPITALISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringUtils.capitaliseFirstWord(extraParameters.get(dirtyString)); } return StringUtils.capitaliseFirstWord((String) PropertyUtils.getProperty(bean, dirtyString).toString()); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if ((variableName.startsWith(SANITISE) || variableName.startsWith(SANITIZE)) && variableName.endsWith(SUFFIX)){ try { String dirtyString = variableName.substring(SANITISE.length(), variableName.length() - SUFFIX.length()); if (extraParameters.containsKey(dirtyString)){ return StringSanitiser.sanitise(extraParameters.get(dirtyString)); } return StringSanitiser.sanitise((String) getProperty(bean, dirtyString)); } catch (NullPointerException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } } if (variableName.startsWith(SECURE) && variableName.endsWith(SUFFIX)){ String tokenString = variableName.substring(SECURE.length(), variableName.length() - SUFFIX.length()); String resolvedString = this.webHookSecretResolver.getSecret(sProject, tokenString); if (resolvedString != null){ return resolvedString; } } try { if (extraParameters.containsKey(variableName)){ value = extraParameters.get(variableName); } try { value = getProperty(bean, variableName).toString(); } catch (NullPointerException npe){ value = (String) getProperty(bean, variableName); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: " + e.getClass() + " thrown when trying to resolve value for " + variableName); } return value; } WebHooksBeanUtilsVariableResolver(
			SProject sProject,
			WebHookContentObjectSerialiser webhookPayload, 
			Object javaBean, 
			ExtraParameters extraAndTeamCityProperties,
			WebHookSecretResolver webHookSecretResolver); @Override String resolve(String variableName);  }