f0a68d59ea4429bdd636075b8df6bee76ae64aa0
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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.openo.commontosca.catalog.filemanage.http;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21
22 import org.openo.commontosca.catalog.filemanage.FileManager;
23 import org.openo.commontosca.catalog.filemanage.entity.FileLink;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class HttpFileManagerImpl implements FileManager {
28     private static final Logger LOGGER = LoggerFactory.getLogger(HttpFileManagerImpl.class);
29
30     @Override
31     public boolean upload(String srcPath, String dstPath) {
32         boolean flag = true;
33         LOGGER.info("start upload file.srcPath:" + srcPath + " dstPath" + dstPath);
34         File srcFile = new File(srcPath);
35         if (!srcFile.exists()) {
36             LOGGER.error("src file not exist!");
37             return false;
38         }
39         // File dstFile = new File(ToolUtil.getHttpServerPath() + dstPath);
40         // LOGGER.info("dstFile AbsolutePath:" + dstFile.getAbsolutePath());
41         String targetDir = Class.class.getClass().getResource("/").getPath() + ToolUtil.getHttpServerPath() + dstPath;
42         try {
43             ToolUtil.copyDirectory(srcPath, targetDir, true);
44         } catch (IOException e) {
45             flag = false;
46             LOGGER.error("copy file failed.errorMsg:" + e.getMessage());
47         }
48         LOGGER.info("upload file success!");
49         return flag;
50     }
51
52     @Override
53     public boolean download(String srcPath, String dstPath) {
54         // TODO Auto-generated method stub
55         return false;
56     }
57
58     @Override
59     public boolean delete(String srcPath) {
60         boolean flag = true;
61         LOGGER.info("start delete file from http server.srcPath:" + srcPath);
62         flag = ToolUtil.deleteDir(new File(ToolUtil.getHttpServerPath() + srcPath));
63         LOGGER.info("delete file from http server end.flag:" + flag);
64         return flag;
65     }
66
67     @Override
68     public ArrayList<FileLink> queryWorkFlow(String path) {
69         LOGGER.info("start query workFlow from http server.path:" + path);
70         File workFlowRoot = new File(ToolUtil.getHttpServerPath() + path);
71         ArrayList<FileLink> fileLinks = new ArrayList<FileLink>();
72         File[] files = workFlowRoot.listFiles();
73         if (files != null && files.length != 0) {
74             for (File file : files) {
75                 if (file.isFile() && file.getName().endsWith(".zip")) {
76                     FileLink fileLink = new FileLink();
77                     fileLink.setFileName(file.getName());
78                     fileLink.setDownloadUri(path + "/" + file.getName());
79                 }
80             }
81         }
82         LOGGER.info("start query workFlow from http server.size:" + fileLinks.size());
83         return fileLinks;
84     }
85
86 }