use of com.baidu.dsp.common.exception.DocumentNotFoundException in project disconf by knightliao.
the class ConfigFetcherController method getFile.
/**
* 获取配置文件
*
* @return
*/
@NoAuth
@RequestMapping(value = "/file", method = RequestMethod.GET)
@ResponseBody
public HttpEntity<byte[]> getFile(ConfForm confForm) {
boolean hasError = false;
//
// 校验
//
ConfigFullModel configModel = null;
try {
configModel = configValidator4Fetch.verifyConfForm(confForm, false);
} catch (Exception e) {
LOG.error(e.toString());
hasError = true;
}
if (hasError == false) {
try {
//
Config config = configFetchMgr.getConfByParameter(configModel.getApp().getId(), configModel.getEnv().getId(), configModel.getVersion(), configModel.getKey(), DisConfigTypeEnum.FILE);
if (config == null) {
hasError = true;
throw new DocumentNotFoundException(configModel.getKey());
}
//API获取节点内容也需要同样做格式转换
return downloadDspBill(configModel.getKey(), config.getValue());
} catch (Exception e) {
LOG.error(e.toString());
}
}
if (confForm.getKey() != null) {
throw new DocumentNotFoundException(confForm.getKey());
} else {
throw new DocumentNotFoundException("");
}
}
use of com.baidu.dsp.common.exception.DocumentNotFoundException in project disconf by knightliao.
the class MyExceptionHandler method resolveException.
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
LOG.warn(request.getRequestURI() + " ExceptionHandler FOUND. " + e.toString() + "\t" + e.getCause());
// PathVariable 出错
if (e instanceof TypeMismatchException) {
return getParamErrors((TypeMismatchException) e);
// Bean 参数无法映射错误
} else if (e instanceof InvalidPropertyException) {
return getParamErrors((InvalidPropertyException) e);
// @Valid 出错
} else if (e instanceof BindException) {
return ParamValidateUtils.getParamErrors((BindException) e);
// 业务校验处理
} else if (e instanceof FieldException) {
return getParamErrors((FieldException) e);
} else if (e instanceof DocumentNotFoundException) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
try {
FileUtils.closeWriter(response.getWriter());
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
// 用户没有请求方法的访问权限
} else if (e instanceof AccessDeniedException) {
LOG.warn("details: " + ((AccessDeniedException) e).getErrorMessage());
return buildError("auth.access.denied", ErrorCode.ACCESS_NOAUTH_ERROR);
} else if (e instanceof HttpRequestMethodNotSupportedException) {
return buildError("syserror.httpmethod", ErrorCode.HttpRequestMethodNotSupportedException);
} else if (e instanceof MissingServletRequestParameterException) {
return buildError("syserror.param.miss", ErrorCode.MissingServletRequestParameterException);
} else if (e instanceof GlobalExceptionAware) {
LOG.error("details: ", e);
GlobalExceptionAware g = (GlobalExceptionAware) e;
return buildError(g.getErrorMessage(), g.getErrorCode());
} else {
LOG.warn("details: ", e);
return buildError("syserror.inner", ErrorCode.GLOBAL_ERROR);
}
}
use of com.baidu.dsp.common.exception.DocumentNotFoundException in project disconf by knightliao.
the class ConfigReadController method downloadDspBill.
/**
* 下载
*
* @param configId
*
* @return
*/
@RequestMapping(value = "/download/{configId}", method = RequestMethod.GET)
public HttpEntity<byte[]> downloadDspBill(@PathVariable long configId) {
// 业务校验
configValidator.valideConfigExist(configId);
ConfListVo config = configMgr.getConfVo(configId);
HttpHeaders header = new HttpHeaders();
byte[] res = config.getValue().getBytes();
if (res == null) {
throw new DocumentNotFoundException(config.getKey());
}
String name = null;
try {
name = URLEncoder.encode(config.getKey(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
header.set("Content-Disposition", "attachment; filename=" + name);
header.setContentLength(res.length);
return new HttpEntity<byte[]>(res, header);
}
use of com.baidu.dsp.common.exception.DocumentNotFoundException in project disconf by knightliao.
the class ConfigReadController method download2.
/**
* 批量下载配置文件
*
* @param confListForm
*
* @return
*/
@RequestMapping(value = "/downloadfilebatch", method = RequestMethod.GET)
public HttpEntity<byte[]> download2(@Valid ConfListForm confListForm) {
LOG.info(confListForm.toString());
//
// get files
//
List<File> fileList = configMgr.getDisconfFileList(confListForm);
//
// prefix
//
String prefixString = "APP" + confListForm.getAppId() + "_" + "ENV" + confListForm.getEnvId() + "_" + "VERSION" + confListForm.getVersion();
HttpHeaders header = new HttpHeaders();
String targetFileString = "";
File targetFile = null;
byte[] res = null;
try {
targetFileString = TarUtils.tarFiles("tmp", prefixString, fileList);
targetFile = new File(targetFileString);
res = IOUtils.toByteArray(new FileInputStream(targetFile));
} catch (Exception e) {
throw new DocumentNotFoundException("");
}
header.set("Content-Disposition", "attachment; filename=" + targetFile.getName());
header.setContentLength(res.length);
return new HttpEntity<byte[]>(res, header);
}
Aggregations