WSDL 策略样本

WS-I WSDL 确认策略确保 WSDL 符合 WS-I 构面。PolicyGovernor 调用的每个策略与此策略相似。 WS-I 的某个构面是针对操作的参数名称具有正确的名称和绑定。在此示例中,策略确保 "XX" 列于 <wsdl:input name="XXgoodname"... 之前。
有关创建定制策略的指示信息,请参阅开发定制策略
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;
	}

以下方法返回此策略的唯一标识:

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

以下方法返回将显示给用户的名称:

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

以下方法返回策略的控制器:

public PolicyGovernor getPolicyGovernor() {
	return fGovernor;
}

使用此定制策略时,可以使用以下代码输入逻辑来描述验证资产所要针对的内容:

public Result test() {
结果对象通过使用以下语法来返回策略一致性测试的总体状态:
Result status = new Result(this)

您可以使用策略上下文 GetRAMAsset 方法,然后使用客户端 API 以获取对资产的所有部分的访问权。工件对象是资产中所有工件的列表:

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

以下代码验证工件:

	if (artifacts != null) { 
validateArtifacts(status, artifacts); 
}		
如果没有任何工件包含错误,请设置整体一致性消息:
if(status.getReturnCode() != ResultDetail.ERROR){
	status.setMessage("You comply with the WS-I standards");
}
如果工件包含错误,请显示失败消息:
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) {}
			}
		}			
	}
以下代码对资产中的所有工件运行验证测试。它通过确保文件名以 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 {

以下代码关闭输入流:

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

反馈