Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.shared.rest; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.opentestsystem.shared.docs.domain.ApiExample; import org.opentestsystem.shared.docs.domain.ApiExampleList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.databind.ObjectMapper; @SuppressWarnings({ "PMD.UseConcurrentHashMap", "PMD.AvoidInstantiatingObjectsInLoops" }) @Controller public class ApiController { private static final Logger LOGGER = LoggerFactory.getLogger(ApiController.class); private static final int ZERO = 0; private static Map<String, List<ApiExample>> apiRequestsByURI = new HashMap<String, List<ApiExample>>(); private void parseExamples() { if (apiRequestsByURI.isEmpty()) { try { ClassPathResource res = new ClassPathResource("api_examples.json"); File apiExamples = res.getFile(); String content = FileUtils.readFileToString(apiExamples); content = "[" + content.substring(0, content.length() - 1) + "]"; ObjectMapper objectMapper = new ObjectMapper(); String withWrapper = objectMapper.writeValueAsString(new ApiExampleList()); withWrapper = withWrapper.replace("null", content); ApiExampleList values = objectMapper.readValue(withWrapper, ApiExampleList.class); for (ApiExample webRequest : values.getExamples()) { String uri = webRequest.getRequestUri(); String context = uri; int index = uri.indexOf('/', 1); if (index > ZERO) { context = uri.substring(0, index); } List<ApiExample> requests = apiRequestsByURI.get(context); if (requests == null) { requests = new ArrayList<ApiExample>(); apiRequestsByURI.put(context, requests); } requests.add(webRequest); Collections.sort(requests); } } catch (IOException e) { LOGGER.error("", e); } } } @RequestMapping(value = "/api", produces = MediaType.TEXT_HTML_VALUE) public ModelAndView apidoc() { parseExamples(); LOGGER.info("GET /api/"); return new ModelAndView("api/index", "apiRequestsByURI", apiRequestsByURI); } @RequestMapping(value = "/api/{context}", produces = MediaType.TEXT_HTML_VALUE) public ModelAndView apispecific(@PathVariable("context") final String context) { parseExamples(); LOGGER.info("GET /api/context" + context); return new ModelAndView("api/api_context", "examples", apiRequestsByURI.get("/" + context)); } }