001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.axis2.pojo;
019    
020    import java.security.Principal;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.xml.ws.WebServiceContext;
024    import javax.xml.ws.handler.MessageContext;
025    
026    /**
027     * Implementation of WebServiceContext for POJO WS to ensure that getUserPrincipal()
028     * and isUserInRole() are properly handled.
029     * 
030     * @version $Rev$ $Date$
031     */
032    public class POJOWebServiceContext implements WebServiceContext {
033    
034        private static ThreadLocal<MessageContext> context = new ThreadLocal<MessageContext>();
035        
036        public POJOWebServiceContext() {        
037        }
038            
039        public final MessageContext getMessageContext() {
040            return context.get();
041        }
042    
043        private HttpServletRequest getHttpServletRequest() {
044            MessageContext ctx = getMessageContext();
045            return (ctx != null) ? (HttpServletRequest)ctx.get(MessageContext.SERVLET_REQUEST) : null;
046        }
047    
048        public final Principal getUserPrincipal() {
049            HttpServletRequest request = getHttpServletRequest();
050            return (request != null) ? request.getUserPrincipal() : null;
051        }
052    
053        public final boolean isUserInRole(String user) {
054            HttpServletRequest request = getHttpServletRequest();
055            return (request != null) ? request.isUserInRole(user) : false;
056        }
057                
058        public static void setMessageContext(MessageContext ctx) {
059            context.set(ctx);
060        }
061    
062        public static void clear() {
063            context.set(null);
064        }
065    
066    }