ResourcesController.java 36.6 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
17

Q
qiaozhanwei 已提交
18
package org.apache.dolphinscheduler.api.controller;
L
ligang 已提交
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import static org.apache.dolphinscheduler.api.enums.Status.AUTHORIZED_FILE_RESOURCE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.AUTHORIZED_UDF_FUNCTION_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.AUTHORIZE_RESOURCE_TREE;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_RESOURCE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_RESOURCE_FILE_ON_LINE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_UDF_FUNCTION_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.DELETE_RESOURCE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.DELETE_UDF_FUNCTION_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.DOWNLOAD_RESOURCE_FILE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.EDIT_RESOURCE_FILE_ON_LINE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_DATASOURCE_BY_TYPE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_RESOURCES_LIST_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_RESOURCES_LIST_PAGING;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_UDF_FUNCTION_LIST_PAGING_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.RESOURCE_FILE_IS_EMPTY;
import static org.apache.dolphinscheduler.api.enums.Status.RESOURCE_NOT_EXIST;
import static org.apache.dolphinscheduler.api.enums.Status.UNAUTHORIZED_UDF_FUNCTION_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_RESOURCE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_UDF_FUNCTION_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.VERIFY_RESOURCE_BY_NAME_AND_TYPE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.VERIFY_UDF_FUNCTION_NAME_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.VIEW_RESOURCE_FILE_ON_LINE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.VIEW_UDF_FUNCTION_ERROR;

44
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
Q
qiaozhanwei 已提交
45
import org.apache.dolphinscheduler.api.enums.Status;
46
import org.apache.dolphinscheduler.api.exceptions.ApiException;
Q
qiaozhanwei 已提交
47 48 49
import org.apache.dolphinscheduler.api.service.ResourcesService;
import org.apache.dolphinscheduler.api.service.UdfFuncService;
import org.apache.dolphinscheduler.api.utils.Result;
50
import org.apache.dolphinscheduler.common.Constants;
B
baoliang 已提交
51
import org.apache.dolphinscheduler.common.enums.ProgramType;
Q
qiaozhanwei 已提交
52 53 54
import org.apache.dolphinscheduler.common.enums.ResourceType;
import org.apache.dolphinscheduler.common.enums.UdfType;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
55
import org.apache.dolphinscheduler.common.utils.StringUtils;
Q
qiaozhanwei 已提交
56
import org.apache.dolphinscheduler.dao.entity.User;
57 58 59

import java.util.Map;

L
ligang 已提交
60 61 62 63 64 65 66
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
67 68 69 70 71 72 73 74
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
L
ligang 已提交
75 76
import org.springframework.web.multipart.MultipartFile;

77 78 79 80 81
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
82

L
ligang 已提交
83 84 85
/**
 * resources controller
 */
86
@Api(tags = "RESOURCES_TAG")
L
ligang 已提交
87 88
@RestController
@RequestMapping("resources")
89
public class ResourcesController extends BaseController {
L
ligang 已提交
90 91 92 93 94 95 96 97

    private static final Logger logger = LoggerFactory.getLogger(ResourcesController.class);

