PowerShell DSC 5.x的Report是基于OData V3的标准,而这个标准是微软提出已经标准化的一个开放的工业标准。在我的另外一篇文章中也提到了有下面的几个Java的开源框架支持调用oData的服务。
- 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 behaviorConfiguration.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