Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / info / impl / src / main / java / com / highstreet / technologies / info / InfoServlet.java
1 package com.highstreet.technologies.info;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
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;
10 import java.net.URL;
11 import java.net.URLDecoder;
12 import java.nio.charset.Charset;
13 import java.nio.file.Path;
14
15 import javax.servlet.ServletException;
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
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;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class InfoServlet extends HttpServlet {
29
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";
39
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;
44
45         public InfoServlet() {
46                 this.basePath = new File("/html").toPath();
47         }
48
49         @Override
50         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
51
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/"))
57                 {
58                         uri = uri.substring("api/".length());
59                         if (uri.startsWith("data/")) {
60                                 uri = uri.substring("data/".length());
61                                 this.doGetDataRequest(uri, req, resp);
62                         }
63                 }
64                 else
65                 {
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
70                         {
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");
93                                 } else {
94                                         LOG.debug("unknown file type request");
95                                         resp.setStatus(404);
96                                         return;
97                                 }
98                                 try (InputStream in = this.getClass().getResourceAsStream(p.toString())) {
99                                         OutputStream out = resp.getOutputStream();
100                                         byte[] buffer = new byte[1024];
101                                         int len;
102                                         while ((len = in.read(buffer)) != -1) {
103                                                 out.write(buffer, 0, len);
104                                         }
105                                         in.close();
106                                         out.flush();
107                                         out.close();
108                                 }
109
110                         } else // resource file not found
111                         {
112                                 LOG.debug("resource file not found");
113                                 resp.setStatus(404);
114                         }
115                 }
116         }
117
118         private void doGetDataRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
119                 switch(uri)
120                 {
121                 case "log.download":
122                         try {
123                                 this.writeFileStream(KARAFLOG_TARGZ, resp, "application/x-gzip",true);
124                         } catch (Exception e) {
125                                 LOG.warn("problem reading " + KARAFLOG_TARGZ + ": " + e.getMessage());
126                                 resp.setStatus(500);
127                         }
128                         break;
129                 }
130
131         }
132
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);
146                         } else {
147                                 resp.setStatus(404);
148                         }
149
150                 } else
151                 {
152                         LOG.debug("unknown uri to handle");
153                         resp.setStatus(404);
154                 }
155         }
156
157         private void doPostTaskRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
158                 switch(uri)
159                 {
160                 case "dbbackup.create":
161                         try {
162                                 resp.getOutputStream().write("not yet supported".getBytes());
163                         } catch (IOException e) {
164
165                         }
166                         break;
167                 case "dbbackup.restore":
168                         try {
169                                 resp.getOutputStream().write("not yet supported".getBytes());
170                         } catch (IOException e) {
171
172                         }
173                         break;
174                 default:
175                         resp.setStatus(404);
176                         break;
177                 }
178
179         }
180
181         private void doPostDataRequest(String uri, HttpServletRequest req, HttpServletResponse resp) {
182                 String acceptFormat=req.getHeader("Accept");
183                 switch (uri) {
184                 case "akka.conf":
185                         try {
186                                 this.writeFileStream(AKKACONF_FILENAME, resp);
187                         } catch (Exception e) {
188                                 LOG.warn("problem reading " + AKKACONF_FILENAME + ": " + e.getMessage());
189                                 resp.setStatus(500);
190                         }
191                         break;
192                 case "geo.conf":
193                         try {
194                                 this.writeFileStream(GEOCONF_FILENAME, resp);
195                         } catch (Exception e) {
196                                 LOG.warn("problem reading " + GEOCONF_FILENAME + ": " + e.getMessage());
197                                 resp.setStatus(500);
198
199                         }
200                         break;
201                 case "devmgr.prop":
202                         try {
203                                 this.writeFileStream(DEVMGRCONF_FILENAME, resp);
204                         } catch (Exception e) {
205                                 LOG.warn("problem reading " + DEVMGRCONF_FILENAME + ": " + e.getMessage());
206                                 resp.setStatus(500);
207
208                         }
209                         break;
210                 case "es.yml":
211                         try {
212                                 this.writeFileStream(DATABASECONF_FILENAME, resp);
213                         } catch (Exception e) {
214                                 LOG.warn("problem reading " + DATABASECONF_FILENAME + ": " + e.getMessage());
215                                 resp.setStatus(500);
216
217                         }
218                         break;
219                 case "log.prop":
220                         try {
221                                 this.writeFileStream(KARAFLOG_FILENAME, resp);
222                         } catch (Exception e) {
223                                 LOG.warn("problem reading " + KARAFLOG_FILENAME + ": " + e.getMessage());
224                                 resp.setStatus(500);
225
226                         }
227                         break;
228                 case "bundle.list":
229                         try
230                         {
231                                 KarafBundleList list=new KarafBundleList(BUNDLE_FOLDER);
232                                 list.scan();
233                                 this.writeOutput(list.toJSON(),resp, "application/json");
234                         }
235                         catch(IOException e)
236                         {
237                                 LOG.warn("problem reading bundlelist: " + e.getMessage());
238                                 resp.setStatus(500);
239                         }
240                 case "log.download":
241                         try {
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());
246                                 resp.setStatus(500);
247                         }
248                         break;
249
250                 default:
251                         resp.setStatus(404);
252                         break;
253                 }
254         }
255
256         private void writeOutput(String str, HttpServletResponse resp) throws IOException
257         {
258                 this.writeOutput(str,resp,"text/plain");
259         }
260         private void writeOutput(String str, HttpServletResponse resp, String contentType) throws IOException
261         {
262                 this.writeOutput(str,resp,contentType,null);
263         }
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];
271                 int len;
272                 out.write(str.getBytes(CHARSET));
273                 out.flush();
274                 out.close();
275         }
276
277         private void createLogDownload(String tarFilename) {
278                 LOG.debug("start creating tar file "+tarFilename);
279                 File f = new File(tarFilename);
280                 if (f.exists())
281                         f.delete();
282                 FileOutputStream fOut = null;
283                 BufferedOutputStream bOut = null;
284                 GzipCompressorOutputStream gzOut = null;
285                 TarArchiveOutputStream tOut = null;
286                 try {
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());
295                 } finally {
296                         try {
297                                 if (tOut != null) {
298                                         tOut.finish();
299                                         tOut.close();
300                                 }
301                                 if (gzOut != null)
302                                         gzOut.close();
303                                 if (bOut != null)
304                                         bOut.close();
305                                 if (fOut != null)
306                                         fOut.close();
307                                 LOG.debug("finished creating tar file");
308                         } catch (IOException e) {
309                                 LOG.warn("problem closing streams:" + e.getMessage());
310                         }
311                 }
312
313         }
314
315         private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base, final String filter)
316                         throws IOException {
317                 File f = new File(path);
318                 String entryName = base + f.getName();
319                 TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
320                 tOut.putArchiveEntry(tarEntry);
321
322                 if (f.isFile())
323                 {
324                         if( f.getName().contains(filter)) {
325                                 LOG.debug("adding to tar:"+f.getName());
326                                 IOUtils.copy(new FileInputStream(f), tOut);
327                                 tOut.closeArchiveEntry();
328                         }
329                         else
330                                 LOG.debug("file "+f.getName()+" filtered out, filter="+filter);
331                 } else {
332                         tOut.closeArchiveEntry();
333                         File[] children = f.listFiles();
334                         if (children != null) {
335                                 for (File child : children) {
336                                         addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/", filter);
337                                 }
338                         }
339                 }
340         }
341
342         private void writeFileStream(String filename, HttpServletResponse resp) throws IOException {
343                 this.writeFileStream(filename, resp, null);
344         }
345         private void writeFileStream(String filename, HttpServletResponse resp,String contentType) throws IOException {
346                 this.writeFileStream(filename, resp, contentType,false);
347         }
348         private void writeFileStream(String filename, HttpServletResponse resp, String contentType,boolean asDownload) throws IOException {
349                 File file=new File(filename);
350                 if(!file.exists())
351                 {
352                         LOG.debug("unable to write filestream to http. file not found: "+filename);
353                         resp.setStatus(404);
354                         return;
355                 }
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);
361                 if(asDownload)
362                         resp.setHeader("Content-Disposition","inline; filename=\""+file.getName()+"\"");
363                 byte[] buffer = new byte[1024];
364                 int len;
365                 while ((len = in.read(buffer)) != -1) {
366                         out.write(buffer, 0, len);
367                 }
368                 in.close();
369                 out.flush();
370                 out.close();
371         }
372
373         private boolean isHTMLFile(URL url) {
374                 return url != null ? (url.toString().endsWith("html") || url.toString().endsWith("htm")) : false;
375         }
376
377         private boolean isTextFile(URL url) {
378                 return url != null ? this.isTextFile(url.toString()) : false;
379         }
380
381         private boolean ispdf(URL url) {
382                 return url != null ? this.ispdf(url.toString()) : false;
383         }
384
385         private boolean isImageFile(URL url) {
386                 return url != null ? this.isImageFile(url.toString()) : false;
387         }
388
389         private boolean ispdf(File f) {
390                 return f != null ? this.ispdf(f.getName()) : false;
391         }
392
393         private boolean ispdf(String name) {
394                 return name != null ? name.toLowerCase().endsWith("pdf") : false;
395         }
396
397         private boolean isImageFile(File f) {
398                 return f != null ? this.isImageFile(f.getName()) : false;
399         }
400
401         private boolean isImageFile(String name) {
402
403                 return name != null
404                                 ? (name.toLowerCase().endsWith("png") || name.toLowerCase().endsWith("jpg")
405                                                 || name.toLowerCase().endsWith("jpeg") || name.toLowerCase().endsWith("svg")
406                                                 || name.toLowerCase().endsWith("eps"))
407                                 : false;
408         }
409
410         private boolean isTextFile(File f) {
411                 return f != null ? this.isTextFile(f.getName()) : false;
412
413         }
414
415         private boolean isJavascript(URL url) {
416                 return url != null ? url.toString().endsWith("js") : false;
417         }
418
419         private boolean isStylesheet(URL url) {
420                 return url != null ? url.toString().endsWith("css") : false;
421         }
422
423         private boolean isTextFile(String name) {
424                 return name != null
425                                 ? (name.toLowerCase().endsWith("md") || name.toLowerCase().endsWith("txt")
426                                                 || name.toLowerCase().endsWith("map"))
427                                 : false;
428         }
429
430 }