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