[28] Window PowerShell DSC 学习系列----如何用Java调用PowerShell DSC 5.x的oData服务
2017-05-21 21:38
891 查看
PowerShell DSC 5.x的Report是基于OData V3的标准,而这个标准是微软提出已经标准化的一个开放的工业标准。在我的另外一篇文章中也提到了有下面的几个Java的开源框架支持调用oData的服务。
Apache Olingo: V2 and V4
SDL OData 框架:V4
odata4j: V1-V3
ODataJClient:V3
Jello 框架: Jello 框架
<dependency>
<groupId>com.msopentech.odatajclient</groupId>
<artifactId>odatajclient-engine</artifactId>
<version>0.9.0</version>
</dependency>
当前的release版本是0.9.0,当前正在开发的版本是0.9.1-snapshot,感兴趣的朋友可以到下面的网站去Fork代码 https://github.com/mkostin/ODataJClient
Apache Maven >= 3.0.3
JDK >= 1.6.0-23
import java.util.List;
import org.junit.Test;
import com.msopentech.odatajclient.engine.communication.request.retrieve.ODataEntitySetRequest;
import com.msopentech.odatajclient.engine.communication.request.retrieve.ODataRetrieveRequestFactory;
import com.msopentech.odatajclient.engine.communication.response.ODataRetrieveResponse;
import com.msopentech.odatajclient.engine.data.ODataCollectionValue;
import com.msopentech.odatajclient.engine.data.ODataEntity;
import com.msopentech.odatajclient.engine.data.ODataEntitySet;
import com.msopentech.odatajclient.engine.data.ODataLink;
import com.msopentech.odatajclient.engine.data.ODataProperty;
import com.msopentech.odatajclient.engine.format.ODataPubFormat;
import com.msopentech.odatajclient.engine.uri.ODataURIBuilder;
public class RequestEntityTest {
String testODataServiceRootURL="http://dsc-server:8080/PSDSCPullServer.svc/";
@Test
public void testName() throws Exception {
ODataURIBuilder uriBuilder = new ODataURIBuilder(testODataServiceRootURL).appendEntityTypeSegment("Nodes(AgentId='304DC9EE-12D4-11E7-80B8-B9D2EE16DFG9')/Reports");
ODataEntitySetRequest req = ODataRetrieveRequestFactory.getEntitySetRequest(uriBuilder.build());
req.setFormat(ODataPubFormat.ATOM);
ODataRetrieveResponse<ODataEntitySet> res = req.execute();
res.getStatusCode();
res.getStatusMessage();
res.getContentType();
res.getEtag();
res.getHeaderNames();
res.getHeader("DataServiceVersion");
ODataEntitySet entitySet = res.getBody();
System.out.println(entitySet.getCount());
ODataEntity entity=entitySet.getEntities().get(0);
List<ODataLink> lsODataLink= entity.getAssociationLinks();
lsODataLink.forEach( ODataLink ->
{
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.println(ODataLink);
}
);
ODataProperty oDataPropertyJobID=entity.getProperty("JobId");
System.out.println(".........oDataPropertyJobID:"+oDataPropertyJobID.getValue());
List<ODataProperty> properties = entity.getProperties();
properties.forEach(oDataProperty ->{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(oDataProperty.getValue());
if( oDataProperty.hasCollectionValue()){
System.out.println("---hasCollectionValue---");
System.out.println(oDataProperty.getName());
ODataCollectionValue oDataCollectionValue= oDataProperty.getCollectionValue();
System.out.println(oDataCollectionValue.getTypeName());
System.out.println(oDataProperty.getPrimitiveValue());
}
if(oDataProperty.hasComplexValue()){
System.out.println("---hasComplexValue---");
System.out.println(oDataProperty.getName());
}
});
}
}
注意,下面的代码,没有和DSC Report OData直接相关,但是显示的是示范代码,调用过程类似,另外需要注意的是DSC Report的查询不支持OData的filter的语法,如果要让其支持,请自己定义实现:
appendEntityTypeSegment("Customer(1)");
ODataEntityRequest req = ODataRetrieveRequestFactory.getEntityRequest(uriBuilder.build());
req.setFormat(ODataPubFormat.JSON_FULL_METADATA);
ODataRetrieveResponse<ODataEntity> res = req.execute();
res.getStatusCode();
res.getStatusMessage();
res.getContentType();
res.getEtag();
res.getHeaderNames();
res.getHeader("DataServiceVersion");
ODataEntity entity = res.getBody();
URI editLink = entity.getEditLink();
List<ODataProperty> properties = entity.getProperties();
ODataProperty property = entity.getProperty("Id");
List<ODataLink> navLinks = getNavigationLinks();
List<ODataLink> assLinks = getAssociationLinks();
List<ODataLink> meLinks = getEditMediaLinks();
List<ODataOperation> operations = getOperations();
appendEntitySetSegment("Car").top(3);
ODataEntitySetRequest req = ODataRetrieveRequestFactory.getEntitySetRequest(uriBuilder.build());
ODataRetrieveResponse<ODataEntitySet> res = req.execute();
ODataEntitySet entitySet = res.getBody();
URI nextPageLink = getNext();
List<ODataEntity> entities = getEntities();
appendEntityTypeSegment("Car").filter("VIN lt 16");
ODataEntitySet result = ODataRetrieveRequestFactory.getEntitySetRequest(
uriBuilder.build()).execute().getBody();
具体的查询格式,请参考微软的一篇文章:https://msdn.microsoft.com/en-us/library/dd541437.aspx
// for username 'sampleUsername' and password ' samplePassword'
Configuration.setHttpClientFactory(new AbstractBasicAuthHttpClientFactory() {
@Override
protected String getUsername() {
return "sampleUsername";
}
@Override
protected String getPassword() {
return "samplePassword";
}
});
// perform any operation via ODataJClient: all requests will bear the BasicAuth HTTP header
// this restores the default unauthenticated behavior
Configuration.setHttpClientFactory(new DefaultHttpClientFactory());
http://blog.csdn.net/chancein007/article/details/54016636 https://github.com/mkostin/ODataJClient
Apache Olingo: V2 and V4
SDL OData 框架:V4
odata4j: V1-V3
ODataJClient:V3
Jello 框架: Jello 框架
1.介绍
因为PowerShell DSC 5.x的Report是基于OData V3的标准,所以理论上只有odata4j和ODataJClient的框架支持V3版本;但是实际上odata4j宣传支持,其实代码并没有实现。经过研究论证,只有ODataJClient能够支持PowerShell DSC 5.x的Report是基于OData V3的标准的调用。其核心Jar主要包括两个。2.Maven或者Gradle依赖
如果我们需要使用,则在我们的Gradle或者Maven文件里面,只需要引入odatajclient-engine,odatajclient-engine-xml会自动引入。<dependency>
<groupId>com.msopentech.odatajclient</groupId>
<artifactId>odatajclient-engine</artifactId>
<version>0.9.0</version>
</dependency>
当前的release版本是0.9.0,当前正在开发的版本是0.9.1-snapshot,感兴趣的朋友可以到下面的网站去Fork代码 https://github.com/mkostin/ODataJClient
3.使用的前提条件
注意使用的前提条件是:Apache Maven >= 3.0.3
JDK >= 1.6.0-23
4.代码例子
下面给出1个调用的例子代码,import java.util.List;
import org.junit.Test;
import com.msopentech.odatajclient.engine.communication.request.retrieve.ODataEntitySetRequest;
import com.msopentech.odatajclient.engine.communication.request.retrieve.ODataRetrieveRequestFactory;
import com.msopentech.odatajclient.engine.communication.response.ODataRetrieveResponse;
import com.msopentech.odatajclient.engine.data.ODataCollectionValue;
import com.msopentech.odatajclient.engine.data.ODataEntity;
import com.msopentech.odatajclient.engine.data.ODataEntitySet;
import com.msopentech.odatajclient.engine.data.ODataLink;
import com.msopentech.odatajclient.engine.data.ODataProperty;
import com.msopentech.odatajclient.engine.format.ODataPubFormat;
import com.msopentech.odatajclient.engine.uri.ODataURIBuilder;
public class RequestEntityTest {
String testODataServiceRootURL="http://dsc-server:8080/PSDSCPullServer.svc/";
@Test
public void testName() throws Exception {
ODataURIBuilder uriBuilder = new ODataURIBuilder(testODataServiceRootURL).appendEntityTypeSegment("Nodes(AgentId='304DC9EE-12D4-11E7-80B8-B9D2EE16DFG9')/Reports");
ODataEntitySetRequest req = ODataRetrieveRequestFactory.getEntitySetRequest(uriBuilder.build());
req.setFormat(ODataPubFormat.ATOM);
ODataRetrieveResponse<ODataEntitySet> res = req.execute();
res.getStatusCode();
res.getStatusMessage();
res.getContentType();
res.getEtag();
res.getHeaderNames();
res.getHeader("DataServiceVersion");
ODataEntitySet entitySet = res.getBody();
System.out.println(entitySet.getCount());
ODataEntity entity=entitySet.getEntities().get(0);
List<ODataLink> lsODataLink= entity.getAssociationLinks();
lsODataLink.forEach( ODataLink ->
{
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.println(ODataLink);
}
);
ODataProperty oDataPropertyJobID=entity.getProperty("JobId");
System.out.println(".........oDataPropertyJobID:"+oDataPropertyJobID.getValue());
List<ODataProperty> properties = entity.getProperties();
properties.forEach(oDataProperty ->{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(oDataProperty.getValue());
if( oDataProperty.hasCollectionValue()){
System.out.println("---hasCollectionValue---");
System.out.println(oDataProperty.getName());
ODataCollectionValue oDataCollectionValue= oDataProperty.getCollectionValue();
System.out.println(oDataCollectionValue.getTypeName());
System.out.println(oDataProperty.getPrimitiveValue());
}
if(oDataProperty.hasComplexValue()){
System.out.println("---hasComplexValue---");
System.out.println(oDataProperty.getName());
}
});
}
}
注意,下面的代码,没有和DSC Report OData直接相关,但是显示的是示范代码,调用过程类似,另外需要注意的是DSC Report的查询不支持OData的filter的语法,如果要让其支持,请自己定义实现:
@获取单个的实体
ODataURIBuilder uriBuilder = new ODataURIBuilder(testODataServiceRootURL).appendEntityTypeSegment("Customer(1)");
ODataEntityRequest req = ODataRetrieveRequestFactory.getEntityRequest(uriBuilder.build());
req.setFormat(ODataPubFormat.JSON_FULL_METADATA);
ODataRetrieveResponse<ODataEntity> res = req.execute();
res.getStatusCode();
res.getStatusMessage();
res.getContentType();
res.getEtag();
res.getHeaderNames();
res.getHeader("DataServiceVersion");
ODataEntity entity = res.getBody();
URI editLink = entity.getEditLink();
List<ODataProperty> properties = entity.getProperties();
ODataProperty property = entity.getProperty("Id");
List<ODataLink> navLinks = getNavigationLinks();
List<ODataLink> assLinks = getAssociationLinks();
List<ODataLink> meLinks = getEditMediaLinks();
List<ODataOperation> operations = getOperations();
@获取实体集
ODataURIBuilder uriBuilder = new ODataURIBuilder(testODataServiceRootURL).appendEntitySetSegment("Car").top(3);
ODataEntitySetRequest req = ODataRetrieveRequestFactory.getEntitySetRequest(uriBuilder.build());
ODataRetrieveResponse<ODataEntitySet> res = req.execute();
ODataEntitySet entitySet = res.getBody();
URI nextPageLink = getNext();
List<ODataEntity> entities = getEntities();
@Filter的过滤查询
ODataURIBuilder uriBuilder = new ODataURIBuilder(testODataServiceRootURL).appendEntityTypeSegment("Car").filter("VIN lt 16");
ODataEntitySet result = ODataRetrieveRequestFactory.getEntitySetRequest(
uriBuilder.build()).execute().getBody();
具体的查询格式,请参考微软的一篇文章:https://msdn.microsoft.com/en-us/library/dd541437.aspx
@Basic认证调用
// instructs ODataJClient to send a BasicAuth HTTP header// for username 'sampleUsername' and password ' samplePassword'
Configuration.setHttpClientFactory(new AbstractBasicAuthHttpClientFactory() {
@Override
protected String getUsername() {
return "sampleUsername";
}
@Override
protected String getPassword() {
return "samplePassword";
}
});
// perform any operation via ODataJClient: all requests will bear the BasicAuth HTTP header
// this restores the default unauthenticated behavior
Configuration.setHttpClientFactory(new DefaultHttpClientFactory());
参考文献:
https://github.com/mkostin/ODataJClient/wiki/User-guide-(Engine)http://blog.csdn.net/chancein007/article/details/54016636 https://github.com/mkostin/ODataJClient
相关文章推荐
- [32] Window PowerShell DSC 学习系列----使用DSC Package安装时,如何获取Product ID?
- [29] Window PowerShell DSC 学习系列----如何备份数据库和解决一个MS Server 2008上的大坑?
- [7] Window PowerShell DSC 学习系列----如何被管理的设置节点注册到Pull Server?
- [22]Window PowerShell DSC学习系列---- 如何用PowerShell脚本查看DSC服务器的执行报表(Report)
- [3] Window PowerShell DSC 学习系列----如何在PowerShell DSC 5.x 安装最新的DSC Module?
- [6] Window PowerShell DSC 学习系列----如何安装最新的PowerShell DSC 5.1 Pull Server?
- [30] Window PowerShell DSC 学习系列----如何解决跨节点(across computers)依赖?
- [5] Window PowerShell DSC 学习系列----如何生成一个DSC MOF文件?
- [2] Window PowerShell DSC 学习系列----DSC的资源(Resource)以及配置和格式(Configuration && Sytax )
- [14]Windows PowerShell DSC学习系列---如何Debug PowerShell DSC?
- [10] Window PowerShell DSC 学习系列----目标节点和Pull 服务器底层通信原理剖析
- [27] Window PowerShell DSC 学习系列----DSC 5.x 查询节点注册和执行报告(Report)查询
- [31] Window PowerShell DSC 学习系列----PowerShell DSC 专栏竟然在CSDN推荐的优秀专栏里面
- [4] Window PowerShell DSC 学习系列----PowerShell DSC 5.x 提供了哪些有用好用的工具命令?
- [8] Window PowerShell DSC 学习系列----MOF文件和DSC Module的部署
- [9] Window PowerShell DSC 学习系列----错误诊断和分析
- [11] Windows PowerShell DSC学习系列---如何定制一个基于PowerShell脚本实现的DSC Resource
- [25]Window PowerShell DSC学习系列----如何更换DSC Pull服务器数据库为Access数据库?
- [1] Window PowerShell DSC 学习系列----DSC的定义和2种架构模式
- Axis2学习总结(2)-----通过wsdl2java工具调用第三方服务