Update for more Logging Info
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / taf / LoginPageTafResp.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
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
22 package org.onap.aaf.cadi.taf;
23
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletResponse;
29
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;
34
35 public class LoginPageTafResp extends AbsTafResp {
36     private final HttpServletResponse httpResp;
37     private final String loginPageURL;
38
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");
41         httpResp = resp;
42         this.loginPageURL = loginPageURL;
43     }
44
45     @Override
46     public RESP authenticate() throws IOException {
47         httpResp.sendRedirect(loginPageURL);
48         return RESP.HTTP_REDIRECT_INVOKED;
49     }
50     
51     @Override
52     public RESP isAuthenticated() {
53         return RESP.TRY_AUTHENTICATING;
54     }
55     
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);
61             }
62             return NullTafResp.singleton();
63         }
64
65         try {
66             Item item = locator.best();
67             URI uri = locator.get(item);
68             if (uri == null) {
69                 return NullTafResp.singleton();
70             }
71
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) {
76                 if (first) {
77                     sb.append('?');
78                     first = false;
79                 }
80                 else {
81                     sb.append('&');
82                 }
83                 sb.append(redir.get());
84             }
85             if (!redirectables.isEmpty()) {
86                 return new LoginPageTafResp(access, resp, sb.toString());
87             }
88         } catch (Exception e) {
89             access.log(e, "Error deriving Login Page location");
90         }
91
92         return NullTafResp.singleton();
93     }
94     
95     @Override
96     public String taf() {
97         return "LoginPage";
98     }
99
100 }