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;
	}

다음 메소드는 이 정책의 고유 ID를 리턴합니다.

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 {
								query.setMaxResults(maxResults);
								} 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)
										query.setMaxResults(maxResults);
												if (is != null)
										query.setMaxResults(maxResults);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
		public ConfigurationDetails[] getConfigurationDetails(Locale locale) {
				return null;
	}
}

피드백