    @Autowired
    private ResourcesService resourceService;
    @Autowired
    private UdfFuncService udfFuncService;

98 99
    /**
     *
100 101 102 103 104 105
     * @param loginUser   login user
     * @param type        type
     * @param alias       alias
     * @param description description
     * @param pid         parent id
     * @param currentDir  current directory
B
baoliang 已提交
106
     * @return create result code
107
     */
108
    @ApiOperation(value = "createDirctory", notes = "CREATE_RESOURCE_NOTES")
109
    @ApiImplicitParams({
110 111 112
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
            @ApiImplicitParam(name = "name", value = "RESOURCE_NAME", required = true, dataType = "String"),
            @ApiImplicitParam(name = "description", value = "RESOURCE_DESC", dataType = "String"),
113
            @ApiImplicitParam(name = "pid", value = "RESOURCE_PID", required = true, dataType = "Int", example = "10"),
114
            @ApiImplicitParam(name = "currentDir", value = "RESOURCE_CURRENTDIR", required = true, dataType = "String")
115 116
    })
    @PostMapping(value = "/directory/create")
117
    @ApiException(CREATE_RESOURCE_ERROR)
118
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
119
    public Result createDirectory(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
120 121 122 123 124 125
                                  @RequestParam(value = "type") ResourceType type,
                                  @RequestParam(value = "name") String alias,
                                  @RequestParam(value = "description", required = false) String description,
                                  @RequestParam(value = "pid") int pid,
                                  @RequestParam(value = "currentDir") String currentDir) {
        return resourceService.createDirectory(loginUser, alias, description, type, pid, currentDir);
126 127
    }

L
ligang 已提交
128 129
    /**
     * create resource
130 131 132 133 134 135 136
     * @param loginUser
     * @param type
     * @param alias
     * @param description
     * @param file
     * @param pid
     * @param currentDir
B
bao liang 已提交
137
     * @return create result code
L
ligang 已提交
138
     */
139
    @ApiOperation(value = "createResource", notes = "CREATE_RESOURCE_NOTES")
L
lidongdai 已提交
140
    @ApiImplicitParams({
141 142 143
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
            @ApiImplicitParam(name = "name", value = "RESOURCE_NAME", required = true, dataType = "String"),
            @ApiImplicitParam(name = "description", value = "RESOURCE_DESC", dataType = "String"),
144
            @ApiImplicitParam(name = "file", value = "RESOURCE_FILE", required = true, dataType = "MultipartFile"),
145
            @ApiImplicitParam(name = "pid", value = "RESOURCE_PID", required = true, dataType = "Int", example = "10"),
146
            @ApiImplicitParam(name = "currentDir", value = "RESOURCE_CURRENTDIR", required = true, dataType = "String")
L
lidongdai 已提交
147
    })
L
ligang 已提交
148
    @PostMapping(value = "/create")
149
    @ApiException(CREATE_RESOURCE_ERROR)
150
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
151
    public Result createResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
152
                                 @RequestParam(value = "type") ResourceType type,
153
                                 @RequestParam(value = "name") String alias,
B
bao liang 已提交
154
                                 @RequestParam(value = "description", required = false) String description,
155
                                 @RequestParam("file") MultipartFile file,
156 157 158
                                 @RequestParam(value = "pid") int pid,
                                 @RequestParam(value = "currentDir") String currentDir) {
        return resourceService.createResource(loginUser, alias, description, type, file, pid, currentDir);
L
ligang 已提交
159 160 161 162 163
    }

    /**
     * update resource
     *
164 165 166 167
     * @param loginUser login user
     * @param alias alias
     * @param resourceId resource id
     * @param type resource type
B
bao liang 已提交
168
     * @param description description
B
baoliang 已提交
169
     * @param file        resource file
B
bao liang 已提交
170
     * @return update result code
L
ligang 已提交
171
     */
172
    @ApiOperation(value = "updateResource", notes = "UPDATE_RESOURCE_NOTES")
L
lidongdai 已提交
173
    @ApiImplicitParams({
174 175 176
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100"),
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
            @ApiImplicitParam(name = "name", value = "RESOURCE_NAME", required = true, dataType = "String"),
B
baoliang 已提交
177 178
            @ApiImplicitParam(name = "description", value = "RESOURCE_DESC", dataType = "String"),
            @ApiImplicitParam(name = "file", value = "RESOURCE_FILE", required = true, dataType = "MultipartFile")
L
lidongdai 已提交
179
    })
L
ligang 已提交
180
    @PostMapping(value = "/update")
181
    @ApiException(UPDATE_RESOURCE_ERROR)
182
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
183
    public Result updateResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
184
                                 @RequestParam(value = "id") int resourceId,
L
ligang 已提交
185
                                 @RequestParam(value = "type") ResourceType type,
186
                                 @RequestParam(value = "name") String alias,
B
baoliang 已提交
187 188 189
                                 @RequestParam(value = "description", required = false) String description,
                                 @RequestParam(value = "file" ,required = false) MultipartFile file) {
        return resourceService.updateResource(loginUser, resourceId, alias, description, type, file);
L
ligang 已提交
190 191 192 193 194
    }

    /**
     * query resources list
     *
B
bao liang 已提交
195
     * @param loginUser login user
196
     * @param type resource type
B
bao liang 已提交
197
     * @return resource list
L
ligang 已提交
198
     */
199
    @ApiOperation(value = "queryResourceList", notes = "QUERY_RESOURCE_LIST_NOTES")
L
lidongdai 已提交
200
    @ApiImplicitParams({
201
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType")
L
lidongdai 已提交
202
    })
203
    @GetMapping(value = "/list")
L
ligang 已提交
204
    @ResponseStatus(HttpStatus.OK)
205
    @ApiException(QUERY_RESOURCES_LIST_ERROR)
206
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
B
bao liang 已提交
207
    public Result queryResourceList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
