deed77c03dc7d478f1bdceac366f5f5da7ae3fd1
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / OrgLookupFilter.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.auth.gui;
23
24 import java.io.IOException;
25 import java.security.Principal;
26
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.org.OrganizationException;
37 import org.onap.aaf.auth.org.Organization.Identity;
38 import org.onap.aaf.auth.rserv.TransFilter;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.principal.TaggedPrincipal;
41
42 public class OrgLookupFilter implements Filter {
43     
44     @Override
45     public void init(FilterConfig arg0) throws ServletException {
46     }
47
48     @Override
49     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain fc) throws IOException, ServletException {
50         final AuthzTrans trans = (AuthzTrans) req.getAttribute(TransFilter.TRANS_TAG);
51         if (req instanceof HttpServletRequest) {
52             Principal p = ((HttpServletRequest)req).getUserPrincipal();
53             if (p instanceof TaggedPrincipal) {
54                 ((TaggedPrincipal)p).setTagLookup(new TaggedPrincipal.TagLookup() {
55                     @Override
56                     public String lookup() throws CadiException {
57                         Identity id;
58                         try {
59                             id = trans.org().getIdentity(trans, p.getName());
60                             if (id!=null && id.isFound()) {
61                                 return id.firstName();
62                             }
63                         } catch (OrganizationException e) {
64                             throw new CadiException(e);
65                         }
66                         return p.getName();
67                     }
68                 });
69             }
70             fc.doFilter(req, resp);
71         }
72         
73     }
74
75
76     @Override
77     public void destroy() {
78     }
79 }