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