|
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* yyyymmdd bug Email and other contact information |
| 11 |
* -------- -------- ----------------------------------------------------------- |
| 12 |
* 20070305 117034 makandre@ca.ibm.com - Andrew Mak, Web Services Explorer should support SOAP Headers |
| 13 |
*******************************************************************************/ |
| 14 |
|
| 15 |
package org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl; |
| 16 |
|
| 17 |
import java.net.URI; |
| 18 |
import java.net.URISyntaxException; |
| 19 |
import java.util.Hashtable; |
| 20 |
import java.util.Vector; |
| 21 |
|
| 22 |
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataException; |
| 23 |
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataParser; |
| 24 |
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.constants.FragmentConstants; |
| 25 |
import org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.IXSDFragment; |
| 26 |
import org.w3c.dom.Document; |
| 27 |
import org.w3c.dom.Element; |
| 28 |
|
| 29 |
/** |
| 30 |
* A SOAP header fragment, which is a wrapper around the fragment that represents the SOAP header content. |
| 31 |
*/ |
| 32 |
public class SOAPHeaderWrapperFragment extends XSDDelegationFragment { |
| 33 |
|
| 34 |
private static final String MUST_UNDERSTAND = "mustUnderstand"; |
| 35 |
private static final String ACTOR = "actor"; |
| 36 |
|
| 37 |
private boolean mustUnderstand = false; |
| 38 |
private String actor = ""; |
| 39 |
private boolean validActor = true; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
* |
| 44 |
* @param fragment The fragment that this SOAP header fragment wraps around. |
| 45 |
*/ |
| 46 |
public SOAPHeaderWrapperFragment(IXSDFragment fragment) { |
| 47 |
super(fragment.genID(), fragment.getName(), null); |
| 48 |
setXSDDelegationFragment(fragment); |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Retrieves the first element from a string array. |
| 53 |
*/ |
| 54 |
private String getFirstElement(String[] stringArray) { |
| 55 |
if (stringArray == null || stringArray.length == 0) |
| 56 |
return null; |
| 57 |
return stringArray[0]; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* Retrieves the first element from a vector. |
| 62 |
*/ |
| 63 |
private String getFirstElement(Vector vector) { |
| 64 |
if (vector == null || vector.isEmpty()) |
| 65 |
return null; |
| 66 |
|
| 67 |
Object obj = vector.firstElement(); |
| 68 |
if (!(obj instanceof String)) |
| 69 |
return null; |
| 70 |
|
| 71 |
return (String) obj; |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Sets the mustUnderstand value. Any non-empty string (except "0") |
| 76 |
* translates to a mustUnderstand value of true. |
| 77 |
*/ |
| 78 |
private void setMustUnderstand(String param) { |
| 79 |
mustUnderstand = (param != null && !param.equals("0")); |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Sets the actor value. Also sets the flag for validActor. |
| 84 |
*/ |
| 85 |
private void setActor(String param) { |
| 86 |
if (param == null || param.length() == 0) |
| 87 |
actor = ""; |
| 88 |
else { |
| 89 |
actor = param; |
| 90 |
try { |
| 91 |
new URI(actor); |
| 92 |
} |
| 93 |
catch (URISyntaxException e) { |
| 94 |
validActor = false; |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
validActor = true; |
| 100 |
} |
| 101 |
|
| 102 |
/* (non-Javadoc) |
| 103 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#processParameterValues(org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataParser) |
| 104 |
*/ |
| 105 |
public boolean processParameterValues(MultipartFormDataParser parser) throws MultipartFormDataException { |
| 106 |
|
| 107 |
setMustUnderstand(parser.getParameter(getMustUnderstandID())); |
| 108 |
setActor(parser.getParameter(getActorID())); |
| 109 |
|
| 110 |
// mustUnderstand is either set or unset, no further validation necessary |
| 111 |
// only need to validate actor URI |
| 112 |
return super.processParameterValues(parser) && validateActor(); |
| 113 |
} |
| 114 |
|
| 115 |
/* (non-Javadoc) |
| 116 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#setParameterValues(java.lang.String, java.lang.String[]) |
| 117 |
*/ |
| 118 |
public void setParameterValues(String paramKey, String[] params) { |
| 119 |
if (getMustUnderstandID().equals(paramKey)) |
| 120 |
setMustUnderstand(getFirstElement(params)); |
| 121 |
else if (getActorID().equals(paramKey)) |
| 122 |
setActor(getFirstElement(params)); |
| 123 |
else |
| 124 |
super.setParameterValues(paramKey, params); |
| 125 |
} |
| 126 |
|
| 127 |
/* (non-Javadoc) |
| 128 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#setParameterValues(java.lang.String, java.util.Vector) |
| 129 |
*/ |
| 130 |
public void setParameterValues(String paramKey, Vector params) { |
| 131 |
if (getMustUnderstandID().equals(paramKey)) |
| 132 |
setMustUnderstand(getFirstElement(params)); |
| 133 |
else if (getActorID().equals(paramKey)) |
| 134 |
setActor(getFirstElement(params)); |
| 135 |
else |
| 136 |
super.setParameterValues(paramKey, params); |
| 137 |
} |
| 138 |
|
| 139 |
/* (non-Javadoc) |
| 140 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#getParameterValues(java.lang.String) |
| 141 |
*/ |
| 142 |
public String[] getParameterValues(String paramKey) { |
| 143 |
if (getMustUnderstandID().equals(paramKey)) |
| 144 |
return new String[] { mustUnderstand ? "1" : "0" }; |
| 145 |
else if (getActorID().equals(paramKey)) |
| 146 |
return new String[] { actor }; |
| 147 |
else |
| 148 |
return super.getParameterValues(paramKey); |
| 149 |
} |
| 150 |
|
| 151 |
/* (non-Javadoc) |
| 152 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#getParameterValue(java.lang.String, int) |
| 153 |
*/ |
| 154 |
public String getParameterValue(String paramKey, int paramIndex) { |
| 155 |
return getParameterValues(paramKey)[paramIndex]; |
| 156 |
} |
| 157 |
|
| 158 |
/* (non-Javadoc) |
| 159 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#validateAllParameterValues() |
| 160 |
*/ |
| 161 |
public boolean validateAllParameterValues() { |
| 162 |
return super.validateAllParameterValues() && validateActor(); |
| 163 |
} |
| 164 |
|
| 165 |
/* (non-Javadoc) |
| 166 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#validateParameterValues(java.lang.String) |
| 167 |
*/ |
| 168 |
public boolean validateParameterValues(String paramKey) { |
| 169 |
if (getMustUnderstandID().equals(paramKey)) |
| 170 |
return true; |
| 171 |
else if (getActorID().equals(paramKey)) |
| 172 |
return validateActor(); |
| 173 |
else |
| 174 |
return super.validateParameterValues(paramKey); |
| 175 |
} |
| 176 |
|
| 177 |
/* (non-Javadoc) |
| 178 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#validateParameterValue(java.lang.String, int) |
| 179 |
*/ |
| 180 |
public boolean validateParameterValue(String paramKey, int paramIndex) { |
| 181 |
if (getMustUnderstandID().equals(paramKey)) |
| 182 |
return true; |
| 183 |
else if (getActorID().equals(paramKey)) |
| 184 |
return validateActor(); |
| 185 |
else |
| 186 |
return super.validateParameterValue(paramKey, paramIndex); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets the values for this SOAP header given a hashtable of instance documents and a namespace table. |
| 191 |
* |
| 192 |
* @param instanceDocuments The hashtable of instance documents. The key is the document's name with namespace prefix. |
| 193 |
* @param namespaceTable The namespace table. |
| 194 |
* |
| 195 |
* @return True if all values extracted from the instance document are valid. |
| 196 |
*/ |
| 197 |
public boolean setParameterValuesFromInstanceDocuments(Hashtable instanceDocuments, Hashtable namespaceTable) { |
| 198 |
String tagName = ((XSDFragment) getXSDDelegationFragment()).getInstanceDocumentTagName(namespaceTable); |
| 199 |
Element instanceDocument = (Element) instanceDocuments.get(tagName); |
| 200 |
|
| 201 |
if (instanceDocument == null) |
| 202 |
return false; |
| 203 |
|
| 204 |
boolean valid = setParameterValuesFromInstanceDocuments(new Element[] { instanceDocument }); |
| 205 |
|
| 206 |
String prefix = getPrefixFromNamespaceURI(FragmentConstants.NS_URI_SOAP_ENV, namespaceTable) + FragmentConstants.COLON; |
| 207 |
|
| 208 |
String mustUnderstandValue = instanceDocument.getAttribute(prefix + MUST_UNDERSTAND); |
| 209 |
if ("".equals(mustUnderstandValue)) |
| 210 |
mustUnderstand = false; |
| 211 |
else if ("0".equals(mustUnderstandValue) || "1".equals(mustUnderstandValue)) |
| 212 |
setMustUnderstand(mustUnderstandValue); |
| 213 |
else |
| 214 |
valid = false; |
| 215 |
|
| 216 |
setActor(instanceDocument.getAttribute(prefix + ACTOR)); |
| 217 |
|
| 218 |
return valid && validateActor(); |
| 219 |
} |
| 220 |
|
| 221 |
/* (non-Javadoc) |
| 222 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#genInstanceDocumentsFromParameterValues(boolean, java.util.Hashtable, org.w3c.dom.Document) |
| 223 |
*/ |
| 224 |
public Element[] genInstanceDocumentsFromParameterValues(boolean genXSIType, Hashtable namespaceTable, Document doc) { |
| 225 |
Element[] elements = super.genInstanceDocumentsFromParameterValues(genXSIType, namespaceTable, doc); |
| 226 |
|
| 227 |
if (elements.length == 0) |
| 228 |
return elements; |
| 229 |
|
| 230 |
String prefix = getPrefixFromNamespaceURI(FragmentConstants.NS_URI_SOAP_ENV, namespaceTable) + FragmentConstants.COLON; |
| 231 |
|
| 232 |
for (int i = 0; i < elements.length; i++) { |
| 233 |
|
| 234 |
if (mustUnderstand) |
| 235 |
elements[i].setAttribute(prefix + MUST_UNDERSTAND, "1"); |
| 236 |
|
| 237 |
if (actor.length() > 0) |
| 238 |
elements[i].setAttribute(prefix + ACTOR, actor); |
| 239 |
} |
| 240 |
|
| 241 |
return elements; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Convenience method for getting for the mustUnderstand value |
| 246 |
* @return The mustUnderstand value, true if mustUnderstand="1", false otherwise. |
| 247 |
*/ |
| 248 |
public boolean isMustUnderstand() { |
| 249 |
return mustUnderstand; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Convenience method for getting the actor value |
| 254 |
* @return The actor value |
| 255 |
*/ |
| 256 |
public String getActor() { |
| 257 |
return actor; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Determines if the actor value is a valid URI. |
| 262 |
* @return True if the actor value is valid, false otherwise. |
| 263 |
*/ |
| 264 |
public boolean validateActor() { |
| 265 |
return validActor; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns the ID of the mustUnderstand input element |
| 270 |
* @return The ID of the mustUnderstand input element |
| 271 |
*/ |
| 272 |
public String getMustUnderstandID() { |
| 273 |
return getID() + FragmentConstants.ID_SEPERATOR + MUST_UNDERSTAND; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Returns the ID of the actor input element |
| 278 |
* @return The ID of the actor input element |
| 279 |
*/ |
| 280 |
public String getActorID() { |
| 281 |
return getID() + FragmentConstants.ID_SEPERATOR + ACTOR; |
| 282 |
} |
| 283 |
|
| 284 |
/* (non-Javadoc) |
| 285 |
* @see org.eclipse.wst.ws.internal.explorer.platform.wsdl.fragment.impl.XSDDelegationFragment#getWriteFragment() |
| 286 |
*/ |
| 287 |
public String getWriteFragment() { |
| 288 |
return "/wsdl/fragment/SOAPHeaderWrapperWFragmentJSP.jsp"; |
| 289 |
} |
| 290 |
} |