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.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;
33 public class ResFilesServlet extends HttpServlet {
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";
40 private final IndexOdluxBundle indexBundle;
42 public ResFilesServlet() {
44 indexBundle = new IndexOdluxBundle();
48 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
50 final String fn = req.getRequestURI();
51 LOG.debug("Get request with for URI: {}", fn);
53 if(LOGO_URL.equals(fn)) {
54 File f = new File(LOGO_OVERWRITE_FILENAME);
56 resp.setStatus(HttpURLConnection.HTTP_OK);
57 resp.setContentType("image/gif");
59 Files.copy(f, resp.getOutputStream());
60 } catch (IOException e) {
61 LOG.warn("Can not copy data", e);
67 OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
68 if (odluxBundleLoader != null) {
69 String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
70 if (fileContent != null) {
72 String mimeType = getMimeType(fn);
73 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
74 int length = byteContent.length;
76 LOG.debug("Found file in resources. Name {} mimetype {} length {} and write to output stream", fn,
78 resp.setContentType(mimeType);
79 resp.setContentLength(length);
80 resp.setStatus(HttpURLConnection.HTTP_OK);
81 try (OutputStream os = resp.getOutputStream()) {
82 os.write(byteContent);
84 } catch (IOException e) {
85 LOG.warn("Can not write data", e);
89 LOG.debug("File {} not found in res.", fn);
90 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
93 LOG.debug("BundleLoaderInstance not found. {}", fn);
94 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
98 public String loadFileContent(String filename) {
99 return this.indexBundle.getResourceFileContent(filename);
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";