WSDL policy sample

The WS-I WSDL validation policy ensures that the WSDL complies with the WS-I facets. Each policy that PolicyGovernor calls resembles this policy. One of the facets of WS-I is to have correct names and bindings for the argument name of the operation. In this example, the policy ensures that "XX" is listed in front of <wsdl:input name="XXgoodname"....
For instructions to create custom policies, see Developing custom policies.
package com.ibm.ram.extension.demo.policy;

import java.io.*;
import java.util.Locale;

import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.ram.client.*;
import com.ibm.ram.common.data.Artifact;
import com.ibm.ram.extension.ConfigurationDetails;
import com.ibm.ram.extension.PolicyGovernor;
import com.ibm.ram.policy.*;
public class WSIPolicy extends AssetPolicy {
	
	private MyGovernor fGovernor;
	
	private static String WSDL_MATCH0 = "<wsdl:input";
	private static String WSDL_MATCH1 = "<wsdl:message";
	private static String WSDL_MATCH2 = "name=\"";
	private static String WSDL_MATCH3 = "XX";

	public WSIPolicy(MyGovernor governor) {
		fGovernor = governor;
	}

The following method returns a unique ID for this policy:

public String getID() {
	return "WSDL_000001";
}

The following method returns the name that will be displayed to the user:

public String getName(Locale locale) {
	return "Interoperable Web Services (WS-I)";
}
public String getDescription(Locale locale) {
	return "Ensuring Interoperable Web Services - DEMO";
}

The following method returns the governor for the policy:

public PolicyGovernor getPolicyGovernor() {
	return fGovernor;
}

You can use the following code to enter the logic that describes what to validate assets against when you use this custom policy:

public Result test() {
The result object returns the overall status of the policy compliance test by using the following syntax:
Result status = new Result(this)

You can use the policy context GetRAMAsset method and then the client APIs to gain access to all parts of an asset. The artifacts object is a list of all the artifacts in the asset:

		RAMAsset asset = this.getPolicyContext().getRAMAsset();
		RAMFolderArtifact folderArtifact = (RAMFolderArtifact)asset.getArtifactsRoot();
		Artifact[] artifacts = folderArtifact.computeArtifactsAsFlatList(new NullProgressMonitor());

The following code validates the artifacts:

	if (artifacts != null) { 
	validateArtifacts(status, artifacts); 
}		
If no artifacts contain errors, set an overall compliance message:
if(status.getReturnCode() != ResultDetail.ERROR){
	status.setMessage("You comply with the WS-I standards");
}
If artifacts contain errors, show a failure message:
else{
			status.setMessage("<font color=\"red\">You do not comply with the WS-I standards</font> To find out more check out our <a href=\"www.google.com\">corporate standard guidlines handbook.</a>");
		}

		return status;
	}
	private void validateWSDL(Result status, InputStream is, Artifact artifact) {
		BufferedReader reader = null;
		try {
			if (is!=null) {
				int lineNo = 0;
				reader = new BufferedReader(new InputStreamReader(is));
				String line;
				StringBuffer message = new StringBuffer();
				while ((line = reader.readLine())!=null) {
					lineNo++;
					int idx = 0;
					while (idx>=0 && idx<line.length()) {
						int ori = idx;
						idx = line.indexOf(WSDL_MATCH0, idx);
						if (idx<0) {
							idx = line.indexOf(WSDL_MATCH1, ori);
						}
						if (idx>=0) {
							idx = line.indexOf(WSDL_MATCH2, idx);
							
							if (idx>=0) {
								idx+=WSDL_MATCH2.length();
								if (idx+WSDL_MATCH3.length()<line.length()) {
									if (line.substring(idx, idx+WSDL_MATCH3.length()).equalsIgnoreCase(WSDL_MATCH3)) {
										
										message.append("<a href=\"http://www.ws-i.org/Profiles/BasicProfile-1.0-2004-04-16.html\">All WSDLs must WS-I Compliant </a>: ");
										message.append (artifact.getName());
										message.append ("["+lineNo+"]");
										
										status.addDetail(new ArtifactResultDetail(
													artifact, 
													ResultDetail.ERROR,
													message.toString()));										
									}									
								}
								idx+=WSDL_MATCH3.length();
							}
						}
					}
					
				}
			}
		} 
		catch (Throwable e) {}
		finally {
			if (reader!=null) {
				try {
					reader.close();
				} catch (IOException e) {}
			}
		}			
	}
The following code runs the validation test on all the artifacts in the asset. It validates an artifact by ensuring that the file name ends in wsdl:
/**
	 * Runs validation on all the artifacts in the asset
	 */
	private void validateArtifacts(Result status, Artifact[] artifacts) {
		for (int i = 0; i < artifacts.length; i++) {
			Artifact artifact = artifacts[i];

			// Validate an XML artifact - make sure the file name end in XML
			if (artifact.getName() != null
					&& artifact.getName().endsWith(".wsdl")) {
				InputStream is = null;
				BufferedReader reader = null;
				try {
					if (artifact instanceof RAMArtifact) {
					    // Use the artifactAccessor to read the contents of an artifact
						is = ((RAMArtifact)artifact).downloadContents();
						if (is != null) {
							validateWSDL(status, is, artifact);
						}
					}
				} catch (Throwable e) {
					//print out any exception
					e.printStackTrace();
				} finally {

The following code closes the input stream:

					try {
						if (reader != null)
							reader.close();
						if (is != null)
							is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public ConfigurationDetails[] getConfigurationDetails(Locale locale) {
		return null;
	}
}

Feedback