2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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====================================================
22 package org.onap.aaf.cadi.taf;
24 import java.io.IOException;
26 import java.util.List;
28 import javax.servlet.http.HttpServletResponse;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Locator;
32 import org.onap.aaf.cadi.Access.Level;
33 import org.onap.aaf.cadi.Locator.Item;
35 public class LoginPageTafResp extends AbsTafResp {
36 private final HttpServletResponse httpResp;
37 private final String loginPageURL;
39 private LoginPageTafResp(Access access, final HttpServletResponse resp, String loginPageURL) {
40 super(access, "LoginPage","unknown", "Multiple Possible HTTP Logins available. Redirecting to Login Choice Page");
42 this.loginPageURL = loginPageURL;
46 public RESP authenticate() throws IOException {
47 httpResp.sendRedirect(loginPageURL);
48 return RESP.HTTP_REDIRECT_INVOKED;
52 public RESP isAuthenticated() {
53 return RESP.TRY_AUTHENTICATING;
56 public static TafResp create(Access access, Locator<URI> locator, final HttpServletResponse resp, List<Redirectable> redirectables) {
57 if (locator == null) {
58 if (!redirectables.isEmpty()) {
59 access.log(Level.DEBUG,"LoginPage Locator is not configured. Taking first Redirectable Taf");
60 return redirectables.get(0);
62 return NullTafResp.singleton();
66 Item item = locator.best();
67 URI uri = locator.get(item);
69 return NullTafResp.singleton();
72 StringBuilder sb = new StringBuilder(uri.toString());
73 String query = uri.getQuery();
74 boolean first = ((query == null) || (query.length() == 0));
75 for (Redirectable redir : redirectables) {
83 sb.append(redir.get());
85 if (!redirectables.isEmpty()) {
86 return new LoginPageTafResp(access, resp, sb.toString());
88 } catch (Exception e) {
89 access.log(e, "Error deriving Login Page location");
92 return NullTafResp.singleton();