Search in sources :

Example 1 with TeslaException

use of io.github.tesla.ops.common.TeslaException in project tesla by linking12.

the class FilterRouteController method save.

@Log("保存路由")
@RequiresPermissions("filter:route:add")
@PostMapping("/save")
@ResponseBody()
public CommonResponse save(RouteVo zuulVo, @RequestParam(name = "zipFile", required = false) MultipartFile zipFile) {
    try {
        // grpc路由
        if (zipFile != null) {
            InputStream directoryZipStream = zipFile.getInputStream();
            CommonResponse response = judgeFileType(directoryZipStream, "zip");
            if (response != null) {
                return response;
            } else {
                String serviceFileName = zuulVo.getServiceFileName();
                byte[] protoContext = protobufService.compileDirectoryProto(zipFile, serviceFileName);
                FilterRouteDto zuulDto = zuulVo.buildRouteDto();
                zuulDto.setProtoContext(protoContext);
                routeService.save(zuulDto);
            }
        } else {
            FilterRouteDto zuulDto = zuulVo.buildRouteDto();
            routeService.save(zuulDto);
        }
    } catch (IOException e) {
        throw new TeslaException("保存路由失败", e);
    }
    return CommonResponse.ok();
}
Also used : InputStream(java.io.InputStream) FilterRouteDto(io.github.tesla.ops.filter.dto.FilterRouteDto) IOException(java.io.IOException) CommonResponse(io.github.tesla.ops.common.CommonResponse) TeslaException(io.github.tesla.ops.common.TeslaException) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(io.github.tesla.ops.common.Log) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with TeslaException

use of io.github.tesla.ops.common.TeslaException in project tesla by linking12.

the class FilterRouteController method update.

@Log("更新路由")
@RequiresPermissions("filter:route:edit")
@PostMapping("/update")
@ResponseBody()
public CommonResponse update(RouteVo zuulVo, @RequestParam(name = "zipFile", required = false) MultipartFile zipFile) {
    try {
        // grpc路由
        if (zipFile != null) {
            InputStream directoryZipStream = zipFile.getInputStream();
            CommonResponse response = judgeFileType(directoryZipStream, "zip");
            if (response != null) {
                return response;
            } else {
                String serviceFileName = zuulVo.getServiceFileName();
                byte[] protoContext = protobufService.compileDirectoryProto(zipFile, serviceFileName);
                FilterRouteDto zuulDto = zuulVo.buildRouteDto();
                zuulDto.setProtoContext(protoContext);
                routeService.update(zuulDto);
            }
        } else {
            FilterRouteDto zuulDto = zuulVo.buildRouteDto();
            routeService.update(zuulDto);
        }
    } catch (IOException e) {
        throw new TeslaException("保存路由失败", e);
    }
    return CommonResponse.ok();
}
Also used : InputStream(java.io.InputStream) FilterRouteDto(io.github.tesla.ops.filter.dto.FilterRouteDto) IOException(java.io.IOException) CommonResponse(io.github.tesla.ops.common.CommonResponse) TeslaException(io.github.tesla.ops.common.TeslaException) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(io.github.tesla.ops.common.Log) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with TeslaException

use of io.github.tesla.ops.common.TeslaException in project tesla by linking12.

the class ProtobufServiceImpl method runProtoc.

private byte[] runProtoc(String includePath, String protoFilePath) {
    try {
        Path descriptorPath = Files.createTempFile("descriptor", ".pb.bin");
        ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
        if (includePath != null) {
            builder.add("-I" + includePath);
        }
        builder.add("--descriptor_set_out=" + descriptorPath.toAbsolutePath().toString());
        ImmutableList<String> protocArgs = builder.add(protoFilePath).build();
        int status = Protoc.runProtoc(protocArgs.toArray(new String[0]));
        if (status != 0) {
            throw new IllegalArgumentException(String.format("Got exit code [%d] from protoc with args [%s]", status, protocArgs));
        }
        return Files.readAllBytes(descriptorPath);
    } catch (IOException | InterruptedException e) {
        throw new TeslaException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) TeslaException(io.github.tesla.ops.common.TeslaException)

Example 4 with TeslaException

use of io.github.tesla.ops.common.TeslaException in project tesla by linking12.

the class ProtobufServiceImpl method compileDirectoryProto.

@Override
public byte[] compileDirectoryProto(MultipartFile directoryZipStream, String serviceFileName) {
    String fileDirectory = null;
    try {
        fileDirectory = this.uploadZipFile(directoryZipStream, serviceFileName);
        ProtoFileServiceFilter filter = new ProtoFileServiceFilter(serviceFileName);
        Files.walkFileTree(Paths.get(fileDirectory), filter);
        String protoFilePath = filter.getProtoFilePath();
        return this.runProtoc(fileDirectory, protoFilePath);
    } catch (IOException e) {
        throw new TeslaException(e.getMessage(), e);
    } finally {
        if (fileDirectory != null) {
            FileUtils.deleteQuietly(new File(fileDirectory));
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) TeslaException(io.github.tesla.ops.common.TeslaException)

Aggregations

TeslaException (io.github.tesla.ops.common.TeslaException)4 IOException (java.io.IOException)4 CommonResponse (io.github.tesla.ops.common.CommonResponse)2 Log (io.github.tesla.ops.common.Log)2 FilterRouteDto (io.github.tesla.ops.filter.dto.FilterRouteDto)2 InputStream (java.io.InputStream)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 Path (java.nio.file.Path)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1