peacewong commented on a change in pull request #1352: URL: https://github.com/apache/incubator-linkis/pull/1352#discussion_r795039070
########## File path: linkis-public-enhancements/linkis-datasource/linkis-datasource-manager/server/src/main/java/org/apache/linkis/datasourcemanager/core/formdata/CustomMultiPartFormDataTransformer.java ########## @@ -0,0 +1,309 @@ +/* + * 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.datasourcemanager.core.formdata; + +//import org.glassfish.jersey.media.multipart.FormDataBodyPart; +//import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +//import org.glassfish.jersey.media.multipart.FormDataMultiPart; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Custom Transformer of multipart form + */ +public class CustomMultiPartFormDataTransformer implements MultiPartFormDataTransformer{ + private static final Logger LOG = LoggerFactory.getLogger(CustomMultiPartFormDataTransformer.class); +// @Override +// public <T> T transformToObject(FormDataMultiPart formData, Class<?> clazz, +// Validator beanValidator) +// throws ValidationException, ErrorException { Review comment: Is it better to delete the code? ########## File path: linkis-public-enhancements/linkis-datasource/linkis-datasource-manager/server/src/main/java/org/apache/linkis/datasourcemanager/core/formdata/MultiPartFormDataTransformer.java ########## @@ -0,0 +1,41 @@ +/* + * 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.datasourcemanager.core.formdata; + +import org.apache.linkis.common.exception.ErrorException; + +import javax.validation.ValidationException; + +/** + * Transformer of multipart form + */ +public interface MultiPartFormDataTransformer { + /** + * Transform the form data to object and validate its fields + * @param formData form data + * @param clazz clazz Review comment: Is it better to delete the interface? ########## File path: linkis-public-enhancements/linkis-datasource/linkis-datasource-manager/server/src/main/java/org/apache/linkis/datasourcemanager/core/restful/DataSourceAdminRestfulApi.java ########## @@ -0,0 +1,230 @@ +/* + * 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.datasourcemanager.core.restful; + +import org.apache.linkis.common.exception.ErrorException; +import org.apache.linkis.datasourcemanager.common.domain.DataSourceEnv; +import org.apache.linkis.datasourcemanager.common.domain.DataSourceParamKeyDefinition; +import org.apache.linkis.datasourcemanager.core.formdata.FormDataTransformerFactory; +import org.apache.linkis.datasourcemanager.core.formdata.MultiPartFormDataTransformer; +import org.apache.linkis.datasourcemanager.core.service.DataSourceInfoService; +import org.apache.linkis.datasourcemanager.core.service.DataSourceRelateService; +import org.apache.linkis.datasourcemanager.core.validate.ParameterValidator; +import org.apache.linkis.datasourcemanager.core.vo.DataSourceEnvVo; +import org.apache.linkis.server.Message; +import org.apache.linkis.server.security.SecurityFilter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validator; +import javax.validation.groups.Default; +import java.util.Calendar; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@RestController +@RequestMapping(value = "/data-source-manager",produces = {"application/json"}) +public class DataSourceAdminRestfulApi { + + @Autowired + private DataSourceInfoService dataSourceInfoService; + + @Autowired + private DataSourceRelateService dataSourceRelateService; + + @Autowired + private ParameterValidator parameterValidator; + + @Autowired + private Validator beanValidator; + + private MultiPartFormDataTransformer formDataTransformer; + + @PostConstruct + public void initRestful(){ + this.formDataTransformer = FormDataTransformerFactory.buildCustom(); + } + + @RequestMapping(value = "/env/json",method = RequestMethod.POST) + public Message insertJsonEnv(@RequestBody DataSourceEnv dataSourceEnv, HttpServletRequest req) throws ErrorException { + return RestfulApiHelper.doAndResponse(()-> { + String userName = SecurityFilter.getLoginUsername(req); + if (!RestfulApiHelper.isAdminUser(userName)) { + return Message.error("User '" + userName + "' is not admin user[非管理员用户]"); + } + //Bean validation + Set<ConstraintViolation<DataSourceEnv>> result = beanValidator.validate(dataSourceEnv, Default.class); + if (result.size() > 0) { + throw new ConstraintViolationException(result); + } + dataSourceEnv.setCreateUser(userName); + insertDataSourceEnv(dataSourceEnv); + return Message.ok().data("insert_id", dataSourceEnv.getId()); + },"/data-source-manager/env/json", "Fail to insert data source environment[新增数据源环境失败]"); + } + +// @RequestMapping(value = "/env/form",method = RequestMethod.POST) +// public Message insertFormEnv(FormDataMultiPart multiPartForm, HttpServletRequest request) throws ErrorException { +// String userName = SecurityFilter.getLoginUsername(request); +// if(!RestfulApiHelper.isAdminUser(userName)){ +// return Message.error("User '" + userName + "' is not admin user[非管理员用户]"); +// } +// DataSourceEnv dataSourceEnv = formDataTransformer.transformToObject(multiPartForm, DataSourceEnv.class, beanValidator); +// dataSourceEnv.setCreateUser(userName); +// insertDataSourceEnv(dataSourceEnv); +// return Message.ok().data("insert_id", dataSourceEnv.getId()); +// } + + @RequestMapping(value = "/env_list/all/type/{type_id}",method = RequestMethod.GET) + public Message getAllEnvListByDataSourceType(@PathVariable("type_id")Long typeId){ + return RestfulApiHelper.doAndResponse(()-> { + List<DataSourceEnv> envList = dataSourceInfoService.listDataSourceEnvByType(typeId); + return Message.ok().data("env_list", envList); + }, "/data-source-manager/env_list/all/type/" + typeId, "Fail to get data source environment list[获取数据源环境清单失败]"); + } + + @RequestMapping (value = "/env/{env_id}",method = RequestMethod.GET) + public Message getEnvEntityById(@PathVariable("env_id")Long envId){ + return RestfulApiHelper.doAndResponse(()-> { + DataSourceEnv dataSourceEnv = dataSourceInfoService.getDataSourceEnv(envId); + return Message.ok().data("env", dataSourceEnv); + }, "/data-source-manager/env/" + envId, "Fail to get data source environment[获取数据源环境信息失败]"); + } + + @RequestMapping(value = "/env/{env_id}",method = RequestMethod.DELETE) + public Message removeEnvEntity(@PathVariable("env_id")Long envId, + HttpServletRequest request){ + return RestfulApiHelper.doAndResponse(() -> { + String userName = SecurityFilter.getLoginUsername(request); + if (!RestfulApiHelper.isAdminUser(userName)) { + return Message.error("User '" + userName + "' is not admin user[非管理员用户]"); + } + Long removeId = dataSourceInfoService.removeDataSourceEnv(envId); + if (removeId < 0) { + return Message.error("Fail to remove data source environment[删除数据源环境信息失败], [id:" + + envId + "]"); + } + return Message.ok().data("remove_id", removeId); + }, "/data-source-manager/env/" + envId, "Fail to remove data source environment[删除数据源环境信息失败]"); + } + + @RequestMapping(value = "/env/{env_id}/json",method = RequestMethod.PUT) + public Message updateJsonEnv(@RequestBody DataSourceEnv dataSourceEnv, + @PathVariable("env_id")Long envId, + HttpServletRequest request) throws ErrorException { + return RestfulApiHelper.doAndResponse(() -> { + String userName = SecurityFilter.getLoginUsername(request); + if (!RestfulApiHelper.isAdminUser(userName)) { + return Message.error("User '" + userName + "' is not admin user[非管理员用户]"); + } + //Bean validation + Set<ConstraintViolation<DataSourceEnv>> result = beanValidator.validate(dataSourceEnv, Default.class); + if (result.size() > 0) { + throw new ConstraintViolationException(result); + } + dataSourceEnv.setId(envId); + dataSourceEnv.setModifyUser(userName); + dataSourceEnv.setModifyTime(Calendar.getInstance().getTime()); + DataSourceEnv storedDataSourceEnv = dataSourceInfoService.getDataSourceEnv(envId); + if (null == storedDataSourceEnv) { + return Message.error("Fail to update data source environment[更新数据源环境失败], " + "[Please check the id:'" + + envId + " is correct ']"); + } + dataSourceEnv.setCreateUser(storedDataSourceEnv.getCreateUser()); + updateDataSourceEnv(dataSourceEnv, storedDataSourceEnv); + return Message.ok().data("update_id", envId); + }, "/data-source-manager/env/" + envId + "/json", "Fail to update data source environment[更新数据源环境失败]"); + } + +// @RequestMapping(value = "/env/{env_id}/form",method = RequestMethod.PUT,consumes = MediaType.MULTIPART_FORM_DATA_VALUE) +// public Message updateFormEnv(FormDataMultiPart multiPartForm, +// @PathVariable("env_id")Long envId, +// HttpServletRequest request) throws ErrorException { +// if(null != multiPartForm) { +// String userName = SecurityFilter.getLoginUsername(request); +// if (!RestfulApiHelper.isAdminUser(userName)) { +// return Message.error("User '" + userName + "' is not admin user[非管理员用户]"); +// } +// DataSourceEnv dataSourceEnv = formDataTransformer.transformToObject(multiPartForm, DataSourceEnv.class, beanValidator); +// dataSourceEnv.setId(envId); +// dataSourceEnv.setModifyUser(userName); +// dataSourceEnv.setModifyTime(Calendar.getInstance().getTime()); Review comment: Is it better to delete the code? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
