2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
24 import java.io.IOException;
26 import java.util.HashMap;
28 import java.util.Map.Entry;
29 import java.util.jar.Attributes;
30 import java.util.jar.Manifest;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.onap.ccsdk.features.sdnr.wt.common.Resources;
39 import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
40 import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ODLVersionLUT;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.SystemInfo;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class AboutHttpServlet extends HttpServlet {
51 private static final long serialVersionUID = 1L;
52 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
53 private static final String UNKNOWN = "unknown";
54 private static final String METAINF_MAVEN = "/META-INF/maven/";
55 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
57 private static final String URI_PRE = "/about";
58 private static final String RES_BASEPATH = "about/";
60 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
61 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
62 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
63 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
64 private static final String PLACEHOLDER_ODLUX_REVISION = "{odlux-revision}";
65 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
66 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
67 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
68 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
69 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
70 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
71 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
72 private static final String README_FILE = "README.md";
74 private final String groupId = "org.onap.ccsdk.features.sdnr.wt";
75 private final String artifactId = "sdnr-wt-data-provider-provider";
77 private final Map<String, String> data;
78 private final String readmeContent;
80 public AboutHttpServlet() {
82 this.data = new HashMap<>();
83 this.collectStaticData();
84 this.readmeContent = this.render(this.getResourceFileContent(README_FILE));
88 * collect static versioning data
90 private void collectStaticData() {
91 PomPropertiesFile props = this.getPomProperties();
92 final String ccsdkVersion = this.getPomParentVersion();
93 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
94 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(ccsdkVersion, UNKNOWN));
95 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
96 this.data.put(PLACEHOLDER_ODLUX_REVISION, this.getPomProperty("odlux.buildno"));
97 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
98 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
99 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, "1.8.1-SNAPSHOT");
100 this.data.put(PLACEHOLDER_MDSAL_VERSION, SystemInfo.getMdSalVersion(UNKNOWN));
101 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
102 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
106 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
108 String uri = req.getRequestURI().substring(URI_PRE.length());
109 LOG.debug("request for {}", uri);
110 if (uri.length() <= 0 || uri.equals("/")) {
114 String content = this.render();
115 byte[] output = content != null ? content.getBytes() : new byte[0];
117 resp.setStatus(HttpServletResponse.SC_OK);
118 resp.setContentLength(output.length);
119 resp.setContentType("text/plain");
120 ServletOutputStream os = null;
122 os = resp.getOutputStream();
124 } catch (IOException e) {
125 LOG.warn("problem writing response for {}: {}", uri, e);
130 } catch (IOException e) {
131 LOG.warn("problem closing response stream: {}", e);
137 this.doGetFile(uri, resp);
142 * load git.commit.id from jar /META-INF/git.properties
146 private String getGitHash(String def) {
147 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
148 if (content == null) {
151 String lines[] = content.split("\n");
152 for (String line : lines) {
153 if (line.startsWith("git.commit.id")) {
154 def = line.substring("git.commit.id=".length());
161 private String getResourceFileContent(String filename) {
162 LOG.debug("try ti get content of {}", filename);
163 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
167 * collect dynamic data for about.md
169 private void collectData() {
170 LOG.info("collecting dynamic data");
172 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
173 } catch (Exception e) {
174 LOG.warn("problem collecting system data: {}", e);
179 * get value for key out of /META-INF/MANIFEST.MF
184 private String getManifestValue(String key) {
185 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
191 manifest = new Manifest(url.openStream());
192 Attributes attr = manifest.getMainAttributes();
193 return attr.getValue(key);
194 } catch (IOException e) {
195 LOG.warn("problem reading manifest: {}", e);
202 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
206 private PomPropertiesFile getPomProperties() {
207 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
208 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
209 PomPropertiesFile propfile;
214 propfile = new PomPropertiesFile(url.openStream());
216 } catch (Exception e) {
217 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
223 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
228 private String getPomProperty(String key) {
229 LOG.info("try to get pom property for {}", key);
230 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
231 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
237 pomfile = new PomFile(url.openStream());
238 return pomfile.getProperty(key);
239 } catch (Exception e) {
240 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
246 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
250 private String getPomParentVersion() {
251 LOG.info("try to get pom parent version");
252 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
253 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
259 pomfile = new PomFile(url.openStream());
260 return pomfile.getParentVersion();
261 } catch (Exception e) {
262 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
268 * get file by uri from resources and write out to response stream
273 private void doGetFile(String uri, HttpServletResponse resp) {
274 String content = this.getResourceFileContent(uri);
275 if (content == null) {
277 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
278 } catch (IOException e) {
279 LOG.debug("unable to send error response : {}", e);
282 byte[] data = content.getBytes();
283 resp.setStatus(HttpServletResponse.SC_OK);
284 resp.setContentType(this.getContentType(uri));
286 resp.getOutputStream().write(data);
287 } catch (IOException e) {
288 LOG.debug("unable to send data : {}", e);
295 * create http response contentType by filename
300 private String getContentType(String filename) {
301 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
309 return "image/" + ext;
311 return "application/json";
323 * render this.readmeContent with this.data
327 private String render() {
328 return this.render(null);
332 * render content with this.data
337 private String render(String content) {
338 if (content == null) {
339 content = this.readmeContent;
341 if (content == null) {
344 for (Entry<String, String> entry : this.data.entrySet()) {
345 if (entry.getValue() != null && content.contains(entry.getKey())) {
346 content = content.replace(entry.getKey(), entry.getValue());
353 public void setClusterSize(String value) {
354 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);