@SuppressWarnings("serial") @Test public void generateCompleteURI_successfulURIGeneration_optionalParams() { String uriSignature = "http: Map<String, String> allParameters = new HashMap<String, String>() { { put("catalogue", "catalogue"); put("count?", "10"); put("startPage?", "1"); put("startIndex?", "1"); put("searchTerms?", "term1"); } }; Map<String, String> parametersMissingOptional = new HashMap<String, String>() { { put("catalogue", "catalogue"); put("count?", "10"); put("searchTerms?", "term1"); } }; Map<String, String> parametersMissingFirstOptional = new HashMap<String, String>() { { put("catalogue", "catalogue"); put("startPage?", "1"); put("startIndex?", "1"); put("searchTerms?", "term1"); } }; String completeURI1 = URISignatureHandler.generateCompleteURI( uriSignature, allParameters, false); assertEquals( "http: completeURI1); String completeURI2 = URISignatureHandler.generateCompleteURI( uriSignature, parametersMissingOptional, false); assertEquals( "http: completeURI2); String completeURI3 = URISignatureHandler.generateCompleteURI( uriSignature, parametersMissingFirstOptional, false); assertEquals( "http: completeURI3); }
public static String generateCompleteURI(String uriSignature, Map<String, String> specifiedParameters, boolean escapeParameters) throws URISignatureParsingException, URIGenerationFromSignatureException { StringBuilder completeURI = new StringBuilder(uriSignature); LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature); if (placeholdersWithPositions.keySet().size() > 0) { Map<String, String> parameters; if (specifiedParameters == null || specifiedParameters.isEmpty()) { parameters = Collections.emptyMap(); } else { parameters = specifiedParameters; } LinkedList<String> placeholders = new LinkedList<String>( placeholdersWithPositions.keySet()); Collections.reverse(placeholders); Iterator<String> placeholdersIterator = placeholders.iterator(); while (placeholdersIterator.hasNext()) { String placeholder = placeholdersIterator.next(); int placeholderStartPos = placeholdersWithPositions .get(placeholder) - 1; int placeholderEndPos = placeholderStartPos + placeholder.length() + 2; if (parameters.containsKey(placeholder)) { if (escapeParameters) { completeURI.replace(placeholderStartPos, placeholderEndPos, urlEncodeQuery(parameters .get(placeholder))); } else { completeURI.replace(placeholderStartPos, placeholderEndPos, parameters.get(placeholder)); } } else { int qnPos = completeURI.lastIndexOf("?", placeholderStartPos); int ampPos = completeURI.lastIndexOf("&", placeholderStartPos); int slashPos = completeURI.lastIndexOf("/", placeholderStartPos); int startParamPos = Math.max(qnPos, ampPos); if (startParamPos > -1 && startParamPos > slashPos) { if (qnPos > ampPos) { if (placeholderEndPos >= (completeURI.length() - 1)) { completeURI.replace(startParamPos, placeholderEndPos, ""); } else { completeURI.replace(startParamPos + 1, placeholderEndPos + 1, ""); } } else { completeURI.replace(startParamPos, placeholderEndPos, ""); } } else { throw new URIGenerationFromSignatureException( "Parameter map does not contain a key/value for \"" + placeholder + "\" mandatory placeholder"); } } } } return (completeURI.toString()); }
URISignatureHandler { public static String generateCompleteURI(String uriSignature, Map<String, String> specifiedParameters, boolean escapeParameters) throws URISignatureParsingException, URIGenerationFromSignatureException { StringBuilder completeURI = new StringBuilder(uriSignature); LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature); if (placeholdersWithPositions.keySet().size() > 0) { Map<String, String> parameters; if (specifiedParameters == null || specifiedParameters.isEmpty()) { parameters = Collections.emptyMap(); } else { parameters = specifiedParameters; } LinkedList<String> placeholders = new LinkedList<String>( placeholdersWithPositions.keySet()); Collections.reverse(placeholders); Iterator<String> placeholdersIterator = placeholders.iterator(); while (placeholdersIterator.hasNext()) { String placeholder = placeholdersIterator.next(); int placeholderStartPos = placeholdersWithPositions .get(placeholder) - 1; int placeholderEndPos = placeholderStartPos + placeholder.length() + 2; if (parameters.containsKey(placeholder)) { if (escapeParameters) { completeURI.replace(placeholderStartPos, placeholderEndPos, urlEncodeQuery(parameters .get(placeholder))); } else { completeURI.replace(placeholderStartPos, placeholderEndPos, parameters.get(placeholder)); } } else { int qnPos = completeURI.lastIndexOf("?", placeholderStartPos); int ampPos = completeURI.lastIndexOf("&", placeholderStartPos); int slashPos = completeURI.lastIndexOf("/", placeholderStartPos); int startParamPos = Math.max(qnPos, ampPos); if (startParamPos > -1 && startParamPos > slashPos) { if (qnPos > ampPos) { if (placeholderEndPos >= (completeURI.length() - 1)) { completeURI.replace(startParamPos, placeholderEndPos, ""); } else { completeURI.replace(startParamPos + 1, placeholderEndPos + 1, ""); } } else { completeURI.replace(startParamPos, placeholderEndPos, ""); } } else { throw new URIGenerationFromSignatureException( "Parameter map does not contain a key/value for \"" + placeholder + "\" mandatory placeholder"); } } } } return (completeURI.toString()); } }
URISignatureHandler { public static String generateCompleteURI(String uriSignature, Map<String, String> specifiedParameters, boolean escapeParameters) throws URISignatureParsingException, URIGenerationFromSignatureException { StringBuilder completeURI = new StringBuilder(uriSignature); LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature); if (placeholdersWithPositions.keySet().size() > 0) { Map<String, String> parameters; if (specifiedParameters == null || specifiedParameters.isEmpty()) { parameters = Collections.emptyMap(); } else { parameters = specifiedParameters; } LinkedList<String> placeholders = new LinkedList<String>( placeholdersWithPositions.keySet()); Collections.reverse(placeholders); Iterator<String> placeholdersIterator = placeholders.iterator(); while (placeholdersIterator.hasNext()) { String placeholder = placeholdersIterator.next(); int placeholderStartPos = placeholdersWithPositions .get(placeholder) - 1; int placeholderEndPos = placeholderStartPos + placeholder.length() + 2; if (parameters.containsKey(placeholder)) { if (escapeParameters) { completeURI.replace(placeholderStartPos, placeholderEndPos, urlEncodeQuery(parameters .get(placeholder))); } else { completeURI.replace(placeholderStartPos, placeholderEndPos, parameters.get(placeholder)); } } else { int qnPos = completeURI.lastIndexOf("?", placeholderStartPos); int ampPos = completeURI.lastIndexOf("&", placeholderStartPos); int slashPos = completeURI.lastIndexOf("/", placeholderStartPos); int startParamPos = Math.max(qnPos, ampPos); if (startParamPos > -1 && startParamPos > slashPos) { if (qnPos > ampPos) { if (placeholderEndPos >= (completeURI.length() - 1)) { completeURI.replace(startParamPos, placeholderEndPos, ""); } else { completeURI.replace(startParamPos + 1, placeholderEndPos + 1, ""); } } else { completeURI.replace(startParamPos, placeholderEndPos, ""); } } else { throw new URIGenerationFromSignatureException( "Parameter map does not contain a key/value for \"" + placeholder + "\" mandatory placeholder"); } } } } return (completeURI.toString()); }  }
URISignatureHandler { public static String generateCompleteURI(String uriSignature, Map<String, String> specifiedParameters, boolean escapeParameters) throws URISignatureParsingException, URIGenerationFromSignatureException { StringBuilder completeURI = new StringBuilder(uriSignature); LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature); if (placeholdersWithPositions.keySet().size() > 0) { Map<String, String> parameters; if (specifiedParameters == null || specifiedParameters.isEmpty()) { parameters = Collections.emptyMap(); } else { parameters = specifiedParameters; } LinkedList<String> placeholders = new LinkedList<String>( placeholdersWithPositions.keySet()); Collections.reverse(placeholders); Iterator<String> placeholdersIterator = placeholders.iterator(); while (placeholdersIterator.hasNext()) { String placeholder = placeholdersIterator.next(); int placeholderStartPos = placeholdersWithPositions .get(placeholder) - 1; int placeholderEndPos = placeholderStartPos + placeholder.length() + 2; if (parameters.containsKey(placeholder)) { if (escapeParameters) { completeURI.replace(placeholderStartPos, placeholderEndPos, urlEncodeQuery(parameters .get(placeholder))); } else { completeURI.replace(placeholderStartPos, placeholderEndPos, parameters.get(placeholder)); } } else { int qnPos = completeURI.lastIndexOf("?", placeholderStartPos); int ampPos = completeURI.lastIndexOf("&", placeholderStartPos); int slashPos = completeURI.lastIndexOf("/", placeholderStartPos); int startParamPos = Math.max(qnPos, ampPos); if (startParamPos > -1 && startParamPos > slashPos) { if (qnPos > ampPos) { if (placeholderEndPos >= (completeURI.length() - 1)) { completeURI.replace(startParamPos, placeholderEndPos, ""); } else { completeURI.replace(startParamPos + 1, placeholderEndPos + 1, ""); } } else { completeURI.replace(startParamPos, placeholderEndPos, ""); } } else { throw new URIGenerationFromSignatureException( "Parameter map does not contain a key/value for \"" + placeholder + "\" mandatory placeholder"); } } } } return (completeURI.toString()); }  static LinkedHashMap<String, Integer> extractPlaceholdersWithPositions(
			String uriSignature); static List<String> extractPlaceholders(String uriSignature); static void validate(String uriSignature); static void checkForUnsafeCharacters(String candidateURLSignature); static boolean isValid(String uriSignature); static String generateCompleteURI(String uriSignature,
			Map<String, String> specifiedParameters, boolean escapeParameters); static String urlEncodeQuery(String query); }
