View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.xml.internal.ws.client;
27  
28  import com.sun.xml.internal.ws.api.handler.MessageHandler;
29  import com.sun.xml.internal.ws.handler.HandlerException;
30  
31  import javax.xml.namespace.QName;
32  import javax.xml.ws.handler.Handler;
33  import javax.xml.ws.handler.LogicalHandler;
34  import javax.xml.ws.handler.soap.SOAPHandler;
35  import java.util.*;
36  
37  /**
38   * This class holds the handler information and roles on the Binding (mutable info in the binding).
39   *
40   * HandlerConfiguration is immutable, and a new object is created when the BindingImpl is created or User calls
41   * Binding.setHandlerChain() or SOAPBinding.setRoles().
42   *
43   * During invocation in Stub.process(), snapshot of the handler configuration is set in Packet.handlerConfig. The
44   * information in the HandlerConfiguration is used by MUPipe and HandlerTube implementations.
45   *
46   * @author Rama Pulavarthi
47   */
48  public class HandlerConfiguration {
49      private final Set<String> roles;
50      /**
51       * This chain may contain both soap and logical handlers.
52       */
53      private final List<Handler> handlerChain;
54      private final List<LogicalHandler> logicalHandlers;
55      private final List<SOAPHandler> soapHandlers;
56      private final List<MessageHandler> messageHandlers;
57      private final Set<QName> handlerKnownHeaders;
58  
59      /**
60       * @param roles               This contains the roles assumed by the Binding implementation.
61       * @param handlerChain        This contains the handler chain set on the Binding
62       */
63      public HandlerConfiguration(Set<String> roles, List<Handler> handlerChain) {
64          this.roles = roles;
65          this.handlerChain = handlerChain;
66          logicalHandlers = new ArrayList<LogicalHandler>();
67          soapHandlers = new ArrayList<SOAPHandler>();
68          messageHandlers = new ArrayList<MessageHandler>();
69          Set<QName> modHandlerKnownHeaders = new HashSet<QName>();
70  
71          for (Handler handler : handlerChain) {
72              if (handler instanceof LogicalHandler) {
73                  logicalHandlers.add((LogicalHandler) handler);
74              } else if (handler instanceof SOAPHandler) {
75                  soapHandlers.add((SOAPHandler) handler);
76                  Set<QName> headers = ((SOAPHandler<?>) handler).getHeaders();
77                  if (headers != null) {
78                      modHandlerKnownHeaders.addAll(headers);
79                  }
80              } else if (handler instanceof MessageHandler) {
81                  messageHandlers.add((MessageHandler) handler);
82                  Set<QName> headers = ((MessageHandler<?>) handler).getHeaders();
83                  if (headers != null) {
84                      modHandlerKnownHeaders.addAll(headers);
85                  }
86              }else {
87                  throw new HandlerException("handler.not.valid.type",
88                      handler.getClass());
89              }
90          }
91  
92          handlerKnownHeaders = Collections.unmodifiableSet(modHandlerKnownHeaders);
93      }
94  
95      /**
96       * This is called when roles as reset on binding using SOAPBinding#setRoles(), to save reparsing the handlers again.
97       * @param roles
98       * @param oldConfig
99       */
100     public HandlerConfiguration(Set<String> roles, HandlerConfiguration oldConfig) {
101         this.roles = roles;
102         this.handlerChain = oldConfig.handlerChain;
103         this.logicalHandlers = oldConfig.logicalHandlers;
104         this.soapHandlers = oldConfig.soapHandlers;
105         this.messageHandlers = oldConfig.messageHandlers;
106         this.handlerKnownHeaders = oldConfig.handlerKnownHeaders;
107     }
108 
109     public Set<String> getRoles() {
110         return roles;
111     }
112 
113     /**
114      *
115      * @return return a copy of handler chain
116      */
117     public List<Handler> getHandlerChain() {
118         if(handlerChain == null)
119             return Collections.emptyList();
120         return new ArrayList<Handler>(handlerChain);
121 
122     }
123 
124     public List<LogicalHandler> getLogicalHandlers() {
125         return logicalHandlers;
126     }
127 
128     public List<SOAPHandler> getSoapHandlers() {
129         return soapHandlers;
130     }
131 
132     public List<MessageHandler> getMessageHandlers() {
133         return messageHandlers;
134     }
135 
136     public Set<QName> getHandlerKnownHeaders() {
137         return handlerKnownHeaders;
138     }
139 
140 }