|
7 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 8 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
9 | 9 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
10 | 11 | import com.fasterxml.jackson.databind.ser.FilterProvider; |
11 | 12 | import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
12 | 13 | import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
|
19 | 20 | import java.io.IOException; |
20 | 21 | import java.nio.file.Files; |
21 | 22 | import java.nio.file.Paths; |
| 23 | +import java.nio.file.attribute.FileTime; |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.format.DateTimeFormatter; |
| 26 | +import java.time.format.FormatStyle; |
22 | 27 | import java.util.ArrayList; |
23 | 28 | import java.util.List; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.TreeMap; |
| 32 | +import java.nio.file.attribute.BasicFileAttributes; |
24 | 33 |
|
25 | 34 | import javax.persistence.EntityManager; |
26 | 35 | import javax.persistence.TypedQuery; |
@@ -58,6 +67,7 @@ public class CodeGov { |
58 | 67 |
|
59 | 68 | // logger instance |
60 | 69 | private static final Logger log = LoggerFactory.getLogger(CodeGov.class); |
| 70 | + |
61 | 71 | // absolute filesystem location to store uploaded files, if any |
62 | 72 | private static final String FILE_UPLOADS = DoeServletContextListener.getConfigurationProperty("file.uploads"); |
63 | 73 |
|
@@ -221,6 +231,89 @@ public Response listCodeGovData() |
221 | 231 | } |
222 | 232 | } |
223 | 233 |
|
| 234 | + /** |
| 235 | + * Acquire info about the latest CodeGov file. |
| 236 | + * |
| 237 | + * @return extra Code.gov JSON information |
| 238 | + * @throws JsonProcessingException |
| 239 | + */ |
| 240 | + @GET |
| 241 | + @Path("/info") |
| 242 | + @Produces(MediaType.APPLICATION_JSON) |
| 243 | + public Response getCodeGovInfo() throws JsonProcessingException { |
| 244 | + try { |
| 245 | + java.nio.file.Path codegovFile = Paths.get(FILE_UPLOADS, "codegov", "code.json"); |
| 246 | + |
| 247 | + // if no file was found, fail |
| 248 | + if (!Files.exists(codegovFile)) |
| 249 | + return ErrorResponse.status(Response.Status.NOT_FOUND, "Code.gov JSON file not found!").build(); |
| 250 | + |
| 251 | + // read file info |
| 252 | + BasicFileAttributes attr; |
| 253 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/dd/yyyy h:mm:ss a").withLocale(Locale.US).withZone(ZoneId.systemDefault()); |
| 254 | + |
| 255 | + try { |
| 256 | + attr = Files.readAttributes(codegovFile, BasicFileAttributes.class); |
| 257 | + } catch (Exception e) { |
| 258 | + log.warn("Cannot get the Code Gov JSON file attributes - " + e); |
| 259 | + return ErrorResponse.status(Response.Status.NOT_FOUND, "Unable to get file attributes from Code.gov JSON file!").build(); |
| 260 | + } |
| 261 | + |
| 262 | + // read file |
| 263 | + JsonNode json = JSON_MAPPER.readTree(new FileInputStream(codegovFile.toString())); |
| 264 | + |
| 265 | + // get releases |
| 266 | + JsonNode releases = json.get("releases"); |
| 267 | + |
| 268 | + // iterate, counting usage types |
| 269 | + Map<String, Integer> usageMap = new TreeMap(); |
| 270 | + if (releases.isArray()) { |
| 271 | + ArrayNode releasesArray = (ArrayNode) releases; |
| 272 | + |
| 273 | + for (int i = 0; i < releasesArray.size(); i++) { |
| 274 | + JsonNode projectNode = releasesArray.get(i); |
| 275 | + |
| 276 | + String permission = "Unknown"; |
| 277 | + try { |
| 278 | + permission = projectNode.get("permissions").get("usageType").asText(); |
| 279 | + } catch (Exception e) { |
| 280 | + permission = "Unknown"; |
| 281 | + } |
| 282 | + |
| 283 | + Integer count = usageMap.get(permission); |
| 284 | + count = (count == null) ? 1 : count + 1; |
| 285 | + |
| 286 | + usageMap.put(permission, count); |
| 287 | + |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + // generate return JSON |
| 292 | + ObjectNode info = JSON_MAPPER.createObjectNode(); |
| 293 | + ObjectNode records = JSON_MAPPER.createObjectNode(); |
| 294 | + ObjectNode usage = JSON_MAPPER.createObjectNode(); |
| 295 | + |
| 296 | + for (String key : usageMap.keySet()) { |
| 297 | + int count = usageMap.get(key); |
| 298 | + usage.put(key, count); |
| 299 | + } |
| 300 | + |
| 301 | + records.put("total", (releases != null ? releases.size() : 0)); |
| 302 | + records.put("usage", usage); |
| 303 | + |
| 304 | + info.put("records", records); |
| 305 | + info.put("last_accessed", formatter.format(attr.lastAccessTime().toInstant())); |
| 306 | + info.put("last_modified", formatter.format(attr.lastModifiedTime().toInstant())); |
| 307 | + |
| 308 | + return Response.status(Response.Status.OK) |
| 309 | + .entity(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(info)).build(); |
| 310 | + |
| 311 | + } catch (IOException e) { // IO |
| 312 | + log.warn("JSON conversion error: " + e.getMessage()); |
| 313 | + return ErrorResponse.status(Response.Status.INTERNAL_SERVER_ERROR, "JSON conversion error.").build(); |
| 314 | + } |
| 315 | + } |
| 316 | + |
224 | 317 | /** |
225 | 318 | * Acquire a listing of all Approved records. |
226 | 319 | * |
|
0 commit comments