Repository: stratos
Updated Branches:
  refs/heads/4.0.0-grouping a8c72790d -> eb37fa90b


improving generic metadata client


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/8e7a00b1
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/8e7a00b1
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/8e7a00b1

Branch: refs/heads/4.0.0-grouping
Commit: 8e7a00b1e69ad815057474f6e6b4f1851f29a56f
Parents: 9677d15
Author: Udara Liyanage <[email protected]>
Authored: Wed Sep 24 15:14:09 2014 +0530
Committer: Udara Liyanage <[email protected]>
Committed: Wed Sep 24 15:14:09 2014 +0530

----------------------------------------------------------------------
 .../org.apache.stratos.metadata.client/pom.xml  |  5 ++
 .../client/DefaultMetaDataServiceClient.java    | 29 ++++++--
 .../metadata/client/MetaDataServiceClient.java  |  4 +-
 .../metadata/client/beans/PropertyBean.java     | 64 ++++++++++++++++++
 .../metadata/client/rest/DefaultRestClient.java | 71 ++++++++++++++++++--
 .../sample/MetaDataServiceClientSample.java     |  5 +-
 .../registry/CarbonRegistry.java                |  2 +-
 .../metadataservice/services/MetaDataAdmin.java | 41 +++++------
 8 files changed, 183 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/pom.xml
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.metadata.client/pom.xml 
b/components/org.apache.stratos.metadata.client/pom.xml
index 4139219..fcafdaa 100644
--- a/components/org.apache.stratos.metadata.client/pom.xml
+++ b/components/org.apache.stratos.metadata.client/pom.xml
@@ -27,6 +27,11 @@
           <version>1.9</version>
           <scope>provided</scope>
       </dependency>
+      <dependency>
+          <groupId>com.google.code.gson</groupId>
+          <artifactId>gson</artifactId>
+          <version>2.2.4</version>
+      </dependency>
   </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
index 6f8e9cb..d60dfb2 100644
--- 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
+++ 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/DefaultMetaDataServiceClient.java
@@ -21,7 +21,10 @@ package org.apache.stratos.metadata.client;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.http.HttpResponse;
+import org.apache.stratos.metadata.client.beans.PropertyBean;
 import 
org.apache.stratos.metadata.client.exception.MetaDataServiceClientExeption;
+import org.apache.stratos.metadata.client.exception.RestClientException;
 import org.apache.stratos.metadata.client.rest.DefaultRestClient;
 import org.apache.stratos.metadata.client.rest.RestClient;
 
@@ -36,7 +39,7 @@ public class DefaultMetaDataServiceClient implements 
MetaDataServiceClient {
     private RestClient restClient;
     private String baseUrl;
 
-    public DefaultMetaDataServiceClient (String baseUrl) {
+    public DefaultMetaDataServiceClient (String baseUrl) throws 
RestClientException {
         this.baseUrl = baseUrl;
         restClient = new DefaultRestClient();
     }
@@ -45,12 +48,30 @@ public class DefaultMetaDataServiceClient implements 
MetaDataServiceClient {
         // initialization, if any
     }
 
-    public void addProperty(String appId, String clusterId, String 
propertyKey, String propertyValue)
+    public void addPropertyToCluster(String appId, String clusterId, String 
propertyKey, String propertyValue)
             throws MetaDataServiceClientExeption {
-        //To change body of implemented methods use File | Settings | File 
Templates.
+        String applicationPath = 
baseUrl.concat("application/").concat(appId).concat("/cluster/").concat(clusterId).concat("/property");
+        PropertyBean property = new PropertyBean(propertyKey, propertyValue);
+        try {
+            restClient.doPost(applicationPath, property);
+        } catch (RestClientException e) {
+            log.error(String.format("Error occurred while adding property %s", 
propertyKey));
+        }
     }
 
-//    public void addProperty(String appId, String propertyKey, String 
propertyValue)
+    public void addPropertyToApplication(String appId, String propertyKey, 
String propertyValue)
+            throws MetaDataServiceClientExeption {
+        String applicationPath = 
baseUrl.concat("application/").concat(appId).concat("/property");
+        PropertyBean property = new PropertyBean(propertyKey, propertyValue);
+        HttpResponse x;
+        try {
+            x = restClient.doPost(applicationPath, property);
+        } catch (RestClientException e) {
+            log.error(String.format("Error occurred while adding property %s", 
propertyKey));
+        }
+    }
+
+//    public void addPropertyToCluster(String appId, String propertyKey, 
String propertyValue)
 //            throws MetaDataServiceClientExeption {
 //        //To change body of implemented methods use File | Settings | File 
