2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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===================================
20 package org.oransc.ric.portal.dashboard;
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.net.URLEncoder;
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;
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;
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.
44 * Basically this is do-it-yourself JSP :)
46 public class LoginServlet extends HttpServlet {
48 private static final long serialVersionUID = 1191385178190976568L;
50 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 public void init(ServletConfig servletConfig) throws ServletException {
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);
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);
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"), //
83 "<title>RIC Dashboard</title>", //
86 " font-family: Helvetica, Arial, sans-serif;", //
91 "<h2>RIC Dashboard</h2>", //
92 "<h4>Please log in.</h4>", //
94 aHref, "Click here to authenticate at the ONAP Portal</a>", //
98 writeAndFlush(response, MediaType.TEXT_HTML_VALUE, body);
102 * Sets the content type and writes the response.
106 * @param responseBody
107 * @throws IOException
109 private void writeAndFlush(HttpServletResponse response, String contentType, String responseBody)
111 response.setContentType(contentType);
112 response.getWriter().print(responseBody);
113 response.getWriter().flush();