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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
20 import com.google.common.io.Files;
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;
37 @HttpWhiteboardServletPattern("/odlux/*")
38 @HttpWhiteboardServletName("ResFilesServlet")
39 @Component(service = Servlet.class)
40 public class ResFilesServlet extends HttpServlet {
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";
47 private final IndexOdluxBundle indexBundle;
49 public ResFilesServlet() {
51 indexBundle = new IndexOdluxBundle();
55 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
57 final String fn = req.getRequestURI();
58 LOG.debug("Get request with for URI: {}", fn);
60 if(LOGO_URL.equals(fn)) {
61 File f = new File(LOGO_OVERWRITE_FILENAME);
63 resp.setStatus(HttpURLConnection.HTTP_OK);
64 resp.setContentType("image/gif");
66 Files.copy(f, resp.getOutputStream());
67 } catch (IOException e) {
68 LOG.warn("Can not copy data", e);
74 OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
75 if (odluxBundleLoader != null) {
76 String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
77 if (fileContent != null) {
79 String mimeType = getMimeType(fn);
80 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
81 int length = byteContent.length;
83 LOG.debug("Found file in resources. Name {} mimetype {} length {} and write to output stream", fn,
85 resp.setContentType(mimeType);
86 resp.setContentLength(length);
87 resp.setStatus(HttpURLConnection.HTTP_OK);
88 try (OutputStream os = resp.getOutputStream()) {
89 os.write(byteContent);
91 } catch (IOException e) {
92 LOG.warn("Can not write data", e);
96 LOG.debug("File {} not found in res.", fn);
97 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
100 LOG.debug("BundleLoaderInstance not found. {}", fn);
101 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
105 public String loadFileContent(String filename) {
106 return this.indexBundle.getResourceFileContent(filename);
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";