import java.util.HashMap;
import java.util.Map;
public class QueryStringResourceLoader {
private final Map<String, String> fixedRetryIntervals = new HashMap<>();
static {
// Define fixed retry intervals (e.g., for older systems)
fixedRetryIntervals.put("old_api", "5s");
fixedRetryIntervals.put("legacy_service", "10s");
}
/**
* Loads resources from query string parameters, applying fixed retry intervals
* if specified.
*
* @param queryParams A map of query string parameters.
* @return A map of resource names to their corresponding content,
* or null if an error occurs.
*/
public Map<String, String> loadResources(Map<String, String> queryParams) {
Map<String, String> resources = new HashMap<>();
if (queryParams == null || queryParams.isEmpty()) {
return resources; // Return empty map if no query parameters
}
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
String resourceName = entry.getKey();
String retryInterval = fixedRetryIntervals.getOrDefault(resourceName, ""); // Default to no retry
String resourceContent = getResource(resourceName, retryInterval);
resources.put(resourceName, resourceContent);
}
return resources;
}
/**
* Retrieves the resource content based on the resource name and retry interval.
*
* @param resourceName The name of the resource to retrieve.
* @param retryInterval The retry interval to use (e.g., "5s").
* @return The content of the resource, or null if an error occurs.
*/
private String getResource(String resourceName, String retryInterval) {
// Simulate resource retrieval with retry logic
if ("old_api".equals(resourceName)) {
System.out.println("Fetching old_api with retry interval: " + retryInterval);
return "Content from old_api";
} else if ("legacy_service".equals(resourceName)) {
System.out.println("Fetching legacy_service with retry interval: " + retryInterval);
return "Content from legacy_service";
} else {
System.out.println("Fetching unknown resource: " + resourceName);
return "Default content";
}
}
public static void main(String[] args) {
QueryStringResourceLoader loader = new QueryStringResourceLoader();
Map<String, String> queryParams1 = new HashMap<>();
queryParams1.put("old_api", "value1");
queryParams1.put("legacy_service", "value2");
Map<String, String> resources1 = loader.loadResources(queryParams1);
System.out.println("Resources 1: " + resources1);
Map<String, String> queryParams2 = new HashMap<>();
queryParams2.put("unknown_resource", "value3");
Map<String, String> resources2 = loader.loadResources(queryParams2);
System.out.println("Resources 2: " + resources2);
Map<String, String> queryParams3 = new HashMap<>();
Map<String, String> resources3 = loader.loadResources(queryParams3);
System.out.println("Resources 3: " + resources3);
}
}
Add your comment