1ea27d71fe03b82f8c8de9ad1adebdb1116bdfed
[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
50     @Override
51     protected String loadFileContent(URL url) {
52         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
53     }
54
55     @Override
56     public String getResourceFileContent(String fn, List<String> bundleNames) {
57         return loadFileContent(this.getResource(fn), bundleNames);
58     }
59
60     private static String loadFileContent(URL url, List<String> bundlesNamesList) {
61         if (url == null) {
62             return null;
63         }
64         LOG.debug("try to load res " + url.toString());
65         StringBuilder sb = new StringBuilder();
66         Matcher matcher;
67         BufferedReader in = null;
68         try {
69             in = new BufferedReader(new InputStreamReader(url.openStream()));
70
71             String inputLine;
72             while ((inputLine = in.readLine()) != null) {
73                 if (url.getFile().endsWith("index.html")) {
74                     matcher = patternRequire.matcher(inputLine);
75                     if (matcher.find()) {
76                         inputLine = inputLine.substring(0, matcher.start(1)) + "\""
77                                 + String.join("\",\"", bundlesNamesList) + "\"" + inputLine.substring(matcher.end(1));
78                     }
79                     matcher = patternFunction.matcher(inputLine);
80                     if (matcher.find()) {
81                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
82                                 + inputLine.substring(matcher.end(1));
83                     }
84                     matcher = patternFunctionBody.matcher(inputLine);
85                     if (matcher.find()) {
86                         String hlp = "";
87                         for (String bundle : bundlesNamesList) {
88                             if (!bundle.equals(BUNDLENAME_APP)) {
89                                 hlp += bundle + ".register();" + LR;
90                             }
91                         }
92                         inputLine =
93                                 inputLine.substring(0, matcher.start(1)) + hlp + inputLine.substring(matcher.start(1));
94                     }
95                 }
96                 sb.append(inputLine + LR);
97             }
98
99         } catch (IOException e) {
100             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
101             return null;
102         } finally {
103             try {
104                 if (in != null) {
105                     in.close();
106                 }
107             } catch (IOException e) {
108
109             }
110         }
111
112         return sb.toString();
113     }
114
115 }