Integrate EPSDK-FW library for auth and users
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / LoginServlet.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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 package org.oransc.ric.portal.dashboard;
21
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.net.URLEncoder;
25
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServlet;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
33 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
34 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.http.MediaType;
38
39 /**
40  * Serves a login page that contains a link from configuration to ONAP Portal.
41  * This avoids the immediate redirect to Portal that is confusing to users and
42  * infuriating to developers.
43  * 
44  * Basically this is do-it-yourself JSP :)
45  */
46 public class LoginServlet extends HttpServlet {
47
48         private static final long serialVersionUID = 1191385178190976568L;
49
50         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52         @Override
53         public void init(ServletConfig servletConfig) throws ServletException {
54                 logger.debug("init");
55                 super.init(servletConfig);
56                 final String portalURL = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL);
57                 if (portalURL == null || portalURL.length() == 0)
58                         throw new ServletException("Failed to get property " + PortalApiConstants.ECOMP_REDIRECT_URL);
59         }
60
61         @Override
62         protected void doGet(HttpServletRequest request, HttpServletResponse response)
63                         throws IOException, ServletException {
64                 logger.debug("doGet {}", request.getRequestURI());
65                 // The original page URL should arrive as a query parameter
66                 String appUrl = request.getParameter(PortalAuthenticationFilter.REDIRECT_URL_KEY);
67                 // If a user bookmarks the login page, then nothing arrives;
68                 // use the original URL without the login suffix.
69                 if (appUrl == null || appUrl.isEmpty()) {
70                         String loginUrl = request.getRequestURL().toString();
71                         int indexOfLogin = loginUrl.indexOf(DashboardConstants.LOGIN_PAGE);
72                         appUrl = loginUrl.substring(0, indexOfLogin);
73                 }
74                 String encodedAppUrl = URLEncoder.encode(appUrl, "UTF-8");
75                 String portalBaseUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL);
76                 String redirectUrl = portalBaseUrl + "?" + PortalAuthenticationFilter.REDIRECT_URL_KEY + "=" + encodedAppUrl;
77                 String aHref = "<a href=\"" + redirectUrl + "\">";
78                 // If only Java had "here" documents.
79                 String body = String.join(//
80                                 System.getProperty("line.separator"), //
81                                 "<html>", //
82                                 "<head>", //
83                                 "<title>RIC Dashboard</title>", //
84                                 "<style>", //
85                                 "html, body { ", //
86                                 "  font-family: Helvetica, Arial, sans-serif;", //
87                                 "}", //
88                                 "</style>", //
89                                 "</head>", //
90                                 "<body>", //
91                                 "<h2>RIC Dashboard</h2>", //
92                                 "<h4>Please log in.</h4>", //
93                                 "<p>", //
94                                 aHref, "Click here to authenticate at the ONAP Portal</a>", //
95                                 "</p>", //
96                                 "</body>", //
97                                 "</html>");
98                 writeAndFlush(response, MediaType.TEXT_HTML_VALUE, body);
99         }
100
101         /**
102          * Sets the content type and writes the response.
103          * 
104          * @param response
105          * @param contentType
106          * @param responseBody
107          * @throws IOException
108          */
109         private void writeAndFlush(HttpServletResponse response, String contentType, String responseBody)
110                         throws IOException {
111                 response.setContentType(contentType);
112                 response.getWriter().print(responseBody);
113                 response.getWriter().flush();
114         }
115
116 }