208 209 210 211
                                    @RequestParam(value = "type") ResourceType type
    ) {
        Map<String, Object> result = resourceService.queryResourceList(loginUser, type);
        return returnDataList(result);
L
ligang 已提交
212 213 214 215 216
    }

    /**
     * query resources list paging
     *
B
bao liang 已提交
217
     * @param loginUser login user
218
     * @param type resource type
B
bao liang 已提交
219
     * @param searchVal search value
220 221
     * @param pageNo page number
     * @param pageSize page size
B
bao liang 已提交
222
     * @return resource list page
L
ligang 已提交
223
     */
224
    @ApiOperation(value = "queryResourceListPaging", notes = "QUERY_RESOURCE_LIST_PAGING_NOTES")
L
lidongdai 已提交
225
    @ApiImplicitParams({
226
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
227
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "int", example = "10"),
228
            @ApiImplicitParam(name = "searchVal", value = "SEARCH_VAL", dataType = "String"),
L
lidongdai 已提交
229
            @ApiImplicitParam(name = "pageNo", value = "PAGE_NO", dataType = "Int", example = "1"),
230
            @ApiImplicitParam(name = "pageSize", value = "PAGE_SIZE", dataType = "Int", example = "20")
L
lidongdai 已提交
231
    })
232
    @GetMapping(value = "/list-paging")
L
ligang 已提交
233
    @ResponseStatus(HttpStatus.OK)
234
    @ApiException(QUERY_RESOURCES_LIST_PAGING)
235
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
B
bao liang 已提交
236
    public Result queryResourceListPaging(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
237 238 239 240 241 242 243 244
                                          @RequestParam(value = "type") ResourceType type,
                                          @RequestParam(value = "id") int id,
                                          @RequestParam("pageNo") Integer pageNo,
                                          @RequestParam(value = "searchVal", required = false) String searchVal,
                                          @RequestParam("pageSize") Integer pageSize
    ) {
        Map<String, Object> result = checkPageParams(pageNo, pageSize);
        if (result.get(Constants.STATUS) != Status.SUCCESS) {
L
ligang 已提交
245 246
            return returnDataListPaging(result);
        }
247 248 249 250

        searchVal = ParameterUtils.handleEscapes(searchVal);
        result = resourceService.queryResourceListPaging(loginUser, id, type, searchVal, pageNo, pageSize);
        return returnDataListPaging(result);
L
ligang 已提交
251 252 253 254 255 256
    }


    /**
     * delete resource
     *
257
     * @param loginUser login user
B
bao liang 已提交
258 259
     * @param resourceId resource id
     * @return delete result code
L
ligang 已提交
260
     */
261
    @ApiOperation(value = "deleteResource", notes = "DELETE_RESOURCE_BY_ID_NOTES")
L
lidongdai 已提交
262
    @ApiImplicitParams({
263
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
264
    })
L
ligang 已提交
265 266
    @GetMapping(value = "/delete")
    @ResponseStatus(HttpStatus.OK)
267
    @ApiException(DELETE_RESOURCE_ERROR)
268
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
269
    public Result deleteResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
270 271 272
                                 @RequestParam(value = "id") int resourceId
    ) throws Exception {
        return resourceService.delete(loginUser, resourceId);
L
ligang 已提交
273 274 275 276 277 278
    }


    /**
     * verify resource by alias and type
     *
B
bao liang 已提交
279
     * @param loginUser login user
280 281
     * @param fullName resource full name
     * @param type resource type
B
bao liang 已提交
282
     * @return true if the resource name not exists, otherwise return false
L
ligang 已提交
283
     */
284
    @ApiOperation(value = "verifyResourceName", notes = "VERIFY_RESOURCE_NAME_NOTES")
L
lidongdai 已提交
285
    @ApiImplicitParams({
286 287
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
            @ApiImplicitParam(name = "fullName", value = "RESOURCE_FULL_NAME", required = true, dataType = "String")
L
lidongdai 已提交
288
    })
L
ligang 已提交
289 290
    @GetMapping(value = "/verify-name")
    @ResponseStatus(HttpStatus.OK)
291
    @ApiException(VERIFY_RESOURCE_BY_NAME_AND_TYPE_ERROR)
292
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
293
    public Result verifyResourceName(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
294 295
                                     @RequestParam(value = "fullName") String fullName,
                                     @RequestParam(value = "type") ResourceType type
L
ligang 已提交
296
    ) {
297
        return resourceService.verifyResourceName(fullName, type, loginUser);
L
ligang 已提交
298 299
    }

300 301 302 303
    /**
     * query resources jar list
     *
     * @param loginUser login user
304
     * @param type resource type
305 306
     * @return resource list
     */
B
baoliang 已提交
307
    @ApiOperation(value = "queryResourceByProgramType", notes = "QUERY_RESOURCE_LIST_NOTES")
308
    @ApiImplicitParams({
309
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType")
310
    })
