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.dataprovider.http;
20 import java.io.IOException;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import java.util.jar.Attributes;
26 import java.util.jar.Manifest;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.onap.ccsdk.features.sdnr.wt.common.Resources;
35 import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
36 import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ODLVersionLUT;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.SystemInfo;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class AboutHttpServlet extends HttpServlet {
47 private static final long serialVersionUID = 1L;
48 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
49 private static final String UNKNOWN = "unknown";
50 private static final String METAINF_MAVEN = "/META-INF/maven/";
51 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
53 private static final String URI_PRE = "/about";
54 private static final String RES_BASEPATH = "about/";
56 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
57 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
58 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
59 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
60 private static final String PLACEHOLDER_ODLUX_REVISION = "{odlux-revision}";
61 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
62 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
63 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
64 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
65 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
66 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
67 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
68 private static final String README_FILE = "README.md";
70 private final String groupId = "org.onap.ccsdk.features.sdnr.wt";
71 private final String artifactId = "sdnr-wt-data-provider-provider";
73 private final Map<String, String> data;
74 private final String readmeContent;
76 public AboutHttpServlet() {
78 this.data = new HashMap<>();
79 this.collectStaticData();
80 this.readmeContent = this.render(this.getResourceFileContent(README_FILE));
84 * collect static versioning data
86 private void collectStaticData() {
87 PomPropertiesFile props = this.getPomProperties();
88 final String ccsdkVersion = this.getPomParentVersion();
89 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
90 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(ccsdkVersion, UNKNOWN));
91 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
92 this.data.put(PLACEHOLDER_ODLUX_REVISION, this.getPomProperty("odlux.buildno"));
93 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
94 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
95 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, "1.8.1-SNAPSHOT");
96 this.data.put(PLACEHOLDER_MDSAL_VERSION, SystemInfo.getMdSalVersion(UNKNOWN));
97 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
98 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
102 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
104 String uri = req.getRequestURI().substring(URI_PRE.length());
105 LOG.debug("request for {}", uri);
106 if (uri.length() <= 0 || uri.equals("/")) {
110 String content = this.render();
111 byte[] output = content != null ? content.getBytes() : new byte[0];
113 resp.setStatus(HttpServletResponse.SC_OK);
114 resp.setContentLength(output.length);
115 resp.setContentType("text/plain");
116 ServletOutputStream os = null;
118 os = resp.getOutputStream();
121 catch(IOException e) {
122 LOG.warn("problem writing response for {}: {}",uri,e);
129 catch(IOException e) {
130 LOG.warn("problem closing response stream: {}",e);
136 this.doGetFile(uri, resp);
141 * load git.commit.id from jar /META-INF/git.properties
145 private String getGitHash(String def) {
146 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
147 if (content == null) {
150 String lines[] = content.split("\n");
151 for (String line : lines) {
152 if (line.startsWith("git.commit.id")) {
153 def = line.substring("git.commit.id=".length());
160 private String getResourceFileContent(String filename) {
161 LOG.debug("try ti get content of {}", filename);
162 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
166 * collect dynamic data for about.md
168 private void collectData() {
169 LOG.info("collecting dynamic data");
171 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
172 } catch (Exception e) {
173 LOG.warn("problem collecting system data: {}", e);
178 * get value for key out of /META-INF/MANIFEST.MF
182 private String getManifestValue(String key) {
183 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
189 manifest = new Manifest(url.openStream());
190 Attributes attr = manifest.getMainAttributes();
191 return attr.getValue(key);
192 } catch (IOException e) {
193 LOG.warn("problem reading manifest: {}", e);
199 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
202 private PomPropertiesFile getPomProperties() {
203 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
204 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
205 PomPropertiesFile propfile;
210 propfile = new PomPropertiesFile(url.openStream());
212 } catch (Exception e) {
213 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
218 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
222 private String getPomProperty(String key) {
223 LOG.info("try to get pom property for {}", key);
224 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
225 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
231 pomfile = new PomFile(url.openStream());
232 return pomfile.getProperty(key);
233 } catch (Exception e) {
234 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
239 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
242 private String getPomParentVersion() {
243 LOG.info("try to get pom parent version");
244 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
245 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
251 pomfile = new PomFile(url.openStream());
252 return pomfile.getParentVersion();
253 } catch (Exception e) {
254 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
260 * get file by uri from resources and write out to response stream
264 private void doGetFile(String uri, HttpServletResponse resp) {
265 String content = this.getResourceFileContent(uri);
266 if (content == null) {
268 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
269 } catch (IOException e) {
270 LOG.debug("unable to send error response : {}", e);
273 byte[] data = content.getBytes();
274 resp.setStatus(HttpServletResponse.SC_OK);
275 resp.setContentType(this.getContentType(uri));
277 resp.getOutputStream().write(data);
278 } catch (IOException e) {
279 LOG.debug("unable to send data : {}", e);
286 * create http response contentType by filename
290 private String getContentType(String filename) {
291 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
299 return "image/" + ext;
301 return "application/json";
313 * render this.readmeContent with this.data
316 private String render() {
317 return this.render(null);
321 * render content with this.data
325 private String render(String content) {
326 if (content == null) {
327 content = this.readmeContent;
329 if (content == null) {
332 for (Entry<String, String> entry : this.data.entrySet()) {
333 if (entry.getValue() != null && content.contains(entry.getKey())) {
334 content = content.replace(entry.getKey(), entry.getValue());
341 public void setClusterSize(String value) {
342 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);