4cad5b397ae39b13e813a277a85a5ee477c927f5
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.rnotebookintegration.controller;
39
40 import java.util.HashMap;
41 import java.util.Iterator;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.json.JSONObject;
47 import org.onap.portalsdk.core.controller.RestrictedBaseController;
48 import org.onap.portalsdk.core.domain.User;
49 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 import org.onap.portalsdk.core.restful.domain.EcompUser;
51 import org.onap.portalsdk.core.util.SystemProperties;
52 import org.onap.portalsdk.core.web.support.UserUtils;
53 import org.onap.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
54 import org.onap.portalsdk.rnotebookintegration.service.RNoteBookIntegrationService;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.ResponseEntity;
58 import org.springframework.stereotype.Controller;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.bind.annotation.ResponseBody;
63
64 @Controller
65 @RequestMapping("/rNotebookFE/")
66 public class RNoteBookFEController extends RestrictedBaseController {
67
68         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RNoteBookController.class);
69
70         @Autowired
71         private RNoteBookIntegrationService rNoteBookIntegrationService;
72
73         public RNoteBookIntegrationService getrNoteBookIntegrationService() {
74                 return rNoteBookIntegrationService;
75         }
76
77         public void setrNoteBookIntegrationService(RNoteBookIntegrationService rNoteBookIntegrationService) {
78                 this.rNoteBookIntegrationService = rNoteBookIntegrationService;
79         }
80
81         @RequestMapping(value = { "authCr" }, method = RequestMethod.POST, produces = "application/json")
82         @ResponseBody
83         public ResponseEntity<String> saveRNotebookCredentials(@RequestBody String notebookId, HttpServletRequest request,
84                         HttpServletResponse response) {
85                 logger.debug(EELFLoggerDelegate.debugLogger, "saveRNotebookCredentials: Notebook id {}", notebookId);
86                 logger.debug(EELFLoggerDelegate.debugLogger, "saveRNotebookCredentials: Query parameters {}", request.getParameter("qparams"));
87                 String retUrl = "";
88                 try {
89                         User user = UserUtils.getUserSession(request);
90                         user = (User) getDataAccessService().getDomainObject(User.class, user.getId(), null);
91                         EcompUser ecUser = UserUtils.convertToEcompUser(user);
92                         HashMap<String, String> map = new HashMap<>();
93                         JSONObject jObject = new JSONObject(request.getParameter("qparams"));
94                         Iterator<?> keys = jObject.keys();
95                         while (keys.hasNext()) {
96                                 String key = (String) keys.next();
97                                 String value = jObject.getString(key);
98                                 map.put(key, value);
99                         }
100                         logger.debug(EELFLoggerDelegate.debugLogger, "saveRNotebookCredentials: json {}", jObject);
101                         logger.debug(EELFLoggerDelegate.debugLogger, "saveRNotebookCredentials: map {}", map);
102                         String token = this.getrNoteBookIntegrationService().saveRNotebookCredentials(notebookId, ecUser, map);
103                         final String guardNotebookUrl = "guard_notebook_url";
104                         if (!SystemProperties.containsProperty(guardNotebookUrl))
105                                 throw new IllegalArgumentException("Failed to find property " + guardNotebookUrl);
106                         String guard = SystemProperties.getProperty(guardNotebookUrl);
107                         retUrl = guard + "id=" + token;
108                 } catch (RNotebookIntegrationException re) {
109                         logger.error(EELFLoggerDelegate.errorLogger, "saveRNotebookCredentials failed 1", re);
110                         return new ResponseEntity<>(re.getMessage(), HttpStatus.BAD_REQUEST);
111                 } catch (Exception e) {
112                         logger.error(EELFLoggerDelegate.errorLogger, "saveRNotebookCredentials failed 2", e);
113                         return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
114                 }
115                 return new ResponseEntity<>(retUrl, HttpStatus.OK);
116         }
117
118 }