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