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");
58 Files.copy(f, resp.getOutputStream());
62 OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
63 if (odluxBundleLoader != null) {
64 String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
65 if (fileContent != null) {
67 String mimeType = getMimeType(fn);
68 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
69 int length = byteContent.length;
71 LOG.debug("Found file in resources. Name {} mimetype {} length {} and write to output stream", fn,
73 resp.setContentType(mimeType);
74 resp.setContentLength(length);
75 resp.setStatus(HttpURLConnection.HTTP_OK);
76 OutputStream os = resp.getOutputStream();
77 os.write(byteContent);
81 LOG.debug("File {} not found in res.", fn);
82 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
85 LOG.debug("BundleLoaderInstance to found.", fn);
86 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
90 public String loadFileContent(String filename) {
91 return this.indexBundle.getResourceFileContent(filename);
94 //Provide own function that can be overloaded for test
95 public String getMimeType(String fileName) {
96 String t = getServletContext().getMimeType(fileName);
97 if(t.startsWith("text")) {