aboutsummaryrefslogtreecommitdiffstats
path: root/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/RequestsClient.java
blob: 5f4cfa93d218b794ea426b1d27634a2d0e592e9e (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
package org.openecomp.sdc.webseal.simulator;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

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 userId = request.getParameter("userId");
		String role = request.getParameter("role");
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String email = request.getParameter("email");
		
		String hostname = request.getParameter("hostname") != null?request.getParameter("hostname"):"127.0.0.1";
		String port = request.getParameter("port") != null?request.getParameter("port"):"8080";
		String adminId = request.getParameter("adminId") != null?request.getParameter("adminId"):"jh0003";
		
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		writer.println("userId: " + userId);
		writer.println("role: " + role);

		// Fill the data of the request
		String url = "http://" + hostname + ":" + port + "/sdc2/rest/v1/user";
		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);
		sendHttpPost(url, body, headers);

	}
	
	private String 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 responseString;

	}
	
}