Skip to content

Commit

Permalink
feat(#TOMEE-4268): MicroProfile OpenAPI Reader example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanouii authored and jgallimore committed Nov 8, 2023
1 parent f0b8eec commit 464d7e0
Show file tree
Hide file tree
Showing 17 changed files with 938 additions and 0 deletions.
127 changes: 127 additions & 0 deletions examples/mp-openapi-reader/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.superbiz</groupId>
<artifactId>mp-openapi-reader</artifactId>
<version>10.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>TomEE :: Examples :: Microprofile OpenAPI Reader</name>
<properties>
<version.jakartaee-api>9.1.1</version.jakartaee-api>
<version.arquillian.bom>1.7.0.Final</version.arquillian.bom>
<junit.version>4.13.2</junit.version>
<tomee.version>10.0.0-SNAPSHOT</tomee.version>
<version.microprofile.impl.openapi>3.0.1</version.microprofile.impl.openapi>
<version.openapi-api>3.1.1</version.openapi-api>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>jakartaee-api</artifactId>
<version>${version.jakartaee-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.openapi</groupId>
<artifactId>microprofile-openapi-api</artifactId>
<version>${version.openapi-api}</version>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api</artifactId>
<version>${version.microprofile.impl.openapi}</version>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api-core</artifactId>
<version>${version.microprofile.impl.openapi}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>openejb-cxf-rs</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>${version.arquillian.bom}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>arquillian-tomee-remote</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>apache-tomee</artifactId>
<version>${tomee.version}</version>
<type>zip</type>
<classifier>microprofile</classifier>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>${tomee.version}</version>
<configuration>
<tomeeClassifier>microprofile</tomeeClassifier>
<context>${project.artifactId}</context>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>
<!-- This section allows you to configure where to publish libraries for
sharing. It is not required and may be deleted. For more information see:
http://maven.apache.org/plugins/maven-deploy-plugin/ -->
<distributionManagement>
<repository>
<id>localhost</id>
<url>file://${basedir}/target/repo/</url>
</repository>
<snapshotRepository>
<id>localhost</id>
<url>file://${basedir}/target/snapshot-repo/</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.superbiz.microprofile.openapi;

import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.OpenApiConfigImpl;
import io.smallrye.openapi.api.OpenApiDocument;
import io.smallrye.openapi.runtime.OpenApiProcessor;
import io.smallrye.openapi.runtime.OpenApiStaticFile;
import io.smallrye.openapi.runtime.io.Format;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.OASModelReader;
import org.eclipse.microprofile.openapi.models.OpenAPI;

import java.net.URL;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

import static io.smallrye.openapi.runtime.io.Format.JSON;
import static io.smallrye.openapi.runtime.io.Format.YAML;

public class CustomReader implements OASModelReader, ServletContainerInitializer {

private static final Logger LOGGER = Logger.getLogger(CustomReader.class.getName());

private static Optional<OpenAPI> openAPI;

@Override
public OpenAPI buildModel() {
return openAPI.orElseThrow(() -> new IllegalStateException("OpenAPI document not available."));
}

@Override
public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
try {
final OpenApiConfig openApiConfig = new OpenApiConfigImpl(ConfigProvider.getConfig());;
final Optional<OpenAPI> yaml = readOpenApiFile(ctx, "/WEB-INF/classes/openapi/openapi.yaml", YAML);
final Optional<OpenAPI> json = readOpenApiFile(ctx, "/WEB-INF/classes/my-openapi.json", JSON);

final OpenApiDocument document = OpenApiDocument.INSTANCE;
try {
document.reset();
document.config(openApiConfig);
yaml.ifPresent(document::modelFromStaticFile);
json.ifPresent(document::modelFromStaticFile);
document.initialize();

openAPI = Optional.ofNullable(document.get());

} finally {
document.reset();
}

} catch (final Exception e) {
throw new RuntimeException(e);
}
}

private static Optional<OpenAPI> readOpenApiFile(
final ServletContext servletContext, final String location,
final Format format) throws Exception {

final URL resource = servletContext.getResource(location);
if (resource == null) {
LOGGER.fine("Could not find static OpenAPI file " + location);
return Optional.empty();
}

LOGGER.fine("Found static OpenAPI file " + location);

try (OpenApiStaticFile staticFile = new OpenApiStaticFile(resource.openStream(), format)) {
return Optional.of(OpenApiProcessor.modelFromStaticFile(staticFile));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.superbiz.microprofile.openapi; /**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/weather")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class WeatherService {

@Path("/day/status")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String dayStatus() {
return "Hi, today is a sunny day!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.superbiz.microprofile.openapi.moviefun;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement(name = "movie")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String director;
private String title;
private int year;
private String genre;
private int rating;

public Movie() {
}

public Movie(String title, String director, String genre, int rating, int year) {
this.director = director;
this.title = title;
this.year = year;
this.genre = genre;
this.rating = rating;
}

public Movie(String director, String title, int year) {
this.director = director;
this.title = title;
this.year = year;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getGenre() {
return genre;
}

public void setGenre(String genre) {
this.genre = genre;
}

public int getRating() {
return rating;
}

public void setRating(int rating) {
this.rating = rating;
}
}
Loading

0 comments on commit 464d7e0

Please sign in to comment.