All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mangofactory.swagger.controllers.DefaultSwaggerController Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.mangofactory.swagger.controllers;

import com.mangofactory.swagger.annotations.ApiIgnore;
import com.mangofactory.swagger.core.SwaggerCache;
import com.mangofactory.swagger.models.dto.ApiListing;
import com.mangofactory.swagger.models.dto.ResourceListing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class DefaultSwaggerController {

  public static final String DOCUMENTATION_BASE_PATH = "/api-docs";

  @Autowired
  private SwaggerCache swaggerCache;

  @ApiIgnore
  @RequestMapping(value = {DOCUMENTATION_BASE_PATH}, method = RequestMethod.GET)
  public
  @ResponseBody
  ResponseEntity getResourceListing(
      @RequestParam(value = "group",  required = false) String swaggerGroup) {

    return getSwaggerResourceListing(swaggerGroup);
  }

  @ApiIgnore
  @RequestMapping(value = {DOCUMENTATION_BASE_PATH + "/{swaggerGroup}/{apiDeclaration}"}, method = RequestMethod.GET)
  public
  @ResponseBody
  ResponseEntity getApiListing(@PathVariable String swaggerGroup, @PathVariable String apiDeclaration) {
    return getSwaggerApiListing(swaggerGroup, apiDeclaration);
  }

  private ResponseEntity getSwaggerApiListing(String swaggerGroup, String apiDeclaration) {
    ResponseEntity responseEntity = new ResponseEntity(HttpStatus.NOT_FOUND);
    Map apiListingMap = swaggerCache.getSwaggerApiListingMap().get(swaggerGroup);
    if (null != apiListingMap) {
      ApiListing apiListing = apiListingMap.get(apiDeclaration);
      if (null != apiListing) {
        responseEntity = new ResponseEntity(apiListing, HttpStatus.OK);
      }
    }
    return responseEntity;
  }

  private ResponseEntity getSwaggerResourceListing(String swaggerGroup) {
    ResponseEntity responseEntity = new ResponseEntity(HttpStatus.NOT_FOUND);
    ResourceListing resourceListing = null;

    if (null == swaggerGroup) {
      resourceListing = swaggerCache.getSwaggerApiResourceListingMap().values().iterator().next();
    } else {
      if (swaggerCache.getSwaggerApiResourceListingMap().containsKey(swaggerGroup)) {
        resourceListing = swaggerCache.getSwaggerApiResourceListingMap().get(swaggerGroup);
      }
    }
    if (null != resourceListing) {
      responseEntity = new ResponseEntity(resourceListing, HttpStatus.OK);
    }
    return responseEntity;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy