d9da3fa8ffb220787c90fa04985375fad9165ee0
[aaf/cadi.git] / core / src / main / java / com / att / cadi / taf / LoginPageTafResp.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.taf;\r
25 \r
26 import java.io.IOException;\r
27 import java.net.URI;\r
28 import java.util.List;\r
29 \r
30 import javax.servlet.http.HttpServletResponse;\r
31 \r
32 import com.att.cadi.Access;\r
33 import com.att.cadi.Access.Level;\r
34 import com.att.cadi.Locator;\r
35 import com.att.cadi.Locator.Item;\r
36 \r
37 public class LoginPageTafResp extends AbsTafResp {\r
38         private final HttpServletResponse httpResp;\r
39         private final String loginPageURL;\r
40 \r
41         private LoginPageTafResp(Access access, final HttpServletResponse resp, String loginPageURL) {\r
42                 super(access, null, "Multiple Possible HTTP Logins available.  Redirecting to Login Choice Page");\r
43                 httpResp = resp;\r
44                 this.loginPageURL = loginPageURL;\r
45         }\r
46 \r
47         @Override\r
48         public RESP authenticate() throws IOException {\r
49                 httpResp.sendRedirect(loginPageURL);\r
50                 return RESP.HTTP_REDIRECT_INVOKED;\r
51         }\r
52         \r
53         @Override\r
54         public RESP isAuthenticated() {\r
55                 return RESP.TRY_AUTHENTICATING;\r
56         }\r
57         \r
58         public static TafResp create(Access access, Locator<URI> locator, final HttpServletResponse resp, List<Redirectable> redir) {\r
59                 if(locator!=null) {\r
60                         try {\r
61                                 Item item = locator.best();\r
62                                 URI uri = locator.get(item);\r
63                                 if(uri!=null) {\r
64                                         StringBuilder sb = new StringBuilder(uri.toString());\r
65                                         String query = uri.getQuery();\r
66                                         boolean first = query==null || query.length()==0;\r
67                                         int count=0;\r
68                                         for(Redirectable t : redir) {\r
69                                                 if(first) {\r
70                                                         sb.append('?');\r
71                                                         first=false;\r
72                                                 }\r
73                                                 else sb.append('&');\r
74                                                 sb.append(t.get());\r
75                                                 ++count;\r
76                                         }\r
77                                         if(count>0)return new LoginPageTafResp(access, resp, sb.toString());\r
78                                 }\r
79                         } catch (Exception e) {\r
80                                 access.log(e, "Error deriving Login Page location");\r
81                         }\r
82                 } else if(!redir.isEmpty()) { \r
83                         access.log(Level.DEBUG,"LoginPage Locator is not configured. Taking first Redirectable Taf");\r
84                         return redir.get(0);\r
85                 }\r
86                 return NullTafResp.singleton();\r
87         }\r
88 }\r