From eaaef6f3655500b8d01044f0ce58a07b53d3910e Mon Sep 17 00:00:00 2001 From: v_kkhuang <420895376@qq.com> Date: Wed, 18 Oct 2023 14:54:57 +0800 Subject: [PATCH 1/5] push sup ecm down log --- .../linkis/ecm/restful/ECMRestfulApi.java | 124 ++++++++++++++++++ .../gateway/config/GatewayConfiguration.scala | 3 + .../ujes/parser/ECMRequestGatewayParser.scala | 83 ++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java create mode 100644 linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java new file mode 100644 index 0000000000..ff39c50400 --- /dev/null +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.linkis.ecm.restful; + +import org.apache.linkis.common.utils.JsonUtils; +import org.apache.linkis.ecm.server.exception.ECMErrorException; + +import org.apache.commons.io.IOUtils; +import org.apache.http.Consts; + +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import io.swagger.annotations.Api; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.LOG_IS_NOT_EXISTS; + +@Api(tags = "ECM") +@RequestMapping(path = "/engineconnManager") +@RestController +public class ECMRestfulApi { + + private final Logger logger = LoggerFactory.getLogger(ECMRestfulApi.class); + + @RequestMapping(path = "/downloadEngineLog", method = RequestMethod.POST) + public void downloadEngineLog( + HttpServletRequest req, HttpServletResponse response, @RequestBody JsonNode jsonNode) + throws IOException, ECMErrorException { + // String userName = ModuleUserUtils.getOperationUser(req, "openEngineLog"); + String emInstance = jsonNode.get("emInstance").asText(); + String instance = jsonNode.get("instance").asText(); + Map parameters = new HashMap<>(); + try { + parameters = + JsonUtils.jackson() + .readValue( + jsonNode.get("parameters").toString(), + new TypeReference>() {}); + } catch (JsonProcessingException e) { + logger.error( + "Fail to process the operation parameters: [{}] in request", + jsonNode.get("parameters").toString(), + e); + } + String logType = (String) parameters.get("logType"); + String logDirSuffix = (String) parameters.get("logDirSuffix"); + File inputFile = new File(logDirSuffix, logType); + if (!inputFile.exists()) { + throw new ECMErrorException( + LOG_IS_NOT_EXISTS.getErrorCode(), + MessageFormat.format(LOG_IS_NOT_EXISTS.getErrorDesc(), logDirSuffix)); + } else { + ServletOutputStream outputStream = null; + FileInputStream inputStream = null; + BufferedInputStream fis = null; + PrintWriter writer = null; + try { + inputStream = new FileInputStream(inputFile); + fis = new BufferedInputStream(inputStream); + byte[] buffer = new byte[1024]; + int bytesRead = 0; + response.reset(); + response.setCharacterEncoding(Consts.UTF_8.toString()); + java.nio.file.Path source = Paths.get(inputFile.getPath()); + response.addHeader("Content-Type", Files.probeContentType(source)); + response.addHeader( + "Content-Disposition", + "attachment;filename=" + instance.replace(":", "_") + "_" + logType + ".txt"); + outputStream = response.getOutputStream(); + while ((bytesRead = fis.read(buffer, 0, 1024)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } catch (IOException e) { + logger.error("download failed", e); + response.reset(); + response.setCharacterEncoding(Consts.UTF_8.toString()); + response.setContentType("text/plain; charset=utf-8"); + writer = response.getWriter(); + writer.append("error(错误):" + e.getMessage()); + writer.flush(); + } finally { + if (outputStream != null) { + outputStream.flush(); + } + IOUtils.closeQuietly(outputStream); + IOUtils.closeQuietly(fis); + IOUtils.closeQuietly(inputStream); + } + } + } +} diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-core/src/main/scala/org/apache/linkis/gateway/config/GatewayConfiguration.scala b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-core/src/main/scala/org/apache/linkis/gateway/config/GatewayConfiguration.scala index cace679033..c016b57094 100644 --- a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-core/src/main/scala/org/apache/linkis/gateway/config/GatewayConfiguration.scala +++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-core/src/main/scala/org/apache/linkis/gateway/config/GatewayConfiguration.scala @@ -110,4 +110,7 @@ object GatewayConfiguration { val LINKIS_CLUSTER_NAME = CommonVars("linkis.cluster.name", "") + val ENGINECONN_MANAGER_SPRING_NAME = + CommonVars("wds.linkis.engineconnmanager.name", "linkis-cg-engineconnmanager") + } diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala new file mode 100644 index 0000000000..cdd9171889 --- /dev/null +++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.linkis.gateway.ujes.parser + +import org.apache.linkis.common.ServiceInstance +import org.apache.linkis.gateway.config.GatewayConfiguration +import org.apache.linkis.gateway.http.GatewayContext +import org.apache.linkis.gateway.parser.AbstractGatewayParser +import org.apache.linkis.gateway.springcloud.SpringCloudGatewayConfiguration.{ + normalPath, + API_URL_PREFIX +} +import org.apache.linkis.protocol.utils.ZuulEntranceUtils + +import org.springframework.stereotype.Component + +@Component +class ECMRequestGatewayParser extends AbstractGatewayParser { + override def shouldContainRequestBody(gatewayContext: GatewayContext): Boolean = false + + override def parse(gatewayContext: GatewayContext): Unit = { + logger.info("start begin ECMRequestGatewayParser {}", gatewayContext) + gatewayContext.getRequest.getRequestURI match { + case ECMRequestGatewayParser.ECM_EXECUTION_REGEX(version, execId) => + logger.info("----------------------1") + if (sendResponseWhenNotMatchVersion(gatewayContext, version)) return + logger.info("----------------------2") + val serviceInstance = + if ( + gatewayContext.getRequest.getQueryParams.containsKey(ECMRequestGatewayParser.INSTANCE) + ) { + logger.info("----------------------3") + val instances = + gatewayContext.getRequest.getQueryParams.get(ECMRequestGatewayParser.INSTANCE) + logger.info("----------------------{}", instances) + if (null != instances && instances.length == 1) { + logger.info("----------------------4") + ServiceInstance( + GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, + instances(0) + ) + } else { + logger.info("----------------------5") + ServiceInstance(GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, null) + } + } else { + logger.info("----------------------22") + ServiceInstance(GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, null) + } + logger.info("----------------------serviceInstance {}", serviceInstance) + gatewayContext.getGatewayRoute.setServiceInstance(serviceInstance) + case _ => + } + } + +} + +object ECMRequestGatewayParser { + + val ECM_HEADER = + normalPath(API_URL_PREFIX) + "rest_[a-zA-Z][a-zA-Z_0-9]*/(v\\d+)/engineconnManager/" + + val ECM_EXECUTION_REGEX = + (ECM_HEADER + "(downloadEngineLog)").r + + val INSTANCE = "instance" + +} From 43f845326a71f4b6ba563d29af1d98c0119ccc84 Mon Sep 17 00:00:00 2001 From: v_kkhuang <420895376@qq.com> Date: Mon, 23 Oct 2023 14:41:28 +0800 Subject: [PATCH 2/5] push code --- .../errorcode/EngineconnServerErrorCodeSummary.java | 3 ++- .../org/apache/linkis/ecm/restful/ECMRestfulApi.java | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java index ca4412824d..c9c6b8ba4a 100644 --- a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java @@ -33,7 +33,8 @@ public enum EngineconnServerErrorCodeSummary implements LinkisErrorCode { 11110, "the parameters of engineConnInstance and ticketId are both not exists.(engineConnInstance 和ticketId 的参数都不存在.)"), LOG_IS_NOT_EXISTS(11110, "Log directory {0} does not exists.(日志目录 {0} 不存在.)"), - FAILED_TO_DOWNLOAD(911115, "failed to downLoad(下载失败)"); + FAILED_TO_DOWNLOAD(911115, "failed to downLoad(下载失败)"), + FILE_IS_OVERSIZE(911116, "Download file has exceeded 100MB(下载文件已经超过100M)"); /** (errorCode)错误码 */ private final int errorCode; diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java index ff39c50400..f42555f0e9 100644 --- a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java @@ -19,6 +19,7 @@ import org.apache.linkis.common.utils.JsonUtils; import org.apache.linkis.ecm.server.exception.ECMErrorException; +import org.apache.linkis.server.utils.ModuleUserUtils; import org.apache.commons.io.IOUtils; import org.apache.http.Consts; @@ -46,6 +47,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.FILE_IS_OVERSIZE; import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.LOG_IS_NOT_EXISTS; @Api(tags = "ECM") @@ -59,8 +61,7 @@ public class ECMRestfulApi { public void downloadEngineLog( HttpServletRequest req, HttpServletResponse response, @RequestBody JsonNode jsonNode) throws IOException, ECMErrorException { - // String userName = ModuleUserUtils.getOperationUser(req, "openEngineLog"); - String emInstance = jsonNode.get("emInstance").asText(); + ModuleUserUtils.getOperationUser(req, "downloadEngineLog"); String instance = jsonNode.get("instance").asText(); Map parameters = new HashMap<>(); try { @@ -83,6 +84,13 @@ public void downloadEngineLog( LOG_IS_NOT_EXISTS.getErrorCode(), MessageFormat.format(LOG_IS_NOT_EXISTS.getErrorDesc(), logDirSuffix)); } else { + long fileSizeInBytes = inputFile.length(); + long fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024); + if (fileSizeInMegabytes > 100) { + throw new ECMErrorException( + FILE_IS_OVERSIZE.getErrorCode(), + MessageFormat.format(FILE_IS_OVERSIZE.getErrorDesc(), logDirSuffix)); + } ServletOutputStream outputStream = null; FileInputStream inputStream = null; BufferedInputStream fis = null; From ee62c5b2f5efa17346a541deaf73df79611260ed Mon Sep 17 00:00:00 2001 From: v_kkhuang <420895376@qq.com> Date: Mon, 23 Oct 2023 14:45:50 +0800 Subject: [PATCH 3/5] push code --- .../gateway/ujes/parser/ECMRequestGatewayParser.scala | 8 -------- 1 file changed, 8 deletions(-) diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala index cdd9171889..4484d6ca5f 100644 --- a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala +++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala @@ -37,32 +37,24 @@ class ECMRequestGatewayParser extends AbstractGatewayParser { logger.info("start begin ECMRequestGatewayParser {}", gatewayContext) gatewayContext.getRequest.getRequestURI match { case ECMRequestGatewayParser.ECM_EXECUTION_REGEX(version, execId) => - logger.info("----------------------1") if (sendResponseWhenNotMatchVersion(gatewayContext, version)) return - logger.info("----------------------2") val serviceInstance = if ( gatewayContext.getRequest.getQueryParams.containsKey(ECMRequestGatewayParser.INSTANCE) ) { - logger.info("----------------------3") val instances = gatewayContext.getRequest.getQueryParams.get(ECMRequestGatewayParser.INSTANCE) - logger.info("----------------------{}", instances) if (null != instances && instances.length == 1) { - logger.info("----------------------4") ServiceInstance( GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, instances(0) ) } else { - logger.info("----------------------5") ServiceInstance(GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, null) } } else { - logger.info("----------------------22") ServiceInstance(GatewayConfiguration.ENGINECONN_MANAGER_SPRING_NAME.getValue, null) } - logger.info("----------------------serviceInstance {}", serviceInstance) gatewayContext.getGatewayRoute.setServiceInstance(serviceInstance) case _ => } From 45221b94fd720b519383da58625b96bacdaed7cd Mon Sep 17 00:00:00 2001 From: v_kkhuang <420895376@qq.com> Date: Wed, 25 Oct 2023 15:08:26 +0800 Subject: [PATCH 4/5] Code optimization --- .../EngineconnServerErrorCodeSummary.java | 3 +- .../linkis/ecm/restful/ECMRestfulApi.java | 73 +++++++++++-------- .../ujes/parser/ECMRequestGatewayParser.scala | 2 +- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java index c9c6b8ba4a..463cdecc84 100644 --- a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/errorcode/EngineconnServerErrorCodeSummary.java @@ -34,7 +34,8 @@ public enum EngineconnServerErrorCodeSummary implements LinkisErrorCode { "the parameters of engineConnInstance and ticketId are both not exists.(engineConnInstance 和ticketId 的参数都不存在.)"), LOG_IS_NOT_EXISTS(11110, "Log directory {0} does not exists.(日志目录 {0} 不存在.)"), FAILED_TO_DOWNLOAD(911115, "failed to downLoad(下载失败)"), - FILE_IS_OVERSIZE(911116, "Download file has exceeded 100MB(下载文件已经超过100M)"); + FILE_IS_OVERSIZE(911116, "Download file has exceeded 100MB(下载文件已超过100M)"), + PARAMETER_NOT_NULL(911117, "Parameter {0} cannot be empty (参数 {0} 不能为空)"); /** (errorCode)错误码 */ private final int errorCode; diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java index f42555f0e9..528944d903 100644 --- a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java @@ -17,17 +17,15 @@ package org.apache.linkis.ecm.restful; -import org.apache.linkis.common.utils.JsonUtils; import org.apache.linkis.ecm.server.exception.ECMErrorException; +import org.apache.linkis.server.Message; import org.apache.linkis.server.utils.ModuleUserUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.http.Consts; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; @@ -37,18 +35,16 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Map; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.FILE_IS_OVERSIZE; -import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.LOG_IS_NOT_EXISTS; +import static org.apache.linkis.ecm.errorcode.EngineconnServerErrorCodeSummary.*; @Api(tags = "ECM") @RequestMapping(path = "/engineconnManager") @@ -57,27 +53,42 @@ public class ECMRestfulApi { private final Logger logger = LoggerFactory.getLogger(ECMRestfulApi.class); - @RequestMapping(path = "/downloadEngineLog", method = RequestMethod.POST) + @ApiOperation( + value = "downloadEngineLog", + notes = "download engine log", + response = Message.class) + @ApiImplicitParams({ + @ApiImplicitParam(name = "emInstance", required = true, dataType = "String"), + @ApiImplicitParam(name = "instance", required = true, dataType = "String"), + @ApiImplicitParam(name = "logDirSuffix", required = true, dataType = "String"), + @ApiImplicitParam(name = "logType", required = true, dataType = "String") + }) + @ApiOperationSupport(ignoreParameters = {"json"}) + @RequestMapping(path = "/downloadEngineLog", method = RequestMethod.GET) public void downloadEngineLog( - HttpServletRequest req, HttpServletResponse response, @RequestBody JsonNode jsonNode) + HttpServletRequest req, + HttpServletResponse response, + @RequestParam(value = "emInstance") String emInstance, + @RequestParam(value = "instance") String instance, + @RequestParam(value = "logDirSuffix") String logDirSuffix, + @RequestParam(value = "logType") String logType) throws IOException, ECMErrorException { ModuleUserUtils.getOperationUser(req, "downloadEngineLog"); - String instance = jsonNode.get("instance").asText(); - Map parameters = new HashMap<>(); - try { - parameters = - JsonUtils.jackson() - .readValue( - jsonNode.get("parameters").toString(), - new TypeReference>() {}); - } catch (JsonProcessingException e) { - logger.error( - "Fail to process the operation parameters: [{}] in request", - jsonNode.get("parameters").toString(), - e); + if (StringUtils.isBlank(instance)) { + throw new ECMErrorException( + PARAMETER_NOT_NULL.getErrorCode(), + MessageFormat.format(PARAMETER_NOT_NULL.getErrorDesc(), "instance")); + } + if (StringUtils.isBlank(logDirSuffix)) { + throw new ECMErrorException( + PARAMETER_NOT_NULL.getErrorCode(), + MessageFormat.format(PARAMETER_NOT_NULL.getErrorDesc(), "logDirSuffix")); + } + if (StringUtils.isBlank(logType)) { + throw new ECMErrorException( + PARAMETER_NOT_NULL.getErrorCode(), + MessageFormat.format(PARAMETER_NOT_NULL.getErrorDesc(), "logType")); } - String logType = (String) parameters.get("logType"); - String logDirSuffix = (String) parameters.get("logDirSuffix"); File inputFile = new File(logDirSuffix, logType); if (!inputFile.exists()) { throw new ECMErrorException( @@ -100,19 +111,19 @@ public void downloadEngineLog( fis = new BufferedInputStream(inputStream); byte[] buffer = new byte[1024]; int bytesRead = 0; - response.reset(); response.setCharacterEncoding(Consts.UTF_8.toString()); java.nio.file.Path source = Paths.get(inputFile.getPath()); response.addHeader("Content-Type", Files.probeContentType(source)); response.addHeader( "Content-Disposition", "attachment;filename=" + instance.replace(":", "_") + "_" + logType + ".txt"); + response.addHeader("Content-Length", fileSizeInBytes + ""); outputStream = response.getOutputStream(); while ((bytesRead = fis.read(buffer, 0, 1024)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { - logger.error("download failed", e); + logger.error("download failed:", e); response.reset(); response.setCharacterEncoding(Consts.UTF_8.toString()); response.setContentType("text/plain; charset=utf-8"); diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala index 4484d6ca5f..e1e65cdbb5 100644 --- a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala +++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-server-support/src/main/scala/org/apache/linkis/gateway/ujes/parser/ECMRequestGatewayParser.scala @@ -70,6 +70,6 @@ object ECMRequestGatewayParser { val ECM_EXECUTION_REGEX = (ECM_HEADER + "(downloadEngineLog)").r - val INSTANCE = "instance" + val INSTANCE = "emInstance" } From 5739bd5ef1616e8bb8eba2c6d80f177e425d3a50 Mon Sep 17 00:00:00 2001 From: v_kkhuang <420895376@qq.com> Date: Tue, 31 Oct 2023 17:00:37 +0800 Subject: [PATCH 5/5] add annotation --- .../main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java | 1 + 1 file changed, 1 insertion(+) diff --git a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java index 528944d903..4b5ca65b0a 100644 --- a/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java +++ b/linkis-computation-governance/linkis-engineconn-manager/linkis-engineconn-manager-server/src/main/java/org/apache/linkis/ecm/restful/ECMRestfulApi.java @@ -114,6 +114,7 @@ public void downloadEngineLog( response.setCharacterEncoding(Consts.UTF_8.toString()); java.nio.file.Path source = Paths.get(inputFile.getPath()); response.addHeader("Content-Type", Files.probeContentType(source)); + // filename eg:bdpdws110002_11529_stdout.txt response.addHeader( "Content-Disposition", "attachment;filename=" + instance.replace(":", "_") + "_" + logType + ".txt");