Cisco Cisco Prime Home 6.3 Developer's Guide

Page of 78
Cisco Prime Home – Integration Guide
 
 
OL-28558-01   v5.1
 
68 
Interaction with the API 
com.clearaccess.subscriber.api.SubscriberAPI 
 
/* 
* Copyright (c) 2010 ClearAccess, Inc. 
* This code is provided AS-IS for illustration purposes. 
*/ 
 
package com.clearaccess.subscriber.api; 
 
import org.springframework.stereotype.Component; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.client.HttpClientErrorException; 
import org.springframework.http.HttpStatus; 
import org.json.JSONObject; 
import org.json.JSONException; 
import com.clearaccess.rest.RestClient; 
import com.clearaccess.Configuration; 
 
@Component 
public class SubscriberAPI { 
final RestClient restClient; 
final Configuration configuration; 
@Autowired 
public SubscriberAPI(final RestClient restClient, final 
Configuration  
configuration) { 
this.restClient = restClient; 
this.configuration = configuration; 

 
/** 
* looks up a subscriber, by code, and returns it if it exists; 
otherwise, it  
* creates a new default subscriber which can be persisted later 
* @param subscriberCode the subscriber code to look up 
* @return a subscriber with the given code 
*/ 
public Subscriber getSubscriber(final String subscriberCode) { 
try { 
return new Subscriber(new 
JSONObject(restClient.getForObject(getSubscriberURL(subscriberCode), 
String.class))); 
} catch (HttpClientErrorException e) { 
if (e.getStatusCode() == HttpStatus.NOT_FOUND) { 
return new Subscriber(subscriberCode); 

throw new RuntimeException(e); 
} catch (JSONException e) { 
throw new RuntimeException(e); 

 
/** 
* sends the subscriber back to the server. if the subscriber was 
not initially retrieved from 
* the server, it will add it. 
* @param subscriber the subscriber to save 
* @return the subscriber, in the state which was returned by the 
update 
*/ 
public Subscriber updateSubscriber(final Subscriber subscriber) { 
if (subscriber.isNotPersisted()) {