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
 
  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==========================================================================
 
  17  ******************************************************************************/
 
  18 package org.onap.ccsdk.features.sdnr.wt.odlux;
 
  20 import java.io.IOException;
 
  21 import java.io.OutputStream;
 
  22 import java.net.HttpURLConnection;
 
  24 import javax.servlet.ServletException;
 
  25 import javax.servlet.http.HttpServlet;
 
  26 import javax.servlet.http.HttpServletRequest;
 
  27 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 Logger LOG = LoggerFactory.getLogger(ResFilesServlet.class);
 
  39     private final IndexOdluxBundle indexBundle;
 
  41     public ResFilesServlet() {
 
  43         indexBundle = new IndexOdluxBundle();
 
  47     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
  49         final String fn = req.getRequestURI();
 
  50         LOG.debug("Get request with for URI: {}", fn);
 
  52         OdluxBundleLoader odluxBundleLoader = OdluxBundleLoaderImpl.getInstance();
 
  53                 if (odluxBundleLoader != null) {
 
  54                         String fileContent = odluxBundleLoader.getResourceContent(fn, indexBundle);
 
  55                         if (fileContent != null) {
 
  57                                 String mimeType = getMimeType(fn);
 
  58                                 byte[] byteContent = fileContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
 
  59                                 int length = byteContent.length;
 
  61                                 LOG.debug("Found file in resources. Name {} mimetype {} length {}  and write to output stream", fn, mimeType, length);
 
  62                                 resp.setContentType(mimeType);
 
  63                                 resp.setContentLength(length);
 
  64                                 resp.setStatus(HttpURLConnection.HTTP_OK);
 
  65                                 OutputStream os = resp.getOutputStream();
 
  66                                 os.write(byteContent);
 
  70                                 LOG.debug("File {} not found in res.", fn);
 
  71                                 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
 
  74                         LOG.debug("BundleLoaderInstance to found.", fn);
 
  75                         resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
 
  79     public String loadFileContent(String filename) {
 
  80         return this.indexBundle.getResourceFileContent(filename);
 
  83     //Provide own function that can be overloaded for test
 
  84     public String getMimeType(String fileName) {
 
  85         return getServletContext().getMimeType(fileName);