1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class QueryStringResourceLoader {
  4. private final Map<String, String> fixedRetryIntervals = new HashMap<>();
  5. static {
  6. // Define fixed retry intervals (e.g., for older systems)
  7. fixedRetryIntervals.put("old_api", "5s");
  8. fixedRetryIntervals.put("legacy_service", "10s");
  9. }
  10. /**
  11. * Loads resources from query string parameters, applying fixed retry intervals
  12. * if specified.
  13. *
  14. * @param queryParams A map of query string parameters.
  15. * @return A map of resource names to their corresponding content,
  16. * or null if an error occurs.
  17. */
  18. public Map<String, String> loadResources(Map<String, String> queryParams) {
  19. Map<String, String> resources = new HashMap<>();
  20. if (queryParams == null || queryParams.isEmpty()) {
  21. return resources; // Return empty map if no query parameters
  22. }
  23. for (Map.Entry<String, String> entry : queryParams.entrySet()) {
  24. String resourceName = entry.getKey();
  25. String retryInterval = fixedRetryIntervals.getOrDefault(resourceName, ""); // Default to no retry
  26. String resourceContent = getResource(resourceName, retryInterval);
  27. resources.put(resourceName, resourceContent);
  28. }
  29. return resources;
  30. }
  31. /**
  32. * Retrieves the resource content based on the resource name and retry interval.
  33. *
  34. * @param resourceName The name of the resource to retrieve.
  35. * @param retryInterval The retry interval to use (e.g., "5s").
  36. * @return The content of the resource, or null if an error occurs.
  37. */
  38. private String getResource(String resourceName, String retryInterval) {
  39. // Simulate resource retrieval with retry logic
  40. if ("old_api".equals(resourceName)) {
  41. System.out.println("Fetching old_api with retry interval: " + retryInterval);
  42. return "Content from old_api";
  43. } else if ("legacy_service".equals(resourceName)) {
  44. System.out.println("Fetching legacy_service with retry interval: " + retryInterval);
  45. return "Content from legacy_service";
  46. } else {
  47. System.out.println("Fetching unknown resource: " + resourceName);
  48. return "Default content";
  49. }
  50. }
  51. public static void main(String[] args) {
  52. QueryStringResourceLoader loader = new QueryStringResourceLoader();
  53. Map<String, String> queryParams1 = new HashMap<>();
  54. queryParams1.put("old_api", "value1");
  55. queryParams1.put("legacy_service", "value2");
  56. Map<String, String> resources1 = loader.loadResources(queryParams1);
  57. System.out.println("Resources 1: " + resources1);
  58. Map<String, String> queryParams2 = new HashMap<>();
  59. queryParams2.put("unknown_resource", "value3");
  60. Map<String, String> resources2 = loader.loadResources(queryParams2);
  61. System.out.println("Resources 2: " + resources2);
  62. Map<String, String> queryParams3 = new HashMap<>();
  63. Map<String, String> resources3 = loader.loadResources(queryParams3);
  64. System.out.println("Resources 3: " + resources3);
  65. }
  66. }

Add your comment