Templates.
 //    }

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java
 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java
index ff26264..fa89f96 100644
--- 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java
+++ 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/MetaDataServiceClient.java
@@ -41,7 +41,7 @@ public interface MetaDataServiceClient {
      * @param propertyValue Value of the Property
      * @throws MetaDataServiceClientExeption
      */
-    public void addProperty (String appId, String clusterId, String 
propertyKey, String propertyValue) throws MetaDataServiceClientExeption;
+    public void addPropertyToCluster(String appId, String clusterId, String 
propertyKey, String propertyValue) throws MetaDataServiceClientExeption;
 
 //    /**
 //     * Adds a property key value pair for the specified app
@@ -51,7 +51,7 @@ public interface MetaDataServiceClient {
 //     * @param propertyValue Value of the Property
 //     * @throws MetaDataServiceClientExeption
 //     */
-//    public void addProperty (String appId, String propertyKey, String 
propertyValue) throws MetaDataServiceClientExeption;
+//    public void addPropertyToCluster (String appId, String propertyKey, 
String propertyValue) throws MetaDataServiceClientExeption;
 //
 //    /**
 //     * Retrieves the property key value pairs for the relevant cluster of 
the specified app

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java
 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java
new file mode 100644
index 0000000..dd2d777
--- /dev/null
+++ 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/beans/PropertyBean.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.metadata.client.beans;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@XmlRootElement(name="properties")
+public class PropertyBean {
+    private String key;
+    private List<String> values = new ArrayList<String>();
+
+    public PropertyBean(){}
+    public PropertyBean(String key, String value){
+        this.key=key;
+        this.values.add(value);
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String[] getValues(){
+        String[] values = new String[this.values.size()];
+        values = this.values.toArray(values);
+        return values;
+    }
+
+    public void setValues(String[] values) {
+        this.values.addAll(Arrays.asList(values));
+    }
+
+
+    public void setValues(String value) {
+        this.values.add(value);
+    }
+
+    public void addValue(String value){
+        this.values.add(value);
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
index 8c767e2..c8ccad5 100644
--- 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
+++ 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/rest/DefaultRestClient.java
@@ -19,6 +19,9 @@
 
 package org.apache.stratos.metadata.client.rest;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpResponse;
@@ -26,25 +29,49 @@ import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContextBuilder;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.HttpClients;
 import org.apache.stratos.metadata.client.exception.RestClientException;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
 
 public class DefaultRestClient implements RestClient {
 
+    private static final String APPLICATION_JSON = "application/json";
     private static Log log = LogFactory.getLog(DefaultRestClient.class);
 
     private HttpClient httpClient;
 
-    public DefaultRestClient() {
-        this.httpClient = new DefaultHttpClient();
+    public DefaultRestClient() throws  RestClientException{
+        SSLContextBuilder builder = new SSLContextBuilder();
+
+        SSLConnectionSocketFactory sslsf = null;
+        try {
+            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
+            sslsf = new SSLConnectionSocketFactory(
+                    builder.build());
+        } catch (NoSuchAlgorithmException e) {
+            throw  new RestClientException(e);
+        } catch (KeyManagementException e) {
+            throw  new RestClientException(e);
+        } catch (KeyStoreException e) {
+            throw  new RestClientException(e);
+        }
+        this.httpClient = 
HttpClients.custom().setSSLSocketFactory(sslsf).build();
     }
 
     public HttpResponse doPost(String resourcePath, Object payload) throws 
RestClientException {
 
         HttpPost post = new HttpPost(resourcePath);
-        //TODO set params
+        addPayloadJsonString(payload, post);
+        setAuthHeader(post);
         try {
             return httpClient.execute(post);
 
@@ -55,6 +82,30 @@ public class DefaultRestClient implements RestClient {
         }
     }
 
+    private void setAuthHeader(HttpPost post) {
+        String username = getUsername();
+        String password = getPassword();
+        String identity = username+":"+password;
+        String encoding = new String(Base64.encodeBase64(identity.getBytes()));
+        post.setHeader("Authorization", "Basic " + encoding);
+    }
+
+    private String getUsername() {
+        return null;
+    }
+
+    private String getPassword() {
+        return null;
+    }
+
+    private void addPayloadJsonString(Object payload, HttpPost post) {
+        GsonBuilder gsonBuilder = new GsonBuilder();
+        Gson gson = gsonBuilder.create();
+
+        String payloadText =  gson.toJson(payload, payload.getClass());
+        addStringPayload(post, payloadText);
+    }
+
     public HttpResponse doGet(String resourcePath) throws RestClientException {
 
         HttpGet get = new HttpGet(resourcePath);
@@ -82,4 +133,16 @@ public class DefaultRestClient implements RestClient {
             throw new RestClientException(errorMsg, e);
         }
     }
+
+
+    private void addStringPayload(HttpPost post, String payloadText) {
+        StringEntity stringEntity = null;
+        try {
+            stringEntity = new StringEntity(payloadText);
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        stringEntity.setContentType(APPLICATION_JSON);
+        post.setEntity(stringEntity);
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java
 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java
index ad7eb2f..fd50cb7 100644
--- 
a/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java
+++ 
b/components/org.apache.stratos.metadata.client/src/main/java/org/apache/stratos/metadata/client/sample/MetaDataServiceClientSample.java
@@ -26,6 +26,7 @@ import 
org.apache.stratos.metadata.client.MetaDataServiceClient;
 import org.apache.stratos.metadata.client.config.MetaDataClientConfig;
 import org.apache.stratos.metadata.client.data.extractor.MetaDataExtractor;
 import org.apache.stratos.metadata.client.exception.MetaDataExtractorException;
+import org.apache.stratos.metadata.client.exception.RestClientException;
 import org.apache.stratos.metadata.client.factory.MetaDataExtractorFactory;
 import org.apache.stratos.metadata.client.pojo.DataContext;
 
@@ -42,11 +43,11 @@ public class MetaDataServiceClientSample {
     private MetaDataClientConfig metaDataClientConfig;
 
 
-    public MetaDataServiceClientSample() {
+    public MetaDataServiceClientSample() throws RestClientException {
         initialize();
     }
 
-    private void initialize() {
+    private void initialize() throws RestClientException {
 
         metaDataClientConfig = MetaDataClientConfig.getInstance();
         metaDataServiceClient = new 
DefaultMetaDataServiceClient(metaDataClientConfig.

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/registry/CarbonRegistry.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/registry/CarbonRegistry.java
 
b/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/registry/CarbonRegistry.java
index 8078f91..520c846 100644
--- 
a/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/registry/CarbonRegistry.java
+++ 
b/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/registry/CarbonRegistry.java
@@ -216,7 +216,7 @@ public class CarbonRegistry extends AbstractAdmin 
implements DataStore {
         Resource regResource = createOrGetResourceforCluster(tempRegistry, 
resourcePath);
 
         regResource.setProperty(property.getKey(), 
Arrays.asList(property.getValues()));
-        tempRegistry.put(regResource.getPath(), regResource);
+        tempRegistry.put(resourcePath, regResource);
 
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/8e7a00b1/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/services/MetaDataAdmin.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/services/MetaDataAdmin.java
 
b/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/services/MetaDataAdmin.java
index 11d927f..ccc2b20 100644
--- 
a/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/services/MetaDataAdmin.java
+++ 
b/components/org.apache.stratos.metadataservice/src/main/java/org/apache/stratos/metadataservice/services/MetaDataAdmin.java
@@ -1,31 +1,22 @@
 package org.apache.stratos.metadataservice.services;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
-
 import org.apache.commons.configuration.XMLConfiguration;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.metadataservice.annotation.AuthorizationAction;
-import org.apache.stratos.metadataservice.definition.ApplicationBean;
 import org.apache.stratos.metadataservice.definition.CartridgeMetaData;
-import org.apache.stratos.metadataservice.definition.ClusterBean;
 import org.apache.stratos.metadataservice.definition.NewProperty;
 import org.apache.stratos.metadataservice.exception.RestAPIException;
 import org.apache.stratos.metadataservice.registry.DataRegistryFactory;
 import org.apache.stratos.metadataservice.util.ConfUtil;
 import org.wso2.carbon.registry.api.RegistryException;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.*;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
 import java.net.URI;
-import java.util.ArrayList;
 import java.util.List;
 
 @Path("/")
@@ -118,7 +109,7 @@ public class MetaDataAdmin {
                 propertiesArr = properties.toArray(propertiesArr);
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while getting properties ", e);
         }
 
         Response.ResponseBuilder rb=null;
@@ -156,7 +147,7 @@ public class MetaDataAdmin {
                 }
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while getting property ", e);
         }
 
         Response.ResponseBuilder rb=null;
@@ -194,7 +185,7 @@ public class MetaDataAdmin {
                 }
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while getting properties ", e);
         }
         Response.ResponseBuilder rb=null;
         if(property == null){
@@ -226,7 +217,7 @@ public class MetaDataAdmin {
                 propertiesArr = properties.toArray(propertiesArr);
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while getting properties ", e);
         }
 
         Response.ResponseBuilder rb=null;
@@ -263,8 +254,8 @@ public class MetaDataAdmin {
                     break;
                 }
             }
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (RegistryException e) {
+            log.error("Error occurred while getting property", e);
         }
 
         Response.ResponseBuilder rb=null;
@@ -294,7 +285,7 @@ public class MetaDataAdmin {
         try {
             
DataRegistryFactory.getDataRegistryFactory(registryType).addPropertyToCluster(applicationId,
 clusterId, property);
         } catch (RegistryException e) {
-            e.printStackTrace();
+            log.error("Error occurred while adding dependencies ", e);
         }
         return Response.created(url).build();
     }
@@ -314,7 +305,7 @@ public class MetaDataAdmin {
         try {
             
DataRegistryFactory.getDataRegistryFactory(registryType).addPropertyToCluster(applicationId,
 clusterId, property);
         } catch (RegistryException e) {
-            e.printStackTrace();
+            log.error("Error occurred while adding property", e);
         }
 
         return Response.created(url).build();
@@ -338,7 +329,7 @@ public class MetaDataAdmin {
         try {
             
DataRegistryFactory.getDataRegistryFactory(registryType).addPropertiesToCluster(applicationId,
 clusterId, properties);
         } catch (Exception e) {
-            e.printStackTrace();
+           log.error("Error occurred while adding properties ", e);
         }
 
 
@@ -362,7 +353,7 @@ public class MetaDataAdmin {
         try {
             
DataRegistryFactory.getDataRegistryFactory(registryType).addPropertiesToApplication(applicationId,
 properties);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while adding properties ", e);
         }
 
 
@@ -386,7 +377,7 @@ public class MetaDataAdmin {
         try {
             
DataRegistryFactory.getDataRegistryFactory(registryType).addPropertyToApplication(applicationId,
 property);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("Error occurred while adding property ", e);
         }
 
 

Reply via email to