fe00662aa8a3b59bd3487a3aaea7c656ca5a2352
[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.net.HttpURLConnection;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoader;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ResFilesServlet extends HttpServlet {
34
35     private static final long serialVersionUID = -6807215213921798293L;
36     private static Logger LOG = LoggerFactory.getLogger(ResFilesServlet.class);
37
38
39     private final IndexOdluxBundle indexBundle;
40
41     public ResFilesServlet() {
42         super();
43         indexBundle = new IndexOdluxBundle();
44     }
45
46     @Override
47     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
48
49         final String fn = req.getRequestURI();
50         LOG.debug("Get request with for URI: {}", fn);
51
52         OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
53                 if (odluxBundleLoader != null) {
54                         String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
55                         if (fileContent != null) {
56                                 //Store header info
57                                 String mimeType = getMimeType(fn);
58                                 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
59                                 int length = byteContent.length;
60
61                                 LOG.debug("Found file in resources. Name {} mimetype {} length {}  and write to output stream", fn, mimeType, length);
62                                 resp.setContentType(mimeType);
63                                 resp.setContentLength(length);
64                                 resp.setStatus(HttpURLConnection.HTTP_OK);
65                                 OutputStream os = resp.getOutputStream();
66                                 os.write(byteContent);
67                                 os.flush();
68                                 os.close();
69                         } else {
70                                 LOG.debug("File {} not found in res.", fn);
71                                 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
72                         }
73                 } else {
74                         LOG.debug("BundleLoaderInstance to found.", fn);
75                         resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
76                 }
77     }
78
79     public String loadFileContent(String filename) {
80         return this.indexBundle.getResourceFileContent(filename);
81     }
82
83     //Provide own function that can be overloaded for test
84     public String getMimeType(String fileName) {
85         return getServletContext().getMimeType(fileName);
86     }
87
88 }