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 java.io.BufferedReader;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.PrintWriter;
28 import java.net.HttpURLConnection;
30 import java.util.HashMap;
32 import java.util.Map.Entry;
33 import javax.servlet.http.HttpServlet;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import org.apache.commons.io.IOUtils;
37 import org.openecomp.sdc.webseal.simulator.conf.Conf;
39 public class RequestsClient extends HttpServlet {
41 private static final long serialVersionUID = 1L;
44 protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
46 String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "jh0003";
47 String createAll = request.getParameter("all");
48 String url = Conf.getInstance().getFeHost() + "/sdc1/feProxy/rest/v1/user";
50 PrintWriter writer = response.getWriter();
54 if ("true".equals(createAll)) {
55 Map<String, User> users = Conf.getInstance().getUsers();
56 for (User user : users.values()) {
57 resultCode = createUser(response, user.getUserId(), user.getRole().toUpperCase(), user.getFirstName(), user.getLastName(),
58 user.getEmail(), url, adminId);
59 writer.println("User " + user.getFirstName() + " " + user.getLastName() + getResultMessage(resultCode) + "<br>");
62 String userId = request.getParameter("userId");
63 String role = request.getParameter("role").toUpperCase();
64 String firstName = request.getParameter("firstName");
65 String lastName = request.getParameter("lastName");
66 String email = request.getParameter("email");
68 resultCode = createUser(response, userId, role, firstName, lastName, email, url, adminId);
70 writer.println("User " + firstName + " " + lastName + getResultMessage(resultCode));
75 private String getResultMessage(int resultCode) {
76 return 201 == resultCode ? " created successfuly" : " not created (" + resultCode + ")";
79 private int createUser(final HttpServletResponse response, String userId, String role, String firstName, String lastName, String email,
80 String url, String adminId) throws IOException {
81 response.setContentType("text/html");
83 String body = "{\"firstName\":\"" + firstName + "\", \"lastName\":\"" + lastName + "\", \"userId\":\"" + userId + "\", \"email\":\"" + email
84 + "\",\"role\":\"" + role + "\"}";
86 HashMap<String, String> headers = new HashMap<String, String>();
87 headers.put("Content-Type", "application/json");
88 headers.put("USER_ID", adminId);
89 return sendHttpPost(url, body, headers);
92 private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
94 String responseString = "";
95 URL obj = new URL(url);
96 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
99 con.setRequestMethod("POST");
101 // add request headers
102 if (headers != null) {
103 for (Entry<String, String> header : headers.entrySet()) {
104 String key = header.getKey();
105 String value = header.getValue();
106 con.setRequestProperty(key, value);
112 con.setDoOutput(true);
113 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
119 int responseCode = con.getResponseCode();
120 // logger.debug("Send POST http request, url: {}", url);
121 // logger.debug("Response Code: {}", responseCode);
123 StringBuilder response = new StringBuilder();
125 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
127 while ((inputLine = in.readLine()) != null) {
128 response.append(inputLine);
131 } catch (Exception e) {
132 // logger.debug("response body is null");
138 result = IOUtils.toString(con.getErrorStream());
139 response.append(result);
140 } catch (Exception e2) {