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; }
@SuppressWarnings("serial") @Test public void generateCompleteURI_successfulURIGeneration() { String uriSignature = "http: Map<String, String> parameters = new HashMap<String, String>() { { put("sop_id", "111"); put("unit", "33"); put("cond_id", "2222"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parameters, true); assertEquals( "http: completeURI); }
@SuppressWarnings("serial") @Test public void generateCompleteURI_successfulURIGeneration_URLParameterEscaping() { String uriSignature = "http: Map<String, String> parameters = new HashMap<String, String>() { { put("sop_id", "1 11"); put("unit", "3;3"); put("cond_id", "2/2$2&2:"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parameters, true); System.err.println(completeURI); assertEquals( "http: completeURI); }
@SuppressWarnings("serial") @Test public void generateCompleteURI_successfulURIGeneration_noURLParameterEscaping() { String uriSignature = "http: Map<String, String> parameters = new HashMap<String, String>() { { put("sop_id", "1 11"); put("unit", "3;3"); put("cond_id", "2/2$2&2:"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parameters, false); assertEquals( "http: completeURI); }
@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); }
@Test public void generateCompleteURI_signatureWithNoPlaceholders_nullParameterMap() { String completeURI = URISignatureHandler.generateCompleteURI( validURI_NoPlaceholders, null, true); assertEquals(validURI_NoPlaceholders, completeURI); }
@Test public void generateCompleteURI_signatureWithNoPlaceholders_emptyParameterMap() { String completeURI = URISignatureHandler.generateCompleteURI( validURI_NoPlaceholders, Collections .<String, String> emptyMap(), true); assertEquals(validURI_NoPlaceholders, completeURI); }
@Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class) public void generateCompleteURI_signatureWithPlaceholders_nullParameterMap() { URISignatureHandler.generateCompleteURI(validURI_3MixedPlaceholders, null, true); }
@Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class) public void generateCompleteURI_signatureWithPlaceholders_emptyParameterMap() { URISignatureHandler.generateCompleteURI(validURI_3MixedPlaceholders, Collections.<String, String> emptyMap(), true); }
@SuppressWarnings("serial") @Test public void generateCompleteURI_signatureWithPlaceholders_missingParameterURIGeneration_FailureNotExpected() { String uriSignature = "http: Map<String, String> parameters = new HashMap<String, String>() { { put("sop_id", "111"); put("cond_id", "2222"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parameters, true); assertEquals( "http: completeURI); }
@SuppressWarnings("serial") @Test(expected = URISignatureHandler.URISignatureParsingException.class) public void generateCompleteURI_duplicatePlaceholderURIGeneration_FailureExpected() { String uriSignature = "http: Map<String, String> parameters = new HashMap<String, String>() { { put("sop_id", "111"); put("unit", "33"); put("cond_id", "2222"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parameters, true); assertEquals( "http: completeURI); }
@SuppressWarnings("serial") @Test(expected = URISignatureHandler.URIGenerationFromSignatureException.class) public void generateCompleteURI_failureURIGeneration_optionalParams() { String uriSignature = "http: Map<String, String> parametersMissingCompulsory = new HashMap<String, String>() { { put("count?", "10"); put("startPage?", "1"); put("startIndex?", "1"); put("searchTerms?", "term1"); } }; String completeURI = URISignatureHandler.generateCompleteURI( uriSignature, parametersMissingCompulsory, false); assertEquals( "http: completeURI); }