31544c2ebdcefd89ef4649c49cec2c28e4e97dd6
[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.IOException;
21 import java.io.OutputStream;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.onap.ccsdk.features.sdnr.wt.odlux.IndexOdluxBundle;
31 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
32 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleList;
33 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoaderImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ResFilesServlet extends HttpServlet {
38
39         /**
40          * 
41          */
42         private static final long serialVersionUID = -6807215213921798293L;
43         private static Logger LOG = LoggerFactory.getLogger(ResFilesServlet.class);
44         private final IndexOdluxBundle indexBundle;
45
46         public ResFilesServlet() {
47                 super();
48                 indexBundle = new IndexOdluxBundle();
49                 OdluxBundleLoaderImpl.getInstance();
50         }
51
52         private List<String> getBundleNames(final List<OdluxBundle> bundles) {
53                 final List<String> names = new ArrayList<String>();
54                 for (OdluxBundle b : bundles)
55                         names.add(b.getBundleName());
56                 names.add(this.indexBundle.getBundleName());
57                 return names;
58         }
59
60         @Override
61         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
62
63                 LOG.debug("get req: " + req.getRequestURI().toString());
64                 final String fn = req.getRequestURI().toString();
65                 String fileContent = null;
66                 final List<OdluxBundle> bundles = OdluxBundleLoaderImpl.getInstance().getBundles();
67                 if (indexBundle.hasResource(fn)) {
68                         bundles.sort(this.indexBundle.getBundleSorter());
69                         fileContent = indexBundle.getResourceFileContent(fn, this.getBundleNames(bundles));
70                 } else {
71                         LOG.debug("not found in framework res. try to find in applications");
72                         synchronized (bundles) {
73                                 final Iterator<OdluxBundle> it = bundles.iterator();
74                                 while (it.hasNext()) {
75                                         OdluxBundle b = it.next();
76                                         if (b.hasResource(fn)) {
77                                                 LOG.debug("found res in " + b.getBundleName());
78                                                 fileContent = b.getResourceFileContent(fn);
79                                                 break;
80                                         }
81                                 }
82                         }
83                 }
84                 if (fileContent != null) {
85                         LOG.debug("found " + fn + " in res. write to output stream");
86                         resp.setStatus(200);
87                         OutputStream os = resp.getOutputStream();
88                         os.write(fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8));
89                         os.flush();
90                 } else {
91                         LOG.debug("file " + fn + " not found in res.");
92                         resp.setStatus(404);
93
94                 }
95         }
96
97         public String loadFileContent(String filename) {
98                 return this.indexBundle.getResourceFileContent(filename);
99         }
100
101 }