Search in sources :

Example 86 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project carbon-apimgt by wso2.

the class RestApiCommonUtil method generateOpenAPIForAsync.

/**
 * Generate a basic OpenAPI definition with given details.
 *
 * @param name             name of the API.
 * @param version          version of the API.
 * @param context          context of the API.
 * @param callbackEndpoint callback URL of the async API.
 * @return OpenAPI definition as String.
 * @throws JsonProcessingException Error occurred while generating the OpenAPI.
 */
public static String generateOpenAPIForAsync(String name, String version, String context, String callbackEndpoint) throws JsonProcessingException {
    OpenAPI openAPI = new OpenAPI();
    Info info = new Info();
    info.setTitle(name);
    info.setDescription("API Definition of " + name);
    info.setVersion(version);
    openAPI.setInfo(info);
    ArrayList<Server> servers = new ArrayList<>();
    Server server = new Server();
    server.setUrl("/");
    servers.add(server);
    openAPI.setServers(Arrays.asList(server));
    Paths paths = new Paths();
    PathItem pathItem = new PathItem();
    Operation operation = new Operation();
    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setDescription("Default response");
    apiResponses.addApiResponse("default", apiResponse);
    operation.setResponses(apiResponses);
    pathItem.setPost(operation);
    paths.addPathItem("/*", pathItem);
    openAPI.paths(paths);
    List<String> urls = new ArrayList<>();
    urls.add(callbackEndpoint);
    Map<String, Object> tempMap = new HashMap();
    tempMap.put("type", "http");
    tempMap.put("urls", urls);
    openAPI.addExtension(X_WSO2_PRODUCTION_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_SANDBOX_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_AUTH_HEADER, "Authorization");
    openAPI.addExtension(X_WSO2_BASEPATH, context + "/" + version);
    openAPI.addExtension(X_WSO2_DISABLE_SECURITY, true);
    return Json.mapper().writeValueAsString(openAPI);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 87 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project atlasmap by atlasmap.

the class JavaService method inspectClass.

/**
 * Inspects a Java Class with specified fully qualified class name and return a Document object.
 * @param requestIn request
 * @return {@link ClassInspectionResponse}
 */
@POST
@Path("/class")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Inspect Class", description = "Inspect a Java Class with specified fully qualified class name and return a Document object")
@RequestBody(description = "ClassInspectionRequest object", content = @Content(schema = @Schema(implementation = ClassInspectionRequest.class)))
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = ClassInspectionResponse.class)), description = "Return a Document object represented by JavaClass"))
public Response inspectClass(InputStream requestIn) {
    ClassInspectionRequest request = fromJson(requestIn, ClassInspectionRequest.class);
    ClassInspectionResponse response = new ClassInspectionResponse();
    ClassInspectionService classInspectionService = new ClassInspectionService();
    classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
    configureInspectionService(classInspectionService, request);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Class inspection request: {}", new String(toJson(request)));
    }
    long startTime = System.currentTimeMillis();
    try {
        JavaClass c = null;
        if (request.getClasspath() == null || request.getClasspath().isEmpty()) {
            AtlasService atlasService = resourceContext.getResource(AtlasService.class);
            c = classInspectionService.inspectClass(atlasService.getLibraryLoader(), request.getClassName(), request.getCollectionType(), request.getCollectionClassName());
        } else {
            c = classInspectionService.inspectClass(request.getClassName(), request.getCollectionType(), request.getClasspath());
        }
        response.setJavaClass(c);
    } catch (Throwable e) {
        String msg = String.format("Error inspecting class %s - %s: %s", request.getClassName(), e.getClass().getName(), e.getMessage());
        LOG.error(msg, e);
        response.setErrorMessage(msg);
    } finally {
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Class inspection response: {}", new String(toJson(response)));
    }
    return Response.ok().entity(toJson(response)).build();
}
Also used : ClassInspectionResponse(io.atlasmap.java.v2.ClassInspectionResponse) AtlasService(io.atlasmap.service.AtlasService) JavaClass(io.atlasmap.java.v2.JavaClass) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) ClassInspectionRequest(io.atlasmap.java.v2.ClassInspectionRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 88 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project atlasmap by atlasmap.

the class AtlasService method validateMappingRequest.

/**
 * Validates the mapping file.
 * @param mapping mapping
 * @param mappingDefinitionId mapping definition ID
 * @param uriInfo URI info
 * @return {@link Validations} validation result
 */
@PUT
@Path("/mapping/validate/{mappingDefinitionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Validate Mapping", description = "Validate mapping file")
@RequestBody(description = "Mapping file content", content = @Content(schema = @Schema(implementation = AtlasMapping.class)))
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Validations.class)), description = "Return a validation result"))
public Response validateMappingRequest(InputStream mapping, @Parameter(description = "Mapping ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId, @Context UriInfo uriInfo) {
    try {
        AtlasMapping atlasMapping = fromJson(mapping, AtlasMapping.class);
        LOG.debug("Validate mappings: {}", atlasMapping.getName());
        return validateMapping(mappingDefinitionId, atlasMapping, uriInfo);
    } catch (AtlasException | IOException e) {
        throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 89 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project atlasmap by atlasmap.

the class AtlasService method getMappingRequest.

/**
 * Retrieve a mapping file saved on the server.
 * @param mappingFormat file type
 * @param mappingDefinitionId mapping definition ID
 * @return file
 */
@GET
@Path("/mapping/{mappingFormat}/{mappingDefinitionId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM })
@Operation(summary = "Get Mapping", description = "Retrieve a mapping file saved on the server")
@ApiResponses({ @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = AtlasMapping.class)), description = "Return a mapping file content"), @ApiResponse(responseCode = "204", description = "Mapping file was not found"), @ApiResponse(responseCode = "500", description = "Mapping file access error") })
public Response getMappingRequest(@Parameter(description = "Mapping Format") @PathParam("mappingFormat") MappingFileType mappingFormat, @Parameter(description = "Mapping ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId) {
    LOG.debug("getMappingRequest: {} '{}'", mappingFormat, mappingDefinitionId);
    ADMArchiveHandler admHandler = loadExplodedMappingDirectory(mappingDefinitionId);
    switch(mappingFormat) {
        case JSON:
            byte[] serialized = null;
            try {
                serialized = admHandler.getMappingDefinitionBytes();
            } catch (Exception e) {
                LOG.error("Error retrieving mapping definition file for ID:" + mappingDefinitionId, e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
            if (LOG.isDebugEnabled() && serialized != null) {
                LOG.debug(new String(serialized));
            }
            if (serialized == null) {
                LOG.debug("Mapping definition not found for ID:{}", mappingDefinitionId);
                return Response.noContent().build();
            }
            return Response.ok().entity(serialized).build();
        case GZ:
            try {
                if (admHandler.getGzippedADMDigestBytes() == null) {
                    LOG.debug("ADM Digest file not found for ID:{}", mappingDefinitionId);
                    return Response.noContent().build();
                }
                return Response.ok().entity(admHandler.getGzippedADMDigestBytes()).build();
            } catch (Exception e) {
                LOG.error("Error getting compressed ADM digest file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
        case ZIP:
            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                admHandler.setIgnoreLibrary(false);
                admHandler.setLibraryDirectory(Paths.get(this.libFolder));
                admHandler.export(out);
                return Response.ok().entity(out.toByteArray()).build();
            } catch (Exception e) {
                LOG.error("Error getting ADM archive file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
        default:
            throw new WebApplicationException("Unrecognized mapping format: " + mappingFormat, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasException(io.atlasmap.api.AtlasException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 90 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project atlasmap by atlasmap.

the class AtlasService method resetAllMappings.

/**
 * Removes all mapping files and catalogs saved on the server.
 * @return empty response
 */
@DELETE
@Path("/mapping/RESET/ALL")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Remove All Mappings", description = "Remove all mapping files and catalogs saved on the server")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "All mapping files were removed successfully"), @ApiResponse(responseCode = "204", description = "Unable to remove all mapping files") })
public Response resetAllMappings() {
    LOG.debug("resetAllMappings");
    java.nio.file.Path mappingFolderPath = Paths.get(mappingFolder);
    File mappingFolderPathFile = mappingFolderPath.toFile();
    if (mappingFolderPathFile == null || !mappingFolderPathFile.exists()) {
        return Response.ok().build();
    }
    AtlasUtil.deleteDirectoryContents(mappingFolderPathFile);
    return Response.ok().build();
}
Also used : File(java.io.File) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)113 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)99 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)85 Test (org.testng.annotations.Test)53 Operation (io.swagger.v3.oas.models.Operation)51 OpenAPI (io.swagger.v3.oas.models.OpenAPI)48 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)48 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)44 PathItem (io.swagger.v3.oas.models.PathItem)41 Schema (io.swagger.v3.oas.models.media.Schema)38 MediaType (io.swagger.v3.oas.models.media.MediaType)31 Path (javax.ws.rs.Path)30 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)29 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)27 Content (io.swagger.v3.oas.models.media.Content)26 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)25 ArrayList (java.util.ArrayList)25 Components (io.swagger.v3.oas.models.Components)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)19