Java Code Examples for io.swagger.models.Swagger#setHost()

The following examples show how to use io.swagger.models.Swagger#setHost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAdaCodegen.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
    objs.put("orderedModels", orderedModels);
    Swagger swagger = (Swagger)objs.get("swagger");
    if(swagger != null) {
        String host = swagger.getBasePath();
        try {
            swagger.setHost("SWAGGER_HOST");
            objs.put("swagger-json", Json.pretty().writeValueAsString(swagger).replace("\r\n", "\n"));
        } catch (JsonProcessingException e) {
            LOGGER.error(e.getMessage(), e);
        }
        swagger.setHost(host);
    }

    /**
     * Collect the scopes to generate unique identifiers for each of them.
     */
    List<CodegenSecurity> authMethods = (List<CodegenSecurity>) objs.get("authMethods");
    postProcessAuthMethod(authMethods);

    return super.postProcessSupportingFileData(objs);
}
 
Example 2
Source File: SwaggerDefinitionProcessor.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void process(SwaggerGenerator swaggerGenerator, SwaggerDefinition definitionAnnotation) {
  Swagger swagger = swaggerGenerator.getSwagger();

  if (StringUtils.isNotEmpty(definitionAnnotation.basePath())) {
    swaggerGenerator.setBasePath(definitionAnnotation.basePath());
  }
  if (StringUtils.isNotEmpty(definitionAnnotation.host())) {
    swagger.setHost(definitionAnnotation.host());
  }

  SwaggerUtils.setConsumes(swagger, definitionAnnotation.consumes());
  SwaggerUtils.setProduces(swagger, definitionAnnotation.produces());
  convertSchemes(definitionAnnotation, swagger);
  convertTags(definitionAnnotation, swagger);
  convertInfo(definitionAnnotation.info(), swagger);
  swagger.setExternalDocs(convertExternalDocs(definitionAnnotation.externalDocs()));
}
 
Example 3
Source File: DocumentController.java    From sc-generator with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/swaggerDocProxy/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Swagger swaggerDocProxy(@PathVariable(value = "id") Integer id,
                               @RequestParam(value = "trimSystemController", defaultValue = "true") boolean trimSystemController,
                               HttpServletRequest request) {
    Document document = documentService.findOne(id);
    if (document != null) {
        Swagger swagger = new SwaggerParser()
                .parse(document.getContent());
        if (trimSystemController) {
            swagger = Generator.convertSwagger(swagger);
        }
        swagger.setHost(request.getServerName() + ":" + request.getServerPort() + prefix + "/" + id);
        return swagger;
    }
    return null;
}
 
Example 4
Source File: EntrypointsSwaggerV2Transformer.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
@Override
public void transform(SwaggerV2Descriptor descriptor) {
    if (asBoolean(SwaggerProperties.ENTRYPOINTS_AS_SERVERS) && entrypoints != null && ! entrypoints.isEmpty()) {
        Swagger swagger = descriptor.getSpecification();

        // Swagger vs2 supports only a single server
        ApiEntrypointEntity first = entrypoints.iterator().next();
        URI target = URI.create(first.getTarget());
        swagger.setSchemes(Collections.singletonList(Scheme.forValue(target.getScheme())));
        swagger.setHost(target.getHost());

        if (getProperty(SwaggerProperties.ENTRYPOINT_AS_BASEPATH) == null
                || getProperty(SwaggerProperties.ENTRYPOINT_AS_BASEPATH).isEmpty()
                || asBoolean(SwaggerProperties.ENTRYPOINT_AS_BASEPATH)) {
            swagger.setBasePath(target.getPath());
        }
    }
}
 
Example 5
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Update OAS definition with GW endpoints and API information
 *
 * @param swagger        Swagger
 * @param basePath       API context
 * @param transports     transports types
 * @param hostsWithSchemes GW hosts with protocol mapping
 */
