001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.server.sei;
038:
039: import com.sun.istack.Nullable;
040: import com.sun.xml.ws.api.WSBinding;
041: import com.sun.xml.ws.api.message.Message;
042: import com.sun.xml.ws.api.message.Packet;
043: import com.sun.xml.ws.fault.SOAPFaultBuilder;
044: import com.sun.xml.ws.model.AbstractSEIModelImpl;
045: import com.sun.xml.ws.model.JavaMethodImpl;
046: import com.sun.xml.ws.resources.ServerMessages;
047: import com.sun.xml.ws.util.QNameMap;
048:
049: import javax.xml.namespace.QName;
050: import java.util.ArrayList;
051: import java.util.List;
052: import java.util.logging.Logger;
053:
054: /**
055: * An {@link com.sun.xml.ws.server.sei.EndpointMethodDispatcher} that uses
056: * SOAP payload first child's QName as the key for dispatching.
057: * <p/>
058: * A map of all payload QNames on the port and the corresponding {@link EndpointMethodHandler}
059: * is initialized in the constructor. The payload QName is extracted from the
060: * request {@link Packet} and used as the key to return the correct
061: * handler.
062: *
063: * @author Arun Gupta
064: * @author Jitendra Kotamraju
065: */
066: final class PayloadQNameBasedDispatcher implements
067: EndpointMethodDispatcher {
068: private static final Logger LOGGER = Logger
069: .getLogger(PayloadQNameBasedDispatcher.class.getName());
070:
071: private static final String EMPTY_PAYLOAD_LOCAL = "";
072: private static final String EMPTY_PAYLOAD_NSURI = "";
073: private static final QName EMPTY_PAYLOAD = new QName(
074: EMPTY_PAYLOAD_NSURI, EMPTY_PAYLOAD_LOCAL);
075:
076: private final QNameMap<EndpointMethodHandler> methodHandlers = new QNameMap<EndpointMethodHandler>();
077: private final QNameMap<List<String>> unique = new QNameMap<List<String>>();
078: private final WSBinding binding;
079:
080: public PayloadQNameBasedDispatcher(AbstractSEIModelImpl model,
081: WSBinding binding, SEIInvokerTube invokerTube) {
082: this .binding = binding;
083: // Find if any payload QNames repeat for operations
084: for (JavaMethodImpl m : model.getJavaMethods()) {
085: QName name = m.getRequestPayloadName();
086: if (name == null)
087: name = EMPTY_PAYLOAD;
088: List<String> methods = unique.get(name);
089: if (methods == null) {
090: methods = new ArrayList<String>();
091: unique.put(name, methods);
092: }
093: methods.add(m.getMethod().getName());
094: }
095:
096: // Log warnings about non unique payload QNames
097: for (QNameMap.Entry<List<String>> e : unique.entrySet()) {
098: if (e.getValue().size() > 1) {
099: LOGGER.warning(ServerMessages
100: .NON_UNIQUE_DISPATCH_QNAME(e.getValue(), e
101: .createQName()));
102: }
103: }
104:
105: for (JavaMethodImpl m : model.getJavaMethods()) {
106: QName name = m.getRequestPayloadName();
107: if (name == null)
108: name = EMPTY_PAYLOAD;
109: // Set up method handlers only for unique QNames. So that dispatching
110: // happens consistently for a method
111: if (unique.get(name).size() == 1) {
112: methodHandlers.put(name, new EndpointMethodHandler(
113: invokerTube, m, binding));
114: }
115: }
116: }
117:
118: /**
119: *
120: * @return not null if it finds a unique handler for the request
121: * null otherwise
122: * @throws DispatchException if the payload itself is incorrect
123: */
124: public @Nullable
125: EndpointMethodHandler getEndpointMethodHandler(Packet request)
126: throws DispatchException {
127: Message message = request.getMessage();
128: String localPart = message.getPayloadLocalPart();
129: String nsUri;
130: if (localPart == null) {
131: localPart = EMPTY_PAYLOAD_LOCAL;
132: nsUri = EMPTY_PAYLOAD_NSURI;
133: } else {
134: nsUri = message.getPayloadNamespaceURI();
135: }
136: EndpointMethodHandler mh = methodHandlers.get(nsUri, localPart);
137:
138: // Check if payload itself is correct. Usually it is, so let us check last
139: if (mh == null && !unique.containsKey(nsUri, localPart)) {
140: String dispatchKey = "{" + nsUri + "}" + localPart;
141: String faultString = ServerMessages
142: .DISPATCH_CANNOT_FIND_METHOD(dispatchKey);
143: throw new DispatchException(SOAPFaultBuilder
144: .createSOAPFaultMessage(binding.getSOAPVersion(),
145: faultString,
146: binding.getSOAPVersion().faultCodeClient));
147: }
148: return mh;
149: }
150:
151: }
|