2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.webseal.simulator;
23 import org.apache.commons.io.IOUtils;
24 import org.openecomp.sdc.webseal.simulator.conf.Conf;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import java.net.HttpURLConnection;
33 import java.util.HashMap;
35 import java.util.Map.Entry;
37 public class RequestsClient extends HttpServlet {
39 private static final long serialVersionUID = 1L;
42 protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
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";
48 PrintWriter writer = response.getWriter();
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>");
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");
65 resultCode = createUser(response, userId, role, firstName, lastName, email, url, adminId);
67 writer.println("User "+ firstName + " " + lastName +getResultMessage(resultCode));
74 private String getResultMessage(int resultCode){
75 return 201 == resultCode? " created successfuly":" not created ("+ resultCode +")";
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");
81 String body = "{\"firstName\":\"" + firstName + "\", \"lastName\":\"" + lastName + "\", \"userId\":\"" + userId + "\", \"email\":\"" + email + "\",\"role\":\"" + role + "\"}";
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);
89 private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
91 String responseString = "";
92 URL obj = new URL(url);
93 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
96 con.setRequestMethod("POST");
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);
109 con.setDoOutput(true);
110 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
116 int responseCode = con.getResponseCode();
117 // logger.debug("Send POST http request, url: {}", url);
118 // logger.debug("Response Code: {}", responseCode);
120 StringBuffer response = new StringBuffer();
122 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
124 while ((inputLine = in.readLine()) != null) {
125 response.append(inputLine);
128 } catch (Exception e) {
129 // logger.debug("response body is null");
135 result = IOUtils.toString(con.getErrorStream());
136 response.append(result);
138 } catch (Exception e2) {
141 // logger.debug("Response body: {}", response);
143 if (response != null) {
144 responseString = response.toString();
147 // Map<String, List<String>> headerFields = con.getHeaderFields();
148 // String responseMessage = con.getResponseMessage();