3b0fcc494f55d24337187fb5a4f83310e1361cbe
[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.portalsdk.rnotebookintegration.controller;
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.json.JSONObject;
29 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
30 import org.openecomp.portalsdk.core.domain.User;
31 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
32 import org.openecomp.portalsdk.core.util.SystemProperties;
33 import org.openecomp.portalsdk.core.web.support.UserUtils;
34 import org.openecomp.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
35 import org.openecomp.portalsdk.rnotebookintegration.service.RNoteBookIntegrationService;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RequestMethod;
43 import org.springframework.web.bind.annotation.ResponseBody;
44
45 @Controller
46 @RequestMapping("/rNotebookFE/")
47 public class RNoteBookFEController extends RestrictedBaseController {
48         @Autowired
49         private RNoteBookIntegrationService rNoteBookIntegrationService;
50         
51         
52
53         public RNoteBookIntegrationService getrNoteBookIntegrationService() {
54                 return rNoteBookIntegrationService;
55         }
56
57
58
59         public void setrNoteBookIntegrationService(
60                         RNoteBookIntegrationService rNoteBookIntegrationService) {
61                 this.rNoteBookIntegrationService = rNoteBookIntegrationService;
62         }
63         
64         @RequestMapping(value = { "authCr" }, method = RequestMethod.POST, produces = "application/json")
65         public @ResponseBody ResponseEntity<String> saveRNotebookCredentials (@RequestBody String notebookId, HttpServletRequest request,
66                         HttpServletResponse response) throws Exception {
67                 //ObjectMapper mapper = new ObjectMapper();
68                 //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
69                 //JsonNode root = mapper.readTree(request.getReader());
70                 //String token = root.get("authenticationToken").textValue();
71                 System.out.println("Notebook id "+notebookId);
72                 System.out.println("Query parameters "+request.getParameter("qparams"));
73                 String retUrl = "";
74                 try{
75                         
76                         User user = UserUtils.getUserSession(request);
77                         user = (User) this.getDataAccessService().getDomainObject(User.class, user.getId(), null);
78                         
79                         EcompUser ecUser =UserUtils.convertToEcompUser(user);
80                         
81                         HashMap<String, String> map = new HashMap<String, String>();
82                 JSONObject jObject = new JSONObject(request.getParameter("qparams"));
83                 Iterator<?> keys = jObject.keys();
84
85                 while( keys.hasNext() ){
86                     String key = (String)keys.next();
87                     String value = jObject.getString(key); 
88                     map.put(key, value);
89
90                 }
91
92                 System.out.println("json : "+jObject);
93                 System.out.println("map : "+map);
94                         
95                 //      String token = this.getrNoteBookIntegrationService().saveRNotebookCredentials(notebookId, ecUser, new HashMap<String, String>());
96                 String token = this.getrNoteBookIntegrationService().saveRNotebookCredentials(notebookId, ecUser, map);
97                         
98                         String guard = SystemProperties.getProperty("guard_notebook_url");
99                         
100                         retUrl = guard + "id=" + token;
101                         
102                 
103                 } catch (RNotebookIntegrationException re){
104                         return new ResponseEntity<String>(re.getMessage(), HttpStatus.BAD_REQUEST);
105                 } catch (Exception e){
106                         return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
107                 }
108                 
109                 return new ResponseEntity<String>(retUrl, HttpStatus.OK);
110                 
111         }
112
113 }