use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.
the class AbstractShenyuClientRegisterServiceImpl method doRegisterURI.
/**
* Register uri string.
*
* @param selectorName the selector name
* @param uriList the uri list
* @return the string
*/
@Override
public String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList) {
if (CollectionUtils.isEmpty(uriList)) {
return "";
}
SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType()));
if (Objects.isNull(selectorDO)) {
throw new ShenyuException("doRegister Failed to execute,wait to retry.");
}
// fetch UPSTREAM_MAP data from db
// upstreamCheckService.fetchUpstreamData();
// update upstream
String handler = buildHandle(uriList, selectorDO);
selectorDO.setHandle(handler);
SelectorData selectorData = selectorService.buildByName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType()));
selectorData.setHandle(handler);
// update db
selectorService.updateSelective(selectorDO);
// publish change event.
eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE, Collections.singletonList(selectorData)));
return ShenyuResultMessage.SUCCESS;
}
use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.
the class ShenyuHttpRegistryControllerBeanPostProcessor method postProcessAfterInitialization.
@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
if (bean instanceof ShenyuHttpRegistryController) {
try {
RequestMappingHandlerMapping requestMappingHandlerMapping = SpringBeanUtils.getInstance().getBean(RequestMappingHandlerMapping.class);
Method method = requestMappingHandlerMapping.getClass().getSuperclass().getSuperclass().getDeclaredMethod("detectHandlerMethods", Object.class);
method.setAccessible(true);
method.invoke(requestMappingHandlerMapping, beanName);
} catch (Exception e) {
throw new ShenyuException(e.getMessage());
}
}
return bean;
}
use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.
the class ContextRegisterListener method onApplicationEvent.
@Override
public void onApplicationEvent(@NonNull final ContextRefreshedEvent contextRefreshedEvent) {
if (!registered.compareAndSet(false, true)) {
return;
}
if (Boolean.TRUE.equals(isFull)) {
publisher.publishEvent(buildMetaDataDTO());
}
try {
final int mergedPort = port <= 0 ? PortUtils.findPort(beanFactory) : port;
publisher.publishEvent(buildURIRegisterDTO(mergedPort));
} catch (ShenyuException e) {
throw new ShenyuException(e.getMessage() + "please config ${shenyu.client.http.props.port} in xml/yml !");
}
}
use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.
the class JsonServerServiceInterceptor method useMarshalledMessages.
/**
* wrap method.
* @param serviceDef ServerServiceDefinition
* @param marshaller message
* @param <T> message type
* @return wrap ServerServiceDefinition
* @throws IllegalArgumentException IllegalArgumentException
* @throws IllegalAccessException IllegalAccessException
*/
public static <T> ServerServiceDefinition useMarshalledMessages(final ServerServiceDefinition serviceDef, final MethodDescriptor.Marshaller<T> marshaller) throws IllegalArgumentException, IllegalAccessException {
List<ServerMethodDefinition<?, ?>> wrappedMethods = new ArrayList<>();
List<MethodDescriptor<?, ?>> wrappedDescriptors = new ArrayList<>();
// Wrap the descriptors
for (final ServerMethodDefinition<?, ?> definition : serviceDef.getMethods()) {
MethodDescriptor.Marshaller<?> requestMarshaller = definition.getMethodDescriptor().getRequestMarshaller();
Field defaultInstanceField = ReflectUtils.getField(requestMarshaller.getClass(), "defaultInstance");
if (Objects.isNull(defaultInstanceField)) {
throw new ShenyuException(String.format("can not get defaultInstance Field of %s", requestMarshaller.getClass()));
}
defaultInstanceField.setAccessible(true);
String fullMethodName = definition.getMethodDescriptor().getFullMethodName();
MethodDescriptor.MethodType methodType = definition.getMethodDescriptor().getType();
METHOD_TYPE_MAP.put(fullMethodName, methodType);
String[] splitMethodName = fullMethodName.split("/");
fullMethodName = splitMethodName[0] + GrpcConstants.GRPC_JSON_SERVICE + "/" + splitMethodName[1];
REQUEST_CLAZZ_MAP.put(fullMethodName, defaultInstanceField.get(requestMarshaller).getClass());
final MethodDescriptor<?, ?> originalMethodDescriptor = definition.getMethodDescriptor();
final MethodDescriptor<T, T> wrappedMethodDescriptor = originalMethodDescriptor.toBuilder(marshaller, marshaller).build();
wrappedDescriptors.add(wrappedMethodDescriptor);
wrappedMethods.add(wrapMethod(definition, wrappedMethodDescriptor));
}
// Build the new service descriptor
ServiceDescriptor.Builder build = ServiceDescriptor.newBuilder(serviceDef.getServiceDescriptor().getName() + GrpcConstants.GRPC_JSON_SERVICE);
for (MethodDescriptor<?, ?> md : wrappedDescriptors) {
Field fullMethodNameField = ReflectUtils.getField(md.getClass(), "fullMethodName");
if (Objects.isNull(fullMethodNameField)) {
throw new ShenyuException(String.format("can not get fullMethodName Field of %s", md.getClass()));
}
fullMethodNameField.setAccessible(true);
String fullMethodName = (String) fullMethodNameField.get(md);
String[] splitMethodName = fullMethodName.split("/");
fullMethodName = splitMethodName[0] + GrpcConstants.GRPC_JSON_SERVICE + "/" + splitMethodName[1];
fullMethodNameField.set(md, fullMethodName);
Field serviceNameField = ReflectUtils.getField(md.getClass(), "serviceName");
if (Objects.isNull(serviceNameField)) {
throw new ShenyuException(String.format("can not get serviceName Field Field of %s", md.getClass()));
}
serviceNameField.setAccessible(true);
String serviceName = (String) serviceNameField.get(md);
serviceName = serviceName + GrpcConstants.GRPC_JSON_SERVICE;
serviceNameField.set(md, serviceName);
build.addMethod(md);
}
final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(build.build());
// Create the new service definition
for (ServerMethodDefinition<?, ?> definition : wrappedMethods) {
serviceBuilder.addMethod(definition);
}
return serviceBuilder.build();
}
use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.
the class Md5Utils method md5.
/**
* Md 5 string.
*
* @param src the src
* @param charset the charset
*
* @return the string
*/
private static String md5(final String src, final String charset) {
MessageDigest md5;
StringBuilder hexValue = new StringBuilder(32);
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new ShenyuException("MD5 not supported", e);
}
byte[] byteArray = new byte[0];
try {
byteArray = src.getBytes(charset);
} catch (UnsupportedEncodingException e) {
LOG.error(e.getMessage(), e);
}
byte[] md5Bytes = md5.digest(byteArray);
for (byte md5Byte : md5Bytes) {
int val = ((int) md5Byte) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
Aggregations