ae212b502509069b21e38bafb3c51743d10c6a0e
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.core;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.jar.Attributes;
27 import java.util.jar.Manifest;
28
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31
32 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
37 import org.springframework.web.bind.annotation.ResponseBody;
38 import org.springframework.web.bind.annotation.RestController;
39
40 /**
41  * This controller responds to a request for the web application manifest,
42  * returning a JSON with the information that was created at build time.
43  * 
44  * Manifest entries have names with hyphens, which means Javascript code can't
45  * simply use the shorthand object.key; instead use object['key'].
46  */
47 @RestController
48 @RequestMapping("/")
49 public class ManifestController extends RestrictedBaseController {
50
51         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ManifestController.class);
52
53         /**
54          * Required to obtain the webapp manifest.
55          */
56         @Autowired
57         private ServletContext context;
58
59         /** Path to resource on classpath */
60         private final String MANIFEST_RESOURCE_PATH = "/META-INF/MANIFEST.MF";
61
62         /**
63          * Gets the content of the webapp manifest file META-INF/MANIFEST.MF.
64          * 
65          * @return Attributes object with key-value pairs from the manifest
66          * @throws IOException
67          */
68         private Attributes getWebappManifest() throws IOException {
69                 // Manifest is formatted as Java-style properties
70                 InputStream inputStream = context.getResourceAsStream(MANIFEST_RESOURCE_PATH);
71                 if (inputStream == null)
72                         throw new IOException("getWebappManifest: failed to get resource at path " + MANIFEST_RESOURCE_PATH);
73                 Manifest manifest = new Manifest(inputStream);
74                 inputStream.close();
75                 return manifest.getMainAttributes();
76         }
77
78         /**
79          * Gets the webapp manifest contents as a JSON object.
80          * 
81          * @param request
82          * @return A map of key-value pairs. On success:
83          * 
84          *         <pre>
85          * { 
86          *       "key1": "value1", 
87          *   "key2": "value2" 
88          * }
89          *         </pre>
90          * 
91          *         On failure:
92          * 
93          *         <pre>
94          * { "error": "message" }
95          *         </pre>
96          */
97         @RequestMapping(value = { "/manifest" }, method = RequestMethod.GET, produces = "application/json")
98         @ResponseBody
99         public Map<Object, Object> getManifest(HttpServletRequest request) {
100                 try {
101                         Attributes attributes = getWebappManifest();
102                         return attributes;
103                 } catch (Exception ex) {
104                         logger.error(EELFLoggerDelegate.errorLogger, "getManifest failed", ex);
105                         Map<Object, Object> response = new HashMap<Object, Object>();
106                         response.put("error", "failed to get manifest: " + ex.toString());
107                         return response;
108                 }
109         }
110
111 }