Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2013-2014 by Cisco Systems, Inc. All rights reserved.
  
  package com.cisco.oss.foundation.directory.impl;
  
 import java.util.List;
 import java.util.Map;
 
 
This is the client object to invoke the remote service in ServiceDirectory Server Node. It implements the HTTP transportation for ServiceDirectoryService, and hides the HTTPClient details from the upper application layer.

Author(s):
zuxiang
 
 public class DirectoryServiceClient{
 	
 	private static final Logger LOGGER = LoggerFactory.getLogger(DirectoryServiceClient.class);

The http client read timeout property.
 
 	public static final String SD_API_HTTPCLIENT_READ_TIMEOUT_PROPERTY = "httpclient.read.timeout";

The http client default read timeout value.
 
 	public static final int SD_API_HTTPCLIENT_READ_TIMEOUT_DEFAULT = 5;

The Service Directory server FQDN property name.
 
 	public static final String SD_API_SD_SERVER_FQDN_PROPERTY = "server.fqdn";

The default Service Directory server FQDN name.
 
 	public static final String SD_API_SD_SERVER_FQDN_DEFAULT = "vcsdirsvc";

The Service Directory server port property name.
 
 	public static final String SD_API_SD_SERVER_PORT_PROPERTY = "server.port";

The default Service Directory server port.
 
 	public static final int SD_API_SD_SERVER_PORT_DEFAULT = 2013;

The HTTP invoker to access remote ServiceDirectory node.
 
 	
Constructor.
 
 	public DirectoryServiceClient() {
 		String directoryAddresses = "http://" + sdFQDN + ":" + port;
 		this. = new DirectoryInvoker(directoryAddresses);
 	}

Reinitialize the Client.
 
	public void reinit(){
		String directoryAddresses = "http://" + sdFQDN + ":" + port;
		this. = new DirectoryInvoker(directoryAddresses);
	}

Close the DirectoryServiceClient.
	public void close(){
		this. = null;
		this. = null;
	}

Register a ServiceInstance.

Parameters:
instance the ProvidedServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void registerInstance(ProvidedServiceInstance instance){
		String body = serialize(instance);
		HttpResponse result = .invoke("/service/" + instance.getServiceName() + "/" + instance.getProviderId(), body,
		if (result.getHttpCode() != 201) {
			.error("Register Service failed, httpCode="
result.getHttpCode());
					"HTTP Code is not OK, code=" + result.getHttpCode());
		}
	}

Update a ServiceInstance.

Parameters:
instance the ProvidedServiceInstance.
	public void updateInstance(ProvidedServiceInstance instance){
		String body = serialize(instance);
		HttpResponse result = .invoke("/service/" + instance.getServiceName() + "/" + instance.getProviderId(), body,
		if (result.getHttpCode() != 201) {
			.error("Update Service failed, httpCode="
result.getHttpCode());
					"HTTP Code is not OK, code=" + result.getHttpCode());
		}
	}

Update the ServiceInstance OperationalStatus by serviceName and instanceId.

Parameters:
serviceName the service name.
instanceId the instance id.
status the ServiceInstance OperationalStatus.
isOwned whether the DirectoryAPI owns this ServiceProvider.
	public void updateInstanceStatus(String serviceNameString instanceIdOperationalStatus statusboolean isOwned){
		String uri = "/service/" + serviceName + "/" + instanceId + "/status";
		String body = null;
		try {
			body = "status=" + URLEncoder.encode(status.toString(), "UTF-8") + "&isOwned=" + isOwned;
			.error("URLEncoder can not encode the status - " + status);
			.debug("URLEncoder can not encode the status."e);
		}
		Map<StringStringheaders = new HashMap<StringString>();
		headers.put("Content-Type""application/x-www-form-urlencoded");
		HttpResponse result = .invoke(uribody,
				.headers);
		if (result.getHttpCode() != 200) {
			.error("Update Service OperationalStatus failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
	}

Update the ServiceInstance URI by serviceName and instanceId.

Parameters:
serviceName the service name.
instanceId the instance id.
uri the ServiceInstance URI.
isOwned whether the DirectoryAPI owns this ServiceProvider.
	public void updateInstanceUri(String serviceNameString instanceIdString uriboolean isOwned){
		String serviceUri = "/service/" + serviceName + "/" + instanceId + "/uri" ;
		String body = null;
		try {
			body = "uri=" + URLEncoder.encode(uri"UTF-8") + "&isOwned=" + isOwned;
			.error("URLEncoder can not encode the URI - " + uri);
			.debug("URLEncoder can not encode the URI."e);
		}
		Map<StringStringheaders = new HashMap<StringString>();
		headers.put("Content-Type""application/x-www-form-urlencoded");
		HttpResponse result = .invoke(serviceUribody,
				.headers);
		if (result.getHttpCode() != 200) {
			.error("Update Service URI failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
	}

Unregister a ServiceInstance.

Parameters:
serviceName service name.
instanceId the instance id.
isOwned whether the DirectoryAPI owns this ServiceProvider.
	public void unregisterInstance(String serviceNameString instanceIdboolean isOwned){
		String uri = "/service/" + serviceName + "/" + instanceId + "/" + isOwned;
		HttpResponse result = .invoke(urinull,
		if (result.getHttpCode() != 200) {
			.error("UnregisterService failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
	}

Send ServiceInstance heartbeats.

Parameters:
heartbeatMap the ServiceInstances heartbeat Map.
		String body = serialize(heartbeatMap);
		HttpResponse result = .invoke("/service/heartbeat"body,
		if (result.getHttpCode() != 200) {
			.error("Send Heartbeat failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
		return operateRsult;
	}

Lookup a Service by serviceName.

Parameters:
serviceName the service name.
Returns:
the ModelService.
	public ModelService lookupService(String serviceName){
		HttpResponse result = .invoke("/service/" + serviceName , null.);
		if (result.getHttpCode() != 200) {
			.error("LookupService failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
		ModelService service = deserialize(result.getRetBody(), ModelService.class);
		return service;
	}
		HttpResponse result = .invoke("/service" , null.);
		if (result.getHttpCode() != 200) {
			.error("getAllInstances failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
		return allInstances;
	}

Get the MetadataKey by key name.

Parameters:
keyName the key name.
Returns:
the ModelMetadataKey.
		HttpResponse result = .invoke("/metadatakey/" + keyName , null.);
		if (result.getHttpCode() != 200) {
			.error("Get MetadataKey failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
					result.getRetBody(), ModelMetadataKey.class);
		return key;
	}

Get the changed services for Services list.

Parameters:
services the Service list.
Returns:
the Service list that has modification.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
		String body = serialize(services);
		HttpResponse result = .invoke("/service/changing" , body.);
		if (result.getHttpCode() != 200) {
			.error("Get Service Changed List failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
		return newservices;
	}

Get the changed MetadataKey.

Parameters:
keys the MetadataKey List.
Returns:
the ModelMetadataKey that has been changed.
		String body = serialize(keys);
		HttpResponse result = .invoke("/metadatakey/changing" , body.);
		if (result.getHttpCode() != 200) {
			.error("Get metadatakey changed list failed, httpCode=" + result.getHttpCode());
					."HTTP Code is not OK, code=" + result.getHttpCode());
		}
		return newkeys;
	}

Deserialize a JSON String to the target object.

Parameters:
body the JSON String.
clazz the Object class name deserialized to.
Returns:
the deserialized Object instance.
Throws:
java.io.IOException
org.codehaus.jackson.map.JsonMappingException
org.codehaus.jackson.JsonParseException
	@SuppressWarnings("unchecked")
	private <T> T deserialize(String bodyClass<T> clazz) {
		if(body == null || body.isEmpty()){
			.error("the message body is empty");
					."the message body is empty");
		}
		try {
			return (T) .deserialize(body.getBytes(), clazz);
catch (IOException e) {
			.error("deserialize failed - " + e.getMessage());
			.debug("deserialize failed"e);
					."unrecognized message, deserialized failed.");
			throw new DirectoryServerClientException(sdee);
	}

Deserialize a JSON String to a generic object. This method is used when the target object is generic.

Parameters:
body the JSON String.
typeRef the generic type.
Returns:
the deserialized object instance.
Throws:
java.io.IOException
org.codehaus.jackson.map.JsonMappingException
org.codehaus.jackson.JsonParseException
	@SuppressWarnings("unchecked")
	private <T> T deserialize(String bodyTypeReference<T> typeRef){
		if(body == null || body.isEmpty()){
			.error("the message body is empty");
					."the message body is empty");
		}
		try {
			return (T) .deserialize(body.getBytes(), typeRef);
catch (IOException e) {
			.error("deserialize failed - " + e.getMessage());
			.debug("deserialize failed"e);
					."unrecognized message, deserialized failed.");
			throw new DirectoryServerClientException(sdee);
	}

Serialize a object to JSON String.

Parameters:
o the target object.
Returns:
the JSON String.
	private String serialize(Object o) {
		String body = null;
			try {
				body = new String(.serialize(o));
catch (IOException e) {
				.error("serialize failed - " + e.getMessage());
				.debug("serialize failed"e);
						."serialized failed.");
				throw new DirectoryServerClientException(sdee);
		return body;
	}

Keep it default for unit test.

Returns:
the DirectoryInvoker
		return ;
	}
New to GrepCode? Check out our FAQ X