diff --git a/src/main/java/com/loafle/overflow/commons/exception/OverflowException.java b/src/main/java/com/loafle/overflow/commons/exception/OverflowException.java index 161ff32..bb3cdf4 100644 --- a/src/main/java/com/loafle/overflow/commons/exception/OverflowException.java +++ b/src/main/java/com/loafle/overflow/commons/exception/OverflowException.java @@ -1,4 +1,10 @@ package com.loafle.overflow.commons.exception; -public class OverflowException extends Exception { +import io.grpc.Status; +import io.grpc.StatusException; + +public class OverflowException extends StatusException { + public OverflowException(Status status) { + super(status); + } } diff --git a/src/main/java/com/loafle/overflow/commons/exception/OverflowRuntimeException.java b/src/main/java/com/loafle/overflow/commons/exception/OverflowRuntimeException.java new file mode 100644 index 0000000..5f72a99 --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/exception/OverflowRuntimeException.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.commons.exception; + +import io.grpc.Status; +import io.grpc.StatusRuntimeException; + +public class OverflowRuntimeException extends StatusRuntimeException { + public OverflowRuntimeException(Class clazz) { + super(Status.fromCode(Status.Code.UNKNOWN).withDescription(clazz.getSimpleName())); + } +} diff --git a/src/main/java/com/loafle/overflow/module/member/exception/EmailNotConfirmedException.java b/src/main/java/com/loafle/overflow/module/member/exception/EmailNotConfirmedException.java index fed29ad..3aeef7b 100644 --- a/src/main/java/com/loafle/overflow/module/member/exception/EmailNotConfirmedException.java +++ b/src/main/java/com/loafle/overflow/module/member/exception/EmailNotConfirmedException.java @@ -1,6 +1,9 @@ package com.loafle.overflow.module.member.exception; -import com.loafle.overflow.commons.exception.OverflowException; +import com.loafle.overflow.commons.exception.OverflowRuntimeException; -public class EmailNotConfirmedException extends OverflowException { -} +public class EmailNotConfirmedException extends OverflowRuntimeException { + public EmailNotConfirmedException() { + super(EmailNotConfirmedException.class); + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/module/member/exception/SignInIdNotExistException.java b/src/main/java/com/loafle/overflow/module/member/exception/SignInIdNotExistException.java index 712ee0b..74c0022 100644 --- a/src/main/java/com/loafle/overflow/module/member/exception/SignInIdNotExistException.java +++ b/src/main/java/com/loafle/overflow/module/member/exception/SignInIdNotExistException.java @@ -1,6 +1,9 @@ package com.loafle.overflow.module.member.exception; -import com.loafle.overflow.commons.exception.OverflowException; +import com.loafle.overflow.commons.exception.OverflowRuntimeException; -public class SignInIdNotExistException extends OverflowException { +public class SignInIdNotExistException extends OverflowRuntimeException { + public SignInIdNotExistException() { + super(SignInIdNotExistException.class); + } } diff --git a/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotExactException.java b/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotExactException.java deleted file mode 100644 index ca3fe4d..0000000 --- a/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotExactException.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.loafle.overflow.module.member.exception; - -import com.loafle.overflow.commons.exception.OverflowException; - -public class SignInPwNotExactException extends OverflowException { -} diff --git a/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotMatchException.java b/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotMatchException.java new file mode 100644 index 0000000..2b1d2e3 --- /dev/null +++ b/src/main/java/com/loafle/overflow/module/member/exception/SignInPwNotMatchException.java @@ -0,0 +1,9 @@ +package com.loafle.overflow.module.member.exception; + +import com.loafle.overflow.commons.exception.OverflowRuntimeException; + +public class SignInPwNotMatchException extends OverflowRuntimeException { + public SignInPwNotMatchException() { + super(SignInPwNotMatchException.class); + } +} diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java index 15ae4ac..3324d2b 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java @@ -4,7 +4,7 @@ import com.loafle.overflow.module.email.service.EmailAuthService; import com.loafle.overflow.module.member.dao.MemberDAO; import com.loafle.overflow.module.member.exception.EmailNotConfirmedException; import com.loafle.overflow.module.member.exception.SignInIdNotExistException; -import com.loafle.overflow.module.member.exception.SignInPwNotExactException; +import com.loafle.overflow.module.member.exception.SignInPwNotMatchException; import com.loafle.overflow.module.member.model.Member; import com.loafle.overflow.module.meta.model.MetaMemberStatus; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +23,7 @@ public class MemberService { @Autowired private EmailAuthService emailAuthService; - public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotExactException { + public Member signin(String signinId, String signinPw) throws SignInIdNotExistException, EmailNotConfirmedException, SignInPwNotMatchException { Member m = this.memberDAO.findByEmail(signinId); if ( null == m ) { @@ -37,7 +37,7 @@ public class MemberService { BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); Boolean match = passwordEncoder.matches(signinPw, m.getPw()); if(!match) { - throw new SignInPwNotExactException(); + throw new SignInPwNotMatchException(); } return m; diff --git a/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java b/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java deleted file mode 100644 index 6507129..0000000 --- a/src/main/java/com/loafle/overflow/proxy/MethodSeeker.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.loafle.overflow.proxy; - -import java.lang.reflect.Method; -import java.util.List; - -/** - * Created by root on 17. 6. 23. - */ -public class MethodSeeker { - - public static Method getMethod(Class cls, String methodName, List paramTypes) { - - Method method = null; - - if(paramTypes != null) { - method = MethodSeeker.getMethodType(cls, methodName, paramTypes.toArray(new Class[paramTypes.size()])); - } - - return method; - - } - - private static Method getMethodType(Class cls, String methodName, Class ...type) { - if (cls == null) { - return null; - } - Method m = null; - try { - m = cls.getMethod(methodName, type); - } catch (NoSuchMethodException e) { - return MethodSeeker.getMethodType(cls.getSuperclass(), methodName, type); - } - - return m; - } -} diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java index ce2b3da..d9b3026 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java @@ -2,10 +2,17 @@ package com.loafle.overflow.proxy; import com.google.gson.internal.Primitives; import com.google.protobuf.ByteString; +import com.loafle.overflow.proxy.exception.InvalidParameterException; +import com.loafle.overflow.proxy.exception.InvalidRequestException; +import com.loafle.overflow.proxy.exception.NoSuchMethodException; +import com.loafle.overflow.proxy.exception.NoSuchServiceException; +import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.JavaType; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @@ -50,7 +57,7 @@ public class ServiceInvoker { return serviceCache.bean; } - private MethodCache getMethodCache(String methodName) throws NoSuchMethodException, ClassNotFoundException { + private MethodCache getMethodCache(String methodName) throws NoSuchMethodException { MethodCache methodCache = methodCacheMap.get(methodName); if (null != methodCache) { return methodCache; @@ -101,27 +108,36 @@ public class ServiceInvoker { } } - private Cache getServiceCache(String serviceName) { + private Cache getServiceCache(String serviceName) throws NoSuchServiceException { Cache serviceCache = serviceCacheMap.get(serviceName); - if (null == serviceCache) { + if (null != serviceCache) { + return serviceCache; + } + try { Object bean = context.getBean(serviceName); serviceCache = new Cache(bean); serviceCacheMap.put(serviceName, serviceCache); + } catch (BeansException e) { + throw new NoSuchServiceException(); } return serviceCache; } - private Object getValue(Type parameterType, String json) throws IOException { + private Object getValue(Type parameterType, String json) throws InvalidParameterException { JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType); if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) { - return objectMapper.readValue(json, targetType); + try { + return objectMapper.readValue(json, targetType); + } catch (IOException e) { + throw new InvalidParameterException(); + } } return objectMapper.convertValue(json, targetType); } - private Object[] getParameters(Type[] parameterTypes, List params) throws IOException { + private Object[] getParameters(Type[] parameterTypes, List params) throws InvalidParameterException { if (null == parameterTypes || null == params) { return null; } @@ -140,18 +156,40 @@ public class ServiceInvoker { } - public String invoke(String serviceName, String methodName, List params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException { + public String invoke(String serviceName, String methodName, List params) throws Throwable { Cache serviceCache = getServiceCache(serviceName); Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName); + if (null == methodCache.parameterTypes) { + if (null != params && 0 < params.size()) { + throw new InvalidParameterException(); + } + } else { + if (methodCache.parameterTypes.length != params.size()) { + throw new InvalidParameterException(); + } + } + Object[] parameters = null; if (null != methodCache.parameterTypes && 0 < methodCache.parameterTypes.length) { parameters = getParameters(methodCache.parameterTypes, params); } - Object result = methodCache.method.invoke(serviceCache.getBean(), parameters); + Object result = null; + String jsonInString = null; + try { + result = methodCache.method.invoke(serviceCache.getBean(), parameters); + } catch (IllegalAccessException e) { + throw new InvalidRequestException(); + } catch (InvocationTargetException e) { + throw e.getTargetException(); + } - String jsonInString = objectMapper.writeValueAsString(result); + try { + jsonInString = objectMapper.writeValueAsString(result); + } catch (IOException e) { + throw new InvalidRequestException(); + } return jsonInString; } diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java index 15aecb8..d6c140e 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java @@ -4,11 +4,11 @@ import com.loafle.overflow.api.OverflowApiServerGrpc; import com.loafle.overflow.api.ServerInput; import com.loafle.overflow.api.ServerOutput; import io.grpc.ServerBuilder; +import io.grpc.StatusException; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.util.logging.Logger; /** @@ -66,34 +66,20 @@ public class ServiceProxy { String result = null; try { - result =this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList()); + result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList()); ServerOutput reply = ServerOutput.newBuilder() .setResult(result) .build(); responseObserver.onNext(reply); responseObserver.onCompleted(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); + } catch (StatusException e) { + logger.warning(e.toString()); responseObserver.onError(e); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - responseObserver.onError(e); - } catch (IOException e) { - e.printStackTrace(); - responseObserver.onError(e); - } catch (InvocationTargetException e) { - e.printStackTrace(); - responseObserver.onError(e); - } catch (IllegalAccessException e) { - e.printStackTrace(); - responseObserver.onError(e); - } catch (Exception e) { - e.printStackTrace(); + } catch (Throwable e) { responseObserver.onError(e); } - } } diff --git a/src/main/java/com/loafle/overflow/proxy/exception/InvalidParameterException.java b/src/main/java/com/loafle/overflow/proxy/exception/InvalidParameterException.java new file mode 100644 index 0000000..c5e6ae2 --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/exception/InvalidParameterException.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.proxy.exception; + +import com.loafle.overflow.commons.exception.OverflowException; +import io.grpc.Status; + +public class InvalidParameterException extends OverflowException { + public InvalidParameterException() { + super(Status.fromCode(Status.Code.INVALID_ARGUMENT).withDescription(InvalidParameterException.class.getSimpleName())); + } +} diff --git a/src/main/java/com/loafle/overflow/proxy/exception/InvalidRequestException.java b/src/main/java/com/loafle/overflow/proxy/exception/InvalidRequestException.java new file mode 100644 index 0000000..7d89fca --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/exception/InvalidRequestException.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.proxy.exception; + +import com.loafle.overflow.commons.exception.OverflowException; +import io.grpc.Status; + +public class InvalidRequestException extends OverflowException { + public InvalidRequestException() { + super(Status.fromCode(Status.Code.INTERNAL).withDescription(InvalidRequestException.class.getSimpleName())); + } +} diff --git a/src/main/java/com/loafle/overflow/proxy/exception/NoSuchMethodException.java b/src/main/java/com/loafle/overflow/proxy/exception/NoSuchMethodException.java new file mode 100644 index 0000000..003c74a --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/exception/NoSuchMethodException.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.proxy.exception; + +import com.loafle.overflow.commons.exception.OverflowException; +import io.grpc.Status; + +public class NoSuchMethodException extends OverflowException { + public NoSuchMethodException() { + super(Status.fromCode(Status.Code.UNIMPLEMENTED).withDescription(NoSuchMethodException.class.getSimpleName())); + } +} diff --git a/src/main/java/com/loafle/overflow/proxy/exception/NoSuchServiceException.java b/src/main/java/com/loafle/overflow/proxy/exception/NoSuchServiceException.java new file mode 100644 index 0000000..f84cd73 --- /dev/null +++ b/src/main/java/com/loafle/overflow/proxy/exception/NoSuchServiceException.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.proxy.exception; + +import com.loafle.overflow.commons.exception.OverflowException; +import io.grpc.Status; + +public class NoSuchServiceException extends OverflowException { + public NoSuchServiceException() { + super(Status.fromCode(Status.Code.UNAVAILABLE).withDescription(NoSuchServiceException.class.getSimpleName())); + } +} diff --git a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java index f629c05..3872f80 100644 --- a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java +++ b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java @@ -27,7 +27,7 @@ public class ServiceInvokerTest { private ServiceInvoker serviceInvoker; @Test - public void invoke() throws Exception { + public void invoke() throws Throwable { // List params = new ArrayList<>(2); // params.add(ByteString.copyFromUtf8("[{\"id\": 1," +