Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

OM2M/one/App

< OM2M‎ | one
Revision as of 18:44, 13 November 2016 by Unnamed Poltroon (Talk) (Created page with "'''Before doing this tutorial you have to''' [https://wiki.eclipse.org/OM2M/Clone clone and build OM2M using Eclipse] __TOC__ = Develop standalone oneM2M applications = ==...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Before doing this tutorial you have to clone and build OM2M using Eclipse

Develop standalone oneM2M applications

Create a new Maven project

  • Open Eclipse IDE
  • Got to File -> New -> other -> Maven Project
  • Select "Create a simple Project (skip archetype selection)" then click Next
  • Enter "org.eclipse.om2m" as Group Id and "org.eclipse.om2m.app" as "Artifact Id" then click on Finish
  • Create package "org.eclipse.om2m.app" inside the folder src/main/java
  • Add dependencies for HTTP and JSON to your project by adding the following section to your pom.xml file inside the <project> tag.
<dependencies>
   <dependency>
      <groupId>com.sun.net.httpserver</groupId>
      <artifactId>http</artifactId>
      <version>20070405</version>
   </dependency>
   <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.1</version>
   </dependency>
   <dependency>
   <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20160212</version>
   </dependency>
</dependencies>

HTTP Client tools

RestHttpClient class

package org.eclipse.om2m.app;
 
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class RestHttpClient {
 
	public static HttpResponse get(String originator, String uri) {
		System.out.println("HTTP GET "+uri);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet= new HttpGet(uri);
 
		httpGet.addHeader("X-M2M-Origin",originator);
		httpGet.addHeader("Accept","application/json");
 
		HttpResponse httpResponse = new HttpResponse();
 
		try {
			CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpGet);
			try{
				httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode());
				httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity()));
			}finally{
				closeableHttpResponse.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody());
		return httpResponse;	
	}
 
	public static HttpResponse post(String originator, String uri, String body, int ty) {
		System.out.println("HTTP POST "+uri+"\n"+body);
 
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(uri);
 
		httpPost.addHeader("X-M2M-Origin",originator);
		httpPost.addHeader("Accept","application/json");	
		httpPost.addHeader("Content-Type","application/json;ty="+ty);
 
		HttpResponse httpResponse = new HttpResponse();
		try {
			CloseableHttpResponse closeableHttpResponse=null;
			try{
				httpPost.setEntity(new StringEntity(body));
				closeableHttpResponse = httpclient.execute(httpPost);
			httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode());
				httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity()));
 
			}finally{
				closeableHttpResponse.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody());
		return httpResponse ;	
	}
}

HttpResponse class

package org.eclipse.om2m.app;
 
public class HttpResponse {
	private int statusCode;
	private String body;
 
	public int getStatusCode() {
		return statusCode;
	}
 
	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}
 
	public String getBody() {
		return body;
	}
 
	public void setBody(String body) {
		this.body = body;
	}
}

Back to the top