summaryrefslogtreecommitdiffstats
path: root/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/RequestsClient.java
blob: 17eaa1b945e0cbab6a624008e80acfee5301bb06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.openecomp.sdc.webseal.simulator;

import org.apache.commons.io.IOUtils;
import org.openecomp.sdc.webseal.simulator.conf.Conf;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RequestsClient extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

		String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "jh0003";
		String createAll = request.getParameter("all");
		String url = Conf.getInstance().getFeHost() + "/sdc1/feProxy/rest/v1/user";

		PrintWriter writer = response.getWriter();
		
		int resultCode;
		
		if ("true".equals(createAll)) {
			Map<String, User> users = Conf.getInstance().getUsers();
			for (User user : users.values()) {
				resultCode = createUser(response, user.getUserId(), user.getRole().toUpperCase(), user.getFirstName(), user.getLastName(), user.getEmail(), url, adminId);
				writer.println("User "+ user.getFirstName() + " " + user.getLastName() + getResultMessage(resultCode) + "<br>");
			}
		} else {
			String userId = request.getParameter("userId");
			String role = request.getParameter("role").toUpperCase();
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
						
			resultCode = createUser(response, userId, role, firstName, lastName, email, url, adminId);
			
			writer.println("User "+ firstName + " " + lastName +getResultMessage(resultCode));	
		}

		

	}
	
	private String getResultMessage(int resultCode){
		return 201 == resultCode? " created successfuly":" not created ("+ resultCode +")";
	}

	private int createUser(final HttpServletResponse response, String userId, String role, String firstName, String lastName, String email, String url, String adminId) throws IOException {
		response.setContentType("text/html");

		String body = "{\"firstName\":\"" + firstName + "\", \"lastName\":\"" + lastName + "\", \"userId\":\"" + userId + "\", \"email\":\"" + email + "\",\"role\":\"" + role + "\"}";

		HashMap<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", "application/json");
		headers.put("USER_ID", adminId);
		return sendHttpPost(url, body, headers);
	}

	private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {

		String responseString = "";
		URL obj = new URL(url);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();

		// add request method
		con.setRequestMethod("POST");

		// add request headers
		if (headers != null) {
			for (Entry<String, String> header : headers.entrySet()) {
				String key = header.getKey();
				String value = header.getValue();
				con.setRequestProperty(key, value);
			}
		}

		// Send post request
		if (body != null) {
			con.setDoOutput(true);
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(body);
			wr.flush();
			wr.close();
		}

		int responseCode = con.getResponseCode();
		// logger.debug("Send POST http request, url: {}", url);
		// logger.debug("Response Code: {}", responseCode);

		StringBuffer response = new StringBuffer();
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		} catch (Exception e) {
			// logger.debug("response body is null");
		}

		String result;

		try {
			result = IOUtils.toString(con.getErrorStream());
			response.append(result);

		} catch (Exception e2) {
			result = null;
		}
		// logger.debug("Response body: {}", response);

		if (response != null) {
			responseString = response.toString();
		}

		// Map<String, List<String>> headerFields = con.getHeaderFields();
		// String responseMessage = con.getResponseMessage();

		con.disconnect();
		return responseCode;

	}

}