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;
}
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;
}
}
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;
}
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("不允许执行的操作!");
}
}
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);
}
Aggregations