更新于 

FGDCChecker

FGDC介绍

FGDC,即美国联邦地理数据委员会(FederalGeodata Commission),其提供了服务状态检查器(Service Status Checker ,SSC)来验证、测试和评分地理空间 Web 服务。

在这里,我们使用liveTest API来测试我们监测的服务状态,作为综合评分的参考。

Supported API Method: GET
Request Format: https://statuschecker.fgdc.gov/api/v2/liveTest?auth=&type=&url=&[other = pairs]

结果解析

设置返回格式为XML,使用jdom2进行解析,在pom.xml中引入依赖:

1
2
3
4
5
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
JDOM操作xml常用类

Document:表示整个xml文档,是一个树形结构
Eelment:表示一个xml的元素,提供方法操作其子元素,如文本,属性和名称空间等
Attribute:表示元素包含的属性
Text:表示xml文本信息
XMLOutputter:xml输出流,底层是通过JDK中流实现
Format:提供xml文件输出的编码、样式和排版等设置

一个示例返回文档如下:

可以发现,我们关心的属性位于response/service/summary/scoredTest/performance下,相应的解析代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SAXBuilder parser = new SAXBuilder();
Document FGDCDoc = parser.build(FGDCRequestURL);
Element RootEle = FGDCDoc.getRootElement();
Element Service = RootEle.getChild("service", RootEle.getNamespace());
Element summary = Service.getChild("summary", RootEle.getNamespace());
Element scoredTest = summary.getChild("scoredTest", RootEle.getNamespace());
List<Element> performance = scoredTest.getChildren("performance", RootEle.getNamespace());
fp.setFGDC_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
Element currentScore = performance.get(1);
String scoreString = currentScore.getValue();
fp.setFGDC_score(Float.parseFloat(scoreString));
Element currentSpeed = performance.get(0);
String currentSpeedString = currentSpeed.getValue();
fp.setFGDC_speed(Float.parseFloat(currentSpeedString));

其中,FGDC_score为FGDC基于web服务 速度性能响应正确性 所给出的综合打分(百分制),FGDC_speed为测试所用耗时(秒)

同时,FGDC还进行了一系列测试,将测试成功与否存于路径response/service/test下,解析代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//<!-- 0 = Failed, 1 = Success, 2 = IP banned - Not tested and -99.99 = Not tested -->
List<Element> testList = Service.getChildren("test", RootEle.getNamespace());
for (Element element : testList) {
if (element.getAttributeValue("type").compareTo("httpServer") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setHttpServerSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("getCapabilities") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGetCapabilitiesSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("generatedGetCapabilities") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGeneratedGetCapabilitiesSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("getMap") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGetMapSuccess(Float.parseFloat(output.getValue()));
}
}

详细代码

FGDCChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@NoArgsConstructor
@AllArgsConstructor
public class FGDCChecker {
private String URL;

public FGDCPerformance GetFGDCPerformance(){
FGDCPerformance fp = new FGDCPerformance();
String FGDCRequestURL = "https://statuschecker.fgdc.gov/api/v2/liveTest?auth={yourKey}&type=WMS&url=" + this.URL + "&formattype=XML";
try {
SAXBuilder parser = new SAXBuilder();
Document FGDCDoc = parser.build(FGDCRequestURL);
Element RootEle = FGDCDoc.getRootElement();
Element Service = RootEle.getChild("service", RootEle.getNamespace());
Element summary = Service.getChild("summary", RootEle.getNamespace());
Element scoredTest = summary.getChild("scoredTest", RootEle.getNamespace());
List<Element> performance = scoredTest.getChildren("performance", RootEle.getNamespace());

fp.setFGDC_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));

Element currentScore = performance.get(1);
String scoreString = currentScore.getValue();
fp.setFGDC_score(Float.parseFloat(scoreString));

Element currentSpeed = performance.get(0);
String currentSpeedString = currentSpeed.getValue();
fp.setFGDC_speed(Float.parseFloat(currentSpeedString));

// <!-- 0 = Failed, 1 = Success, 2 = IP banned - Not tested and -99.99 = Not tested -->
List<Element> testList = Service.getChildren("test", RootEle.getNamespace());
for (Element element : testList) {
if (element.getAttributeValue("type").compareTo("httpServer") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setHttpServerSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("getCapabilities") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGetCapabilitiesSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("generatedGetCapabilities") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGeneratedGetCapabilitiesSuccess(Float.parseFloat(output.getValue()));
}
else if (element.getAttributeValue("type").compareTo("getMap") == 0) {
Element output = element.getChild("output", RootEle.getNamespace());
fp.setGetMapSuccess(Float.parseFloat(output.getValue()));
}
}
return fp;
} catch (Exception e) {
fp.setFGDC_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
fp.setFGDC_score(-1);
fp.setFGDC_speed(-1);
fp.setHttpServerSuccess(-1);
fp.setGetCapabilitiesSuccess(-1);
fp.setGeneratedGetCapabilitiesSuccess(-1);
fp.setGetMapSuccess(-1);
return fp;
}
}
}