c9d8a2d2005d95c9455fa465b3ced6d37ee1bde9
[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 com.google.common.io.Files;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.net.HttpURLConnection;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
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 final Logger LOG = LoggerFactory.getLogger(ResFilesServlet.class);
37     private static final String LOGO_OVERWRITE_FILENAME = "etc/logo.gif";
38     private static final String LOGO_URL="/odlux/images/onapLogo.gif";
39
40     private final IndexOdluxBundle indexBundle;
41
42     public ResFilesServlet() {
43         super();
44         indexBundle = new IndexOdluxBundle();
45     }
46
47     @Override
48     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
49
50         final String fn = req.getRequestURI();
51         LOG.debug("Get request with for URI: {}", fn);
52
53         if(LOGO_URL.equals(fn)) {
54             File f = new File(LOGO_OVERWRITE_FILENAME);
55             if(f.exists()) {
56                 resp.setStatus(HttpURLConnection.HTTP_OK);
57                 resp.setContentType("image/gif");
58                 try {
59                     Files.copy(f, resp.getOutputStream());
60                 } catch (IOException e) {
61                     LOG.warn("Can not copy data", e);
62                     resp.setStatus(500);
63                 }
64                 return;
65             }
66         }
67         OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
68         if (odluxBundleLoader != null) {
69             String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
70             if (fileContent != null) {
71                 //Store header info
72                 String mimeType = getMimeType(fn);
73                 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
74                 int length = byteContent.length;
75
76                 LOG.debug("Found file in resources. Name {} mimetype {} length {}  and write to output stream", fn,
77                         mimeType, length);
78                 resp.setContentType(mimeType);
79                 resp.setContentLength(length);
80                 resp.setStatus(HttpURLConnection.HTTP_OK);
81                 try (OutputStream os = resp.getOutputStream()) {
82                     os.write(byteContent);
83                     os.flush();
84                 } catch (IOException e) {
85                     LOG.warn("Can not write data", e);
86                     resp.setStatus(500);
87                 }
88             } else {
89                 LOG.debug("File {} not found in res.", fn);
90                 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
91             }
92         } else {
93             LOG.debug("BundleLoaderInstance not found. {}", fn);
94             resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
95         }
96     }
97
98     public String loadFileContent(String filename) {
99         return this.indexBundle.getResourceFileContent(filename);
100     }
101
102     //Provide own function that can be overloaded for test
103     public String getMimeType(String fileName) {
104         String t =  getServletContext().getMimeType(fileName);
105         if(t.startsWith("text")) {
106             t+="; charset=utf-8";
107         }
108         return t;
109     }
110
111
112 }