001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.description;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.addressing.EndpointReference;
025: import org.apache.axis2.client.Options;
026: import org.apache.axis2.context.MessageContext;
027: import org.apache.axis2.engine.AxisConfiguration;
028: import org.apache.axis2.engine.ListenerManager;
029: import org.apache.axis2.i18n.Messages;
030:
031: import java.net.URI;
032: import java.net.URISyntaxException;
033:
034: /**
035: * Utility methods for various clients to use.
036: */
037: public class ClientUtils {
038:
039: public static synchronized TransportOutDescription inferOutTransport(
040: AxisConfiguration ac, EndpointReference epr,
041: MessageContext msgctx) throws AxisFault {
042: String transportURI = (String) msgctx
043: .getProperty(Constants.Configuration.TRANSPORT_URL);
044: if (transportURI != null && !"".equals(transportURI)) {
045: int index = transportURI.indexOf(':');
046: String transport = (index > 0) ? transportURI.substring(0,
047: index) : null;
048: if (transport != null) {
049: TransportOutDescription transportOut = ac
050: .getTransportOut(transport);
051: if (transportOut == null) {
052: throw new AxisFault(
053: "No Tranport Sender found for : "
054: + transport);
055: } else {
056: return ac.getTransportOut(transport);
057: }
058: } else {
059: throw new AxisFault(Messages.getMessage(
060: "cannotInferTransport", transportURI));
061: }
062: } else {
063: if (msgctx.getOptions().getTransportOut() != null) {
064: if (msgctx.getOptions().getTransportOut().getSender() == null) {
065: throw new AxisFault(
066: "Incomplete transport sender: missing sender!");
067: }
068: return msgctx.getOptions().getTransportOut();
069: }
070: if (epr == null || (epr.getAddress() == null)) {
071: throw new AxisFault(Messages
072: .getMessage("cannotInferTransportNoAddr"));
073: }
074: String uri = epr.getAddress();
075: int index = uri.indexOf(':');
076: String transport = (index > 0) ? uri.substring(0, index)
077: : null;
078: if (transport != null) {
079: return ac.getTransportOut(transport);
080: } else {
081: throw new AxisFault(Messages.getMessage(
082: "cannotInferTransport", uri));
083: }
084: }
085: }
086:
087: public static synchronized TransportInDescription inferInTransport(
088: AxisConfiguration ac, Options options,
089: MessageContext msgCtxt) throws AxisFault {
090: String listenerTransportProtocol = options
091: .getTransportInProtocol();
092: if (listenerTransportProtocol == null) {
093: EndpointReference replyTo = msgCtxt.getReplyTo();
094: if (replyTo != null) {
095: try {
096: URI uri = new URI(replyTo.getAddress());
097: listenerTransportProtocol = uri.getScheme();
098: } catch (URISyntaxException e) {
099: //need to ignore
100: }
101: } else {
102: //assume listener transport as sender transport
103: if (msgCtxt.getTransportOut() != null) {
104: listenerTransportProtocol = msgCtxt
105: .getTransportOut().getName();
106: }
107: }
108: }
109: TransportInDescription transportIn = null;
110: if (options.isUseSeparateListener()) {
111: if ((listenerTransportProtocol != null)
112: && !"".equals(listenerTransportProtocol)) {
113: transportIn = ac
114: .getTransportIn(listenerTransportProtocol);
115: ListenerManager listenerManager = msgCtxt
116: .getConfigurationContext().getListenerManager();
117: if (transportIn == null) {
118: // TODO : User should not be mandated to give an IN transport. If it is not given, we should
119: // ask from the ListenerManager to give any available transport for this client.
120: throw new AxisFault(Messages.getMessage(
121: "unknownTransport",
122: listenerTransportProtocol));
123: }
124: if (!listenerManager.isListenerRunning(transportIn
125: .getName())) {
126: listenerManager.addListener(transportIn, false);
127: }
128: }
129: if (msgCtxt.getAxisService() != null) {
130: if (!msgCtxt.isEngaged(Constants.MODULE_ADDRESSING)) {
131: throw new AxisFault(Messages
132: .getMessage("2channelNeedAddressing"));
133: }
134: } else {
135: if (!ac.isEngaged(Constants.MODULE_ADDRESSING)) {
136: throw new AxisFault(Messages
137: .getMessage("2channelNeedAddressing"));
138: }
139: }
140: }
141:
142: return transportIn;
143: }
144: }
|