Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / WebConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-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.pap.xacml.rest;
22
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Base64;
28 import java.util.Properties;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletRegistration;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.springframework.web.WebApplicationInitializer;
36 import org.springframework.web.context.support.XmlWebApplicationContext;
37 import org.springframework.web.servlet.DispatcherServlet;
38
39 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
40
41
42 public class WebConfig implements WebApplicationInitializer {
43         
44         private static final Log logger = LogFactory.getLog(WebConfig.class);
45         
46         @Override
47         public void onStartup(ServletContext container) {
48                 
49                 //need to get properties for userid and password on the pap to get authorization string used in URI Mapping
50                 Properties prop = new Properties();
51                 String propFileName = "xacml.pap.properties";
52                 
53                 try {
54                         InputStream is = new FileInputStream(propFileName);
55                         prop.load(is);
56                 } catch (Exception e) {
57                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "property file '" + propFileName + "' not found in the classpath");
58                 }
59                 
60                 String papID = prop.getProperty("xacml.rest.pap.userid");
61                 String papPass = prop.getProperty("xacml.rest.pap.password");
62                 
63                 String usernameAndPassword = papID+":"+papPass;         
64                 String authorizationString = Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
65                 
66                 
67                 XmlWebApplicationContext appContext = new XmlWebApplicationContext();
68                 appContext.setConfigLocation("classpath:spring.xml");
69                 
70                 System.out.println("Spring XML File Location: " + appContext.getConfigLocations());
71                 logger.info("Spring XML File Location: " + appContext.getConfigLocations());
72
73                 ServletRegistration.Dynamic dispatcher =
74                                 container.addServlet("dispatcher", new DispatcherServlet(appContext));
75                 dispatcher.setLoadOnStartup(1);
76                 dispatcher.addMapping("/@Auth@"+authorizationString+"/ecomp/*");
77         }
78
79
80 }