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 org.openecomp.sdc.webseal.simulator.conf.Conf;
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;
36 public class Login extends HttpServlet {
38 private static final long serialVersionUID = 1L;
41 public void init(final ServletConfig config) throws ServletException {
46 protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
47 throws ServletException, IOException {
49 if (null != request.getParameter("userId")) {
50 doPost(request, response);
53 System.out.println("about to build login page");
54 response.setContentType("text/html");
55 PrintWriter writer = response.getWriter();
57 Collection<User> allUsers = Conf.getInstance().getUsers().values();
58 writer.println("<html>");
60 writer.println("<head>");
61 writer.println("<style>");
62 writer.println("body {padding: 40px; font-family: Arial; font-size: 14px;}");
63 writer.println("h1 {background-color: #DDDDDD; padding: 4px 10px;}");
64 writer.println("h2 {margin-top: 20px;}");
65 writer.println(".label {width: 100px; float:left;}");
66 writer.println(".break {display: block; margin-bottom: 10px;}");
67 writer.println("tr {padding: 4px 10px;}");
68 writer.println("th {padding: 4px 10px; text-align: left; background-color: #dddddd;}");
69 writer.println("td {padding: 4px 10px; text-align: left;}");
70 writer.println("</style>");
71 writer.println("</head>");
73 writer.println("<body>");
75 writer.println("<h1>Webseal simulator</h1>");
76 writer.println("<h2>Login:</h2>");
78 writer.println("<form action=\"\" method=\"post\">");
79 writer.println(" <div class='label'>User id:</div>");
80 writer.println(" <input type='text' name='userId'>");
81 writer.println(" <div class='break'></div>");
83 writer.println(" <div class='label'>Password:</div>");
84 writer.println(" <input type='password' name='password'>");
85 writer.println(" <div class='break'></div>");
87 writer.println(" <input type='submit' value='Login'>");
88 writer.println(" <label name='message'></label>");
89 writer.println("</form>");
91 writer.println("<hr/>");
92 writer.println("<h2>Quick links:</h2>");
93 writer.println("<table>");
94 writer.println("<tr>");
95 writer.println("<th>full name</th>");
96 writer.println("<th>user id</th>");
97 writer.println("<th>role</th>");
98 writer.println("<th>action</th>");
99 writer.println("</tr>");
100 Iterator<User> iterator = allUsers.iterator();
101 while (iterator.hasNext()) {
102 User user = iterator.next();
103 writer.println("<tr>");
104 writer.println("<td>" + user.getUserRef() + "</td>");
105 writer.println("<td>" + user.getUserId() + "</td>");
106 writer.println("<td>" + user.getRole() + "</td>");
107 writer.println("<td>" + user.getUserCreateRef() + "</td>");
108 writer.println("</tr>");
110 writer.println("</table>");
112 writer.println("<a href='create?all=true' target='resultFrame'>Create All</a>");
113 writer.println("<hr/><iframe name='resultFrame' width='400' height='300'></iframe>");
115 writer.println("</body>");
116 writer.println("</html>");
120 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
122 String userId = request.getParameter("userId");
123 String password = request.getParameter("password");
124 request.setAttribute("message", "OK");
126 System.out.println("Login -> doPost userId=" + userId);
127 User user = getUser(userId, password);
129 response.sendError(500, "ERROR: userId or password incorrect");
130 // doGet(request, response);
132 System.out.println("Login -> doPost redirect to /sdc1 (to proxy)");
133 Cookie cookieUser = new Cookie("HTTP_IV_USER", user.getUserId());
134 Cookie cookieUserId = new Cookie("USER_ID", user.getUserId());
135 Cookie cookieFirstName = new Cookie("HTTP_CSP_FIRSTNAME", user.getFirstName());
136 Cookie cookieEmail = new Cookie("HTTP_CSP_EMAIL", user.getEmail());
137 Cookie cookieLastName = new Cookie("HTTP_CSP_LASTNAME", user.getLastName());
138 Cookie cookieRemoteAddress = new Cookie("HTTP_IV_REMOTE_ADDRESS", "0.0.0.0");
139 Cookie cookieWsType = new Cookie("HTTP_CSP_WSTYPE", "Intranet");
140 response.addCookie(cookieUser);
141 response.addCookie(cookieUserId);
142 response.addCookie(cookieFirstName);
143 response.addCookie(cookieEmail);
144 response.addCookie(cookieLastName);
145 response.addCookie(cookieRemoteAddress);
146 response.addCookie(cookieWsType);
147 response.addCookie(new Cookie(Conf.getInstance().getPortalCookieName(), "portal"));
148 response.sendRedirect("/sdc1");
153 private User getUser(String userId, String password) {
154 User user = Conf.getInstance().getUsers().get(userId);
158 if (!password.equals(user.getPassword())) {
165 public String getServletInfo() {
166 return "Http Proxy Servlet";