private void updateEndpoints(Swagger swagger, String basePath, String transports,
                             Map<String, String> hostsWithSchemes) {

    String host = StringUtils.EMPTY;
    String[] apiTransports = transports.split(",");
    List<Scheme> schemes = new ArrayList<>();
    if (ArrayUtils.contains(apiTransports, APIConstants.HTTPS_PROTOCOL)
            && hostsWithSchemes.get(APIConstants.HTTPS_PROTOCOL) != null) {
        schemes.add(Scheme.HTTPS);
        host = hostsWithSchemes.get(APIConstants.HTTPS_PROTOCOL).trim()
                .replace(APIConstants.HTTPS_PROTOCOL_URL_PREFIX, "");
    }
    if (ArrayUtils.contains(apiTransports, APIConstants.HTTP_PROTOCOL)
            && hostsWithSchemes.get(APIConstants.HTTP_PROTOCOL) != null) {
        schemes.add(Scheme.HTTP);
        if (StringUtils.isEmpty(host)) {
            host = hostsWithSchemes.get(APIConstants.HTTP_PROTOCOL).trim()
                    .replace(APIConstants.HTTP_PROTOCOL_URL_PREFIX, "");
        }
    }
    swagger.setSchemes(schemes);
    swagger.setBasePath(basePath);
    swagger.setHost(host);
}
 
Example 6
Source File: Java2SwaggerMojo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void configureSwagger() {
    swagger = new Swagger();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    Info info = new Info();
    Contact swaggerContact = new Contact();
    License swaggerLicense = new License();
    swaggerLicense.name(this.license)
        .url(this.licenseUrl);
    swaggerContact.name(this.contact);
    info.version(this.version)
        .description(this.description)
        .contact(swaggerContact)
        .license(swaggerLicense)
        .title(this.title);
    swagger.setInfo(info);
    if (this.schemes != null) {
        for (String scheme : this.schemes) {
            swagger.scheme(Scheme.forValue(scheme));
        }
    }
    swagger.setHost(this.host);
    swagger.setBasePath(this.basePath);
}
 
Example 7
Source File: SwaggerUtils.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method will create the info section of the swagger document.
 *
 * @param dataServiceName name of the data-service.
 * @param transports      enabled transports.
 * @param serverConfig    Server config object.
 * @param swaggerDoc      Swagger document object.
 * @throws AxisFault Exception occured while getting the host address from transports.
 */
private static void addSwaggerInfoSection(String dataServiceName, List<String> transports,
                                          MIServerConfig serverConfig, Swagger swaggerDoc) throws AxisFault {

    swaggerDoc.basePath("/" + SwaggerProcessorConstants.SERVICES_PREFIX + "/" + dataServiceName);

    if (transports.contains("https")) {
        swaggerDoc.addScheme(Scheme.HTTPS);
        swaggerDoc.addScheme(Scheme.HTTP);
        swaggerDoc.setHost(serverConfig.getHost("https"));
    } else {
        swaggerDoc.addScheme(Scheme.HTTP);
        swaggerDoc.setHost(serverConfig.getHost("http"));
    }

    Info info = new Info();
    info.title(dataServiceName);
    info.setVersion("1.0");
    info.description("API Definition of dataservice : " + dataServiceName);
    swaggerDoc.setInfo(info);

    swaggerDoc.addConsumes("application/json");
    swaggerDoc.addConsumes("application/xml");

    swaggerDoc.addProduces("application/json");
    swaggerDoc.addProduces("application/xml");
}
 
Example 8
Source File: OpenAPIV2Generator.java    From spring-openapi with MIT License 5 votes vote down vote up
public Swagger generate(OpenApiV2GeneratorConfig config) {
	logger.info("Starting OpenAPI v2 generation");
	environment = config.getEnvironment();
	Swagger openAPI = new Swagger();
	openAPI.setDefinitions(createDefinitions());
	openAPI.setPaths(createPaths(config));
	openAPI.setInfo(info);
	openAPI.setBasePath(config.getBasePath());
	openAPI.setHost(config.getHost());
	logger.info("OpenAPI v2 generation done!");
	return openAPI;
}
 
Example 9
Source File: ApiDocV2Service.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Updates scheme and hostname, and adds API doc link to Swagger
 *
 * @param swagger   the API doc
 * @param serviceId the unique service id
 * @param hidden    do not add link for automatically generated API doc
 */
