Search in sources :

Example 1 with DocumentNotFoundException

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("");
    }
}
Also used : DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) Config(com.baidu.disconf.web.service.config.bo.Config) ConfigFullModel(com.baidu.disconf.web.web.config.dto.ConfigFullModel) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoAuth(com.baidu.dsp.common.annotation.NoAuth) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with 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);
    }
}
Also used : AccessDeniedException(com.baidu.dsp.common.exception.AccessDeniedException) FieldException(com.baidu.dsp.common.exception.FieldException) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) GlobalExceptionAware(com.baidu.dsp.common.exception.base.GlobalExceptionAware) TypeMismatchException(org.springframework.beans.TypeMismatchException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) BindException(org.springframework.validation.BindException) IOException(java.io.IOException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

Example 3 with DocumentNotFoundException

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ConfListVo(com.baidu.disconf.web.service.config.vo.ConfListVo) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) HttpEntity(org.springframework.http.HttpEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with DocumentNotFoundException

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) HttpEntity(org.springframework.http.HttpEntity) File(java.io.File) FileInputStream(java.io.FileInputStream) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DocumentNotFoundException (com.baidu.dsp.common.exception.DocumentNotFoundException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 Config (com.baidu.disconf.web.service.config.bo.Config)1 ConfListVo (com.baidu.disconf.web.service.config.vo.ConfListVo)1 ConfigFullModel (com.baidu.disconf.web.web.config.dto.ConfigFullModel)1 NoAuth (com.baidu.dsp.common.annotation.NoAuth)1 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1 FieldException (com.baidu.dsp.common.exception.FieldException)1 GlobalExceptionAware (com.baidu.dsp.common.exception.base.GlobalExceptionAware)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InvalidPropertyException (org.springframework.beans.InvalidPropertyException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 BindException (org.springframework.validation.BindException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)1