URISignatureHandler { public static String generateCompleteURI(String uriSignature, Map<String, String> specifiedParameters, boolean escapeParameters) throws URISignatureParsingException, URIGenerationFromSignatureException { StringBuilder completeURI = new StringBuilder(uriSignature); LinkedHashMap<String, Integer> placeholdersWithPositions = extractPlaceholdersWithPositions(uriSignature); if (placeholdersWithPositions.keySet().size() > 0) { Map<String, String> parameters; if (specifiedParameters == null || specifiedParameters.isEmpty()) { parameters = Collections.emptyMap(); } else { parameters = specifiedParameters; } LinkedList<String> placeholders = new LinkedList<String>( placeholdersWithPositions.keySet()); Collections.reverse(placeholders); Iterator<String> placeholdersIterator = placeholders.iterator(); while (placeholdersIterator.hasNext()) { String placeholder = placeholdersIterator.next(); int placeholderStartPos = placeholdersWithPositions .get(placeholder) - 1; int placeholderEndPos = placeholderStartPos + placeholder.length() + 2; if (parameters.containsKey(placeholder)) { if (escapeParameters) { completeURI.replace(placeholderStartPos, placeholderEndPos, urlEncodeQuery(parameters .get(placeholder))); } else { completeURI.replace(placeholderStartPos, placeholderEndPos, parameters.get(placeholder)); } } else { int qnPos = completeURI.lastIndexOf("?", placeholderStartPos); int ampPos = completeURI.lastIndexOf("&", placeholderStartPos); int slashPos = completeURI.lastIndexOf("/", placeholderStartPos); int startParamPos = Math.max(qnPos, ampPos); if (startParamPos > -1 && startParamPos > slashPos) { if (qnPos > ampPos) { if (placeholderEndPos >= (completeURI.length() - 1)) { completeURI.replace(startParamPos, placeholderEndPos, ""); } else { completeURI.replace(startParamPos + 1, placeholderEndPos + 1, ""); } } else { completeURI.replace(startParamPos, placeholderEndPos, ""); } } else { throw new URIGenerationFromSignatureException( "Parameter map does not contain a key/value for \"" + placeholder + "\" mandatory placeholder"); } } } } return (completeURI.toString()); }  static LinkedHashMap<String, Integer> extractPlaceholdersWithPositions(
			String uriSignature); static List<String> extractPlaceholders(String uriSignature); static void validate(String uriSignature); static void checkForUnsafeCharacters(String candidateURLSignature); static boolean isValid(String uriSignature); static String generateCompleteURI(String uriSignature,
			Map<String, String> specifiedParameters, boolean escapeParameters); static String urlEncodeQuery(String query); static final char PLACEHOLDER_START_SYMBOL; static final char PLACEHOLDER_END_SYMBOL; }