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.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class HelpServlet extends HttpServlet implements AutoCloseable {
39 private static Logger LOG = LoggerFactory.getLogger(HelpServlet.class);
40 private static final long serialVersionUID = -4285072760648493461L;
42 private static final String BASEURI = "/help";
44 private final Path basePath;
46 public HelpServlet() {
47 LOG.info("Starting HelpServlet instance {}", this.hashCode());
48 HelpInfrastructureObject.createFilesFromResources();
49 this.basePath = HelpInfrastructureObject.getHelpDirectoryBase();
53 public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54 resp.addHeader("Access-Control-Allow-Origin", "*");
55 resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
56 resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
60 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
61 String query = req.getQueryString();
62 resp.addHeader("Access-Control-Allow-Origin", "*");
63 resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
64 resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
65 if (query != null && query.contains("meta")) {
67 File f = new File(HelpInfrastructureObject.KARAFHELPDIRECTORY, "meta.json");
69 LOG.debug("found local meta file");
70 try (BufferedReader rd = new BufferedReader(new FileReader(f));) {
71 String line = rd.readLine();
72 while (line != null) {
73 resp.getOutputStream().println(line);
77 } catch (IOException e) {
78 LOG.debug("Can not read meta file", e);
81 LOG.debug("start walking from path=" + basePath.toAbsolutePath().toString());
82 HelpInfrastructureObject o = null;
84 o = new HelpInfrastructureObject(this.basePath);
85 } catch (URISyntaxException e) {
86 LOG.debug("Can not relsolve URI. ", e);
88 resp.getOutputStream().println(o != null ? o.toString() : "");
90 resp.setHeader("Content-Type", "application/json");
92 LOG.debug("received get with uri=" + req.getRequestURI());
93 String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8");
94 if (uri.startsWith("/")) {
95 uri = uri.substring(1);
97 Path p = basePath.resolve(uri);
99 if (f.isFile() && f.exists()) {
100 LOG.debug("found file for request");
101 if (this.isTextFile(f)) {
102 resp.setHeader("Content-Type", "application/text");
103 resp.setHeader("charset", "utf-8");
104 } else if (this.isImageFile(f)) {
105 resp.setHeader("Content-Type", "image/*");
106 } else if (this.ispdf(f)) {
107 resp.setHeader("Content-Type", "application/pdf");
109 LOG.debug("file is not allowed to deliver");
113 LOG.debug("delivering file");
114 try (OutputStream out = resp.getOutputStream()) {
115 try (FileInputStream in = new FileInputStream(f)) {
117 byte[] buffer = new byte[1024];
119 while ((len = in.read(buffer)) != -1) {
120 out.write(buffer, 0, len);
126 } catch (IOException e) {
127 LOG.warn("Can not write meta file", e);
131 LOG.debug("found not file for request");
137 private boolean ispdf(File f) {
138 return f != null && this.ispdf(f.getName());
141 private boolean ispdf(String name) {
142 return name != null && name.toLowerCase().endsWith("pdf");
145 private boolean isImageFile(File f) {
146 return f != null && this.isImageFile(f.getName());
149 private boolean isImageFile(String name) {
152 ? name.toLowerCase().endsWith("png") || name.toLowerCase().endsWith("jpg")
153 || name.toLowerCase().endsWith("jpeg") || name.toLowerCase().endsWith("svg")
154 || name.toLowerCase().endsWith("eps")
158 private boolean isTextFile(File f) {
159 return f != null && this.isTextFile(f.getName());
163 private boolean isTextFile(String name) {
165 ? name.toLowerCase().endsWith("md") || name.toLowerCase().endsWith("txt")
166 || name.toLowerCase().endsWith("html") || name.toLowerCase().endsWith("htm")
167 || name.toLowerCase().endsWith("js") || name.toLowerCase().endsWith("css")
172 public void close() throws Exception {}