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.util;
021:
022: import org.apache.axiom.om.util.UUIDGenerator;
023: import org.apache.axiom.soap.SOAPEnvelope;
024: import org.apache.axiom.soap.SOAPFault;
025: import org.apache.axis2.AxisFault;
026: import org.apache.axis2.Constants;
027: import org.apache.axis2.context.ConfigurationContext;
028: import org.apache.axis2.context.ConfigurationContextFactory;
029: import org.apache.axis2.context.MessageContext;
030: import org.apache.axis2.context.ServiceContext;
031: import org.apache.axis2.context.ServiceGroupContext;
032: import org.apache.axis2.description.AxisModule;
033: import org.apache.axis2.description.AxisOperation;
034: import org.apache.axis2.description.AxisService;
035: import org.apache.axis2.description.AxisServiceGroup;
036: import org.apache.axis2.description.Flow;
037: import org.apache.axis2.description.HandlerDescription;
038: import org.apache.axis2.description.InOnlyAxisOperation;
039: import org.apache.axis2.description.InOutAxisOperation;
040: import org.apache.axis2.description.OutInAxisOperation;
041: import org.apache.axis2.description.Parameter;
042: import org.apache.axis2.description.PhaseRule;
043: import org.apache.axis2.description.WSDL2Constants;
044: import org.apache.axis2.engine.AxisConfiguration;
045: import org.apache.axis2.engine.AxisError;
046: import org.apache.axis2.engine.Handler;
047: import org.apache.axis2.engine.MessageReceiver;
048: import org.apache.axis2.i18n.Messages;
049: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
050: import org.apache.axis2.wsdl.WSDLConstants;
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: import javax.xml.namespace.QName;
055: import java.io.File;
056: import java.util.HashMap;
057: import java.util.Iterator;
058:
059: public class Utils {
060: private static final Log log = LogFactory.getLog(Utils.class);
061:
062: public static void addHandler(Flow flow, Handler handler,
063: String phaseName) {
064: HandlerDescription handlerDesc = new HandlerDescription(handler
065: .getName());
066: PhaseRule rule = new PhaseRule(phaseName);
067:
068: handlerDesc.setRules(rule);
069: handler.init(handlerDesc);
070: handlerDesc.setHandler(handler);
071: flow.addHandler(handlerDesc);
072: }
073:
074: /**
075: * @see org.apache.axis2.util.MessageContextBuilder:createOutMessageContext()
076: * @deprecated (post1.1branch)
077: */
078: public static MessageContext createOutMessageContext(
079: MessageContext inMessageContext) throws AxisFault {
080: return MessageContextBuilder
081: .createOutMessageContext(inMessageContext);
082: }
083:
084: public static AxisService createSimpleService(QName serviceName,
085: String className, QName opName) throws AxisFault {
086: return createSimpleService(serviceName,
087: new RawXMLINOutMessageReceiver(), className, opName);
088: }
089:
090: public static AxisService createSimpleServiceforClient(
091: QName serviceName, String className, QName opName)
092: throws AxisFault {
093: return createSimpleServiceforClient(serviceName,
094: new RawXMLINOutMessageReceiver(), className, opName);
095: }
096:
097: public static AxisService createSimpleInOnlyService(
098: QName serviceName, MessageReceiver messageReceiver,
099: QName opName) throws AxisFault {
100: AxisService service = new AxisService(serviceName
101: .getLocalPart());
102: service.setClassLoader(Thread.currentThread()
103: .getContextClassLoader());
104:
105: AxisOperation axisOp = new InOnlyAxisOperation(opName);
106:
107: axisOp.setMessageReceiver(messageReceiver);
108: axisOp.setStyle(WSDLConstants.STYLE_RPC);
109: service.addOperation(axisOp);
110: service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI
111: + "/" + opName.getLocalPart(), axisOp);
112:
113: return service;
114: }
115:
116: public static AxisService createSimpleService(QName serviceName,
117: MessageReceiver messageReceiver, String className,
118: QName opName) throws AxisFault {
119: AxisService service = new AxisService(serviceName
120: .getLocalPart());
121:
122: service.setClassLoader(Thread.currentThread()
123: .getContextClassLoader());
124: service.addParameter(new Parameter(Constants.SERVICE_CLASS,
125: className));
126:
127: AxisOperation axisOp = new InOutAxisOperation(opName);
128:
129: axisOp.setMessageReceiver(messageReceiver);
130: axisOp.setStyle(WSDLConstants.STYLE_RPC);
131: service.addOperation(axisOp);
132: service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI
133: + "/" + opName.getLocalPart(), axisOp);
134:
135: return service;
136: }
137:
138: public static AxisService createSimpleServiceforClient(
139: QName serviceName, MessageReceiver messageReceiver,
140: String className, QName opName) throws AxisFault {
141: AxisService service = new AxisService(serviceName
142: .getLocalPart());
143:
144: service.setClassLoader(Thread.currentThread()
145: .getContextClassLoader());
146: service.addParameter(new Parameter(Constants.SERVICE_CLASS,
147: className));
148:
149: AxisOperation axisOp = new OutInAxisOperation(opName);
150:
151: axisOp.setMessageReceiver(messageReceiver);
152: axisOp.setStyle(WSDLConstants.STYLE_RPC);
153: service.addOperation(axisOp);
154:
155: return service;
156: }
157:
158: public static ServiceContext fillContextInformation(
159: AxisService axisService,
160: ConfigurationContext configurationContext) throws AxisFault {
161:
162: // 2. if null, create new opCtxt
163: // fill the service group context and service context info
164: return fillServiceContextAndServiceGroupContext(axisService,
165: configurationContext);
166: }
167:
168: private static ServiceContext fillServiceContextAndServiceGroupContext(
169: AxisService axisService,
170: ConfigurationContext configurationContext) throws AxisFault {
171: String serviceGroupContextId = UUIDGenerator.getUUID();
172: ServiceGroupContext serviceGroupContext = configurationContext
173: .createServiceGroupContext(axisService
174: .getAxisServiceGroup());
175:
176: serviceGroupContext.setId(serviceGroupContextId);
177: configurationContext
178: .addServiceGroupContextIntoSoapSessionTable(serviceGroupContext);
179: return serviceGroupContext.getServiceContext(axisService);
180: }
181:
182: /**
183: * Break a full path into pieces
184: *
185: * @return an array where element [0] always contains the service, and element 1, if not null, contains
186: * the path after the first element. all ? parameters are discarded.
187: */
188: public static String[] parseRequestURLForServiceAndOperation(
189: String path, String servicePath) {
190: if (log.isDebugEnabled()) {
191: log.debug("parseRequestURLForServiceAndOperation : ["
192: + path + "][" + servicePath + "]");
193: }
194: if (path == null) {
195: return null;
196: }
197: String[] values = new String[2];
198:
199: // TODO. This is kind of brittle. Any service with the name /services would cause fun.
200: int index = path.lastIndexOf(servicePath);
201: String service;
202:
203: if (-1 != index) {
204: int serviceStart = index + servicePath.length();
205:
206: if (path.length() > serviceStart + 1) {
207: service = path.substring(serviceStart + 1);
208:
209: int queryIndex = service.indexOf('?');
210:
211: if (queryIndex > 0) {
212: service = service.substring(0, queryIndex);
213: }
214:
215: int operationIndex = service.indexOf('/');
216:
217: if (operationIndex > 0) {
218: values[0] = service.substring(0, operationIndex);
219: values[1] = service.substring(operationIndex + 1);
220: operationIndex = values[1].lastIndexOf('/');
221: if (operationIndex > 0) {
222: values[1] = values[1]
223: .substring(operationIndex + 1);
224: }
225: } else {
226: values[0] = service;
227: }
228: }
229: } else {
230: log.info("Unable to parse request URL [" + path + "]["
231: + servicePath + "]");
232: }
233:
234: return values;
235: }
236:
237: public static ConfigurationContext getNewConfigurationContext(
238: String repositry) throws Exception {
239: File file = new File(repositry);
240: if (!file.exists()) {
241: throw new Exception("repository directory "
242: + file.getAbsolutePath() + " does not exists");
243: }
244: File axis2xml = new File(file, "axis.xml");
245: String axis2xmlString = null;
246: if (axis2xml.exists()) {
247: axis2xmlString = axis2xml.getName();
248: }
249: return ConfigurationContextFactory
250: .createConfigurationContextFromFileSystem(file
251: .getAbsolutePath(), axis2xmlString);
252: }
253:
254: public static String getParameterValue(Parameter param) {
255: if (param == null) {
256: return null;
257: } else {
258: return (String) param.getValue();
259: }
260: }
261:
262: /**
263: * Get the name of the module , where archive name is combination of module name + its version
264: * The format of the name is as follows:
265: * moduleName-00.0000
266: * Example: "addressing-01.0001.mar" would return "addressing"
267: *
268: * @param moduleName the name of the module archive
269: * @return the module name parsed out of the file name
270: */
271: public static String getModuleName(String moduleName) {
272: if (moduleName.endsWith("-SNAPSHOT")) {
273: return moduleName.substring(0, moduleName
274: .indexOf("-SNAPSHOT"));
275: }
276: char delimiter = '-';
277: int version_index = moduleName.lastIndexOf(delimiter);
278: if (version_index > 0) {
279: String versionString = getModuleVersion(moduleName);
280: if (versionString == null) {
281: return moduleName;
282: } else {
283: return moduleName.substring(0, version_index);
284: }
285: } else {
286: return moduleName;
287: }
288: }
289:
290: public static String getModuleVersion(String moduleName) {
291: if (moduleName.endsWith("-SNAPSHOT")) {
292: return "SNAPSHOT";
293: }
294: char version_seperator = '-';
295: int version_index = moduleName.lastIndexOf(version_seperator);
296: if (version_index > 0) {
297: String versionString = moduleName.substring(
298: version_index + 1, moduleName.length());
299: try {
300: Float.parseFloat(versionString);
301: return versionString;
302: } catch (NumberFormatException e) {
303: return null;
304: }
305: } else {
306: return null;
307: }
308: }
309:
310: public static String getModuleName(String moduleName,
311: String moduleVersion) {
312: if (moduleVersion != null && moduleVersion.length() != 0) {
313: moduleName = moduleName + "-" + moduleVersion;
314: }
315: return moduleName;
316: }
317:
318: /**
319: * - if he trying to engage the same module then method will returen false
320: * - else it will return true
321: *
322: */
323: public static boolean checkVersion(String module1version,
324: String module2version) throws AxisFault {
325: if ((module1version != null && !module1version
326: .equals(module2version))
327: || module2version != null
328: && !module2version.equals(module1version)) {
329: throw new AxisFault(
330: "trying to engage two different module versions "
331: + module1version + " : " + module2version);
332: }
333: return true;
334: }
335:
336: public static void calculateDefaultModuleVersion(HashMap modules,
337: AxisConfiguration axisConfig) {
338: Iterator allModules = modules.values().iterator();
339: HashMap defaultModules = new HashMap();
340: while (allModules.hasNext()) {
341: AxisModule axisModule = (AxisModule) allModules.next();
342: String moduleName = axisModule.getName();
343: String moduleNameString;
344: String moduleVersionString;
345: if (AxisModule.VERSION_SNAPSHOT.equals(axisModule
346: .getVersion())) {
347: moduleNameString = axisModule.getName();
348: moduleVersionString = axisModule.getVersion();
349: } else {
350: if (axisModule.getVersion() == null) {
351: moduleNameString = getModuleName(moduleName);
352: moduleVersionString = getModuleVersion(moduleName);
353: if (moduleVersionString != null) {
354: try {
355: Float.valueOf(moduleVersionString);
356: axisModule.setVersion(moduleVersionString);
357: axisModule.setName(moduleName);
358: } catch (NumberFormatException e) {
359: moduleVersionString = null;
360: }
361: }
362: } else {
363: moduleNameString = axisModule.getName();
364: moduleVersionString = axisModule.getVersion();
365: }
366: }
367: String currentDefaultVerison = (String) defaultModules
368: .get(moduleNameString);
369: if (currentDefaultVerison != null) {
370: // if the module version is null then , that will be ignore in this case
371: if (!AxisModule.VERSION_SNAPSHOT
372: .equals(currentDefaultVerison)) {
373: if (moduleVersionString != null
374: && isLatest(moduleVersionString,
375: currentDefaultVerison)) {
376: defaultModules.put(moduleNameString,
377: moduleVersionString);
378: }
379: }
380: } else {
381: defaultModules.put(moduleNameString,
382: moduleVersionString);
383: }
384:
385: }
386: Iterator def_mod_itr = defaultModules.keySet().iterator();
387: while (def_mod_itr.hasNext()) {
388: String moduleName = (String) def_mod_itr.next();
389: axisConfig.addDefaultModuleVersion(moduleName,
390: (String) defaultModules.get(moduleName));
391: }
392: }
393:
394: public static boolean isLatest(String moduleVersion,
395: String currentDefaultVersion) {
396: if (AxisModule.VERSION_SNAPSHOT.equals(moduleVersion)) {
397: return true;
398: } else {
399: float m_version = Float.parseFloat(moduleVersion);
400: float m_c_vresion = Float.parseFloat(currentDefaultVersion);
401: return m_version > m_c_vresion;
402: }
403: }
404:
405: /**
406: * Check if a MessageContext property is true.
407: *
408: * @param messageContext the MessageContext
409: * @param propertyName the property name
410: * @return true if the property is Boolean.TRUE, "true", 1, etc. or false otherwise
411: * @deprecated please use MessageContext.isTrue(propertyName) instead
412: */
413: public static boolean isExplicitlyTrue(
414: MessageContext messageContext, String propertyName) {
415: Object flag = messageContext.getProperty(propertyName);
416: return JavaUtils.isTrueExplicitly(flag);
417: }
418:
419: /**
420: * Maps the String URI of the Message exchange pattern to a integer.
421: * Further, in the first lookup, it will cache the looked
422: * up value so that the subsequent method calls are extremely efficient.
423: */
424: public static int getAxisSpecifMEPConstant(
425: String messageExchangePattern) {
426:
427: int mepConstant = WSDLConstants.MEP_CONSTANT_INVALID;
428:
429: if (WSDL2Constants.MEP_URI_IN_OUT
430: .equals(messageExchangePattern)
431: || WSDLConstants.WSDL20_2006Constants.MEP_URI_IN_OUT
432: .equals(messageExchangePattern)
433: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_IN_OUT
434: .equals(messageExchangePattern)) {
435: mepConstant = WSDLConstants.MEP_CONSTANT_IN_OUT;
436: } else if (WSDL2Constants.MEP_URI_IN_ONLY
437: .equals(messageExchangePattern)
438: || WSDLConstants.WSDL20_2006Constants.MEP_URI_IN_ONLY
439: .equals(messageExchangePattern)
440: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_IN_ONLY
441: .equals(messageExchangePattern)) {
442: mepConstant = WSDLConstants.MEP_CONSTANT_IN_ONLY;
443: } else if (WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT
444: .equals(messageExchangePattern)
445: || WSDLConstants.WSDL20_2006Constants.MEP_URI_IN_OPTIONAL_OUT
446: .equals(messageExchangePattern)
447: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_IN_OPTIONAL_OUT
448: .equals(messageExchangePattern)) {
449: mepConstant = WSDLConstants.MEP_CONSTANT_IN_OPTIONAL_OUT;
450: } else if (WSDL2Constants.MEP_URI_OUT_IN
451: .equals(messageExchangePattern)
452: || WSDLConstants.WSDL20_2006Constants.MEP_URI_OUT_IN
453: .equals(messageExchangePattern)
454: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_OUT_IN
455: .equals(messageExchangePattern)) {
456: mepConstant = WSDLConstants.MEP_CONSTANT_OUT_IN;
457: } else if (WSDL2Constants.MEP_URI_OUT_ONLY
458: .equals(messageExchangePattern)
459: || WSDLConstants.WSDL20_2006Constants.MEP_URI_OUT_ONLY
460: .equals(messageExchangePattern)
461: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_OUT_ONLY
462: .equals(messageExchangePattern)) {
463: mepConstant = WSDLConstants.MEP_CONSTANT_OUT_ONLY;
464: } else if (WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN
465: .equals(messageExchangePattern)
466: || WSDLConstants.WSDL20_2006Constants.MEP_URI_OUT_OPTIONAL_IN
467: .equals(messageExchangePattern)
468: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_OUT_OPTIONAL_IN
469: .equals(messageExchangePattern)) {
470: mepConstant = WSDLConstants.MEP_CONSTANT_OUT_OPTIONAL_IN;
471: } else if (WSDL2Constants.MEP_URI_ROBUST_IN_ONLY
472: .equals(messageExchangePattern)
473: || WSDLConstants.WSDL20_2006Constants.MEP_URI_ROBUST_IN_ONLY
474: .equals(messageExchangePattern)
475: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_ROBUST_IN_ONLY
476: .equals(messageExchangePattern)) {
477: mepConstant = WSDLConstants.MEP_CONSTANT_ROBUST_IN_ONLY;
478: } else if (WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY
479: .equals(messageExchangePattern)
480: || WSDLConstants.WSDL20_2006Constants.MEP_URI_ROBUST_OUT_ONLY
481: .equals(messageExchangePattern)
482: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_ROBUST_OUT_ONLY
483: .equals(messageExchangePattern)) {
484: mepConstant = WSDLConstants.MEP_CONSTANT_ROBUST_OUT_ONLY;
485: }
486:
487: if (mepConstant == WSDLConstants.MEP_CONSTANT_INVALID) {
488: throw new AxisError(Messages.getMessage("mepmappingerror"));
489: }
490:
491: return mepConstant;
492: }
493:
494: /**
495: * Get an AxisFault object to represent the SOAPFault in the SOAPEnvelope attached
496: * to the provided MessageContext. This first check for an already extracted AxisFault
497: * and otherwise does a simple extract.
498: * <p/>
499: * MUST NOT be passed a MessageContext which does not contain a SOAPFault
500: *
501: * @param messageContext
502: * @return
503: */
504: public static AxisFault getInboundFaultFromMessageContext(
505: MessageContext messageContext) {
506: // Get the fault if it's already been extracted by a handler
507: AxisFault result = (AxisFault) messageContext
508: .getProperty(Constants.INBOUND_FAULT_OVERRIDE);
509: // Else, extract it from the SOAPBody
510: if (result == null) {
511: SOAPEnvelope envelope = messageContext.getEnvelope();
512: if (envelope == null || envelope.getBody() == null
513: || envelope.getBody().getFault() == null) {
514: // Not going to be able to
515: throw new IllegalArgumentException(
516: "The MessageContext does not have an associated SOAPFault.");
517: }
518: SOAPFault soapFault = envelope.getBody().getFault();
519:
520: // The AxisFault returned needs to have the MessageContext set on it so that
521: // other programming models can potentially handle the fault with an
522: // alternate deserialization.
523: result = new AxisFault(soapFault, messageContext);
524: }
525: return result;
526: }
527: }
|