Remove dead code
[sdc.git] / utils / webseal-simulator / src / main / java / org / openecomp / sdc / webseal / simulator / RequestsClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.webseal.simulator;
22
23 import org.apache.commons.io.IOUtils;
24 import org.openecomp.sdc.webseal.simulator.conf.Conf;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import java.io.*;
31 import java.net.HttpURLConnection;
32 import java.net.URL;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 public class RequestsClient extends HttpServlet {
38
39         private static final long serialVersionUID = 1L;
40
41         @Override
42         protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
43
44                 String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "jh0003";
45                 String createAll = request.getParameter("all");
46                 String url = Conf.getInstance().getFeHost() + "/sdc1/feProxy/rest/v1/user";
47
48                 PrintWriter writer = response.getWriter();
49                 
50                 int resultCode;
51                 
52                 if ("true".equals(createAll)) {
53                         Map<String, User> users = Conf.getInstance().getUsers();
54                         for (User user : users.values()) {
55                                 resultCode = createUser(response, user.getUserId(), user.getRole().toUpperCase(), user.getFirstName(), user.getLastName(), user.getEmail(), url, adminId);
56                                 writer.println("User "+ user.getFirstName() + " " + user.getLastName() + getResultMessage(resultCode) + "<br>");
57                         }
58                 } else {
59                         String userId = request.getParameter("userId");
60                         String role = request.getParameter("role").toUpperCase();
61                         String firstName = request.getParameter("firstName");
62                         String lastName = request.getParameter("lastName");
63                         String email = request.getParameter("email");
64                                                 
65                         resultCode = createUser(response, userId, role, firstName, lastName, email, url, adminId);
66                         
67                         writer.println("User "+ firstName + " " + lastName +getResultMessage(resultCode));      
68                 }
69
70                 
71
72         }
73         
74         private String getResultMessage(int resultCode){
75                 return 201 == resultCode? " created successfuly":" not created ("+ resultCode +")";
76         }
77
78         private int createUser(final HttpServletResponse response, String userId, String role, String firstName, String lastName, String email, String url, String adminId) throws IOException {
79                 response.setContentType("text/html");
80
81                 String body = "{\"firstName\":\"" + firstName + "\", \"lastName\":\"" + lastName + "\", \"userId\":\"" + userId + "\", \"email\":\"" + email + "\",\"role\":\"" + role + "\"}";
82
83                 HashMap<String, String> headers = new HashMap<String, String>();
84                 headers.put("Content-Type", "application/json");
85                 headers.put("USER_ID", adminId);
86                 return sendHttpPost(url, body, headers);
87         }
88
89         private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
90
91                 String responseString = "";
92                 URL obj = new URL(url);
93                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
94
95                 // add request method
96                 con.setRequestMethod("POST");
97
98                 // add request headers
99                 if (headers != null) {
100                         for (Entry<String, String> header : headers.entrySet()) {
101                                 String key = header.getKey();
102                                 String value = header.getValue();
103                                 con.setRequestProperty(key, value);
104                         }
105                 }
106
107                 // Send post request
108                 if (body != null) {
109                         con.setDoOutput(true);
110                         DataOutputStream wr = new DataOutputStream(con.getOutputStream());
111                         wr.writeBytes(body);
112                         wr.flush();
113                         wr.close();
114                 }
115
116                 int responseCode = con.getResponseCode();
117                 // logger.debug("Send POST http request, url: {}", url);
118                 // logger.debug("Response Code: {}", responseCode);
119
120                 StringBuilder response = new StringBuilder();
121                 try {
122                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
123                         String inputLine;
124                         while ((inputLine = in.readLine()) != null) {
125                                 response.append(inputLine);
126                         }
127                         in.close();
128                 } catch (Exception e) {
129                         // logger.debug("response body is null");
130                 }
131
132                 String result;
133
134                 try {
135                         result = IOUtils.toString(con.getErrorStream());
136                         response.append(result);
137                 } catch (Exception e2) {
138                 }
139
140                 con.disconnect();
141                 return responseCode;
142
143         }
144
145 }