Changed code to not log user-controlled data.
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / filemanage / http / HttpFileManagerImpl.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vnfsdk.marketplace.filemanage.http;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Objects;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.vnfsdk.marketplace.filemanage.FileManager;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public class HttpFileManagerImpl implements FileManager {
29   private static final Logger LOGGER = LoggerFactory.getLogger(HttpFileManagerImpl.class);
30
31   @Override
32   public boolean upload(String srcPath, String dstPath) {
33     boolean flag = true;
34     if (LOGGER.isInfoEnabled()) {
35       LOGGER.info("start upload file.srcPath:{} dstPath{}" , loggerPatternBreaking(srcPath) , loggerPatternBreaking(dstPath));
36     }
37     File srcFile = new File(srcPath);
38     if (!srcFile.exists()) {
39       LOGGER.error("src file not exist!");
40       return false;
41     }
42
43     String targetDir = ToolUtil.getHttpServerAbsolutePath() + dstPath;
44     try {
45       ToolUtil.copyDirectory(srcPath, targetDir, true);
46     } catch (IOException e1) {
47       flag = false;
48       LOGGER.error("ErrorMsg: failed to copy file:", e1);
49     }
50     LOGGER.info("upload file success!");
51     return flag;
52   }
53
54   @Override
55   public boolean download(String srcPath, String dstPath) {
56     // TODO Auto-generated method stub
57     return false;
58   }
59
60   @Override
61   public boolean delete(String srcPath) {
62     if (LOGGER.isInfoEnabled()) {
63       LOGGER.info("start delete file from http server.srcPath:{}" , loggerPatternBreaking(srcPath));
64     }
65     boolean flag = ToolUtil.deleteDir(new File(ToolUtil.getHttpServerAbsolutePath() + srcPath));
66     LOGGER.info("delete file from http server end.flag:{}" , flag);
67     return flag;
68   }
69   private String loggerPatternBreaking(String loggerInput) {
70     return Objects.nonNull(loggerInput) ? loggerInput.replaceAll("[\n\r\t]", "_") : StringUtils.EMPTY;
71
72   }
73
74 }