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.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.InputStream;
26 import java.io.OutputStream;
27 import java.net.URISyntaxException;
29 import java.net.URLDecoder;
30 import java.nio.file.Path;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class HelpServlet extends HttpServlet implements AutoCloseable {
43 private static Logger LOG = LoggerFactory.getLogger(HelpServlet.class);
44 private static final long serialVersionUID = -4285072760648493461L;
46 private static final boolean USE_FILESYSTEM = true;
47 private static final boolean USE_RESSOURCES = !USE_FILESYSTEM;
48 private static final String BASEURI = "/help";
50 private static final boolean REDIRECT_LINKS = true;
52 private final Path basePath;
54 public HelpServlet() {
55 LOG.info("Starting HelpServlet instance {}", this.hashCode());
56 HelpInfrastructureObject.createFilesFromResources();
57 this.basePath = HelpInfrastructureObject.getHelpDirectoryBase();
61 protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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");
70 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
71 * javax.servlet.http.HttpServletResponse) Handle Get Request: if query=?meta=send json
72 * infrastructure for README.md else if file exist send file
75 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
76 String query = req.getQueryString();
77 resp.addHeader("Access-Control-Allow-Origin", "*");
78 resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
79 resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
80 if (query != null && query.contains("meta")) {
82 * LOG.debug("received post with uri="+req.getRequestURI()); String
83 * uri=req.getRequestURI().substring(BASEURI.length()); if(uri.startsWith("/"))
84 * uri=uri.substring(1);
86 File f = new File(HelpInfrastructureObject.KARAFHELPDIRECTORY, "meta.json");
88 LOG.debug("found local meta file");
89 try (BufferedReader rd = new BufferedReader(new FileReader(f));) {
90 String line = rd.readLine();
91 while (line != null) {
92 resp.getOutputStream().println(line);
96 } catch (IOException e) {
97 LOG.debug("Can not read meta file", e);
100 LOG.debug("start walking from path=" + basePath.toAbsolutePath().toString());
101 HelpInfrastructureObject o = null;
102 if (USE_FILESYSTEM) {
104 o = new HelpInfrastructureObject(this.basePath);
105 } catch (URISyntaxException e) {
106 LOG.debug("Can not relsolve URI. ", e);
108 } else if (USE_RESSOURCES) {
109 // o=new HelpInfrastructureObject()
111 resp.getOutputStream().println(o != null ? o.toString() : "");
113 resp.setHeader("Content-Type", "application/json");
115 LOG.debug("received get with uri=" + req.getRequestURI());
116 String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8");
117 if (uri.startsWith("/")) {
118 uri = uri.substring(1);
120 Path p = basePath.resolve(uri);
121 if (USE_FILESYSTEM) {
123 if (f.isFile() && f.exists()) {
124 LOG.debug("found file for request");
125 if (this.isTextFile(f)) {
126 resp.setHeader("Content-Type", "application/text");
127 resp.setHeader("charset", "utf-8");
128 } else if (this.isImageFile(f)) {
129 resp.setHeader("Content-Type", "image/*");
130 } else if (this.ispdf(f)) {
131 resp.setHeader("Content-Type", "application/pdf");
133 LOG.debug("file is not allowed to deliver");
137 LOG.debug("delivering file");
138 OutputStream out = resp.getOutputStream();
139 String version = null;
140 if (REDIRECT_LINKS) {
141 version = getVersionFromRequestedUri(uri);
143 if (this.isTextFile(f) && REDIRECT_LINKS && version != null) {
145 "(!?\\[[^\\]]*?\\])\\(((?:(?!http|www\\.|\\#|\\.com|\\.net|\\.info|\\.org|\\.svg|\\.png|\\.jpg|\\.gif|\\.jpeg|\\.pdf).)*?)\\)";
146 final Pattern pattern = Pattern.compile(regex);
149 try (BufferedReader br = new BufferedReader(new FileReader(f))) {
150 line = br.readLine();
151 while (line != null) {
152 // check line for internal link
153 matcher = pattern.matcher(line);
154 if (matcher.find()) {
155 // extend link with specific version
156 line = line.replace(matcher.group(2),
157 "../" + matcher.group(2) + version + "/README.md");
159 out.write((line + "\n").getBytes());
160 line = br.readLine();
169 try (FileInputStream in = new FileInputStream(f)) {
171 byte[] buffer = new byte[1024];
173 while ((len = in.read(buffer)) != -1) {
174 out.write(buffer, 0, len);
182 LOG.debug("found not file for request");
185 } else if (USE_RESSOURCES) {
186 URL resurl = this.getClass().getResource(p.toString());
187 if (resurl != null)// resource file found
189 if (this.isTextFile(resurl)) {
190 resp.setHeader("Content-Type", "application/text");
191 resp.setHeader("charset", "utf-8");
192 } else if (this.isImageFile(resurl)) {
193 resp.setHeader("Content-Type", "image/*");
194 } else if (this.ispdf(resurl)) {
195 resp.setHeader("Content-Type", "application/pdf");
200 try (InputStream in = this.getClass().getResourceAsStream(p.toString())) {
201 OutputStream out = resp.getOutputStream();
202 byte[] buffer = new byte[1024];
204 while ((len = in.read(buffer)) != -1) {
205 out.write(buffer, 0, len);
212 } else // resource file not found
222 * uri = "help/folder1/folder2/version/README.md"
224 private static String getVersionFromRequestedUri(String uri) {
228 int lastidx = uri.lastIndexOf("/");
232 int slastidx = uri.lastIndexOf("/", lastidx - 1);
236 return uri.substring(slastidx + 1, lastidx);
240 private boolean isTextFile(URL url) {
241 return url != null ? this.isTextFile(url.toString()) : false;
244 private boolean ispdf(URL url) {
245 return url != null ? this.ispdf(url.toString()) : false;
248 private boolean isImageFile(URL url) {
249 return url != null ? this.isImageFile(url.toString()) : false;
252 private boolean ispdf(File f) {
253 return f != null ? this.ispdf(f.getName()) : false;
256 private boolean ispdf(String name) {
257 return name != null ? name.toLowerCase().endsWith("pdf") : false;
260 private boolean isImageFile(File f) {
261 return f != null ? this.isImageFile(f.getName()) : false;
264 private boolean isImageFile(String name) {
267 ? name.toLowerCase().endsWith("png") || name.toLowerCase().endsWith("jpg")
268 || name.toLowerCase().endsWith("jpeg") || name.toLowerCase().endsWith("svg")
269 || name.toLowerCase().endsWith("eps")
273 private boolean isTextFile(File f) {
274 return f != null ? this.isTextFile(f.getName()) : false;
278 private boolean isTextFile(String name) {
280 ? name.toLowerCase().endsWith("md") || name.toLowerCase().endsWith("txt")
281 || name.toLowerCase().endsWith("html") || name.toLowerCase().endsWith("htm")
282 || name.toLowerCase().endsWith("js") || name.toLowerCase().endsWith("css")
287 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
290 public void close() throws Exception {}