Search in sources :

Example 21 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RestPackAdvice method beforeBodyWrite.

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    // 不是 RestPack 处理的请求,就不需要特殊处理。
    if (!RestPackAspect.isRestPack()) {
        return body;
    }
    HttpResult result = null;
    Exception e = ExceptionHolder.get();
    if (e != null) {
        BusinessException be = convert(e);
        result = HttpResult.fail(be);
    } else {
        // 将原始返回值包裹在 HttpResult 对象中。
        result = HttpResult.success();
        if (body != null) {
            RestPackUtils.clearIgnoreFields(body);
            result.setData(body);
        }
    }
    setHttpResult(result);
    Logger log = RestPackAspect.getLog();
    if (log != null && log.isInfoEnabled()) {
        log.info("request '{}' end, response:\n{}", MDC.get("requestPath"), result);
    }
    RestPackAspect.clearThreadLocal();
    return result;
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) Logger(org.slf4j.Logger) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) BusinessException(com.terran4j.commons.util.error.BusinessException)

Example 22 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RefluxServerEndpoint method onOpen.

@OnOpen
public final void onOpen(Session session) throws BusinessException {
    if (connectionManager == null) {
        throw new BusinessException(ErrorCodes.INTERNAL_ERROR).setMessage("Server not inited...");
    }
    this.session = session;
    String clientId = getClientId(session);
    if (!authenticate(clientId)) {
        close(session);
        return;
    }
    try {
        client = connectionManager.onOpen(clientId, this);
    } catch (BusinessException e) {
        close(session);
        return;
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException)

Example 23 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RefluxServerImpl method onOpen.

public ClientConnectionInfo onOpen(String clientId, RefluxServerEndpoint conn) throws BusinessException {
    if (StringUtils.isEmpty(clientId)) {
        throw new BusinessException(ErrorCodes.NULL_PARAM).put("clientId", clientId);
    }
    ClientConnectionInfo connInfo = connectionInfoes.get(clientId);
    if (connInfo == null) {
        // 首次连接,保留连接信息。
        connInfo = new ClientConnectionInfo();
        connInfo.setClientId(clientId);
        connInfo.setConnectedTime(System.currentTimeMillis());
        connInfo.setConnection(conn);
        connectionInfoes.put(clientId, connInfo);
        if (log.isInfoEnabled()) {
            log.info("有新连接加入! 当前连接数为 {}", getConnectionCount());
        }
    } else {
        // 重新连接,只更新连接对象就可以了。
        connectionInfoes.get(clientId).setConnection(conn);
        if (log.isInfoEnabled()) {
            log.info("重新建立连接! 当前连接数为 {}", getConnectionCount());
        }
    }
    // 开启推送开关。
    conn.setSendable(true);
    return connInfo;
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException)

Example 24 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RestPackDemoAspect method doBefore.

@Before("httpDemoPackAspect()")
public void doBefore(JoinPoint point) throws BusinessException {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes servlet = (ServletRequestAttributes) requestAttributes;
    HttpServletRequest httpRequest = servlet.getRequest();
    String f = httpRequest.getParameter("f");
    if ("e".equals(f)) {
        throw new BusinessException(ErrorCodes.ACCESS_DENY).setMessage("不允许执行的操作!");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.terran4j.commons.util.error.BusinessException) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 25 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class MessageHandler method onMessage.

public String onMessage(String message) throws BusinessException {
    JsonElement element = jsonParser.parse(message);
    JsonObject json = element.getAsJsonObject();
    Message result = new Message();
    String msgId = json.get("id").getAsString();
    result.setId(msgId);
    String msgType = json.get("type").getAsString();
    result.setType(msgType);
    OnMessageInvoker invoker = invokers.get(msgType);
    if (invoker == null) {
        throw // 
        new BusinessException(ErrorCodes.RESOURCE_NOT_FOUND).put("msgType", msgType);
    }
    String msgContent = json.get("content").toString();
    Object param = gson.fromJson(msgContent, invoker.paramType);
    Object reply = null;
    try {
        reply = invoker.method.invoke(invoker.bean, param);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw // 
        new BusinessException(CommonErrorCode.INTERNAL_ERROR, e).put("msgType", msgType).put("msgContent", msgContent);
    }
    if (reply == null) {
        return null;
    }
    String content = reply.toString();
    result.setContent(content);
    result.setStatus(0);
    return gson.toJson(result);
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) Message(com.terran4j.commons.reflux.Message) OnMessage(com.terran4j.commons.reflux.OnMessage) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BusinessException (com.terran4j.commons.util.error.BusinessException)25 Gson (com.google.gson.Gson)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 Method (java.lang.reflect.Method)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 Logger (org.slf4j.Logger)3 IOException (java.io.IOException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 Test (org.junit.Test)2 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Message (com.terran4j.commons.reflux.Message)1 OnMessage (com.terran4j.commons.reflux.OnMessage)1