Search in sources :

Example 1 with ShenyuException

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;
}
Also used : SelectorDO(org.apache.shenyu.admin.model.entity.SelectorDO) DataChangedEvent(org.apache.shenyu.admin.listener.DataChangedEvent) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) SelectorData(org.apache.shenyu.common.dto.SelectorData)

Example 2 with ShenyuException

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;
}
Also used : ShenyuHttpRegistryController(org.apache.shenyu.admin.controller.ShenyuHttpRegistryController) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Method(java.lang.reflect.Method) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) BeansException(org.springframework.beans.BeansException)

Example 3 with ShenyuException

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 !");
    }
}
Also used : ShenyuException(org.apache.shenyu.common.exception.ShenyuException)

Example 4 with ShenyuException

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();
}
Also used : ArrayList(java.util.ArrayList) MethodDescriptor(io.grpc.MethodDescriptor) Field(java.lang.reflect.Field) ServerMethodDefinition(io.grpc.ServerMethodDefinition) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ShenyuException(org.apache.shenyu.common.exception.ShenyuException)

Example 5 with ShenyuException

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();
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

ShenyuException (org.apache.shenyu.common.exception.ShenyuException)47 ArrayList (java.util.ArrayList)12 Test (org.junit.jupiter.api.Test)12 Method (java.lang.reflect.Method)10 NacosException (com.alibaba.nacos.api.exception.NacosException)8 URIRegisterDTO (org.apache.shenyu.register.common.dto.URIRegisterDTO)8 List (java.util.List)7 MetaData (org.apache.shenyu.common.dto.MetaData)6 Optional (java.util.Optional)5 Map (java.util.Map)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Constants (org.apache.shenyu.common.constant.Constants)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Objects (java.util.Objects)3 Properties (java.util.Properties)3 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 Mono (reactor.core.publisher.Mono)3