Fix security risk 'Improper Input Validation'
[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 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;
29 import java.net.URL;
30 import java.util.HashMap;
31 import java.util.Map;
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;
38
39 public class RequestsClient extends HttpServlet {
40
41     private static final long serialVersionUID = 1L;
42
43     @Override
44     protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
45
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";
49
50         PrintWriter writer = response.getWriter();
51
52         int resultCode;
53
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>");
60             }
61         } else {
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");
67
68             resultCode = createUser(response, userId, role, firstName, lastName, email, url, adminId);
69
70             writer.println("User " + firstName + " " + lastName + getResultMessage(resultCode));
71         }
72
73     }
74
75     private String getResultMessage(int resultCode) {
76         return 201 == resultCode ? " created successfuly" : " not created (" + resultCode + ")";
77     }
78
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");
82
83         String body = "{\"firstName\":\"" + firstName + "\", \"lastName\":\"" + lastName + "\", \"userId\":\"" + userId + "\", \"email\":\"" + email
84             + "\",\"role\":\"" + role + "\"}";
85
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);
90     }
91
92     private int sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
93
94         String responseString = "";
95         URL obj = new URL(url);
96         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
97
98         // add request method
99         con.setRequestMethod("POST");
100
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);
107             }
108         }
109
110         // Send post request
111         if (body != null) {
112             con.setDoOutput(true);
113             DataOutputStream wr = new DataOutputStream(con.getOutputStream());
114             wr.writeBytes(body);
115             wr.flush();
116             wr.close();
117         }
118
119         int responseCode = con.getResponseCode();
120         // logger.debug("Send POST http request, url: {}", url);
121         // logger.debug("Response Code: {}", responseCode);
122
123         StringBuilder response = new StringBuilder();
124         try {
125             BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
126             String inputLine;
127             while ((inputLine = in.readLine()) != null) {
128                 response.append(inputLine);
129             }
130             in.close();
131         } catch (Exception e) {
132             // logger.debug("response body is null");
133         }
134
135         String result;
136
137         try {
138             result = IOUtils.toString(con.getErrorStream());
139             response.append(result);
140         } catch (Exception e2) {
141         }
142
143         con.disconnect();
144         return responseCode;
145
146     }
147
148 }