Skip to content

Commit

Permalink
ServicePublisher: add property to disable parameter names for backwar…
Browse files Browse the repository at this point in the history
…d compatibility
  • Loading branch information
Marat Kamalov committed Feb 12, 2020
1 parent 5e670a8 commit 298d0da
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 94 deletions.
Expand Up @@ -100,11 +100,15 @@
</bean>

<bean id="swaggerInitializationBean"
class="org.openl.rules.ruleservice.publish.jaxrs.swagger.SwaggerInitializationBean" scope="singleton"/>
class="org.openl.rules.ruleservice.publish.jaxrs.swagger.SwaggerInitializationBean"/>

<!-- Initializes OpenL Engine instances according to web services configuration description and calls RuleServicePublisher
to expose corresponding web service -->
<!-- Exposes web services. -->
<bean id="jaxrsServiceEnhancer" class="org.openl.rules.ruleservice.publish.jaxrs.JAXRSOpenLServiceEnhancer">
<property name="methodParameterNamesFromRulesEnabled" value="${ruleservice.jaxrs.methodParameterNamesFromRulesEnabled:true}"/>
</bean>

<bean id="JAXRSServicesRuleServicePublisher" class="org.openl.rules.ruleservice.publish.JAXRSRuleServicePublisher"
depends-on="swaggerInitializationBean">
<property name="baseAddress" value="${ruleservice.baseAddress}"/>
Expand Down
Expand Up @@ -48,6 +48,10 @@
<!-- Initializes OpenL Engine instances according to web services configuration
description and calls DeploymentAdmin to expose corresponding web service -->
<!-- Exposes web services. -->
<bean id="jaxwsServiceEnhancer" class="org.openl.rules.ruleservice.publish.jaxws.JAXWSOpenLServiceEnhancer">
<property name="methodParameterNamesFromRulesEnabled" value="${ruleservice.jaxws.methodParameterNamesFromRulesEnabled:true}"/>
</bean>

