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 package org.apache.servicemix.jbi.resolver;
018
019 import javax.jbi.JBIException;
020 import javax.jbi.component.ComponentContext;
021 import javax.jbi.messaging.MessageExchange;
022 import javax.jbi.servicedesc.ServiceEndpoint;
023 import javax.xml.namespace.QName;
024
025 import org.w3c.dom.Document;
026 import org.w3c.dom.DocumentFragment;
027 import org.w3c.dom.Element;
028 import org.w3c.dom.Text;
029
030 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
031 import org.apache.servicemix.jbi.util.WSAddressingConstants;
032
033 public class URIResolver extends EndpointResolverSupport {
034
035 /**
036 * The uri to resolve
037 */
038 private String uri;
039
040 public URIResolver() {
041 }
042
043 public URIResolver(String uri) {
044 this.uri = uri;
045 }
046
047 protected JBIException createServiceUnavailableException() {
048 return new JBIException("Unable to resolve uri: " + uri);
049 }
050
051 public ServiceEndpoint[] resolveAvailableEndpoints(ComponentContext context,
052 MessageExchange exchange) throws JBIException {
053 if (uri.startsWith("interface:")) {
054 String u = this.uri.substring(10);
055 String[] parts = split2(u);
056 return context.getEndpoints(new QName(parts[0], parts[1]));
057 } else if (uri.startsWith("operation:")) {
058 // ignore operation
059 String u = this.uri.substring(10);
060 String[] parts = split3(u);
061 return context.getEndpoints(new QName(parts[0], parts[1]));
062 } else if (uri.startsWith("service:")) {
063 String u = this.uri.substring(8);
064 String[] parts = split2(u);
065 return context.getEndpointsForService(new QName(parts[0], parts[1]));
066 } else if (uri.startsWith("endpoint:")) {
067 String u = this.uri.substring(9);
068 String[] parts = split3(u);
069 ServiceEndpoint se = context.getEndpoint(new QName(parts[0], parts[1]), parts[2]);
070 if (se != null) {
071 return new ServiceEndpoint[] {se };
072 }
073 // Try an EPR resolution
074 } else {
075 DocumentFragment epr = createWSAEPR(uri);
076 ServiceEndpoint se = context.resolveEndpointReference(epr);
077 if (se != null) {
078 return new ServiceEndpoint[] {se };
079 }
080 }
081 return null;
082 }
083
084 /**
085 * @return the uri
086 */
087 public String getUri() {
088 return uri;
089 }
090
091 /**
092 * @param uri the uri to set
093 */
094 public void setUri(String uri) {
095 this.uri = uri;
096 }
097
098 public static DocumentFragment createWSAEPR(String uri) {
099 Document doc;
100 try {
101 doc = new SourceTransformer().createDocument();
102 } catch (Exception e) {
103 throw new RuntimeException(e);
104 }
105 DocumentFragment epr = doc.createDocumentFragment();
106 Element root = doc.createElement("epr");
107 Element address = doc.createElementNS(WSAddressingConstants.WSA_NAMESPACE_200508,
108 WSAddressingConstants.WSA_PREFIX + ":" + WSAddressingConstants.EL_ADDRESS);
109 Text txt = doc.createTextNode(uri);
110 address.appendChild(txt);
111 root.appendChild(address);
112 epr.appendChild(root);
113 return epr;
114 }
115
116 /**
117 * Configure a JBI exchange with the given URI as the target
118 *
119 * @param exchange the exchange to configure
120 * @param context a component context used to resolve endpoints
121 * @param uri the target uri
122 */
123 public static void configureExchange(MessageExchange exchange, ComponentContext context, String uri) {
124 if (exchange == null) {
125 throw new NullPointerException("exchange is null");
126 }
127 if (context == null) {
128 throw new NullPointerException("context is null");
129 }
130 if (uri == null) {
131 throw new NullPointerException("uri is null");
132 }
133 if (uri.startsWith("interface:")) {
134 String uri2 = uri.substring(10);
135 String[] parts = URIResolver.split2(uri2);
136 exchange.setInterfaceName(new QName(parts[0], parts[1]));
137 } else if (uri.startsWith("operation:")) {
138 String uri2 = uri.substring(10);
139 String[] parts = URIResolver.split3(uri2);
140 exchange.setInterfaceName(new QName(parts[0], parts[1]));
141 exchange.setOperation(new QName(parts[0], parts[2]));
142 } else if (uri.startsWith("service:")) {
143 String uri2 = uri.substring(8);
144 String[] parts = URIResolver.split2(uri2);
145 exchange.setService(new QName(parts[0], parts[1]));
146 } else if (uri.startsWith("endpoint:")) {
147 String uri2 = uri.substring(9);
148 String[] parts = URIResolver.split3(uri2);
149 ServiceEndpoint se = context.getEndpoint(new QName(parts[0], parts[1]), parts[2]);
150 exchange.setEndpoint(se);
151 } else {
152 DocumentFragment epr = URIResolver.createWSAEPR(uri);
153 ServiceEndpoint se = context.resolveEndpointReference(epr);
154 exchange.setEndpoint(se);
155 }
156 }
157
158 public static String[] split3(String uri) {
159 char sep;
160 uri = uri.trim();
161 if (uri.indexOf('/') > 0) {
162 sep = '/';
163 } else {
164 sep = ':';
165 }
166 int idx1 = uri.lastIndexOf(sep);
167 int idx2 = uri.lastIndexOf(sep, idx1 - 1);
168 if (idx1 < 0 || idx2 < 0) {
169 throw new IllegalArgumentException("Bad syntax: expected [part0][sep][part1][sep][part2]");
170 }
171 String epName = uri.substring(idx1 + 1);
172 String svcName = uri.substring(idx2 + 1, idx1);
173 String nsUri = uri.substring(0, idx2);
174 return new String[] {nsUri, svcName, epName };
175 }
176
177 public static String[] split2(String uri) {
178 char sep;
179 uri = uri.trim();
180 if (uri.indexOf('/') > 0) {
181 sep = '/';
182 } else {
183 sep = ':';
184 }
185 int idx1 = uri.lastIndexOf(sep);
186 if (idx1 < 0) {
187 throw new IllegalArgumentException("Bad syntax: expected [part0][sep][part1]");
188 }
189 String svcName = uri.substring(idx1 + 1);
190 String nsUri = uri.substring(0, idx1);
191 return new String[] {nsUri, svcName };
192 }
193
194 }