<commit_before># Sample code

This section contains code snippets (most of them contributed by
external developers) that can be used as examples for programming with
Orion Context Broker in different technologies. Note that Orion Context
Broker evolves with time so it could happen that the code examples get
obsolete in some moment (the publication date is provides as reference).

## Java

Thanks to Kurento (http://www.kurento.org) a Java library that shows how
to access to Orion Context Broker:
<https://github.com/KurentoLegacy/kmf-orion-connector/tree/develop>
(published June 2014).

Thanks to <NAME> (published around October 2013):

       import com.sun.jersey.api.client.Client;
       import com.sun.jersey.api.client.ClientResponse;
       import com.sun.jersey.api.client.WebResource;

       //Test the Orion Broker, query for Room
       String urlString = "http://orion.lab.fiware.org:1026/v1/queryContext";
       String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
            "<queryContextRequest>" + 
            "<entityIdList>" + 
            "<entityId type=\"Room\" isPattern=\"true\">" + 
            "<id>Room.*</id>" + 
            "</entityId>" +
            "</entityIdList>" + 
            "<attributeList>" + 
            "<attribute>temperature</attribute>" + 
            "</attributeList>" + 
            "</queryContextRequest>";      
       try {     
          Client client = Client.create();
          WebResource webResource = client.resource(urlString);
          ClientResponse response = webResource.type("application/xml").header("X-Auth-Token", "your_auth_token").post(ClientResponse.class, XML);
       
          if (response.getStatus() != 200) {
             System.err.println(response.getEntity(String.class));
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
          }
       
          System.out.println("Output from Server .... \n");
          String output = response.getEntity(String.class);
          System.out.println(output);
       
       } 
       catch (Exception e) {
          System.err.println("Failed. Reason is " + e.getMessage());
       }

## JavaScript and NodeJS
SmartSDK team developed and [SDK Client](https://github.com/smartsdk/ngsi-sdk-javascript) compatible with NSGIv2. 
It allows to develop both NodeJS applicaitions and JavaScript applications.

NodeJS:
```javascript
var NgsiV2 = require('ngsi_v2');

var defaultClient = NgsiV2.ApiClient.instance;

// Configure API key authorization: fiware_token
var fiware_token = defaultClient.authentications['fiware_token'];
fiware_token.apiKey = "YOUR API KEY"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//fiware_token.apiKeyPrefix['X-Auth-Token'] = "Token"
var api = new NgsiV2.APIEntryPointApi()

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.retrieveAPIResources(callback);

```

JavaScript:
```html
<script src="https://smartsdk.github.io/ngsi-sdk-javascript/js/ngsi.js"></script>
<script>
  var NgsiV2 = FIWARE.NgsiV2;
  var defaultClient = NgsiV2.ApiClient.instance;
  // Configure API key authorization: fiware_token
  var fiware_token = defaultClient.authentications['fiware_token'];
  fiware_token.apiKey = "My Token"
  // Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
  //fiware_token.apiKeyPrefix['X-Auth-Token'] = "Token"
  var api = new NgsiV2.APIEntryPointApi()
  var callback = function(error, data, response) {
    if (error) {
      console.error(error);<commit_msg>Fixed javascript syntax; added publication date;<commit_after># Sample code

This section contains code snippets (most of them contributed by
external developers) that can be used as examples for programming with
Orion Context Broker in different technologies. Note that Orion Context
Broker evolves with time so it could happen that the code examples get
obsolete in some moment (the publication date is provides as reference).

## Java

Thanks to Kurento (http://www.kurento.org) a Java library that shows how
to access to Orion Context Broker:
<https://github.com/KurentoLegacy/kmf-orion-connector/tree/develop>
(published June 2014).

Thanks to <NAME> (published around October 2013):

       import com.sun.jersey.api.client.Client;
       import com.sun.jersey.api.client.ClientResponse;
       import com.sun.jersey.api.client.WebResource;

       //Test the Orion Broker, query for Room
       String urlString = "http://orion.lab.fiware.org:1026/v1/queryContext";
       String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
            "<queryContextRequest>" + 
            "<entityIdList>" + 
            "<entityId type=\"Room\" isPattern=\"true\">" + 
            "<id>Room.*</id>" + 
            "</entityId>" +
            "</entityIdList>" + 
            "<attributeList>" + 
            "<attribute>temperature</attribute>" + 
            "</attributeList>" + 
            "</queryContextRequest>";      
       try {     
          Client client = Client.create();
          WebResource webResource = client.resource(urlString);
          ClientResponse response = webResource.type("application/xml").header("X-Auth-Token", "your_auth_token").post(ClientResponse.class, XML);
       
          if (response.getStatus() != 200) {
             System.err.println(response.getEntity(String.class));
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
          }
       
          System.out.println("Output from Server .... \n");
          String output = response.getEntity(String.class);
          System.out.println(output);
       
       } 
       catch (Exception e) {
          System.err.println("Failed. Reason is " + e.getMessage());
       }

## JavaScript and NodeJS
SmartSDK team developed and [SDK Client](https://github.com/smartsdk/ngsi-sdk-javascript) compatible with NSGIv2. 
It allows to develop both NodeJS applicaitions and JavaScript applications (published March 2017).

NodeJS:
```javascript
var NgsiV2 = require('ngsi_v2');

var defaultClient = NgsiV2.ApiClient.instance;

// Configure API key authorization: fiware_token
var fiware_token = defaultClient.authentications['fiware_token'];
fiware_token.apiKey = "YOUR API KEY";
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//fiware_token.apiKeyPrefix['X-Auth-Token'] = "Token";
var api = new NgsiV2.APIEntryPointApi();

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.retrieveAPIResources(callback);

```

JavaScript:
```html
<script src="https://smartsdk.github.io/ngsi-sdk-javascript/js/ngsi.js"></script>
<script>
  var NgsiV2 = FIWARE.NgsiV2;
  var defaultClient = NgsiV2.ApiClient.instance;
  // Configure API key authorization: fiware_token
  var fiware_token = defaultClient.authentications['fiware_token'];
  fiware_token.apiKey = "My Token";
  // Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
  //fiware_token.apiKeyPrefix['X-Auth-Token'] = "Token";
  var api = new NgsiV2.APIEntryPointApi();
  var callback = function(error, data, response) {
    if (error) {
      console.error(error);
{'fraction_non_alphanumeric': 0.1235262593783494, 'fraction_numerical': 0.008038585209003215, 'mean_word_length': 3.1357340720221605, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 2, '>': 39, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607922', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2154, 'n_words': 589}