311
    @GetMapping(value = "/list/jar")
312
    @ResponseStatus(HttpStatus.OK)
313
    @ApiException(QUERY_RESOURCES_LIST_ERROR)
314
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
315
    public Result queryResourceJarList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
B
baoliang 已提交
316 317
                                       @RequestParam(value = "type") ResourceType type,
                                       @RequestParam(value = "programType",required = false) ProgramType programType
318
    ) {
B
baoliang 已提交
319
        Map<String, Object> result = resourceService.queryResourceByProgramType(loginUser, type,programType);
320
        return returnDataList(result);
321 322 323 324 325 326
    }

    /**
     * query resource by full name and type
     *
     * @param loginUser login user
327 328 329
     * @param fullName resource full name
     * @param type resource type
     * @param id resource id
330 331
     * @return true if the resource name not exists, otherwise return false
     */
332
    @ApiOperation(value = "queryResource", notes = "QUERY_BY_RESOURCE_NAME")
333
    @ApiImplicitParams({
334
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
335
            @ApiImplicitParam(name = "fullName", value = "RESOURCE_FULL_NAME", required = true, dataType = "String"),
336
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = false, dataType = "Int", example = "10")
337 338 339
    })
    @GetMapping(value = "/queryResource")
    @ResponseStatus(HttpStatus.OK)
340
    @ApiException(RESOURCE_NOT_EXIST)
341
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
342
    public Result queryResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
343 344 345
                                @RequestParam(value = "fullName", required = false) String fullName,
                                @RequestParam(value = "id", required = false) Integer id,
                                @RequestParam(value = "type") ResourceType type
346
    ) {
347 348

        return resourceService.queryResource(fullName, id, type);
349 350
    }

L
ligang 已提交
351 352 353
    /**
     * view resource file online
     *
354 355
     * @param loginUser login user
     * @param resourceId resource id
B
bao liang 已提交
356
     * @param skipLineNum skip line number
357
     * @param limit limit
B
bao liang 已提交
358
     * @return resource content
L
ligang 已提交
359
     */
360
    @ApiOperation(value = "viewResource", notes = "VIEW_RESOURCE_BY_ID_NOTES")
L
lidongdai 已提交
361
    @ApiImplicitParams({
362 363 364
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100"),
            @ApiImplicitParam(name = "skipLineNum", value = "SKIP_LINE_NUM", required = true, dataType = "Int", example = "100"),
            @ApiImplicitParam(name = "limit", value = "LIMIT", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
365
    })
L
ligang 已提交
366
    @GetMapping(value = "/view")
367
    @ApiException(VIEW_RESOURCE_FILE_ON_LINE_ERROR)
368
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
369
    public Result viewResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
370 371 372 373
                               @RequestParam(value = "id") int resourceId,
                               @RequestParam(value = "skipLineNum") int skipLineNum,
                               @RequestParam(value = "limit") int limit
    ) {
374
        return resourceService.readResource(resourceId, skipLineNum, limit);
L
ligang 已提交
375 376 377 378
    }

    /**
     * create resource file online
379 380 381 382 383 384 385 386
     * @param loginUser
     * @param type
     * @param fileName
     * @param fileSuffix
     * @param description
     * @param content
     * @param pid
     * @param currentDir
B
bao liang 已提交
387
     * @return create result code
L
ligang 已提交
388
     */
389
    @ApiOperation(value = "onlineCreateResource", notes = "ONLINE_CREATE_RESOURCE_NOTES")
L
lidongdai 已提交
390
    @ApiImplicitParams({
391 392 393 394
            @ApiImplicitParam(name = "type", value = "RESOURCE_TYPE", required = true, dataType = "ResourceType"),
            @ApiImplicitParam(name = "fileName", value = "RESOURCE_NAME", required = true, dataType = "String"),
            @ApiImplicitParam(name = "suffix", value = "SUFFIX", required = true, dataType = "String"),
            @ApiImplicitParam(name = "description", value = "RESOURCE_DESC", dataType = "String"),
395
            @ApiImplicitParam(name = "content", value = "CONTENT", required = true, dataType = "String"),
396
            @ApiImplicitParam(name = "pid", value = "RESOURCE_PID", required = true, dataType = "Int", example = "10"),
397
            @ApiImplicitParam(name = "currentDir", value = "RESOURCE_CURRENTDIR", required = true, dataType = "String")
L
lidongdai 已提交
398
    })
L
ligang 已提交
399
    @PostMapping(value = "/online-create")
400
    @ApiException(CREATE_RESOURCE_FILE_ON_LINE_ERROR)
401
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
402
    public Result onlineCreateResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
