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();
125 catch(IOException e) {
126 LOG.warn("problem writing response for {}: {}",uri,e);
133 catch(IOException e) {
134 LOG.warn("problem closing response stream: {}",e);
140 this.doGetFile(uri, resp);
145 * load git.commit.id from jar /META-INF/git.properties
149 private String getGitHash(String def) {
150 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
151 if (content == null) {
154 String lines[] = content.split("\n");
155 for (String line : lines) {
156 if (line.startsWith("git.commit.id")) {
157 def = line.substring("git.commit.id=".length());
164 private String getResourceFileContent(String filename) {
165 LOG.debug("try ti get content of {}", filename);
166 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
170 * collect dynamic data for about.md
172 private void collectData() {
173 LOG.info("collecting dynamic data");
175 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
176 } catch (Exception e) {
177 LOG.warn("problem collecting system data: {}", e);
182 * get value for key out of /META-INF/MANIFEST.MF
186 private String getManifestValue(String key) {
187 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
193 manifest = new Manifest(url.openStream());
194 Attributes attr = manifest.getMainAttributes();
195 return attr.getValue(key);
196 } catch (IOException e) {
197 LOG.warn("problem reading manifest: {}", e);
203 * 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);
222 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
226 private String getPomProperty(String key) {
227 LOG.info("try to get pom property for {}", key);
228 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
229 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
235 pomfile = new PomFile(url.openStream());
236 return pomfile.getProperty(key);
237 } catch (Exception e) {
238 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
243 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
246 private String getPomParentVersion() {
247 LOG.info("try to get pom parent version");
248 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
249 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
255 pomfile = new PomFile(url.openStream());
256 return pomfile.getParentVersion();
257 } catch (Exception e) {
258 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
264 * get file by uri from resources and write out to response stream
268 private void doGetFile(String uri, HttpServletResponse resp) {
269 String content = this.getResourceFileContent(uri);
270 if (content == null) {
272 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
273 } catch (IOException e) {
274 LOG.debug("unable to send error response : {}", e);
277 byte[] data = content.getBytes();
278 resp.setStatus(HttpServletResponse.SC_OK);
279 resp.setContentType(this.getContentType(uri));
281 resp.getOutputStream().write(data);
282 } catch (IOException e) {
283 LOG.debug("unable to send data : {}", e);
290 * create http response contentType by filename
294 private String getContentType(String filename) {
295 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
303 return "image/" + ext;
305 return "application/json";
317 * render this.readmeContent with this.data
320 private String render() {
321 return this.render(null);
325 * render content with this.data
329 private String render(String content) {
330 if (content == null) {
331 content = this.readmeContent;
333 if (content == null) {
336 for (Entry<String, String> entry : this.data.entrySet()) {
337 if (entry.getValue() != null && content.contains(entry.getKey())) {
338 content = content.replace(entry.getKey(), entry.getValue());
345 public void setClusterSize(String value) {
346 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);