View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.webservices;
20  
21  import java.io.StringReader;
22  
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.xml.bind.JAXBElement;
29  import javax.xml.bind.Unmarshaller;
30  import javax.xml.namespace.QName;
31  import javax.xml.ws.handler.MessageContext;
32  import javax.xml.ws.handler.MessageContext.Scope;
33  import javax.xml.ws.handler.soap.SOAPHandler;
34  import javax.xml.ws.handler.soap.SOAPMessageContext;
35  
36  
37  public class WebSphereAuthHandler extends AbstractUsernameTokenAuthHandler implements SOAPHandler<SOAPMessageContext>
38  {
39      public Set<QName> getHeaders(  )
40      {
41          return HEADERS;
42      }
43  
44      public void close( MessageContext context )
45      {
46      }
47  
48      public boolean handleFault( SOAPMessageContext context )
49      {
50          return true;
51      }
52  
53      @SuppressWarnings( "unchecked" )
54      public boolean handleMessage( SOAPMessageContext context )
55      {
56          Boolean outboundProperty = (Boolean) context.get( MessageContext.MESSAGE_OUTBOUND_PROPERTY );
57  
58          if ( outboundProperty.booleanValue(  ) )
59          {
60              // we are only looking at inbound messages
61              return true;
62          }
63  
64          Map<String, String> callContextMap = null;
65  
66          Map<QName, List<String>> requestHeaders = (Map<QName, List<String>>) context.get( 
67                  "jaxws.binding.soap.headers.inbound" );
68  
69          if ( requestHeaders != null )
70          {
71              List<String> secHeaders = requestHeaders.get( WSSE_SECURITY );
72  
73              if ( ( secHeaders != null ) && ( secHeaders.size(  ) > 0 ) )
74              {
75                  try
76                  {
77                      Unmarshaller unmarshaller = WSSE_CONTEXT.createUnmarshaller(  );
78  
79                      for ( String h : secHeaders )
80                      {
81                          try
82                          {
83                              JAXBElement<SecurityHeaderType> sht = (JAXBElement<SecurityHeaderType>) unmarshaller.unmarshal( new StringReader( 
84                                          h ) );
85  
86                              callContextMap = extractUsernamePassword( sht );
87  
88                              if ( callContextMap != null )
89                              {
90                                  break;
91                              }
92                          }
93                          catch ( Exception e )
94                          {
95                              // unmarshalling failed, maybe another header -
96                              // ignore
97                          }
98                      }
99                  }
100                 catch ( Exception e )
101                 {
102                     // JAXB problem - ignore
103                 }
104             }
105         }
106 
107         // add user and password to context
108         if ( callContextMap == null )
109         {
110             callContextMap = new HashMap<String, String>(  );
111         }
112 
113         context.put( AbstractService.CALL_CONTEXT_MAP, callContextMap );
114         context.setScope( AbstractService.CALL_CONTEXT_MAP, Scope.APPLICATION );
115 
116         return true;
117     }
118 }