Cisco Cisco Prime Home 6.5 Developer's Guide

Page of 78
Cisco Prime Home – Integration Guide
 
 
OL-28557-01   v5.0.1
 
33 
Utilities Used by the Examples 
HttpConfiguration.scala  
package com.clearaccess.example  
import org.springframework.beans.factory.annotation.Value 
import org.springframework.context.annotation.{ImportResource, 
Configuration} 
 
@Configuration 
@ImportResource(Array("classpath:spring/httpConfiguration.xml"))  
class HttpConfiguration {  
@Value("${http.username}") var username: String = _  
@Value("${http.password}") var password: String = _  
@Value("${http.host}") var host: String = _  
 
 
JSONHelpers.scala  
package com.clearaccess.example.api 
import org.json.{JSONArray, JSONObject}  
import collection.mutable.ListBuffer  
 
/** 
  * This mixin gives implicit conversions for JSON arrays and 
object into rich  
  * wrappers allowing more complex common operations on those 
arrays and 
  * objects.  
 */  
trait JSONHelpers { 
 /**  
  * A rich wrapper around a JSON object  
  * @param jsonObject the wrapped JSON object  
  */  
class RichJSONObject(jsonObject: JSONObject) { 
def getOrUpdateJSONObject(key: String, default: JSONObject): 
JSONObject = { 
      if (jsonObject.has(key)) 
jsonObject.getJSONObject(key) 
      else { 
 jsonObject.put(key, default) 
 default 
     } 

 
/** 
  * Allows you to set an object property deep within a JSON 
object tree. For 
  * example given: { x: { y: {} } }, you can call: 
deepPut(List("x", "y", "z"), "1") to  
  * yield { x: { y: { z: 1 } } }  
  * 
 * @param path a list of keys forming the path of the property 
to set 
 * @return the resulting JSON object 
 */ 
def deepPut(path: List[String], value: String): JSONObject = { 
def getObject(path: List[String], parent: JSONObject): JSONObject 
= if (path.length > 1) { 
val child = 
toRichJSONObject(parent).getOrUpdateJSONObject(path.head, new 
JSONObject)