2 * Copyright (C) 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils
18 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
19 import org.springframework.http.HttpStatus
20 import org.springframework.http.codec.multipart.FilePart
21 import org.springframework.util.StringUtils
23 import java.io.IOException
24 import java.nio.file.Path
27 const val INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE = 500
29 @Throws(BluePrintException::class, IOException::class)
30 fun saveCBAFile(filePart: FilePart, targetDirectory: Path): Path {
32 val fileName = StringUtils.cleanPath(filePart.filename())
34 if (StringUtils.getFilenameExtension(fileName) != "zip") {
35 throw BluePrintException("Invalid file extension required ZIP")
38 val changedFileName = UUID.randomUUID().toString() + ".zip"
40 val targetLocation = targetDirectory.resolve(changedFileName)
42 val file = File(targetLocation.toString())
48 filePart.transferTo(file)
53 fun determineHttpStatusCode(statusCode: Int): HttpStatus {
56 return HttpStatus.valueOf(statusCode)
57 } catch (exception: Exception) {
58 // if statusCode cannot be converted to a proper HttpStatus, the resource still needs to assign a HTTP status
59 // code to the response. In this case, a 500 Internal Server Error will be returned as default.
60 return HttpStatus.valueOf(INTERNAL_SERVER_ERROR_HTTP_STATUS_CODE)