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) {}
}
}
}
/**
* 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;
}
}