|
4 | 4 | import java.util.Optional;
|
5 | 5 | import java.util.UUID;
|
6 | 6 | import java.util.function.Supplier;
|
7 |
| - |
8 |
| -import com.amazonaws.samples.appconfig.AppConfigUtility; |
| 7 | +import java.util.List; |
| 8 | +import java.util.ArrayList; |
| 9 | +import com.amazonaws.samples.appconfig.utils.AppConfigUtility; |
9 | 10 | import com.amazonaws.samples.appconfig.cache.ConfigurationCache;
|
10 | 11 | import com.amazonaws.samples.appconfig.model.ConfigurationKey;
|
| 12 | +import com.amazonaws.samples.appconfig.utils.HTMLBuilder; |
| 13 | +import org.json.JSONArray; |
11 | 14 | import org.json.JSONObject;
|
12 | 15 | import org.springframework.beans.factory.annotation.Autowired;
|
13 | 16 | import org.springframework.core.env.Environment;
|
| 17 | +import org.springframework.validation.BindingResult; |
14 | 18 | import org.springframework.web.bind.annotation.GetMapping;
|
| 19 | +import org.springframework.web.bind.annotation.PathVariable; |
| 20 | +import org.springframework.web.bind.annotation.RequestMapping; |
15 | 21 | import org.springframework.web.bind.annotation.RestController;
|
16 | 22 | import software.amazon.awssdk.services.appconfig.AppConfigClient;
|
17 | 23 | import software.amazon.awssdk.services.appconfig.model.GetConfigurationResponse;
|
| 24 | +import org.apache.logging.log4j.LogManager; |
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | +import javax.validation.Valid; |
18 | 27 |
|
19 |
| - |
| 28 | +import static org.springframework.web.bind.annotation.RequestMethod.POST; |
20 | 29 |
|
21 | 30 | @RestController
|
22 | 31 | public class MoviesController {
|
| 32 | + private static final Logger logger = LogManager.getLogger(MoviesController.class); |
23 | 33 |
|
24 | 34 | /**
|
25 | 35 | * Static Movie Array containing all the list of Movies.
|
26 | 36 | */
|
27 |
| - private static final Movie[] PAIDMOVIES = { |
28 |
| - new Movie(1L, "Paid Movie 1"), |
29 |
| - new Movie(2L, "Paid Movie 2"), |
30 |
| - new Movie(3L, "Paid Movie 3"), |
31 |
| - new Movie(4L, "Paid Movie 4"), |
32 |
| - new Movie(5L, "Paid Movie 5"), |
33 |
| - new Movie(6L, "Paid Movie 6"), |
34 |
| - new Movie(7L, "Paid Movie 7"), |
35 |
| - new Movie(8L, "Paid Movie 8"), |
36 |
| - new Movie(9L, "Paid Movie 9"), |
37 |
| - new Movie(10L, "Paid Movie 10") |
| 37 | + static final Movie[] PAIDMOVIES = { |
| 38 | + new Movie(1L, "Static Movie 1"), |
| 39 | + new Movie(2L, "Static Movie 2"), |
| 40 | + new Movie(3L, "Static Movie 3"), |
| 41 | + new Movie(4L, "Static Movie 4"), |
| 42 | + new Movie(5L, "Static Movie 5"), |
| 43 | + new Movie(6L, "Static Movie 6"), |
| 44 | + new Movie(7L, "Static Movie 7"), |
| 45 | + new Movie(8L, "Static Movie 8"), |
| 46 | + new Movie(9L, "Static Movie 9"), |
| 47 | + new Movie(10L, "Static Movie 10") |
38 | 48 | };
|
39 | 49 | public Duration cacheItemTtl = Duration.ofSeconds(30);
|
40 | 50 | private Boolean boolEnableFeature;
|
41 | 51 | private int intItemLimit;
|
42 |
| - private AppConfigClient client; |
43 |
| - private String clientId; |
44 |
| - private ConfigurationCache cache; |
| 52 | + AppConfigClient client; |
| 53 | + String clientId; |
| 54 | + ConfigurationCache cache; |
45 | 55 |
|
46 | 56 | @Autowired
|
47 |
| - private Environment env; |
| 57 | + Environment env; |
48 | 58 |
|
49 | 59 | /**
|
50 | 60 | * REST API method to get all the Movies based on AWS App Config parameter.
|
51 | 61 | *
|
52 | 62 | * @return List of Movies
|
53 | 63 | */
|
54 | 64 | @GetMapping("/movies/getMovies")
|
55 |
| - public Movie[] movie() { |
| 65 | + public String movie() { |
| 66 | + logger.info("Fetching movies from AWS App Config"); |
| 67 | + try { |
| 68 | + |
56 | 69 |
|
57 |
| - final String application = env.getProperty("appconfig.application"); |
58 |
| - final String environment = env.getProperty("appconfig.environment"); |
59 |
| - final String config = env.getProperty("appconfig.config"); |
60 | 70 | cacheItemTtl = Duration.ofSeconds(Long.parseLong(env.getProperty("appconfig.cacheTtlInSeconds")));
|
61 | 71 |
|
62 | 72 | final AppConfigUtility appConfigUtility = new AppConfigUtility(getOrDefault(this::getClient, this::getDefaultClient),
|
63 | 73 | getOrDefault(this::getConfigurationCache, ConfigurationCache::new),
|
64 | 74 | getOrDefault(this::getCacheItemTtl, () -> cacheItemTtl),
|
65 | 75 | getOrDefault(this::getClientId, this::getDefaultClientId));
|
66 | 76 |
|
| 77 | + final String application = env.getProperty("appconfig.application"); |
| 78 | + final String environment = env.getProperty("appconfig.environment"); |
| 79 | + final String config = env.getProperty("appconfig.config"); |
67 | 80 | final GetConfigurationResponse response = appConfigUtility.getConfiguration(new ConfigurationKey(application, environment, config));
|
68 | 81 | final String appConfigResponse = response.content().asUtf8String();
|
69 | 82 |
|
70 | 83 | final JSONObject jsonResponseObject = new JSONObject(appConfigResponse);
|
71 |
| - boolEnableFeature = jsonResponseObject.getBoolean("boolEnableFeature"); |
72 |
| - intItemLimit = jsonResponseObject.getInt("intItemLimit"); |
73 |
| - final Movie[] limitedMovies = new Movie[intItemLimit]; |
74 |
| - |
75 |
| - for (int i = 0; i < intItemLimit; i++) { |
76 |
| - limitedMovies[i] = PAIDMOVIES[i]; |
| 84 | + System.out.println("json is "+jsonResponseObject); |
| 85 | + |
| 86 | + JSONArray moviesArray = jsonResponseObject.getJSONArray("movies"); |
| 87 | + System.out.println("movies array is "+moviesArray); |
| 88 | + List<Movie> movieList = new ArrayList<>(); |
| 89 | + for (int i = 0; i < moviesArray.length(); i++) { |
| 90 | + JSONObject movieObj = moviesArray.getJSONObject(i); |
| 91 | + long id = movieObj.getLong("id"); |
| 92 | + String movieName = movieObj.getString("movieName"); |
| 93 | + // Extract other fields as needed |
| 94 | + Movie movie = new Movie(id, movieName); |
| 95 | + movieList.add(movie); |
77 | 96 | }
|
78 |
| - |
79 |
| - if (boolEnableFeature) { |
80 |
| - return limitedMovies; |
81 |
| - } else { |
82 |
| - return PAIDMOVIES; |
| 97 | + Movie[] movies = movieList.toArray(new Movie[movieList.size()]); |
| 98 | + HTMLBuilder htmlBuilder = new HTMLBuilder(); |
| 99 | + String moviesHtml = htmlBuilder.getMoviesHtml(movies); |
| 100 | + |
| 101 | + return moviesHtml; |
| 102 | + } catch (Exception e) { |
| 103 | + logger.error("Error fetching movies from AWS App Config", e); |
| 104 | + HTMLBuilder htmlBuilder = new HTMLBuilder(); |
| 105 | + String moviesHtml = htmlBuilder.getMoviesHtml(PAIDMOVIES); |
| 106 | + return moviesHtml; |
83 | 107 | }
|
84 | 108 | }
|
85 | 109 |
|
| 110 | + @RequestMapping(value = "/movies/{movie}/edit", method = POST) |
| 111 | + public String processUpdateMovie(@Valid Movie movie, BindingResult result, @PathVariable("movieId") int movieId) { |
| 112 | + final AppConfigUtility appConfigUtility = new AppConfigUtility(getOrDefault(this::getClient, this::getDefaultClient), |
| 113 | + getOrDefault(this::getConfigurationCache, ConfigurationCache::new), |
| 114 | + getOrDefault(this::getCacheItemTtl, () -> cacheItemTtl), |
| 115 | + getOrDefault(this::getClientId, this::getDefaultClientId)); |
| 116 | + |
| 117 | + |
| 118 | + final String application = env.getProperty("appconfig.application"); |
| 119 | + final String environment = env.getProperty("appconfig.environment"); |
| 120 | + final String config = env.getProperty("appconfig.config"); |
| 121 | + |
| 122 | + final GetConfigurationResponse response = appConfigUtility.updateConfiguration(new ConfigurationKey(application, environment, config),movie.toString()); |
| 123 | + final String appConfigResponse = response.content().asUtf8String(); |
| 124 | + |
| 125 | + final JSONObject jsonResponseObject = new JSONObject(appConfigResponse); |
| 126 | + System.out.println("json is "+jsonResponseObject); |
| 127 | + |
| 128 | + JSONArray moviesArray = jsonResponseObject.getJSONArray("movies"); |
| 129 | + System.out.println("movies array is "+moviesArray); |
| 130 | + List<Movie> movieList = new ArrayList<>(); |
| 131 | + for (int i = 0; i < moviesArray.length(); i++) { |
| 132 | + JSONObject movieObj = moviesArray.getJSONObject(i); |
| 133 | + long id = movieObj.getLong("id"); |
| 134 | + String movieName = movieObj.getString("movieName"); |
| 135 | + // Extract other fields as needed |
| 136 | + movieList.add(movie); |
| 137 | + } |
| 138 | + Movie[] movies = movieList.toArray(new Movie[movieList.size()]); |
| 139 | + HTMLBuilder htmlBuilder = new HTMLBuilder(); |
| 140 | + String moviesHtml = htmlBuilder.getMoviesHtml(movies); |
| 141 | + |
| 142 | + return moviesHtml; |
| 143 | + |
| 144 | + } |
86 | 145 |
|
87 | 146 | private <T> T getOrDefault(final Supplier<T> optionalGetter, final Supplier<T> defaultGetter) {
|
88 | 147 | return Optional.ofNullable(optionalGetter.get()).orElseGet(defaultGetter);
|
89 | 148 | }
|
90 | 149 |
|
91 |
| - private String getDefaultClientId() { |
| 150 | + String getDefaultClientId() { |
92 | 151 | return UUID.randomUUID().toString();
|
93 | 152 | }
|
94 | 153 |
|
|
0 commit comments