Catalog alignment
[sdc.git] / utils / webseal-simulator / src / main / java / org / openecomp / sdc / webseal / simulator / Login.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 org.openecomp.sdc.webseal.simulator.conf.Conf;
24
25 import javax.servlet.ServletConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServlet;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.util.Collection;
34 import java.util.Iterator;
35
36 public class Login extends HttpServlet {
37
38         private static final long serialVersionUID = 1L;
39
40         @Override
41         public void init(final ServletConfig config) throws ServletException {
42                 super.init(config);
43         }
44
45         @Override
46         protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
47                         throws ServletException, IOException {
48
49                 if (null != request.getParameter("userId")) {
50                         doPost(request, response);
51                         return;
52                 }
53                 System.out.println("about to build login page");
54                 response.setContentType("text/html");
55                 PrintWriter writer = response.getWriter();
56                 String message = (String) request.getAttribute("message");
57                 if (message == null) {
58                         message = "";
59                 }
60
61                 Collection<User> allUsers = Conf.getInstance().getUsers().values();
62                 writer.println("<html>");
63                 
64                 writer.println("<head>");
65                 writer.println("<style>");
66                 writer.println("body {padding: 40px; font-family: Arial; font-size: 14px;}");
67                 writer.println("h1 {background-color: #DDDDDD; padding: 4px 10px;}");
68                 writer.println("h2 {margin-top: 20px;}");
69                 writer.println(".label {width: 100px; float:left;}");
70                 writer.println(".break {display: block; margin-bottom: 10px;}");
71                 writer.println("tr {padding: 4px 10px;}");
72                 writer.println("th {padding: 4px 10px; text-align: left; background-color: #dddddd;}");
73                 writer.println("td {padding: 4px 10px; text-align: left;}");
74                 writer.println("</style>");     
75                 writer.println("</head>");
76                 
77                 writer.println("<body>");
78                 
79                 writer.println("<h1>Webseal simulator</h1>");
80                 writer.println("<h2>Login:</h2>");
81                 
82                 writer.println("<form action=\"\" method=\"post\">");
83                 writer.println("  <div class='label'>User id:</div>");
84                 writer.println("  <input type='text' name='userId'>");
85                 writer.println("  <div class='break'></div>");
86                 
87                 writer.println("  <div class='label'>Password:</div>");
88                 writer.println("  <input type='password' name='password'>");
89                 writer.println("  <div class='break'></div>");
90                 
91                 writer.println("  <input type='submit' value='Login'>");
92                 writer.println("  <label name='message'></label>");
93                 writer.println("</form>");
94                 
95                 writer.println("<hr/>");
96                 writer.println("<h2>Quick links:</h2>");
97                 writer.println("<table>");
98                 writer.println("<tr>");
99                 writer.println("<th>full name</th>");
100                 writer.println("<th>user id</th>");
101                 writer.println("<th>role</th>");
102                 writer.println("<th>action</th>");
103                 writer.println("</tr>");
104                 Iterator<User> iterator = allUsers.iterator();
105                 while (iterator.hasNext()) {
106                         User user = iterator.next();
107                         writer.println("<tr>");
108                         writer.println("<td>" + user.getUserRef() + "</td>");
109                         writer.println("<td>" + user.getUserId() + "</td>");
110                         writer.println("<td>" + user.getRole() + "</td>");
111                         writer.println("<td>" + user.getUserCreateRef() + "</td>");
112                         writer.println("</tr>");
113                 }
114                 writer.println("</table>");     
115
116                 writer.println("<a href='create?all=true' target='resultFrame'>Create All</a>");
117                 writer.println("<hr/><iframe name='resultFrame' width='400' height='300'></iframe>");   
118                 
119                 writer.println("</body>");
120                 writer.println("</html>");
121                 
122         }
123
124         public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
125
126                 String userId = request.getParameter("userId");
127                 String password = request.getParameter("password");
128                 request.setAttribute("message", "OK");
129
130                 System.out.println("Login -> doPost userId=" + userId);
131                 User user = getUser(userId, password);
132                 if (user == null) {
133                         response.sendError(500, "ERROR: userId or password incorrect");
134 //                      doGet(request, response);
135                 } else {
136                         System.out.println("Login -> doPost redirect to /sdc1 (to proxy)");
137                         Cookie cookieUser = new Cookie("HTTP_IV_USER", user.getUserId());
138                         Cookie cookieUserId = new Cookie("USER_ID", user.getUserId());
139                         Cookie cookieFirstName = new Cookie("HTTP_CSP_FIRSTNAME", user.getFirstName());
140                         Cookie cookieEmail = new Cookie("HTTP_CSP_EMAIL", user.getEmail());
141                         Cookie cookieLastName = new Cookie("HTTP_CSP_LASTNAME", user.getLastName());
142                         Cookie cookieRemoteAddress = new Cookie("HTTP_IV_REMOTE_ADDRESS", "0.0.0.0");
143                         Cookie cookieWsType = new Cookie("HTTP_CSP_WSTYPE", "Intranet");
144                         response.addCookie(cookieUser);
145                         response.addCookie(cookieUserId);
146                         response.addCookie(cookieFirstName);
147                         response.addCookie(cookieEmail);
148                         response.addCookie(cookieLastName);
149                         response.addCookie(cookieRemoteAddress);
150                         response.addCookie(cookieWsType);
151                         response.addCookie(new Cookie(Conf.getInstance().getPortalCookieName(), "portal"));
152                         response.sendRedirect("/sdc1");
153                 }
154
155         }
156
157         private User getUser(String userId, String password) {
158                 User user = Conf.getInstance().getUsers().get(userId);
159                 if (user == null) {
160                         return null;
161                 }
162                 if (!password.equals(user.getPassword())) {
163                         return null;
164                 }
165                 return user;
166         }
167
168         @Override
169         public String getServletInfo() {
170                 return "Http Proxy Servlet";
171         }
172 }