1 16 17 package org.apache.axis ; 18 19 import org.apache.axis.attachments.Attachments; 20 import org.apache.axis.client.AxisClient; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.description.OperationDesc; 23 import org.apache.axis.description.ServiceDesc; 24 import org.apache.axis.encoding.TypeMapping; 25 import org.apache.axis.encoding.TypeMappingRegistry; 26 import org.apache.axis.constants.Style; 27 import org.apache.axis.constants.Use; 28 import org.apache.axis.handlers.soap.SOAPService; 29 import org.apache.axis.schema.SchemaVersion; 30 import org.apache.axis.session.Session; 31 import org.apache.axis.soap.SOAPConstants; 32 import org.apache.axis.utils.JavaUtils; 33 import org.apache.axis.utils.LockableHashtable; 34 import org.apache.axis.utils.Messages; 35 import org.apache.commons.logging.Log; 36 37 import javax.xml.namespace.QName ; 38 import javax.xml.rpc.Call ; 39 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 40 import java.io.File ; 41 import java.util.ArrayList ; 42 import java.util.Hashtable ; 43 44 73 public class MessageContext implements SOAPMessageContext { 74 75 protected static Log log = 76 LogFactory.getLog(MessageContext.class.getName()); 77 78 83 private Message requestMessage; 84 85 90 private Message responseMessage; 91 92 96 private String targetService; 97 98 102 private String transportName; 103 104 107 private ClassLoader classLoader; 108 109 112 private AxisEngine axisEngine; 113 114 117 private Session session; 118 119 125 private boolean maintainSession = false; 126 127 134 private boolean havePassedPivot = false; 135 136 139 private int timeout = Constants.DEFAULT_MESSAGE_TIMEOUT; 140 141 146 private boolean highFidelity = true; 147 148 152 private LockableHashtable bag = new LockableHashtable(); 153 154 163 private String username = null; 164 private String password = null; 165 private String encodingStyle = Use.ENCODED.getEncoding(); 166 private boolean useSOAPAction = false; 167 private String SOAPActionURI = null; 168 169 172 private String [] roles; 173 174 175 private SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION; 176 177 178 private SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001; 179 180 181 private OperationDesc currentOperation = null; 182 183 188 public OperationDesc getOperation() 189 { 190 return currentOperation; 191 } 192 193 198 public void setOperation(OperationDesc operation) 199 { 200 currentOperation = operation; 201 } 202 203 214 public OperationDesc [] getPossibleOperationsByQName(QName qname) throws AxisFault 215 { 216 if (currentOperation != null) { 217 return new OperationDesc [] { currentOperation }; 218 } 219 220 OperationDesc [] possibleOperations = null; 221 222 if (serviceHandler == null) { 223 try { 224 if (log.isDebugEnabled()) { 225 log.debug(Messages.getMessage("dispatching00", 226 qname.getNamespaceURI())); 227 } 228 229 setService(axisEngine.getConfig(). 231 getServiceByNamespaceURI(qname.getNamespaceURI())); 232 } catch (ConfigurationException e) { 233 } 235 236 } 237 238 if (serviceHandler != null) { 239 ServiceDesc desc = serviceHandler.getInitializedServiceDesc(this); 240 241 if (desc != null) { 242 if (desc.getStyle() != Style.DOCUMENT) { 243 possibleOperations = desc.getOperationsByQName(qname); 244 } else { 245 ArrayList allOperations = desc.getOperations(); 249 ArrayList foundOperations = new ArrayList (); 250 for (int i=0; i < allOperations.size(); i++ ) { 251 OperationDesc tryOp = 252 (OperationDesc) allOperations.get(i); 253 if (tryOp.getParamByQName(qname) != null) { 254 foundOperations.add(tryOp); 255 } 256 } 257 if (foundOperations.size() > 0) { 258 possibleOperations = (OperationDesc[]) 259 JavaUtils.convert(foundOperations, 260 OperationDesc[].class); 261 } 262 } 263 } 264 } 265 return possibleOperations; 266 } 267 268 277 public OperationDesc getOperationByQName(QName qname) throws AxisFault 278 { 279 if (currentOperation == null) { 280 OperationDesc [] possibleOperations = getPossibleOperationsByQName(qname); 281 if (possibleOperations != null && possibleOperations.length > 0) { 282 currentOperation = possibleOperations[0]; 283 } 284 } 285 286 return currentOperation; 287 } 288 289 294 public static MessageContext getCurrentContext() { 295 return AxisEngine.getCurrentMessageContext(); 296 } 297 298 301 protected static String systemTempDir= null; 302 307 static { 308 try { 309 systemTempDir=AxisProperties.getProperty(AxisEngine.ENV_ATTACHMENT_DIR); 311 } catch(Throwable t) { 312 systemTempDir= null; 313 } 314 315 if(systemTempDir== null) { 316 try { 317 File tf= File.createTempFile("Axis", ".tmp"); 320 File dir= tf.getParentFile(); 321 if (tf.exists()) { 322 tf.delete(); 323 } 324 if (dir != null) { 325 systemTempDir= dir.getCanonicalPath(); 326 } 327 } catch(Throwable t) { 328 log.debug("Unable to find a temp dir with write access"); 329 systemTempDir= null; 330 } 331 } 332 } 333 334 340 public MessageContext(AxisEngine engine) { 341 this.axisEngine = engine; 342 343 if(null != engine){ 344 java.util.Hashtable opts= engine.getOptions(); 345 String attachmentsdir= null; 346 if(null!=opts) { 347 attachmentsdir= (String ) opts.get(AxisEngine.PROP_ATTACHMENT_DIR); 348 } 349 if(null == attachmentsdir) { 350 attachmentsdir= systemTempDir; 351 } 352 if(attachmentsdir != null){ 353 setProperty(ATTACHMENTS_DIR, attachmentsdir); 354 } 355 356 String defaultSOAPVersion = (String )engine.getOption( 359 AxisEngine.PROP_SOAP_VERSION); 360 if (defaultSOAPVersion != null && "1.2".equals(defaultSOAPVersion)) { 361 setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS); 362 } 363 364 String singleSOAPVersion = (String )engine.getOption( 365 AxisEngine.PROP_SOAP_ALLOWED_VERSION); 366 if (singleSOAPVersion != null) { 367 if ("1.2".equals(singleSOAPVersion)) { 368 setProperty(Constants.MC_SINGLE_SOAP_VERSION, 369 SOAPConstants.SOAP12_CONSTANTS); 370 } else if ("1.1".equals(singleSOAPVersion)) { 371 setProperty(Constants.MC_SINGLE_SOAP_VERSION, 372 SOAPConstants.SOAP11_CONSTANTS); 373 } 374 } 375 } 376 } 377 378 382 protected void finalize() { 383 dispose(); 384 } 385 386 390 private TypeMappingRegistry mappingRegistry = null; 391 392 398 public void setTypeMappingRegistry(TypeMappingRegistry reg) { 399 mappingRegistry = reg; 400 } 401 402 411 public TypeMappingRegistry getTypeMappingRegistry() { 412 if (mappingRegistry == null) { 413 return axisEngine.getTypeMappingRegistry(); 414 } 415 416 return mappingRegistry; 417 } 418 419 424 public TypeMapping getTypeMapping() 425 { 426 return (TypeMapping)getTypeMappingRegistry(). 427 getTypeMapping(encodingStyle); 428 } 429 430 435 public String getTransportName() 436 { 437 return transportName; 438 } 439 440 448 public void setTransportName(String transportName) 449 { 450 this.transportName = transportName; 451 } 452 453 458 public SOAPConstants getSOAPConstants() { 459 return soapConstants; 460 } 461 462 468 public void setSOAPConstants(SOAPConstants soapConstants) { 469 if (this.soapConstants.getEncodingURI().equals(encodingStyle)) { 472 encodingStyle = soapConstants.getEncodingURI(); 473 } 474 475 this.soapConstants = soapConstants; 476 } 477 478 483 public SchemaVersion getSchemaVersion() { 484 return schemaVersion; 485 } 486 487 492 public void setSchemaVersion(SchemaVersion schemaVersion) { 493 this.schemaVersion = schemaVersion; 494 } 495 496 501 public Session getSession() 502 { 503 return session; 504 } 505 506 511 public void setSession(Session session) 512 { 513 this.session = session; 514 } 515 516 521 public boolean isEncoded() { 522 return (getOperationUse() == Use.ENCODED); 523 } 525 526 531 public void setMaintainSession (boolean yesno) { 532 maintainSession = yesno; 533 } 534 535 541 public boolean getMaintainSession () { 542 return maintainSession; 543 } 544 545 550 public Message getRequestMessage() { 551 return requestMessage ; 552 } 553 554 560 public void setRequestMessage(Message reqMsg) { 561 requestMessage = reqMsg ; 562 if (requestMessage != null) { 563 requestMessage.setMessageContext(this); 564 } 565 } 566 567 572 public Message getResponseMessage() { return responseMessage ; } 573 574 580 public void setResponseMessage(Message respMsg) { 581 responseMessage = respMsg; 582 if (responseMessage != null) { 583 responseMessage.setMessageContext(this); 584 585 Message reqMsg = getRequestMessage(); 588 if (null != reqMsg) { 589 Attachments reqAttch = reqMsg.getAttachmentsImpl(); 590 Attachments respAttch = respMsg.getAttachmentsImpl(); 591 if (null != reqAttch && null != respAttch) { 592 if (respAttch.getSendType() == Attachments.SEND_TYPE_NOTSET) 593 respAttch.setSendType(reqAttch.getSendType()); 595 } 596 } 597 } 598 } 599 600 606 public Message getCurrentMessage() 607 { 608 return (havePassedPivot ? responseMessage : requestMessage); 609 } 610 611 618 public javax.xml.soap.SOAPMessage getMessage() { 619 return getCurrentMessage(); 620 } 621 622 628 public void setCurrentMessage(Message curMsg) 629 { 630 curMsg.setMessageContext(this); 631 632 if (havePassedPivot) { 633 responseMessage = curMsg; 634 } else { 635 requestMessage = curMsg; 636 } 637 } 638 639 647 public void setMessage(javax.xml.soap.SOAPMessage message) { 648 setCurrentMessage((Message)message); 649 } 650 651 656 public boolean getPastPivot() 657 { 658 return havePassedPivot; 659 } 660 661 670 public void setPastPivot(boolean pastPivot) 671 { 672 havePassedPivot = pastPivot; 673 } 674 675 680 public void setTimeout (int value) { 681 timeout = value; 682 } 683 684 689 public int getTimeout () { 690 return timeout; 691 } 692 693 699 public ClassLoader getClassLoader() { 700 if ( classLoader == null ) { 701 classLoader = Thread.currentThread().getContextClassLoader(); 702 } 703 return( classLoader ); 704 } 705 706 712 public void setClassLoader(ClassLoader cl ) { 713 classLoader = cl ; 714 } 715 716 721 public String getTargetService() { 722 return targetService; 723 } 724 725 731 public AxisEngine getAxisEngine() 732 { 733 return axisEngine; 734 } 735 736 747 public void setTargetService(String tServ) throws AxisFault { 748 log.debug("MessageContext: setTargetService(" + tServ+")"); 749 750 if (tServ == null) { 751 setService(null); 752 } 753 else { 754 try { 755 setService(getAxisEngine().getService(tServ)); 756 } catch (AxisFault fault) { 757 if (!isClient()) { 759 throw fault; 760 } 761 } 762 } 763 targetService = tServ; 764 } 765 766 770 private SOAPService serviceHandler ; 771 772 778 public SOAPService getService() { 779 return serviceHandler; 780 } 781 782 790 public void setService(SOAPService sh) throws AxisFault 791 { 792 log.debug("MessageContext: setServiceHandler("+sh+")"); 793 serviceHandler = sh; 794 if (sh != null) { 795 if(!sh.isRunning()) { 796 throw new AxisFault(Messages.getMessage("disabled00")); 797 } 798 targetService = sh.getName(); 799 SOAPService service = sh; 800 TypeMappingRegistry tmr = service.getTypeMappingRegistry(); 801 setTypeMappingRegistry(tmr); 802 803 setEncodingStyle(service.getUse().getEncoding()); 805 806 bag.setParent(sh.getOptions()); 809 810 highFidelity = service.needsHighFidelityRecording(); 814 815 service.getInitializedServiceDesc(this); 816 } 817 } 818 819 824 public boolean isClient() 825 { 826 return (axisEngine instanceof AxisClient); 827 } 828 829 838 public static final String ENGINE_HANDLER = "engine.handler"; 839 840 842 public static final String TRANS_URL = "transport.url"; 843 844 845 public static final String QUIT_REQUESTED = "quit.requested"; 846 847 848 public static final String AUTHUSER = "authenticatedUser"; 849 850 851 public static final String CALL = "call_object" ; 852 853 854 public static final String IS_MSG = "isMsg" ; 855 856 857 public static final String ATTACHMENTS_DIR = "attachments.directory" ; 858 859 862 public final static String ACCEPTMISSINGPARAMS = "acceptMissingParams"; 863 864 867 public static final String WSDLGEN_INTFNAMESPACE = "axis.wsdlgen.intfnamespace"; 868 869 873 public static final String WSDLGEN_SERV_LOC_URL = "axis.wsdlgen.serv.loc.url"; 874 875 883 public static final String HTTP_TRANSPORT_VERSION = "axis.transport.version"; 884 885 891 public static final String SECURITY_PROVIDER = "securityProvider"; 892 893 898 899 907 public String getStrProp(String propName) { 908 return (String ) getProperty(propName); 909 } 910 911 919 public boolean isPropertyTrue(String propName) { 920 return isPropertyTrue(propName, false); 921 } 922 923 942 public boolean isPropertyTrue(String propName, boolean defaultVal) { 943 return JavaUtils.isTrue(getProperty(propName), defaultVal); 944 } 945 946 959 public void setProperty(String name, Object value) { 960 if (name == null || value == null) { 961 return; 962 } 965 else if (name.equals(Call.USERNAME_PROPERTY)) { 966 if (!(value instanceof String )) { 967 throw new IllegalArgumentException ( 968 Messages.getMessage("badProp00", new String [] { 969 name, "java.lang.String", value.getClass().getName()})); 970 } 971 setUsername((String ) value); 972 } 973 else if (name.equals(Call.PASSWORD_PROPERTY)) { 974 if (!(value instanceof String )) { 975 throw new IllegalArgumentException ( 976 Messages.getMessage("badProp00", new String [] { 977 name, "java.lang.String", value.getClass().getName()})); 978 } 979 setPassword((String ) value); 980 } 981 else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY)) { 982 if (!(value instanceof Boolean )) { 983 throw new IllegalArgumentException ( 984 Messages.getMessage("badProp00", new String [] 985 {name, 986 "java.lang.Boolean", 987 value.getClass().getName()})); 988 } 989 setMaintainSession(((Boolean ) value).booleanValue()); 990 } 991 else if (name.equals(Call.SOAPACTION_USE_PROPERTY)) { 992 if (!(value instanceof Boolean )) { 993 throw new IllegalArgumentException ( 994 Messages.getMessage("badProp00", new String [] 995 {name, 996 "java.lang.Boolean", 997 value.getClass().getName()})); 998 } 999 setUseSOAPAction(((Boolean ) value).booleanValue()); 1000 } 1001 else if (name.equals(Call.SOAPACTION_URI_PROPERTY)) { 1002 if (!(value instanceof String )) { 1003 throw new IllegalArgumentException ( 1004 Messages.getMessage("badProp00", new String [] 1005 {name, 1006 "java.lang.String", 1007 value.getClass().getName()})); 1008 } 1009 setSOAPActionURI((String ) value); 1010 } 1011 else if (name.equals(Call.ENCODINGSTYLE_URI_PROPERTY)) { 1012 if (!(value instanceof String )) { 1013 throw new IllegalArgumentException ( 1014 Messages.getMessage("badProp00", new String [] 1015 {name, 1016 "java.lang.String", 1017 value.getClass().getName()})); 1018 } 1019 setEncodingStyle((String ) value); 1020 } 1021 else { 1022 bag.put(name, value); 1023 } 1024 } 1026 1032 public boolean containsProperty(String name) { 1033 Object propertyValue = getProperty(name); 1034 return (propertyValue != null); 1035 } 1036 1037 1043 public java.util.Iterator getPropertyNames() { 1044 return bag.keySet().iterator(); 1048 } 1049 1050 1055 public java.util.Iterator getAllPropertyNames() { 1056 return bag.getAllKeys().iterator(); 1057 } 1058 1059 1066 public Object getProperty(String name) { 1067 if (name != null) { 1068 if (name.equals(Call.USERNAME_PROPERTY)) { 1069 return getUsername(); 1070 } 1071 else if (name.equals(Call.PASSWORD_PROPERTY)) { 1072 return getPassword(); 1073 } 1074 else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY)) { 1075 return getMaintainSession() ? Boolean.TRUE : Boolean.FALSE; 1076 } 1077 else if (name.equals(Call.OPERATION_STYLE_PROPERTY)) { 1078 return (getOperationStyle() == null) ? null : getOperationStyle().getName(); 1079 } 1080 else if (name.equals(Call.SOAPACTION_USE_PROPERTY)) { 1081 return useSOAPAction() ? Boolean.TRUE : Boolean.FALSE; 1082 } 1083 else if (name.equals(Call.SOAPACTION_URI_PROPERTY)) { 1084 return getSOAPActionURI(); 1085 } 1086 else if (name.equals(Call.ENCODINGSTYLE_URI_PROPERTY)) { 1087 return getEncodingStyle(); 1088 } 1089 else if (bag == null) { 1090 return null; 1091 } 1092 else { 1093 return bag.get(name); 1094 } 1095 } 1096 else { 1097 return null; 1098 } 1099 } 1100 1101 1110 public void setPropertyParent(Hashtable parent) 1111 { 1112 bag.setParent(parent); 1113 } 1114 1115 1120 public void setUsername(String username) { 1121 this.username = username; 1122 } 1124 1129 public String getUsername() { 1130 return username; 1131 } 1133 1138 public void setPassword(String password) { 1139 this.password = password; 1140 } 1142 1147 public String getPassword() { 1148 return password; 1149 } 1151 1158 public Style getOperationStyle() { 1159 if (currentOperation != null) { 1160 return currentOperation.getStyle(); 1161 } 1162 1163 if (serviceHandler != null) { 1164 return serviceHandler.getStyle(); 1165 } 1166 1167 return Style.RPC; 1168 } 1170 1175 public Use getOperationUse() { 1176 if (currentOperation != null) { 1177 return currentOperation.getUse(); 1178 } 1179 1180 if (serviceHandler != null) { 1181 return serviceHandler.getUse(); 1182 } 1183 1184 return Use.ENCODED; 1185 } 1187 1196 public void setUseSOAPAction(boolean useSOAPAction) { 1197 this.useSOAPAction = useSOAPAction; 1198 } 1200 1207 public boolean useSOAPAction() { 1208 return useSOAPAction; 1209 } 1211 1222 public void setSOAPActionURI(String SOAPActionURI) 1223 throws IllegalArgumentException { 1224 this.SOAPActionURI = SOAPActionURI; 1225 } 1227 1232 public String getSOAPActionURI() { 1233 return SOAPActionURI; 1234 } 1236 1241 public void setEncodingStyle(String namespaceURI) { 1242 if (namespaceURI == null) { 1243 namespaceURI = Constants.URI_LITERAL_ENC; 1244 } 1245 else if (Constants.isSOAP_ENC(namespaceURI)) { 1246 namespaceURI = soapConstants.getEncodingURI(); 1247 } 1248 1249 encodingStyle = namespaceURI; 1250 } 1252 1258 public String getEncodingStyle() { 1259 return encodingStyle; 1260 } 1262 public void removeProperty(String propName) 1263 { 1264 if (bag != null) { 1265 bag.remove(propName); 1266 } 1267 } 1268 1269 1272 public void reset() 1273 { 1274 if (bag != null) { 1275 bag.clear(); 1276 } 1277 serviceHandler = null; 1278 havePassedPivot = false; 1279 currentOperation = null; 1280 } 1281 1282 1291 public boolean isHighFidelity() { 1292 return highFidelity; 1293 } 1294 1295 1302 public void setHighFidelity(boolean highFidelity) { 1303 this.highFidelity = highFidelity; 1304 } 1305 1306 1325 public String [] getRoles() { 1326 return roles; 1328 } 1329 1330 1338 public void setRoles( String [] roles) { 1339 this.roles = roles; 1340 } 1341 1342 1347 public synchronized void dispose() { 1348 log.debug("disposing of message context"); 1349 if(requestMessage!=null) { 1350 requestMessage.dispose(); 1351 requestMessage=null; 1352 } 1353 if(responseMessage!=null) { 1354 responseMessage.dispose(); 1355 responseMessage=null; 1356 } 1357 } 1358} 1359 | Popular Tags |