提交 5d0a518c 编写于 作者: Z zhourui

增加openapi

上级 c08a2429
......@@ -379,6 +379,18 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
......@@ -1077,6 +1089,21 @@
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
......
......@@ -13,7 +13,7 @@ import com.x.base.core.project.jaxrs.WrapClearCacheRequest;
public interface Cache {
public static final String TYPE_EHCACHE = "ehcache";
// public static final String TYPE_EHCACHE = "ehcache";
public static final String TYPE_REDIS = "redis";
public static final String TYPE_GUAVA = "guava";
......
package com.x.base.core.project.cache;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.BooleanUtils;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.jaxrs.WrapClearCacheRequest;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.management.ManagementService;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
public class CacheEhcacheImpl implements Cache {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheEhcacheImpl.class);
private CacheManager cacheManager;
private CacheEhcacheNotifyReceiveQueue notifyReceiveQueue;
public CacheEhcacheImpl(String application) throws Exception {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.maxEntriesLocalHeap(1000);
cacheConfiguration.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
cacheConfiguration.timeToIdleSeconds(1800);
cacheConfiguration.timeToLiveSeconds(3600);
cacheConfiguration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU);
Configuration configuration = new Configuration();
configuration.setDefaultCacheConfiguration(cacheConfiguration);
configuration.setName("CacheEhcacheManager-" + application);
this.cacheManager = new CacheManager(configuration);
this.notifyReceiveQueue = new CacheEhcacheNotifyReceiveQueue(this.cacheManager);
notifyReceiveQueue.start();
if (BooleanUtils.isTrue(Config.cache().getEhcache().getJmxEnable())) {
ManagementService.registerMBeans(cacheManager, ManagementFactory.getPlatformMBeanServer(), true, true, true,
true);
}
}
@Override
public Optional<Object> get(CacheCategory category, CacheKey key) {
Element element = this.getCache(category.toString()).get(key.toString());
if (element != null) {
return Optional.ofNullable(element.getObjectValue());
} else {
return Optional.empty();
}
}
@Override
public void put(CacheCategory category, CacheKey key, Object o) {
if (null != o) {
this.getCache(category.toString()).put(new Element(key.toString(), o));
}
}
@Override
public void receive(WrapClearCacheRequest wi) {
try {
wi.setType(WrapClearCacheRequest.TYPE_RECEIVE);
notifyReceiveQueue.send(wi);
} catch (Exception e) {
LOGGER.error(e);
}
}
@Override
public void notify(Class<?> clz, List<Object> keys) {
try {
ClearCacheRequest req = new ClearCacheRequest();
req.setType(WrapClearCacheRequest.TYPE_NOTIFY);
req.setClassName(clz.getName());
req.setKeys(keys);
this.notifyReceiveQueue.send(req);
} catch (Exception e) {
LOGGER.error(e);
}
}
@Override
public void shutdown() {
this.notifyReceiveQueue.stop();
this.cacheManager.shutdown();
}
@Override
public String detail() {
return cacheManager.getActiveConfigurationText();
}
private synchronized Ehcache getCache(String name) {
return cacheManager.addCacheIfAbsent(name);
}
}
//package com.x.base.core.project.cache;
//
//import java.lang.management.ManagementFactory;
//import java.util.List;
//import java.util.Optional;
//
//import org.apache.commons.lang3.BooleanUtils;
//
//import com.x.base.core.project.config.Config;
//import com.x.base.core.project.jaxrs.WrapClearCacheRequest;
//import com.x.base.core.project.logger.Logger;
//import com.x.base.core.project.logger.LoggerFactory;
//
//import net.sf.ehcache.CacheManager;
//import net.sf.ehcache.Ehcache;
//import net.sf.ehcache.Element;
//import net.sf.ehcache.config.CacheConfiguration;
//import net.sf.ehcache.config.Configuration;
//import net.sf.ehcache.config.PersistenceConfiguration;
//import net.sf.ehcache.management.ManagementService;
//import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
//
//public class CacheEhcacheImpl implements Cache {
//
// private static final Logger LOGGER = LoggerFactory.getLogger(CacheEhcacheImpl.class);
//
// private CacheManager cacheManager;
//
// private CacheEhcacheNotifyReceiveQueue notifyReceiveQueue;
//
// public CacheEhcacheImpl(String application) throws Exception {
// CacheConfiguration cacheConfiguration = new CacheConfiguration();
// cacheConfiguration.maxEntriesLocalHeap(1000);
// cacheConfiguration.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
// cacheConfiguration.timeToIdleSeconds(1800);
// cacheConfiguration.timeToLiveSeconds(3600);
// cacheConfiguration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU);
// Configuration configuration = new Configuration();
// configuration.setDefaultCacheConfiguration(cacheConfiguration);
// configuration.setName("CacheEhcacheManager-" + application);
// this.cacheManager = new CacheManager(configuration);
// this.notifyReceiveQueue = new CacheEhcacheNotifyReceiveQueue(this.cacheManager);
// notifyReceiveQueue.start();
// if (BooleanUtils.isTrue(Config.cache().getEhcache().getJmxEnable())) {
// ManagementService.registerMBeans(cacheManager, ManagementFactory.getPlatformMBeanServer(), true, true, true,
// true);
// }
// }
//
// @Override
// public Optional<Object> get(CacheCategory category, CacheKey key) {
// Element element = this.getCache(category.toString()).get(key.toString());
// if (element != null) {
// return Optional.ofNullable(element.getObjectValue());
// } else {
// return Optional.empty();
// }
// }
//
// @Override
// public void put(CacheCategory category, CacheKey key, Object o) {
// if (null != o) {
// this.getCache(category.toString()).put(new Element(key.toString(), o));
// }
// }
//
// @Override
// public void receive(WrapClearCacheRequest wi) {
// try {
// wi.setType(WrapClearCacheRequest.TYPE_RECEIVE);
// notifyReceiveQueue.send(wi);
// } catch (Exception e) {
// LOGGER.error(e);
// }
// }
//
// @Override
// public void notify(Class<?> clz, List<Object> keys) {
// try {
// ClearCacheRequest req = new ClearCacheRequest();
// req.setType(WrapClearCacheRequest.TYPE_NOTIFY);
// req.setClassName(clz.getName());
// req.setKeys(keys);
// this.notifyReceiveQueue.send(req);
// } catch (Exception e) {
// LOGGER.error(e);
// }
// }
//
// @Override
// public void shutdown() {
// this.notifyReceiveQueue.stop();
// this.cacheManager.shutdown();
// }
//
// @Override
// public String detail() {
// return cacheManager.getActiveConfigurationText();
// }
//
// private synchronized Ehcache getCache(String name) {
// return cacheManager.addCacheIfAbsent(name);
// }
//
//}
......@@ -35,10 +35,10 @@ public abstract class CacheManager {
if (null == cache) {
if (StringUtils.equals(Config.cache().getType(), Cache.TYPE_REDIS)) {
cache = new CacheRedisImpl(name);
} else if (StringUtils.equals(Config.cache().getType(), Cache.TYPE_EHCACHE)) {
cache = new CacheEhcacheImpl(name);
// } else if (StringUtils.equals(Config.cache().getType(), Cache.TYPE_EHCACHE)) {
// cache = new CacheEhcacheImpl(name);
} else {
cache = new CacheGuavaImpl(name);
cache = new CacheGuavaImpl(Cache.TYPE_GUAVA);
}
}
return cache;
......
......@@ -19,7 +19,7 @@ public class Cache extends ConfigObject {
public Cache() {
this.type = TYPE_GUAVA;
this.redis = Redis.defaultInstance();
this.ehcache = Ehcache.defaultInstance();
// this.ehcache = Ehcache.defaultInstance();
this.guava = Guava.defaultInstance();
}
......@@ -33,8 +33,8 @@ public class Cache extends ConfigObject {
@FieldDescribe("redis配置")
private Redis redis;
@FieldDescribe("ehcache配置")
private Ehcache ehcache;
// @FieldDescribe("ehcache配置")
// private Ehcache ehcache;
@FieldDescribe("guava配置")
private Guava guava;
......@@ -43,9 +43,9 @@ public class Cache extends ConfigObject {
return this.redis == null ? new Redis() : this.redis;
}
public Ehcache getEhcache() {
return this.ehcache == null ? new Ehcache() : this.ehcache;
}
// public Ehcache getEhcache() {
// return this.ehcache == null ? new Ehcache() : this.ehcache;
// }
public Guava getGuava() {
return this.guava == null ? new Guava() : this.guava;
......
......@@ -46,7 +46,8 @@ public abstract class JettySeverTools {
* 需要在WebAppClassLoader加载 jakarta.xml.bind-api-*.jar
*/
private static final Collection<String> FILTER_STRINGS = Arrays.asList("openjpa-*.jar", "ehcache-*.jar",
"jetty-all-*.jar", "jetty-proxy-*.jar", "quartz-*.jar", "filters-*.jar", "jakarta.xml.bind-api-*.jar");
"jetty-all-*.jar", "jetty-proxy-*.jar", "quartz-*.jar", "filters-*.jar", "jakarta.xml.bind-api-*.jar",
"swagger-*.jar");
private static final Optional<IOFileFilter> JARS_FILTER = FILTER_STRINGS.stream().map(WildcardFileFilter::new)
.map(FileFilterUtils::or).reduce(FileFilterUtils::or);
......
......@@ -35,6 +35,8 @@ import com.x.processplatform.assemble.surface.jaxrs.work.WorkAction;
import com.x.processplatform.assemble.surface.jaxrs.workcompleted.WorkCompletedAction;
import com.x.processplatform.assemble.surface.jaxrs.worklog.WorkLogAction;
import io.swagger.v3.jaxrs2.integration.resources.AcceptHeaderOpenApiResource;
@ApplicationPath("jaxrs")
public class ActionApplication extends AbstractActionApplication {
......@@ -69,6 +71,9 @@ public class ActionApplication extends AbstractActionApplication {
classes.add(SnapAction.class);
classes.add(AnonymousAction.class);
classes.add(SignAction.class);
classes.add(OpenApiAction.class);
// classes.add(AcceptHeaderOpenApiResource.class);
return classes;
}
}
package com.x.processplatform.assemble.surface.jaxrs;
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.getContextIdFromServletConfig;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.x.base.core.project.gson.XGsonBuilder;
import io.swagger.v3.core.filter.OpenAPISpecFilter;
import io.swagger.v3.core.filter.SpecFilter;
import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder;
import io.swagger.v3.jaxrs2.integration.resources.BaseOpenApiResource;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.integration.api.OpenAPIConfiguration;
import io.swagger.v3.oas.integration.api.OpenApiContext;
import io.swagger.v3.oas.models.OpenAPI;
@Path("/openapi")
public class OpenApiAction {
private static Logger LOGGER = LoggerFactory.getLogger(BaseOpenApiResource.class);
@Context
ServletConfig servletConfig;
@Context
Application application;
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Operation(hidden = true)
public Response getOpenApiJson(@Context HttpHeaders headers, @Context UriInfo uriInfo) throws Exception {
return this.getOpenApi(headers, servletConfig, application, uriInfo, "json");
}
protected String getContextId(ServletConfig config) {
return getContextIdFromServletConfig(config);
}
protected Response getOpenApi(HttpHeaders headers, ServletConfig config, Application app, UriInfo uriInfo,
String type) throws Exception {
String ctxId = getContextId(config);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("ctxId" + ctxId);
System.out.println("resourcePackages" + resourcePackages);
System.out.println("configLocation" + configLocation);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
OpenApiContext ctx = new JaxrsOpenApiContextBuilder().servletConfig(config).application(app)
.resourcePackages(resourcePackages).configLocation(configLocation)
.openApiConfiguration(openApiConfiguration).ctxId(ctxId).buildContext(true);
OpenAPI oas = ctx.read();
boolean pretty = false;
if (ctx.getOpenApiConfiguration() != null
&& Boolean.TRUE.equals(ctx.getOpenApiConfiguration().isPrettyPrint())) {
pretty = true;
}
if (oas != null) {
if (ctx.getOpenApiConfiguration() != null && ctx.getOpenApiConfiguration().getFilterClass() != null) {
try {
OpenAPISpecFilter filterImpl = (OpenAPISpecFilter) Class
.forName(ctx.getOpenApiConfiguration().getFilterClass()).newInstance();
SpecFilter f = new SpecFilter();
oas = f.filter(oas, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers),
getHeaders(headers));
} catch (Exception e) {
LOGGER.error("failed to load filter", e);
}
}
}
if (oas == null) {
return Response.status(404).build();
}
if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
return Response.status(Response.Status.OK)
.entity(pretty
? ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas)
: ctx.getOutputYamlMapper().writeValueAsString(oas))
.type("application/yaml").build();
} else {
return Response.status(Response.Status.OK).entity(XGsonBuilder.toJson(oas))
.type(MediaType.APPLICATION_JSON_TYPE).build();
}
}
private static Map<String, List<String>> getQueryParams(MultivaluedMap<String, String> params) {
Map<String, List<String>> output = new HashMap<>();
if (params != null) {
params.forEach(output::put);
}
return output;
}
private static Map<String, String> getCookies(HttpHeaders headers) {
Map<String, String> output = new HashMap<>();
if (headers != null) {
headers.getCookies().forEach((k, v) -> output.put(k, v.getValue()));
}
return output;
}
private static Map<String, List<String>> getHeaders(HttpHeaders headers) {
Map<String, List<String>> output = new HashMap<>();
if (headers != null) {
headers.getRequestHeaders().forEach(output::put);
}
return output;
}
protected String configLocation;
public String getConfigLocation() {
return configLocation;
}
public void setConfigLocation(String configLocation) {
this.configLocation = configLocation;
}
public OpenApiAction configLocation(String configLocation) {
setConfigLocation(configLocation);
return this;
}
protected Set<String> resourcePackages;
public Set<String> getResourcePackages() {
return resourcePackages;
}
public void setResourcePackages(Set<String> resourcePackages) {
this.resourcePackages = resourcePackages;
}
public OpenApiAction resourcePackages(Set<String> resourcePackages) {
setResourcePackages(resourcePackages);
return this;
}
protected OpenAPIConfiguration openApiConfiguration;
public OpenAPIConfiguration getOpenApiConfiguration() {
return openApiConfiguration;
}
public void setOpenApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
}
public OpenApiAction openApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
setOpenApiConfiguration(openApiConfiguration);
return this;
}
}
......@@ -29,6 +29,10 @@ import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
@OpenAPIDefinition(info = @Info(title = "待阅xxx"))
@Path("read")
@JaxrsDescribe("待阅操作")
public class ReadAction extends StandardJaxrsAction {
......
......@@ -29,13 +29,25 @@ import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@OpenAPIDefinition(info = @Info(title = "待办xxx"))
@Path("task")
@JaxrsDescribe("待办操作")
public class TaskAction extends StandardJaxrsAction {
private static Logger logger = LoggerFactory.getLogger(TaskAction.class);
@JaxrsMethodDescribe(value = "根据job获取待办.", action = ActionListWithJob.class)
@Operation(summary = "根据指定的job列示待办.", tags = "Task(待办)", operationId = "", responses = {
@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = ActionListWithJob.Wo.class)))) })
@JaxrsMethodDescribe(value = "根据指定的job列示待办.", action = ActionListWithJob.class)
@GET
@Path("list/job/{job}")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
......
......@@ -42,6 +42,9 @@ import com.x.processplatform.core.entity.PersistenceProperties;
import com.x.processplatform.core.entity.element.ActivityType;
import com.x.processplatform.core.entity.element.Route;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
@Entity
@ContainerEntity(dumpSize = 200, type = ContainerEntity.Type.content, reference = ContainerEntity.Reference.strong)
@Table(name = PersistenceProperties.Content.Task.table, uniqueConstraints = {
......@@ -183,6 +186,7 @@ public class Task extends SliceJpaObject implements ProjectionInterface {
}
public static final String job_FIELDNAME = "job";
@Schema(description = "任务标识.")
@FieldDescribe("任务.")
@Column(length = JpaObject.length_id, name = ColumnNamePrefix + job_FIELDNAME)
@Index(name = TABLE + IndexNameMiddle + job_FIELDNAME)
......@@ -191,6 +195,7 @@ public class Task extends SliceJpaObject implements ProjectionInterface {
public static final String title_FIELDNAME = "title";
@FieldDescribe("标题.")
@Parameter(description = "工作标题.")
@Column(length = length_255B, name = ColumnNamePrefix + title_FIELDNAME)
@Index(name = TABLE + IndexNameMiddle + title_FIELDNAME)
@CheckPersist(allowEmpty = true)
......@@ -390,8 +395,9 @@ public class Task extends SliceJpaObject implements ProjectionInterface {
public static final String routeList_FIELDNAME = "routeList";
@FieldDescribe("当前活动可供选择的路由.")
@PersistentCollection(fetch = FetchType.EAGER)
@ContainerTable(name = TABLE + ContainerTableNameMiddle + routeList_FIELDNAME, joinIndex = @Index(name = TABLE
+ IndexNameMiddle + routeList_FIELDNAME + JoinIndexNameSuffix))
@ContainerTable(name = TABLE + ContainerTableNameMiddle
+ routeList_FIELDNAME, joinIndex = @Index(name = TABLE + IndexNameMiddle + routeList_FIELDNAME
+ JoinIndexNameSuffix))
@ElementColumn(length = JpaObject.length_id, name = ColumnNamePrefix + routeList_FIELDNAME)
@OrderColumn(name = ORDERCOLUMNCOLUMN)
@CheckPersist(allowEmpty = true)
......@@ -400,8 +406,9 @@ public class Task extends SliceJpaObject implements ProjectionInterface {
public static final String routeNameList_FIELDNAME = "routeNameList";
@FieldDescribe("当前活动可供选择的路由名称.")
@PersistentCollection(fetch = FetchType.EAGER)
@ContainerTable(name = TABLE + ContainerTableNameMiddle + routeNameList_FIELDNAME, joinIndex = @Index(name = TABLE
+ IndexNameMiddle + routeNameList_FIELDNAME + JoinIndexNameSuffix))
@ContainerTable(name = TABLE + ContainerTableNameMiddle
+ routeNameList_FIELDNAME, joinIndex = @Index(name = TABLE + IndexNameMiddle + routeNameList_FIELDNAME
+ JoinIndexNameSuffix))
@ElementColumn(length = length_255B, name = ColumnNamePrefix + routeNameList_FIELDNAME)
@OrderColumn(name = ORDERCOLUMNCOLUMN)
@CheckPersist(allowEmpty = true)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册