d335e52c7f369bcbe4f8dac319d177a3015c46bd
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.URL;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
28 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleResourceAccess;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class IndexOdluxBundle extends OdluxBundle implements OdluxBundleResourceAccess {
33
34     private static Logger LOG = LoggerFactory.getLogger(IndexOdluxBundle.class);
35
36     private static final String LR = "\n";
37     private static final String BUNDLENAME_APP = "run";
38     private static final String regexRequire = "require\\(\\[(\"" + BUNDLENAME_APP + "\")\\]";
39     private static final String regexFunction = "function[\\ ]*\\((" + BUNDLENAME_APP + ")\\)[\\ ]*\\{";
40     private static final String regexFunctionBody = "(" + BUNDLENAME_APP + "\\.runApplication\\(\\);)";
41     private static final Pattern patternRequire = Pattern.compile(regexRequire);
42     private static final Pattern patternFunction = Pattern.compile(regexFunction);
43     private static final Pattern patternFunctionBody = Pattern.compile(regexFunctionBody);
44
45     public IndexOdluxBundle() {
46         super(null, BUNDLENAME_APP);
47
48     }
49     @Override
50     protected String loadFileContent(URL url)
51     {
52         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
53     }
54
55
56     @Override
57     public String getResourceFileContent(String fn, List<String> bundleNames) {
58         return loadFileContent(this.getResource(fn),bundleNames);
59     }
60
61     private static String loadFileContent(URL url, List<String> bundlesNamesList) {
62         if (url == null) {
63             return null;
64         }
65         LOG.debug("try to load res " + url.toString());
66         StringBuilder sb = new StringBuilder();
67         Matcher matcher;
68         BufferedReader in;
69         try {
70             in = new BufferedReader(new InputStreamReader(url.openStream()));
71
72             String inputLine;
73             while ((inputLine = in.readLine()) != null) {
74                 if (url.getFile().endsWith("index.html")) {
75                     matcher = patternRequire.matcher(inputLine);
76                     if (matcher.find()) {
77                         inputLine = inputLine.substring(0, matcher.start(1)) + "\"" + String.join("\",\"", bundlesNamesList)
78                                 + "\"" + inputLine.substring(matcher.end(1));
79                     }
80                     matcher = patternFunction.matcher(inputLine);
81                     if (matcher.find()) {
82                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
83                                 + inputLine.substring(matcher.end(1));
84                     }
85                     matcher = patternFunctionBody.matcher(inputLine);
86                     if (matcher.find()) {
87                         String hlp = "";
88                         for (String bundle : bundlesNamesList) {
89                             if (!bundle.equals(BUNDLENAME_APP)) {
90                                 hlp += bundle + ".register();" + LR;
91                             }
92                         }
93                         inputLine = inputLine.substring(0, matcher.start(1)) + hlp
94                                 + inputLine.substring(matcher.start(1));
95                     }
96                 }
97                 sb.append(inputLine + LR);
98             }
99             in.close();
100         } catch (IOException e) {
101             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
102             return null;
103         }
104
105         return sb.toString();
106     }
107
108 }