AT&T 2.0.19 Code drop, stage 2
[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, null, "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> redir) {
57                 if(locator!=null) {
58                         try {
59                                 Item item = locator.best();
60                                 URI uri = locator.get(item);
61                                 if(uri!=null) {
62                                         StringBuilder sb = new StringBuilder(uri.toString());
63                                         String query = uri.getQuery();
64                                         boolean first = query==null || query.length()==0;
65                                         int count=0;
66                                         for(Redirectable t : redir) {
67                                                 if(first) {
68                                                         sb.append('?');
69                                                         first=false;
70                                                 }
71                                                 else sb.append('&');
72                                                 sb.append(t.get());
73                                                 ++count;
74                                         }
75                                         if(count>0)return new LoginPageTafResp(access, resp, sb.toString());
76                                 }
77                         } catch (Exception e) {
78                                 access.log(e, "Error deriving Login Page location");
79                         }
80                 } else if(!redir.isEmpty()) { 
81                         access.log(Level.DEBUG,"LoginPage Locator is not configured. Taking first Redirectable Taf");
82                         return redir.get(0);
83                 }
84                 return NullTafResp.singleton();
85         }
86 }