1 package com.highstreet.technologies.info;
3 import java.io.BufferedOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
11 import java.net.URLDecoder;
12 import java.nio.charset.Charset;
13 import java.nio.file.Path;
15 import javax.servlet.ServletException;
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
20 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
21 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
22 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
23 import org.apache.commons.compress.utils.IOUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class InfoServlet extends HttpServlet {
30 private static Logger LOG = LoggerFactory.getLogger(InfoServlet.class);
31 private static final String BASEURI = "/info";
32 private static final String AKKACONF_FILENAME = "configuration/initial/akka.conf";
33 private static final String GEOCONF_FILENAME = "configuration/initial/geo.conf";
34 private static final String DATABASECONF_FILENAME = "etc/elasticsearch.yml";
35 private static final String DEVMGRCONF_FILENAME = "etc/devicemanager.properties";
36 private static final String KARAFLOG_FILENAME = "etc/org.ops4j.pax.logging.cfg";
37 private static final String KARAFLOG_FOLDER = "data/log/";
38 private static final String BUNDLE_FOLDER = "data/cache/org.eclipse.osgi/bundles";
40 private static final String VERSIONTXT_FILENAME = "etc/version.txt";
41 private static final String KARAFLOG_TARGZ = "data/log/karaflog.tar.gz";
42 private static final String CHARSET = "UTF-8";
43 private final Path basePath;
45 public InfoServlet() {
46 this.basePath = new File("/html").toPath();
50 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
52 String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8");
53 LOG.debug("handle get request for uri="+uri);
54 if (uri.startsWith("/"))
55 uri = uri.substring(1);
56 if(uri.startsWith("api/"))
58 uri = uri.substring("api/".length());
59 if (uri.startsWith("data/")) {
60 uri = uri.substring("data/".length());
61 this.doGetDataRequest(uri, req, resp);
66 Path p = basePath.resolve(uri);
67 LOG.debug("try to find file:"+p.toString());
68 URL resurl = this.getClass().getResource(p.toString());
69 if (resurl != null)// resource file found
71 if (this.isHTMLFile(resurl)) {
72 LOG.debug("filetype: html");
73 resp.setHeader("Content-Type", "text/html");
74 resp.setHeader("charset", "utf-8");
75 } else if (this.isJavascript(resurl)) {
76 LOG.debug("filetype: js");
77 resp.setHeader("Content-Type", "application/javascript");
78 resp.setHeader("charset", "utf-8");
79 } else if (this.isStylesheet(resurl)) {
80 LOG.debug("filetype: css");
81 resp.setHeader("Content-Type", "text/css");
82 resp.setHeader("charset", "utf-8");
83 } else if (this.isTextFile(resurl)) {
84 LOG.debug("filetype: text");
85 resp.setHeader("Content-Type", "application/text");
86 resp.setHeader("charset", "utf-8");
87 } else if (this.isImageFile(resurl)) {
88 LOG.debug("filetype: image");
89 resp.setHeader("Content-Type", "image/*");
90 } else if (this.ispdf(resurl)) {
91 LOG.debug("filetype: pdf");
92 resp.setHeader("Content-Type", "application/pdf");
94 LOG.debug("unknown file type request");
98 try (InputStream in = this.getClass().getResourceAsStream(p.toString())) {
99 OutputStream out = resp.getOutputStream();
100 byte[] buffer = new byte[1024];
102 while ((len = in.read(buffer)) != -1) {
103 out.write(buffer, 0, len);
110 } else // resource file not found
112 LOG.debug("resource file not found");
118 private void doGetDataRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
123 this.writeFileStream(KARAFLOG_TARGZ, resp, "application/x-gzip",true);
124 } catch (Exception e) {
125 LOG.warn("problem reading " + KARAFLOG_TARGZ + ": " + e.getMessage());
133 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
134 String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8");
135 LOG.debug("handle get request for uri="+uri);
136 if (uri.startsWith("/"))
137 uri = uri.substring(1);
138 if (uri.startsWith("api/")) {
139 uri = uri.substring("api/".length());
140 if (uri.startsWith("data/")) {
141 uri = uri.substring("data/".length());
142 this.doPostDataRequest(uri, req, resp);
143 } else if (uri.startsWith("task/")) {
144 uri = uri.substring("task/".length());
145 this.doPostTaskRequest(uri, req, resp);
152 LOG.debug("unknown uri to handle");
157 private void doPostTaskRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
160 case "dbbackup.create":
162 resp.getOutputStream().write("not yet supported".getBytes());
163 } catch (IOException e) {
167 case "dbbackup.restore":
169 resp.getOutputStream().write("not yet supported".getBytes());
170 } catch (IOException e) {
181 private void doPostDataRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
182 String acceptFormat=req.getHeader("Accept");
186 this.writeFileStream(AKKACONF_FILENAME, resp);
187 } catch (Exception e) {
188 LOG.warn("problem reading " + AKKACONF_FILENAME + ": " + e.getMessage());
194 this.writeFileStream(GEOCONF_FILENAME, resp);
195 } catch (Exception e) {
196 LOG.warn("problem reading " + GEOCONF_FILENAME + ": " + e.getMessage());
203 this.writeFileStream(DEVMGRCONF_FILENAME, resp);
204 } catch (Exception e) {
205 LOG.warn("problem reading " + DEVMGRCONF_FILENAME + ": " + e.getMessage());
212 this.writeFileStream(DATABASECONF_FILENAME, resp);
213 } catch (Exception e) {
214 LOG.warn("problem reading " + DATABASECONF_FILENAME + ": " + e.getMessage());
221 this.writeFileStream(KARAFLOG_FILENAME, resp);
222 } catch (Exception e) {
223 LOG.warn("problem reading " + KARAFLOG_FILENAME + ": " + e.getMessage());
231 KarafBundleList list=new KarafBundleList(BUNDLE_FOLDER);
233 this.writeOutput(list.toJSON(),resp, "application/json");
237 LOG.warn("problem reading bundlelist: " + e.getMessage());
242 this.createLogDownload(KARAFLOG_TARGZ);
243 this.writeFileStream(KARAFLOG_TARGZ, resp, "application/x-gzip",true);
244 } catch (Exception e) {
245 LOG.warn("problem reading " + KARAFLOG_TARGZ + ": " + e.getMessage());
256 private void writeOutput(String str, HttpServletResponse resp) throws IOException
258 this.writeOutput(str,resp,"text/plain");
260 private void writeOutput(String str, HttpServletResponse resp, String contentType) throws IOException
262 this.writeOutput(str,resp,contentType,null);
264 private void writeOutput(String str, HttpServletResponse resp, String contentType,String asDownloadFilename) throws IOException{
265 OutputStream out = resp.getOutputStream();
266 if (contentType != null)
267 resp.setHeader("Content-Type", contentType);
268 if(asDownloadFilename!=null)
269 resp.setHeader("Content-Disposition","inline; filename=\""+asDownloadFilename+"\"");
270 byte[] buffer = new byte[1024];
272 out.write(str.getBytes(CHARSET));
277 private void createLogDownload(String tarFilename) {
278 LOG.debug("start creating tar file "+tarFilename);
279 File f = new File(tarFilename);
282 FileOutputStream fOut = null;
283 BufferedOutputStream bOut = null;
284 GzipCompressorOutputStream gzOut = null;
285 TarArchiveOutputStream tOut = null;
287 // System.out.println(new File(".").getAbsolutePath());
288 fOut = new FileOutputStream(new File(tarFilename));
289 bOut = new BufferedOutputStream(fOut);
290 gzOut = new GzipCompressorOutputStream(bOut);
291 tOut = new TarArchiveOutputStream(gzOut);
292 this.addFileToTarGz(tOut, KARAFLOG_FOLDER, "", ".log");
293 } catch (IOException e) {
294 LOG.warn("problem creating tar:" + e.getMessage());
307 LOG.debug("finished creating tar file");
308 } catch (IOException e) {
309 LOG.warn("problem closing streams:" + e.getMessage());
315 private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base, final String filter)
317 File f = new File(path);
318 String entryName = base + f.getName();
319 TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
320 tOut.putArchiveEntry(tarEntry);
324 if( f.getName().contains(filter)) {
325 LOG.debug("adding to tar:"+f.getName());
326 IOUtils.copy(new FileInputStream(f), tOut);
327 tOut.closeArchiveEntry();
330 LOG.debug("file "+f.getName()+" filtered out, filter="+filter);
332 tOut.closeArchiveEntry();
333 File[] children = f.listFiles();
334 if (children != null) {
335 for (File child : children) {
336 addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/", filter);
342 private void writeFileStream(String filename, HttpServletResponse resp) throws IOException {
343 this.writeFileStream(filename, resp, null);
345 private void writeFileStream(String filename, HttpServletResponse resp,String contentType) throws IOException {
346 this.writeFileStream(filename, resp, contentType,false);
348 private void writeFileStream(String filename, HttpServletResponse resp, String contentType,boolean asDownload) throws IOException {
349 File file=new File(filename);
352 LOG.debug("unable to write filestream to http. file not found: "+filename);
356 LOG.debug("write file "+filename+" to http with content-type "+contentType);
357 InputStream in = new FileInputStream(file);
358 OutputStream out = resp.getOutputStream();
359 if (contentType != null)
360 resp.setHeader("Content-Type", contentType);
362 resp.setHeader("Content-Disposition","inline; filename=\""+file.getName()+"\"");
363 byte[] buffer = new byte[1024];
365 while ((len = in.read(buffer)) != -1) {
366 out.write(buffer, 0, len);
373 private boolean isHTMLFile(URL url) {
374 return url != null ? (url.toString().endsWith("html") || url.toString().endsWith("htm")) : false;
377 private boolean isTextFile(URL url) {
378 return url != null ? this.isTextFile(url.toString()) : false;
381 private boolean ispdf(URL url) {
382 return url != null ? this.ispdf(url.toString()) : false;
385 private boolean isImageFile(URL url) {
386 return url != null ? this.isImageFile(url.toString()) : false;
389 private boolean ispdf(File f) {
390 return f != null ? this.ispdf(f.getName()) : false;
393 private boolean ispdf(String name) {
394 return name != null ? name.toLowerCase().endsWith("pdf") : false;
397 private boolean isImageFile(File f) {
398 return f != null ? this.isImageFile(f.getName()) : false;
401 private boolean isImageFile(String name) {
404 ? (name.toLowerCase().endsWith("png") || name.toLowerCase().endsWith("jpg")
405 || name.toLowerCase().endsWith("jpeg") || name.toLowerCase().endsWith("svg")
406 || name.toLowerCase().endsWith("eps"))
410 private boolean isTextFile(File f) {
411 return f != null ? this.isTextFile(f.getName()) : false;
415 private boolean isJavascript(URL url) {
416 return url != null ? url.toString().endsWith("js") : false;
419 private boolean isStylesheet(URL url) {
420 return url != null ? url.toString().endsWith("css") : false;
423 private boolean isTextFile(String name) {
425 ? (name.toLowerCase().endsWith("md") || name.toLowerCase().endsWith("txt")
426 || name.toLowerCase().endsWith("map"))