private void updateSchemeHostAndLink(Swagger swagger, String serviceId, boolean hidden) {
    GatewayConfigProperties gatewayConfigProperties = gatewayClient.getGatewayConfigProperties();
    String swaggerLink = OpenApiUtil.getOpenApiLink(serviceId, gatewayConfigProperties);
    log.debug("Updating host for service with id: " + serviceId + " to: " + gatewayConfigProperties.getHostname());
    swagger.setSchemes(Collections.singletonList(Scheme.forValue(gatewayConfigProperties.getScheme())));
    swagger.setHost(gatewayConfigProperties.getHostname());
    if (!hidden) {
        swagger.getInfo().setDescription(swagger.getInfo().getDescription() + swaggerLink);
    }
}
 
Example 10
Source File: JbootSwaggerManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
public void init() {
    if (!config.isConfigOk()) {
        return;
    }

    swagger = new Swagger();
    swagger.setHost(config.getHost());
    swagger.setBasePath("/");
    swagger.addScheme(HTTP);
    swagger.addScheme(HTTPS);


    Info swaggerInfo = new Info();
    swaggerInfo.setDescription(config.getDescription());
    swaggerInfo.setVersion(config.getVersion());
    swaggerInfo.setTitle(config.getTitle());
    swaggerInfo.setTermsOfService(config.getTermsOfService());

    Contact contact = new Contact();
    contact.setName(config.getContactName());
    contact.setEmail(config.getContactEmail());
    contact.setUrl(config.getContactUrl());
    swaggerInfo.setContact(contact);

    License license = new License();
    license.setName(config.getLicenseName());
    license.setUrl(config.getLicenseUrl());
    swaggerInfo.setLicense(license);


    swagger.setInfo(swaggerInfo);

    List<Class> classes = ClassScanner.scanClassByAnnotation(RequestMapping.class, false);

    Reader.read(swagger, classes);

}
 
Example 11
Source File: MySwaggerModelExtension.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public void apply(Swagger swagger) {
    swagger.setHost("newHostName"); //<1>
    swagger.basePath("newBasePath");

    Map<String, Path> paths = swagger.getPaths(); //<2>
    paths.remove("/remove");
    swagger.setPaths(paths);
}
 
Example 12
Source File: MSF4JBeanConfig.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@Override
public void scanAndRead() {
    Swagger swagger = reader.read(classes());
    if (StringUtils.isNotBlank(getHost())) {
        swagger.setHost(getHost());
    }

    if (StringUtils.isNotBlank(getBasePath())) {
        swagger.setBasePath(getBasePath());
    }

    updateInfoFromConfig();
}
 
Example 13
Source File: AbstractDocumentSource.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
public AbstractDocumentSource(Log log, ApiSource apiSource, String encoding) throws MojoFailureException {
    LOG = log;
    this.outputPath = apiSource.getOutputPath();
    this.templatePath = apiSource.getTemplatePath();
    this.swaggerPath = apiSource.getSwaggerDirectory();
    this.modelSubstitute = apiSource.getModelSubstitute();
    this.jsonExampleValues = apiSource.isJsonExampleValues();

    swagger = new Swagger();
    if (apiSource.getSchemes() != null) {
        for (String scheme : apiSource.getSchemes()) {
            swagger.scheme(Scheme.forValue(scheme));
        }
    }

    // read description from file
    if (apiSource.getDescriptionFile() != null) {
        InputStream is = null;
        try {
            is = new FileInputStream(apiSource.getDescriptionFile());
            apiSource.getInfo().setDescription(IOUtils.toString(is));
        } catch (IOException e) {
            throw new MojoFailureException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    swagger.setHost(apiSource.getHost());
    swagger.setInfo(apiSource.getInfo());
    swagger.setBasePath(apiSource.getBasePath());
    swagger.setExternalDocs(apiSource.getExternalDocs());

    this.apiSource = apiSource;
    if (encoding != null) {
        this.encoding = encoding;
    }
}