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