403
                                       @RequestParam(value = "type") ResourceType type,
404 405
                                       @RequestParam(value = "fileName") String fileName,
                                       @RequestParam(value = "suffix") String fileSuffix,
B
bao liang 已提交
406
                                       @RequestParam(value = "description", required = false) String description,
407
                                       @RequestParam(value = "content") String content,
408 409
                                       @RequestParam(value = "pid") int pid,
                                       @RequestParam(value = "currentDir") String currentDir
L
ligang 已提交
410
    ) {
411 412 413
        if (StringUtils.isEmpty(content)) {
            logger.error("resource file contents are not allowed to be empty");
            return error(Status.RESOURCE_FILE_IS_EMPTY.getCode(), RESOURCE_FILE_IS_EMPTY.getMsg());
L
ligang 已提交
414
        }
415
        return resourceService.onlineCreateResource(loginUser, type, fileName, fileSuffix, description, content, pid, currentDir);
L
ligang 已提交
416 417 418 419 420
    }

    /**
     * edit resource file online
     *
421
     * @param loginUser login user
B
bao liang 已提交
422
     * @param resourceId resource id
423
     * @param content content
B
bao liang 已提交
424
     * @return update result code
L
ligang 已提交
425
     */
426
    @ApiOperation(value = "updateResourceContent", notes = "UPDATE_RESOURCE_NOTES")
L
lidongdai 已提交
427
    @ApiImplicitParams({
428 429
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100"),
            @ApiImplicitParam(name = "content", value = "CONTENT", required = true, dataType = "String")
L
lidongdai 已提交
430
    })
L
ligang 已提交
431
    @PostMapping(value = "/update-content")
432
    @ApiException(EDIT_RESOURCE_FILE_ON_LINE_ERROR)
433
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
434
    public Result updateResourceContent(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
435 436 437
                                        @RequestParam(value = "id") int resourceId,
                                        @RequestParam(value = "content") String content
    ) {
438 439 440
        if (StringUtils.isEmpty(content)) {
            logger.error("The resource file contents are not allowed to be empty");
            return error(Status.RESOURCE_FILE_IS_EMPTY.getCode(), RESOURCE_FILE_IS_EMPTY.getMsg());
L
ligang 已提交
441
        }
442
        return resourceService.updateResourceContent(resourceId, content);
L
ligang 已提交
443 444 445 446 447
    }

    /**
     * download resource file
     *
448
     * @param loginUser login user
B
bao liang 已提交
449 450
     * @param resourceId resource id
     * @return resource content
L
ligang 已提交
451
     */
452
    @ApiOperation(value = "downloadResource", notes = "DOWNLOAD_RESOURCE_NOTES")
L
lidongdai 已提交
453
    @ApiImplicitParams({
454
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
455
    })
L
ligang 已提交
456 457
    @GetMapping(value = "/download")
    @ResponseBody
458
    @ApiException(DOWNLOAD_RESOURCE_FILE_ERROR)
459
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
460
    public ResponseEntity downloadResource(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
461 462 463 464
                                           @RequestParam(value = "id") int resourceId) throws Exception {
        Resource file = resourceService.downloadResource(resourceId);
        if (file == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Status.RESOURCE_NOT_EXIST.getMsg());
L
ligang 已提交
465
        }
466 467 468 469
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
L
ligang 已提交
470 471 472 473 474
    }


    /**
     * create udf function
475
     *
476 477 478 479 480
     * @param loginUser login user
     * @param type udf type
     * @param funcName function name
     * @param argTypes argument types
     * @param database database
B
bao liang 已提交
481
     * @param description description
482 483
     * @param className class name
     * @param resourceId resource id
B
bao liang 已提交
484
     * @return create result code
L
ligang 已提交
485
     */
486
    @ApiOperation(value = "createUdfFunc", notes = "CREATE_UDF_FUNCTION_NOTES")
