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

The following examples show how to use io.swagger.models.Swagger#setSchemes() . 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: 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 2
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 3
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 4
Source File: SwaggerFactory.java    From dorado with Apache License 2.0 4 votes vote down vote up
public static Swagger getSwagger() {
	if (!swaggerEnable)
		return new Swagger();

	if (swagger != null)
		return swagger;

	Reader reader = new Reader(new Swagger());

	String[] packages = null;
	Class<?> mainClass = Dorado.mainClass;
	EnableSwagger enableSwagger = mainClass.getAnnotation(EnableSwagger.class);

	if (enableSwagger != null) {
		packages = enableSwagger.value();
	}

	if (packages == null || packages.length == 0) {
		packages = Dorado.serverConfig.scanPackages();
	}

	if (packages == null || packages.length == 0) {
		packages = new String[] { mainClass.getPackage().getName() };
	}

	if (packages == null || packages.length == 0) {
		throw new IllegalArgumentException("缺少scanPackages设置");
	}

	Set<Class<?>> classes = new HashSet<>();
	for (String pkg : packages) {
		try {
			classes.addAll(PackageScanner.scan(pkg));
		} catch (Exception ex) {
			// ignore this ex
		}
	}

	Swagger _swagger = reader.read(classes);
	_swagger.setSchemes(Arrays.asList(Scheme.HTTP, Scheme.HTTPS));

	ApiKey apiKey = apiContext.getApiKey();
	if (apiKey != null) {
		ApiKeyAuthDefinition apiKeyAuth = new ApiKeyAuthDefinition(apiKey.getName(),
				In.forValue(apiKey.getIn() == null ? "header" : apiKey.getIn()));
		_swagger.securityDefinition("auth", apiKeyAuth);

		List<SecurityRequirement> securityRequirements = new ArrayList<>();
		SecurityRequirement sr = new SecurityRequirement();
		sr.requirement("auth");
		securityRequirements.add(sr);
		_swagger.setSecurity(securityRequirements);
	}
	if (apiContext.getInfo() != null)
		_swagger.setInfo(apiContext.getInfo());

	swagger = _swagger;
	return _swagger;
}
 
Example 5
Source File: SwaggerGenMojo.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Gets a new Swagger metadata.
 *
 * @return the Swagger metadata.
 * @throws MojoExecutionException if any problems were encountered.
 */
private Swagger getSwagger() throws MojoExecutionException
{
    getLog().debug("Creating Swagger Metadata");
    // Set up initial Swagger metadata.
    Swagger swagger = new Swagger();
    swagger.setInfo(new Info().title(title).version(version));
    swagger.setBasePath(basePath);

    // Set the schemes.
    if (!CollectionUtils.isEmpty(schemeParameters))
    {
        List<Scheme> schemes = new ArrayList<>();
        for (String schemeParameter : schemeParameters)
        {
            Scheme scheme = Scheme.forValue(schemeParameter);
            if (scheme == null)
            {
                throw new MojoExecutionException("Invalid scheme specified: " + schemeParameter);
            }
            schemes.add(scheme);
        }
        swagger.setSchemes(schemes);
    }

    // Add authorization support if specified.
    if (authType != null)
    {
        // Find the definition for the user specified type.
        SecuritySchemeDefinition securitySchemeDefinition = null;
        for (SecuritySchemeDefinition possibleDefinition : SECURITY_SCHEME_DEFINITIONS)
        {
            if (possibleDefinition.getType().equalsIgnoreCase(authType))
            {
                securitySchemeDefinition = possibleDefinition;
                break;
            }
        }

        // If we found a match, set it on the swagger object.
        if (securitySchemeDefinition != null)
        {
            // Come up with an authentication name for easy identification (e.g. basicAuthentication, etc.).
            String securityName = securitySchemeDefinition.getType() + "Authentication";

            // Add the security definition.
            swagger.addSecurityDefinition(securityName, securitySchemeDefinition);

            // Add the security for everything based on the name of the definition.
            SecurityRequirement securityRequirement = new SecurityRequirement();
            securityRequirement.requirement(securityName);
            swagger.addSecurity(securityRequirement);
        }
    }

    // Use default paths and definitions.
    swagger.setPaths(new TreeMap<>());
    swagger.setDefinitions(new TreeMap<>());
    return swagger;
}