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.helpserver;
 
  20 import java.io.BufferedReader;
 
  22 import java.io.FileInputStream;
 
  23 import java.io.FileReader;
 
  24 import java.io.IOException;
 
  25 import java.io.OutputStream;
 
  26 import java.net.URISyntaxException;
 
  27 import java.net.URLDecoder;
 
  28 import java.nio.file.Path;
 
  29 import javax.servlet.Servlet;
 
  30 import javax.servlet.ServletException;
 
  31 import javax.servlet.http.HttpServlet;
 
  32 import javax.servlet.http.HttpServletRequest;
 
  33 import javax.servlet.http.HttpServletResponse;
 
  34 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
 
  35 import org.osgi.service.component.annotations.Component;
 
  36 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
 
  37 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
 
  38 import org.slf4j.Logger;
 
  39 import org.slf4j.LoggerFactory;
 
  41 @HttpWhiteboardServletPattern("/help/*")
 
  42 @HttpWhiteboardServletName("HelpServlet")
 
  43 @Component(service = Servlet.class)
 
  44 public class HelpServlet extends HttpServlet implements AutoCloseable {
 
  46     private static Logger LOG = LoggerFactory.getLogger(HelpServlet.class);
 
  47     private static final long serialVersionUID = -4285072760648493461L;
 
  49     private static final String BASEURI = "/help";
 
  51     private final Path basePath;
 
  53     public HelpServlet() {
 
  54         LOG.info("Starting HelpServlet instance {}", this.hashCode());
 
  55         HelpInfrastructureObject.createFilesFromResources();
 
  56         this.basePath = HelpInfrastructureObject.getHelpDirectoryBase();
 
  60     public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
  61         resp.addHeader("Access-Control-Allow-Origin", "*");
 
  62         resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
 
  63         resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
 
  67     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
  68         String query = req.getQueryString();
 
  69         resp.addHeader("Access-Control-Allow-Origin", "*");
 
  70         resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
 
  71         resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
 
  72         if (query != null && query.contains("meta")) {
 
  74             File f = new File(HelpInfrastructureObject.KARAFHELPDIRECTORY, "meta.json");
 
  76                 LOG.debug("found local meta file");
 
  77                 try (BufferedReader rd = new BufferedReader(new FileReader(f));) {
 
  78                     String line = rd.readLine();
 
  79                     while (line != null) {
 
  80                         resp.getOutputStream().println(line);
 
  84                 } catch (IOException e) {
 
  85                     LOG.debug("Can not read meta file", e);
 
  88                 LOG.debug("start walking from path=" + basePath.toAbsolutePath().toString());
 
  89                 HelpInfrastructureObject o = null;
 
  91                     o = new HelpInfrastructureObject(this.basePath);
 
  92                 } catch (URISyntaxException e) {
 
  93                     LOG.debug("Can not relsolve URI. ", e);
 
  95                 resp.getOutputStream().println(o != null ? o.toString() : "");
 
  97             resp.setHeader("Content-Type", "application/json");
 
  99             LOG.debug("received get with uri=" + req.getRequestURI());
 
 100             String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8");
 
 101             if (uri.startsWith("/")) {
 
 102                 uri = uri.substring(1);
 
 104             Path p = basePath.resolve(uri);
 
 106             if (f.isFile() && f.exists()) {
 
 107                 LOG.debug("found file for request");
 
 108                 if (this.isTextFile(f)) {
 
 109                     resp.setHeader("Content-Type", "application/text");
 
 110                     resp.setHeader("charset", "utf-8");
 
 111                 } else if (this.isImageFile(f)) {
 
 112                     resp.setHeader("Content-Type", "image/*");
 
 113                 } else if (this.ispdf(f)) {
 
 114                     resp.setHeader("Content-Type", "application/pdf");
 
 116                     LOG.debug("file is not allowed to deliver");
 
 120                 LOG.debug("delivering file");
 
 121                 try (OutputStream out = resp.getOutputStream()) {
 
 122                     try (FileInputStream in = new FileInputStream(f)) {
 
 124                         byte[] buffer = new byte[1024];
 
 126                         while ((len = in.read(buffer)) != -1) {
 
 127                             out.write(buffer, 0, len);
 
 133                 } catch (IOException e) {
 
 134                     LOG.warn("Can not write meta file", e);
 
 138                 LOG.debug("found not file for request");
 
 144     private boolean ispdf(File f) {
 
 145         return f != null && this.ispdf(f.getName());
 
 148     private boolean ispdf(String name) {
 
 149         return name != null && name.toLowerCase().endsWith("pdf");
 
 152     private boolean isImageFile(File f) {
 
 153         return f != null && this.isImageFile(f.getName());
 
 156     private boolean isImageFile(String name) {
 
 159                 ? name.toLowerCase().endsWith("png") || name.toLowerCase().endsWith("jpg")
 
 160                         || name.toLowerCase().endsWith("jpeg") || name.toLowerCase().endsWith("svg")
 
 161                         || name.toLowerCase().endsWith("eps")
 
 165     private boolean isTextFile(File f) {
 
 166         return f != null && this.isTextFile(f.getName());
 
 170     private boolean isTextFile(String name) {
 
 172                 ? name.toLowerCase().endsWith("md") || name.toLowerCase().endsWith("txt")
 
 173                         || name.toLowerCase().endsWith("html") || name.toLowerCase().endsWith("htm")
 
 174                         || name.toLowerCase().endsWith("js") || name.toLowerCase().endsWith("css")
 
 179     public void close() throws Exception {}