L
lidongdai 已提交
487
    @ApiImplicitParams({
488 489 490 491 492 493 494
            @ApiImplicitParam(name = "type", value = "UDF_TYPE", required = true, dataType = "UdfType"),
            @ApiImplicitParam(name = "funcName", value = "FUNC_NAME", required = true, dataType = "String"),
            @ApiImplicitParam(name = "suffix", value = "CLASS_NAME", required = true, dataType = "String"),
            @ApiImplicitParam(name = "argTypes", value = "ARG_TYPES", dataType = "String"),
            @ApiImplicitParam(name = "database", value = "DATABASE_NAME", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "UDF_DESC", dataType = "String"),
            @ApiImplicitParam(name = "resourceId", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
495 496

    })
L
ligang 已提交
497 498
    @PostMapping(value = "/udf-func/create")
    @ResponseStatus(HttpStatus.CREATED)
499
    @ApiException(CREATE_UDF_FUNCTION_ERROR)
500
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
501
    public Result createUdfFunc(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
502
                                @RequestParam(value = "type") UdfType type,
503 504 505 506
                                @RequestParam(value = "funcName") String funcName,
                                @RequestParam(value = "className") String className,
                                @RequestParam(value = "argTypes", required = false) String argTypes,
                                @RequestParam(value = "database", required = false) String database,
507
                                @RequestParam(value = "description", required = false) String description,
L
ligang 已提交
508
                                @RequestParam(value = "resourceId") int resourceId) {
509
        return udfFuncService.createUdfFunction(loginUser, funcName, className, argTypes, database, description, type, resourceId);
L
ligang 已提交
510 511 512 513 514
    }

    /**
     * view udf function
     *
B
bao liang 已提交
515
     * @param loginUser login user
516
     * @param id resource id
B
bao liang 已提交
517
     * @return udf function detail
L
ligang 已提交
518
     */
519
    @ApiOperation(value = "viewUIUdfFunction", notes = "VIEW_UDF_FUNCTION_NOTES")
L
lidongdai 已提交
520
    @ApiImplicitParams({
521
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
522 523

    })
L
ligang 已提交
524 525
    @GetMapping(value = "/udf-func/update-ui")
    @ResponseStatus(HttpStatus.OK)
526
    @ApiException(VIEW_UDF_FUNCTION_ERROR)
527
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
528
    public Result viewUIUdfFunction(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
529 530 531
                                    @RequestParam("id") int id) {
        Map<String, Object> map = udfFuncService.queryUdfFuncDetail(id);
        return returnDataList(map);
L
ligang 已提交
532 533 534
    }

    /**
B
bao liang 已提交
535
     * update udf function
L
ligang 已提交
536
     *
537 538 539 540 541
     * @param loginUser login user
     * @param type resource type
     * @param funcName function name
     * @param argTypes argument types
     * @param database data base
B
bao liang 已提交
542
     * @param description description
543 544 545
     * @param resourceId resource id
     * @param className class name
     * @param udfFuncId udf function id
B
bao liang 已提交
546
     * @return update result code
L
ligang 已提交
547
     */
548
    @ApiOperation(value = "updateUdfFunc", notes = "UPDATE_UDF_FUNCTION_NOTES")
L
lidongdai 已提交
549
    @ApiImplicitParams({
550
            @ApiImplicitParam(name = "id", value = "UDF_ID", required = true, dataType = "Int"),
551 552
            @ApiImplicitParam(name = "type", value = "UDF_TYPE", required = true, dataType = "UdfType"),
            @ApiImplicitParam(name = "funcName", value = "FUNC_NAME", required = true, dataType = "String"),
553
            @ApiImplicitParam(name = "className", value = "CLASS_NAME", required = true, dataType = "String"),
554 555 556
            @ApiImplicitParam(name = "argTypes", value = "ARG_TYPES", dataType = "String"),
            @ApiImplicitParam(name = "database", value = "DATABASE_NAME", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "UDF_DESC", dataType = "String"),
557
            @ApiImplicitParam(name = "resourceId", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
558 559

    })
L
ligang 已提交
560
    @PostMapping(value = "/udf-func/update")
561
    @ApiException(UPDATE_UDF_FUNCTION_ERROR)
562
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
L
lidongdai 已提交
563
    public Result updateUdfFunc(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
564 565
                                @RequestParam(value = "id") int udfFuncId,
                                @RequestParam(value = "type") UdfType type,
566 567 568 569
                                @RequestParam(value = "funcName") String funcName,
                                @RequestParam(value = "className") String className,
                                @RequestParam(value = "argTypes", required = false) String argTypes,
                                @RequestParam(value = "database", required = false) String database,
570
                                @RequestParam(value = "description", required = false) String description,
L
ligang 已提交
571
                                @RequestParam(value = "resourceId") int resourceId) {
572 573
        Map<String, Object> result = udfFuncService.updateUdfFunc(udfFuncId, funcName, className, argTypes, database, description, type, resourceId);
        return returnDataList(result);
L
ligang 已提交
574 575 576 577 578
    }

    /**
     * query udf function list paging
     *
B
bao liang 已提交
579 580
     * @param loginUser login user
     * @param searchVal search value
581 582
     * @param pageNo page number
     * @param pageSize page size
B
bao liang 已提交
583
     * @return udf function list page
L
ligang 已提交
584
     */
585
    @ApiOperation(value = "queryUdfFuncListPaging", notes = "QUERY_UDF_FUNCTION_LIST_PAGING_NOTES")
L
lidongdai 已提交
586
    @ApiImplicitParams({
587
            @ApiImplicitParam(name = "searchVal", value = "SEARCH_VAL", dataType = "String"),
L
lidongdai 已提交
588
            @ApiImplicitParam(name = "pageNo", value = "PAGE_NO", dataType = "Int", example = "1"),
589
            @ApiImplicitParam(name = "pageSize", value = "PAGE_SIZE", dataType = "Int", example = "20")
L
lidongdai 已提交
590
    })
591
    @GetMapping(value = "/udf-func/list-paging")
L
ligang 已提交
592
    @ResponseStatus(HttpStatus.OK)
593
    @ApiException(QUERY_UDF_FUNCTION_LIST_PAGING_ERROR)
594
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
B
baoliang 已提交
595
    public Result<Object> queryUdfFuncListPaging(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
596 597 598
                                   @RequestParam("pageNo") Integer pageNo,
                                   @RequestParam(value = "searchVal", required = false) String searchVal,
                                   @RequestParam("pageSize") Integer pageSize
599 600 601
    ) {
        Map<String, Object> result = checkPageParams(pageNo, pageSize);
        if (result.get(Constants.STATUS) != Status.SUCCESS) {
L
ligang 已提交
602 603
            return returnDataListPaging(result);
        }
604 605 606

        result = udfFuncService.queryUdfFuncListPaging(loginUser, searchVal, pageNo, pageSize);
        return returnDataListPaging(result);
L
ligang 已提交
607 608 609
    }

    /**
B
baoliang 已提交
610
     * query udf func list by type
L
ligang 已提交
611
     *
B
bao liang 已提交
612
     * @param loginUser login user
613
     * @param type resource type
B
bao liang 已提交
614
     * @return resource list
L
ligang 已提交
615
     */
B
baoliang 已提交
616
    @ApiOperation(value = "queryUdfFuncList", notes = "QUERY_UDF_FUNC_LIST_NOTES")
L
lidongdai 已提交
617
    @ApiImplicitParams({
618
            @ApiImplicitParam(name = "type", value = "UDF_TYPE", required = true, dataType = "UdfType")
L
lidongdai 已提交
619
    })
620
    @GetMapping(value = "/udf-func/list")
L
ligang 已提交
621
    @ResponseStatus(HttpStatus.OK)
622
    @ApiException(QUERY_DATASOURCE_BY_TYPE_ERROR)
623
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
B
baoliang 已提交
624
    public Result<Object> queryUdfFuncList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
625
                                    @RequestParam("type") UdfType type) {
B
baoliang 已提交
626
        Map<String, Object> result = udfFuncService.queryUdfFuncList(loginUser, type.ordinal());
627
        return returnDataList(result);
L
ligang 已提交
628 629 630 631 632
    }

    /**
     * verify udf function name can use or not
     *
B
bao liang 已提交
633
     * @param loginUser login user
634
     * @param name name
B
bao liang 已提交
635
     * @return true if the name can user, otherwise return false
L
ligang 已提交
636
     */
637
    @ApiOperation(value = "verifyUdfFuncName", notes = "VERIFY_UDF_FUNCTION_NAME_NOTES")
L
lidongdai 已提交
638
    @ApiImplicitParams({
639
            @ApiImplicitParam(name = "name", value = "FUNC_NAME", required = true, dataType = "String")
L
lidongdai 已提交
640 641

    })
L
ligang 已提交
642 643
    @GetMapping(value = "/udf-func/verify-name")
    @ResponseStatus(HttpStatus.OK)
644
    @ApiException(VERIFY_UDF_FUNCTION_NAME_ERROR)
645
    @AccessLogAnnotation
L
lidongdai 已提交
646
    public Result verifyUdfFuncName(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
647
                                    @RequestParam(value = "name") String name
L
ligang 已提交
648 649
    ) {

650
        return udfFuncService.verifyUdfFuncByName(name);
L
ligang 已提交
651 652 653 654 655
    }

    /**
     * delete udf function
     *
B
bao liang 已提交
656 657 658
     * @param loginUser login user
     * @param udfFuncId udf function id
     * @return delete result code
L
ligang 已提交
659
     */
660
    @ApiOperation(value = "deleteUdfFunc", notes = "DELETE_UDF_FUNCTION_NOTES")
L
lidongdai 已提交
661
    @ApiImplicitParams({
662
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
663
    })
L
ligang 已提交
664 665
    @GetMapping(value = "/udf-func/delete")
    @ResponseStatus(HttpStatus.OK)
666
    @ApiException(DELETE_UDF_FUNCTION_ERROR)
667
    @AccessLogAnnotation
L
lidongdai 已提交
668
    public Result deleteUdfFunc(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
669
                                @RequestParam(value = "id") int udfFuncId
L
ligang 已提交
670
    ) {
671
        return udfFuncService.delete(udfFuncId);
L
ligang 已提交
672 673 674 675 676
    }

    /**
     * authorized file resource list
     *
B
bao liang 已提交
677
     * @param loginUser login user
678
     * @param userId user id
B
bao liang 已提交
679
     * @return authorized result
L
ligang 已提交
680
     */
681
    @ApiOperation(value = "authorizedFile", notes = "AUTHORIZED_FILE_NOTES")
L
lidongdai 已提交
682
    @ApiImplicitParams({
683
            @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
684
    })
L
ligang 已提交
685 686
    @GetMapping(value = "/authed-file")
    @ResponseStatus(HttpStatus.CREATED)
687
    @ApiException(AUTHORIZED_FILE_RESOURCE_ERROR)
688
    @AccessLogAnnotation
L
lidongdai 已提交
689
    public Result authorizedFile(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
690
                                 @RequestParam("userId") Integer userId) {
691 692
        Map<String, Object> result = resourceService.authorizedFile(loginUser, userId);
        return returnDataList(result);
L
ligang 已提交
693 694 695 696 697 698
    }


    /**
     * unauthorized file resource list
     *
B
bao liang 已提交
699
     * @param loginUser login user
700
     * @param userId user id
B
bao liang 已提交
701
     * @return unauthorized result code
L
ligang 已提交
702
     */
703
    @ApiOperation(value = "authorizeResourceTree", notes = "AUTHORIZE_RESOURCE_TREE_NOTES")
L
lidongdai 已提交
704
    @ApiImplicitParams({
705
            @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
706
    })
707
    @GetMapping(value = "/authorize-resource-tree")
L
ligang 已提交
708
    @ResponseStatus(HttpStatus.CREATED)
709
    @ApiException(AUTHORIZE_RESOURCE_TREE)
710
    @AccessLogAnnotation
711
    public Result authorizeResourceTree(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
712 713 714
                                        @RequestParam("userId") Integer userId) {
        Map<String, Object> result = resourceService.authorizeResourceTree(loginUser, userId);
        return returnDataList(result);
L
ligang 已提交
715 716 717 718 719 720
    }


    /**
     * unauthorized udf function
     *
B
bao liang 已提交
721
     * @param loginUser login user
722
     * @param userId user id
B
bao liang 已提交
723
     * @return unauthorized result code
L
ligang 已提交
724
     */
725
    @ApiOperation(value = "unauthUDFFunc", notes = "UNAUTHORIZED_UDF_FUNC_NOTES")
L
lidongdai 已提交
726
    @ApiImplicitParams({
727
            @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
728
    })
L
ligang 已提交
729 730
    @GetMapping(value = "/unauth-udf-func")
    @ResponseStatus(HttpStatus.CREATED)
731
    @ApiException(UNAUTHORIZED_UDF_FUNCTION_ERROR)
732
    @AccessLogAnnotation
L
lidongdai 已提交
733
    public Result unauthUDFFunc(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
734
                                @RequestParam("userId") Integer userId) {
735 736 737

        Map<String, Object> result = resourceService.unauthorizedUDFFunction(loginUser, userId);
        return returnDataList(result);
L
ligang 已提交
738 739 740 741 742 743
    }


    /**
     * authorized udf function
     *
B
bao liang 已提交
744
     * @param loginUser login user
745
     * @param userId user id
B
bao liang 已提交
746
     * @return authorized result code
L
ligang 已提交
747
     */
748
    @ApiOperation(value = "authUDFFunc", notes = "AUTHORIZED_UDF_FUNC_NOTES")
L
lidongdai 已提交
749
    @ApiImplicitParams({
750
            @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100")
L
lidongdai 已提交
751
    })
L
ligang 已提交
752 753
    @GetMapping(value = "/authed-udf-func")
    @ResponseStatus(HttpStatus.CREATED)
754
    @ApiException(AUTHORIZED_UDF_FUNCTION_ERROR)
755
    @AccessLogAnnotation
L
lidongdai 已提交
756
    public Result authorizedUDFFunction(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
L
ligang 已提交
757
                                        @RequestParam("userId") Integer userId) {
758 759
        Map<String, Object> result = resourceService.authorizedUDFFunction(loginUser, userId);
        return returnDataList(result);
L
ligang 已提交
760
    }
761
}