0268284cdc29c2a08852e73c8e02bfaedb0963f3
[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.restful.domain.EcompUser;
50 import org.onap.portalsdk.core.util.SystemProperties;
51 import org.onap.portalsdk.core.web.support.UserUtils;
52 import org.onap.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
53 import org.onap.portalsdk.rnotebookintegration.service.RNoteBookIntegrationService;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.bind.annotation.ResponseBody;
62
63 @Controller
64 @RequestMapping("/rNotebookFE/")
65 public class RNoteBookFEController extends RestrictedBaseController {
66         @Autowired
67         private RNoteBookIntegrationService rNoteBookIntegrationService;
68         
69         
70
71         public RNoteBookIntegrationService getrNoteBookIntegrationService() {
72                 return rNoteBookIntegrationService;
73         }
74
75
76
77         public void setrNoteBookIntegrationService(
78                         RNoteBookIntegrationService rNoteBookIntegrationService) {
79                 this.rNoteBookIntegrationService = rNoteBookIntegrationService;
80         }
81         
82         @RequestMapping(value = { "authCr" }, method = RequestMethod.POST, produces = "application/json")
83         public @ResponseBody ResponseEntity<String> saveRNotebookCredentials (@RequestBody String notebookId, HttpServletRequest request,
84                         HttpServletResponse response) throws Exception {
85                 //ObjectMapper mapper = new ObjectMapper();
86                 //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87                 //JsonNode root = mapper.readTree(request.getReader());
88                 //String token = root.get("authenticationToken").textValue();
89                 System.out.println("Notebook id "+notebookId);
90                 System.out.println("Query parameters "+request.getParameter("qparams"));
91                 String retUrl = "";
92                 try{
93                         
94                         User user = UserUtils.getUserSession(request);
95                         user = (User) this.getDataAccessService().getDomainObject(User.class, user.getId(), null);
96                         
97                         EcompUser ecUser =UserUtils.convertToEcompUser(user);
98                         
99                         HashMap<String, String> map = new HashMap<String, String>();
100                 JSONObject jObject = new JSONObject(request.getParameter("qparams"));
101                 Iterator<?> keys = jObject.keys();
102
103                 while( keys.hasNext() ){
104                     String key = (String)keys.next();
105                     String value = jObject.getString(key); 
106                     map.put(key, value);
107
108                 }
109
110                 System.out.println("json : "+jObject);
111                 System.out.println("map : "+map);
112                         
113                 //      String token = this.getrNoteBookIntegrationService().saveRNotebookCredentials(notebookId, ecUser, new HashMap<String, String>());
114                 String token = this.getrNoteBookIntegrationService().saveRNotebookCredentials(notebookId, ecUser, map);
115                         
116                         String guard = SystemProperties.getProperty("guard_notebook_url");
117                         
118                         retUrl = guard + "id=" + token;
119                         
120                 
121                 } catch (RNotebookIntegrationException re){
122                         return new ResponseEntity<String>(re.getMessage(), HttpStatus.BAD_REQUEST);
123                 } catch (Exception e){
124                         return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
125                 }
126                 
127                 return new ResponseEntity<String>(retUrl, HttpStatus.OK);
128                 
129         }
130
131 }