[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-REST / src / main / java / org / openecomp / policy / rest / util / Webapps.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.rest.util;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.openecomp.policy.rest.XACMLRestProperties;
31
32 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
33 import com.att.research.xacml.util.XACMLProperties;
34
35 import org.openecomp.policy.common.logging.eelf.MessageCodes;
36 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
37
38 public class Webapps {
39         private static String actionHome = null;
40         private static String configHome = null;
41         private static Log logger       = LogFactory.getLog(Webapps.class);
42         
43         private Webapps() {
44         }
45         
46         public static String getConfigHome(){
47                 try {
48                         loadWebapps();
49                 } catch (Exception e) {
50                         logger.error("Exception Occured while loading webapps"+e);
51                         return null;
52                 }
53                 return configHome;
54         }
55         
56         public static String getActionHome(){
57                 try {
58                         loadWebapps();
59                 } catch (Exception e) {
60                         logger.error("Exception Occured while loading webapps"+e);
61                         return null;
62                 }
63                 return actionHome;
64         }
65         
66         private static void loadWebapps() throws Exception{
67                 String errorMessageName = "Invalid Webapps Path Location property :";
68                 if(actionHome == null || configHome == null){
69                         Path webappsPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS));
70                         //Sanity Check
71                         if (webappsPath == null) {
72                                 logger.error(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
73                                 PolicyLogger.error(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
74                                 throw new Exception(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
75                         }
76                         Path webappsPathConfig;
77                         Path webappsPathAction;
78                         if(webappsPath.toString().contains("\\")){
79                                 webappsPathConfig = Paths.get(webappsPath.toString()+"\\Config");
80                                 webappsPathAction = Paths.get(webappsPath.toString()+"\\Action");
81                         }else{
82                                 webappsPathConfig = Paths.get(webappsPath.toString()+"/Config");
83                                 webappsPathAction = Paths.get(webappsPath.toString()+"/Action");
84                         }
85                         
86                         checkConfigActionHomeExists(webappsPathConfig, webappsPathAction);
87                         
88                         actionHome = webappsPathAction.toString();
89                         configHome = webappsPathConfig.toString();
90                 }
91         }
92         
93         private  static void checkConfigActionHomeExists(Path webappsPathConfig, Path webappsPathAction){
94                 if (!webappsPathConfig.toFile().exists()){
95                         try {
96                                 Files.createDirectories(webappsPathConfig);
97                         } catch (IOException e) {
98                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
99                                                 + webappsPathConfig.toAbsolutePath().toString(), e);
100                                 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Webapps", "Failed to create config directory");
101                         }
102                 }
103                 
104                 if (!webappsPathAction.toFile().exists()){
105                         try {
106                                 Files.createDirectories(webappsPathAction);
107                         } catch (IOException e) {
108                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
109                                                 + webappsPathAction.toAbsolutePath().toString(), e);
110                                 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Webapps", "Failed to create config directory");
111                         }
112                 }
113         }
114
115 }