Fixes for sonar critical issues
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / Webapps.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.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.onap.policy.rest.XACMLRestProperties;
31
32 import org.onap.policy.xacml.api.XACMLErrorConstants;
33
34 import com.att.research.xacml.api.pap.PAPException;
35 import com.att.research.xacml.util.XACMLProperties;
36
37 import org.onap.policy.common.logging.eelf.MessageCodes;
38 import org.onap.policy.common.logging.eelf.PolicyLogger;
39
40 public class Webapps {
41         private static String actionHome = null;
42         private static String configHome = null;
43         private static Log logger       = LogFactory.getLog(Webapps.class);
44         
45         private Webapps() {
46         }
47         
48         public static String getConfigHome(){
49                 try {
50                         loadWebapps();
51                 } catch (PAPException e) {
52                         logger.error("Exception Occured while loading webapps",e);
53                         return null;
54                 }
55                 return configHome;
56         }
57         
58         public static String getActionHome(){
59                 try {
60                         loadWebapps();
61                 } catch (PAPException e) {
62                         logger.error("Exception Occured while loading webapps",e);
63                         return null;
64                 }
65                 return actionHome;
66         }
67         
68         private static void loadWebapps() throws PAPException{
69                 String errorMessageName = "Invalid Webapps Path Location property :";
70                 if(actionHome == null || configHome == null){
71                         Path webappsPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS));
72                         //Sanity Check
73                         if (webappsPath == null) {
74                                 logger.error(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
75                                 PolicyLogger.error(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
76                                 throw new PAPException(errorMessageName + XACMLRestProperties.PROP_PAP_WEBAPPS);
77                         }
78                         Path webappsPathConfig;
79                         Path webappsPathAction;
80                         if(webappsPath.toString().contains("\\")){
81                                 webappsPathConfig = Paths.get(webappsPath.toString()+"\\Config");
82                                 webappsPathAction = Paths.get(webappsPath.toString()+"\\Action");
83                         }else{
84                                 webappsPathConfig = Paths.get(webappsPath.toString()+"/Config");
85                                 webappsPathAction = Paths.get(webappsPath.toString()+"/Action");
86                         }
87                         
88                         checkConfigActionHomeExists(webappsPathConfig, webappsPathAction);
89                         
90                         actionHome = webappsPathAction.toString();
91                         configHome = webappsPathConfig.toString();
92                 }
93         }
94         
95         private  static void checkConfigActionHomeExists(Path webappsPathConfig, Path webappsPathAction){
96                 if (!webappsPathConfig.toFile().exists()){
97                         try {
98                                 Files.createDirectories(webappsPathConfig);
99                         } catch (IOException e) {
100                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
101                                                 + webappsPathConfig.toAbsolutePath().toString(), e);
102                                 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Webapps", "Failed to create config directory");
103                         }
104                 }
105                 
106                 if (!webappsPathAction.toFile().exists()){
107                         try {
108                                 Files.createDirectories(webappsPathAction);
109                         } catch (IOException e) {
110                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to create config directory: "
111                                                 + webappsPathAction.toAbsolutePath().toString(), e);
112                                 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Webapps", "Failed to create config directory");
113                         }
114                 }
115         }
116
117 }