<bean id="webServiceRuleServicePublisher"
class="org.openl.rules.ruleservice.publish.JAXWSRuleServicePublisher">
<property name="baseAddress" value="${ruleservice.baseAddress}"/>
Expand Down
@@ -1,6 +1,8 @@
package org.openl.rules.ruleservice.publish;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
Expand All @@ -10,10 +12,15 @@
import org.openl.rules.ruleservice.core.OpenLService;
import org.openl.rules.ruleservice.core.RuleServiceDeployException;
import org.openl.rules.ruleservice.core.RuleServiceUndeployException;
import org.openl.rules.ruleservice.publish.jaxrs.JAXRSEnhancerHelper;
import org.openl.rules.ruleservice.publish.jaxrs.JAXRSOpenLServiceEnhancer;
import org.openl.rules.ruleservice.publish.jaxrs.storelogdata.JacksonObjectSerializer;
import org.openl.rules.ruleservice.publish.jaxrs.swagger.SwaggerStaticFieldsWorkaround;
import org.openl.rules.ruleservice.storelogdata.*;
import org.openl.rules.ruleservice.storelogdata.CollectObjectSerializerInterceptor;
import org.openl.rules.ruleservice.storelogdata.CollectOpenLServiceInterceptor;
import org.openl.rules.ruleservice.storelogdata.CollectOperationResourceInfoInterceptor;
import org.openl.rules.ruleservice.storelogdata.CollectPublisherTypeInterceptor;
import org.openl.rules.ruleservice.storelogdata.ObjectSerializer;
import org.openl.rules.ruleservice.storelogdata.StoreLogDataFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
Expand All @@ -38,6 +45,9 @@ public class JAXRSRuleServicePublisher implements RuleServicePublisher {
private boolean storeLogDataEnabled = false;
private boolean swaggerPrettyPrint = false;

@Autowired
private JAXRSOpenLServiceEnhancer jaxrsServiceEnhancer;

@Autowired
@Qualifier("JAXRSServicesServerPrototype")
private ObjectFactory<JAXRSServerFactoryBean> serverFactoryBeanObjectFactory;
Expand Down Expand Up @@ -87,6 +97,14 @@ public boolean isSwaggerPrettyPrint() {
return swaggerPrettyPrint;
}

public JAXRSOpenLServiceEnhancer getJaxrsServiceEnhancer() {
return jaxrsServiceEnhancer;
}

public void setJaxrsServiceEnhancer(JAXRSOpenLServiceEnhancer jaxrsServiceEnhancer) {
this.jaxrsServiceEnhancer = jaxrsServiceEnhancer;
}

@Override
public void deploy(final OpenLService service) throws RuleServiceDeployException {
Objects.requireNonNull(service, "service cannot be null");
Expand Down Expand Up @@ -118,7 +136,7 @@ public void deploy(final OpenLService service) throws RuleServiceDeployException
svrFactory.getInFaultInterceptors().add(new CollectOperationResourceInfoInterceptor());
}

Object proxyServiceBean = JAXRSEnhancerHelper.decorateServiceBean(service);
Object proxyServiceBean = getJaxrsServiceEnhancer().decorateServiceBean(service);
Class<?> serviceClass = proxyServiceBean.getClass().getInterfaces()[0]; // The first is a decorated
// interface

Expand Down
Expand Up @@ -13,8 +13,8 @@
import org.openl.rules.ruleservice.core.OpenLService;
import org.openl.rules.ruleservice.core.RuleServiceDeployException;
import org.openl.rules.ruleservice.core.RuleServiceUndeployException;
import org.openl.rules.ruleservice.publish.jaxws.JAXWSEnhancerHelper;
import org.openl.rules.ruleservice.publish.jaxws.JAXWSInvocationHandler;
import org.openl.rules.ruleservice.publish.jaxws.JAXWSOpenLServiceEnhancer;
import org.openl.rules.ruleservice.publish.jaxws.storelogdata.AegisObjectSerializer;
import org.openl.rules.ruleservice.storelogdata.CollectObjectSerializerInterceptor;
import org.openl.rules.ruleservice.storelogdata.CollectOpenLServiceInterceptor;
Expand All @@ -41,6 +41,9 @@ public class JAXWSRuleServicePublisher implements RuleServicePublisher {
private String baseAddress;
private boolean storeLogDataEnabled = false;

@Autowired
private JAXWSOpenLServiceEnhancer jaxwsServiceEnhancer;

@Autowired
@Qualifier("webServicesServerPrototype")
private ObjectFactory<ServerFactoryBean> serverFactoryBeanObjectFactory;
Expand Down Expand Up @@ -81,10 +84,18 @@ public void setStoreLoggingFeatureObjectFactory(
this.storeLoggingFeatureObjectFactory = storeLoggingFeatureObjectFactory;
}

private ObjectSerializer getObjectSeializer(ServerFactoryBean svrFactory) {
private ObjectSerializer getObjectSerializer(ServerFactoryBean svrFactory) {
return new AegisObjectSerializer((AegisDatabinding) svrFactory.getDataBinding());
}

public JAXWSOpenLServiceEnhancer getJaxwsServiceEnhancer() {
return jaxwsServiceEnhancer;
}

public void setJaxwsServiceEnhancer(JAXWSOpenLServiceEnhancer jaxwsServiceEnhancer) {
this.jaxwsServiceEnhancer = jaxwsServiceEnhancer;
}

@Override
public void deploy(OpenLService service) throws RuleServiceDeployException {
Objects.requireNonNull(service, "service cannot be null");
Expand All @@ -97,7 +108,7 @@ public void deploy(OpenLService service) throws RuleServiceDeployException {
String serviceAddress = getBaseAddress() + URLHelper.processURL(service.getUrl());
svrFactory.setAddress(serviceAddress);

Class<?> serviceClass = JAXWSEnhancerHelper.decorateServiceInterface(service);
Class<?> serviceClass = getJaxwsServiceEnhancer().decorateServiceInterface(service);
svrFactory.setServiceClass(serviceClass);

Object target = Proxy.newProxyInstance(service.getClassLoader(),
Expand All @@ -111,14 +122,14 @@ public void deploy(OpenLService service) throws RuleServiceDeployException {
svrFactory.getFeatures().add(getStoreLoggingFeatureObjectFactory().getObject());

svrFactory.getInInterceptors()
.add(new CollectObjectSerializerInterceptor(getObjectSeializer(svrFactory)));
.add(new CollectObjectSerializerInterceptor(getObjectSerializer(svrFactory)));
svrFactory.getInInterceptors().add(new CollectOpenLServiceInterceptor(service));
svrFactory.getInInterceptors()
.add(new CollectPublisherTypeInterceptor(RulesDeploy.PublisherType.WEBSERVICE));
svrFactory.getInInterceptors().add(new CollectOperationResourceInfoInterceptor());

svrFactory.getInFaultInterceptors()
.add(new CollectObjectSerializerInterceptor(getObjectSeializer(svrFactory)));
.add(new CollectObjectSerializerInterceptor(getObjectSerializer(svrFactory)));
svrFactory.getInFaultInterceptors().add(new CollectOpenLServiceInterceptor(service));
svrFactory.getInFaultInterceptors()
.add(new CollectPublisherTypeInterceptor(RulesDeploy.PublisherType.WEBSERVICE));
Expand Down Expand Up @@ -155,14 +166,16 @@ public void undeploy(OpenLService service) throws RuleServiceUndeployException {
Objects.requireNonNull(service, "service cannot be null");
ServiceServer server = runningServices.get(service);
if (server == null) {
throw new RuleServiceUndeployException(String.format("There is no running service '%s'.", service.getName()));
throw new RuleServiceUndeployException(
String.format("There is no running service '%s'.", service.getName()));
}
try {
server.getServer().destroy();
runningServices.remove(service);
log.info("Service '{}' has been undeployed succesfully.", service.getName());
} catch (Exception t) {
throw new RuleServiceUndeployException(String.format("Failed to undeploy service '%s'.", service.getName()), t);
throw new RuleServiceUndeployException(String.format("Failed to undeploy service '%s'.", service.getName()),
t);
}

}
Expand Down
Expand Up @@ -29,12 +29,46 @@
* @author Marat Kamalov
*
*/
public final class JAXRSEnhancerHelper {
public final class JAXRSOpenLServiceEnhancer {

private JAXRSEnhancerHelper() {
private boolean methodParameterNamesFromRulesEnabled = true;

public boolean isMethodParameterNamesFromRulesEnabled() {
return methodParameterNamesFromRulesEnabled;
}

public void setMethodParameterNamesFromRulesEnabled(boolean methodParameterNamesFromRulesEnabled) {
this.methodParameterNamesFromRulesEnabled = methodParameterNamesFromRulesEnabled;
}

private static final Set<Class<?>> TEXT_MEDIA_TYPE_SET = new HashSet<>();
static {
TEXT_MEDIA_TYPE_SET.add(Number.class);
TEXT_MEDIA_TYPE_SET.add(Enum.class);
TEXT_MEDIA_TYPE_SET.add(String.class);
TEXT_MEDIA_TYPE_SET.add(Date.class);
}

private static class ParamAnnotationValue {

private Class<?> annotationClass;
private String fieldName;

public ParamAnnotationValue(Class<?> withPathParamValues, String fieldName) {
this.annotationClass = withPathParamValues;
this.fieldName = fieldName;
}

public Class<?> getAnnotationClass() {
return annotationClass;
}

public String getFieldName() {
return fieldName;
}
}

private static class JAXRSInterfaceAnnotationEnhancerClassVisitor extends ClassVisitor {
private class JAXRSInterfaceAnnotationEnhancerClassVisitor extends ClassVisitor {

private static final int MAX_PARAMETERS_COUNT_FOR_GET = 4;

Expand All @@ -48,14 +82,6 @@ private static class JAXRSInterfaceAnnotationEnhancerClassVisitor extends ClassV
private Map<Method, String> paths = null;
private Map<Method, String> methodRequests = null;

private static final Set<Class<?>> TEXT_MEDIA_TYPE_SET = new HashSet();
static {
TEXT_MEDIA_TYPE_SET.add(Number.class);
TEXT_MEDIA_TYPE_SET.add(Enum.class);
TEXT_MEDIA_TYPE_SET.add(String.class);
TEXT_MEDIA_TYPE_SET.add(Date.class);
}

JAXRSInterfaceAnnotationEnhancerClassVisitor(ClassVisitor arg0,
Class<?> originalClass,
ClassLoader classLoader,
Expand Down Expand Up @@ -106,7 +132,8 @@ private String changeArgumentTypes(String signature, Method originalMethod) thro
}

private Class<?> generateWrapperClass(Method originalMethod) throws Exception {
String[] parameterNames = MethodUtils.getParameterNames(originalMethod, service);
String[] parameterNames = MethodUtils.getParameterNames(originalMethod,
JAXRSOpenLServiceEnhancer.this.isMethodParameterNamesFromRulesEnabled() ? service : null);
String requestParameterName = getRequestParameterName(originalMethod);
String beanName = "org.openl.jaxrs." + requestParameterName;

Expand Down Expand Up @@ -185,7 +212,7 @@ String getPath(Method method) {
for (Method m : methods) {
String s = m.getName();
int i = 1;
while (paths.values().contains(s)) {
while (paths.containsValue(s)) {
s = m.getName() + i;
i++;
}
Expand Down Expand Up @@ -276,27 +303,8 @@ public MethodVisitor visitMethod(int arg0, String methodName, String arg2, Strin
return mv;
}

private class ParamAnnotationValue {

private Class<?> annotationClass;
private String fieldName;

public ParamAnnotationValue(Class<?> withPathParamValues, String fieldName) {
this.annotationClass = withPathParamValues;
this.fieldName = fieldName;
}

public Class<?> getAnnotationClass() {
return annotationClass;
}

public String getFieldName() {
return fieldName;
}
}

private List<ParamAnnotationValue> getParamAnnotationsValue(Method originalMethod) {
final List<ParamAnnotationValue> values = new ArrayList(originalMethod.getParameterCount());
final List<ParamAnnotationValue> values = new ArrayList<>(originalMethod.getParameterCount());
for (Annotation[] annotations : originalMethod.getParameterAnnotations()) {
if (annotations.length > 0) {
for (Annotation annotation : annotations) {
Expand Down Expand Up @@ -355,11 +363,11 @@ private void addConsumerProducesMethodAnnotations(MethodVisitor mv,

private void processAnnotationsOnMethodParameters(Method originalMethod, MethodVisitor mv) {
int index = 0;
for (Annotation[] annotatons : originalMethod.getParameterAnnotations()) {
for (Annotation annotaton : annotatons) {
for (Annotation[] annotations : originalMethod.getParameterAnnotations()) {
for (Annotation annotation : annotations) {
AnnotationVisitor av = mv
.visitParameterAnnotation(index, Type.getDescriptor(annotaton.annotationType()), true);
InterfaceTransformer.processAnnotation(annotaton, av);
.visitParameterAnnotation(index, Type.getDescriptor(annotation.annotationType()), true);
InterfaceTransformer.processAnnotation(annotation, av);
}
index++;
}
Expand Down Expand Up @@ -428,7 +436,7 @@ private void addConsumesAnnotation(ClassVisitor cv) {
}
}

public static Object decorateServiceBean(OpenLService service) throws Exception {
public Object decorateServiceBean(OpenLService service) throws Exception {
Class<?> serviceClass = service.getServiceClass();
Objects.requireNonNull(serviceClass, "Service class cannot be null");
if (!serviceClass.isInterface()) {
Expand Down

0 comments on commit 298d0da

Please sign in to comment.