Skip to content
Permalink
Browse files
static document basic support
  • Loading branch information
rmannibucau committed Jun 24, 2018
1 parent 88ab0ee commit 4b29b90b38c00d5362cbaa7bf8a3d7783bf013f0
Showing 4 changed files with 89 additions and 47 deletions.
@@ -21,15 +21,15 @@

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.ext.JAXRSServerFactoryCustomizationExtension;
import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIEndpoint;
import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIFilter;

@Vetoed
public class CxfForceSetup implements JAXRSServerFactoryCustomizationExtension {
@Override
public void customize(final JAXRSServerFactoryBean bean) {
if (bean.getProviders().stream().anyMatch(OpenAPIEndpoint.class::isInstance)) { // default app, nothing to do
if (bean.getProviders().stream().anyMatch(OpenAPIFilter.class::isInstance)) { // default app, nothing to do
return;
}
bean.setProvider(CDI.current().select(OpenAPIEndpoint.class).get());
bean.setProvider(CDI.current().select(OpenAPIFilter.class).get());
}
}
@@ -96,6 +96,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
@@ -174,13 +175,19 @@ public Schema.SchemaType deserialize(final JsonParser p, final DeserializationCo
});

final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.registerModule(module);
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
protected boolean _isIgnorable(final Annotated a) {
return super._isIgnorable(a) || a.getAnnotation(JsonbTransient.class) != null;
}
});
mapper.registerModule(module);
mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PropertyNamingStrategyBase() {
@Override
public String translate(final String propertyName) {
return "ref".equals(propertyName) ? "$ref" : propertyName;
}
});
return mapper.readValue(stream, OpenAPI.class);
} catch (final IOException e) {
throw new IllegalArgumentException(e);
@@ -16,54 +16,20 @@
*/
package org.apache.geronimo.microprofile.openapi.jaxrs;

import static java.util.Optional.ofNullable;
import static javax.ws.rs.Priorities.USER;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import org.apache.geronimo.microprofile.openapi.cdi.GeronimoOpenAPIExtension;
import org.eclipse.microprofile.openapi.models.OpenAPI;

// theorically a jaxrs endpoint to benefit from jaxrs tooling and filters - but forbidden by TCK :(
// @Path("openapi") + @GET
@Provider
@PreMatching
@Priority(USER)
// just here to ensure an app is deployed (depends the container scanning/config otherwise)
@Path("openapi")
@ApplicationScoped
public class OpenAPIEndpoint implements ContainerRequestFilter {

@Inject
private GeronimoOpenAPIExtension extension;

private OpenAPI openApi;

@Override
public void filter(final ContainerRequestContext rc) {
if (!HttpMethod.GET.equals(rc.getRequest().getMethod())) {
return;
}
final String path = rc.getUriInfo().getPath();
if ("openapi".equals(path)) {
rc.abortWith(Response.ok(openApi).type(ofNullable(rc.getMediaType()).orElse(APPLICATION_JSON_TYPE)).build());
}
if ("openapi.json".equals(path)) {
rc.abortWith(Response.ok(openApi).type(APPLICATION_JSON_TYPE).build());
}
}

@Context
public void setApplication(final Application application) {
this.openApi = extension.getOrCreateOpenAPI(application);
public class OpenAPIEndpoint {
@GET
public OpenAPI get() { // filter should handle it
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
}
@@ -0,0 +1,69 @@
/*
* 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.apache.geronimo.microprofile.openapi.jaxrs;

import static java.util.Optional.ofNullable;
import static javax.ws.rs.Priorities.USER;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import org.apache.geronimo.microprofile.openapi.cdi.GeronimoOpenAPIExtension;
import org.eclipse.microprofile.openapi.models.OpenAPI;

// theorically a jaxrs endpoint to benefit from jaxrs tooling and filters - but forbidden by TCK :(
// @Path("openapi") + @GET
@Provider
@PreMatching
@Priority(USER)
@ApplicationScoped
public class OpenAPIFilter implements ContainerRequestFilter {

@Inject
private GeronimoOpenAPIExtension extension;

private OpenAPI openApi;

@Override
public void filter(final ContainerRequestContext rc) {
if (!HttpMethod.GET.equals(rc.getRequest().getMethod())) {
return;
}
final String path = rc.getUriInfo().getPath();
if ("openapi".equals(path)) {
rc.abortWith(Response.ok(openApi).type(ofNullable(rc.getMediaType()).orElse(APPLICATION_JSON_TYPE)).build());
}
if ("openapi.json".equals(path)) {
rc.abortWith(Response.ok(openApi).type(APPLICATION_JSON_TYPE).build());
}
}

@Context
public void setApplication(final Application application) {
this.openApi = extension.getOrCreateOpenAPI(application);
}
}

0 comments on commit 4b29b90

Please sign in to comment.