Updated Branches: refs/heads/master 0e1e04776 -> 2bd2d4aea
Refactor CLI for RESTFul service calls Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/5fc89c38 Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/5fc89c38 Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/5fc89c38 Branch: refs/heads/master Commit: 5fc89c38450f8e62266fc2b35f75b64adb8b3874 Parents: d236533 Author: Manula Thantriwatte <[email protected]> Authored: Thu Nov 28 10:17:47 2013 +0530 Committer: Manula Thantriwatte <[email protected]> Committed: Wed Dec 4 10:38:35 2013 +0530 ---------------------------------------------------------------------- components/org.apache.stratos.cli/pom.xml | 5 + .../java/org/apache/stratos/cli/Cartridge.java | 202 +++++++++++++++++++ .../apache/stratos/cli/CommandLineService.java | 13 +- .../apache/stratos/cli/GenericRestClient.java | 27 +++ .../java/org/apache/stratos/cli/RestClient.java | 121 +++++++++++ .../stratos/cli/RestCommandLineService.java | 104 ++++++++++ .../org.apache.stratos.adc.mgt.stub/pom.xml | 9 +- 7 files changed, 474 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/pom.xml ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/pom.xml b/components/org.apache.stratos.cli/pom.xml index 601b1d0..abeb934 100644 --- a/components/org.apache.stratos.cli/pom.xml +++ b/components/org.apache.stratos.cli/pom.xml @@ -100,6 +100,11 @@ <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.1.1</version> + </dependency> </dependencies> <build> <plugins> http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Cartridge.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Cartridge.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Cartridge.java new file mode 100644 index 0000000..dfcf409 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Cartridge.java @@ -0,0 +1,202 @@ +/* + * 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.cli; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Cartridge implements Comparable<Cartridge> { + + private String displayName; + private String description; + private String cartridgeAlias; + private String cartridgeType; + private int activeInstances; + private String status; + private String ip; + private String password; + private String provider; + private String version; + private boolean multiTenant; + private String hostName; + private String policy; + private String policyDescription; + private String repoURL; + private String dbUserName; + private String mappedDomain; + + private String[] accessURLs; + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCartridgeAlias() { + return cartridgeAlias; + } + + public void setCartridgeAlias(String cartridgeAlias) { + this.cartridgeAlias = cartridgeAlias; + } + + public String getCartridgeType() { + return cartridgeType; + } + + public void setCartridgeType(String cartridgeType) { + this.cartridgeType = cartridgeType; + } + + public int getActiveInstances() { + return activeInstances; + } + + public void setActiveInstances(int activeInstances) { + this.activeInstances = activeInstances; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getProvider() { + return provider; + } + + public void setProvider(String provider) { + this.provider = provider; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public boolean isMultiTenant() { + return multiTenant; + } + + public void setMultiTenant(boolean multiTenant) { + this.multiTenant = multiTenant; + } + + public String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + public String getPolicy() { + return policy; + } + + public void setPolicy(String policy) { + this.policy = policy; + } + + public String getPolicyDescription() { + return policyDescription; + } + + public void setPolicyDescription(String policyDescription) { + this.policyDescription = policyDescription; + } + + public String getRepoURL() { + return repoURL; + } + + public void setRepoURL(String repoURL) { + this.repoURL = repoURL; + } + + public String getDbUserName() { + return dbUserName; + } + + public String[] getAccessURLs() { + return accessURLs; + } + + public void setAccessURLs(String[] accessURLs) { + this.accessURLs = accessURLs; + } + + public void setDbUserName(String dbUserName) { + this.dbUserName = dbUserName; + } + + public String getMappedDomain() { + return mappedDomain; + } + + public void setMappedDomain(String mappedDomain) { + this.mappedDomain = mappedDomain; + } + + @Override + public int compareTo(Cartridge o) { + int compare = 0; + if (cartridgeAlias != null && o.cartridgeAlias != null) { + compare = cartridgeAlias.compareTo(o.cartridgeAlias); + } + if (compare == 0 && cartridgeType != null && o.cartridgeType != null) { + compare = cartridgeType.compareTo(o.cartridgeType); + } + return compare; + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CommandLineService.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CommandLineService.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CommandLineService.java index c5effb4..04aec45 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CommandLineService.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CommandLineService.java @@ -40,9 +40,9 @@ import org.apache.axis2.transport.http.HttpTransportProperties; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.stratos.adc.mgt.dto.xsd.Cartridge; -import org.apache.stratos.adc.mgt.dto.xsd.PolicyDefinition; -import org.apache.stratos.adc.mgt.dto.xsd.SubscriptionInfo; +import org.apache.stratos.adc.mgt.dto.Cartridge; +import org.apache.stratos.adc.mgt.dto.PolicyDefinition; +import org.apache.stratos.adc.mgt.dto.SubscriptionInfo; import org.apache.stratos.adc.mgt.stub.ApplicationManagementServiceADCExceptionException; import org.apache.stratos.adc.mgt.stub.ApplicationManagementServiceAlreadySubscribedExceptionException; import org.apache.stratos.adc.mgt.stub.ApplicationManagementServiceDomainMappingExistsExceptionException; @@ -223,7 +223,12 @@ public class CommandLineService { public void listAvailableCartridges() throws CommandException { try { - Cartridge[] multiTenantCatridges = stub.getAvailableCartridges(true); + System.out.println("Custom called"); + RestCommandLineService restService = new RestCommandLineService(); + restService.listAvailableCartridges(); + System.out.println("Custom end"); + + Cartridge[] multiTenantCatridges = stub.getAvailableCartridges(true); if (multiTenantCatridges == null) { if (logger.isDebugEnabled()) { http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java new file mode 100644 index 0000000..0a19c65 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java @@ -0,0 +1,27 @@ +/** + * 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.cli; + +public interface GenericRestClient { + + public String doPost(String resourcePath, String jsonParamString, String userName, String passWord); + public String doGet(String resourcePath, String userName, String passWord); + public void doDelete(); + public void doPut(); +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java new file mode 100644 index 0000000..18e4820 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java @@ -0,0 +1,121 @@ +/** + * 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.cli; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import java.net.MalformedURLException; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; + +public class RestClient implements GenericRestClient{ + + public String doPost(String resourcePath, String jsonParamString, String userName, String passWord) { + try { + + DefaultHttpClient httpClient = new DefaultHttpClient(); + + HttpPost postRequest = new HttpPost(resourcePath); + + StringEntity input = new StringEntity(jsonParamString); + input.setContentType("application/json"); + postRequest.setEntity(input); + + String userPass = userName + ":" + passWord; + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + postRequest.addHeader("Authorization", basicAuth); + + HttpResponse response = httpClient.execute(postRequest); + + if (response.getStatusLine().getStatusCode() != 200) { + System.out.println("Error"); + throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); + } + + BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); + + String output; + System.out.println("Output from Server .... \n"); + while ((output = br.readLine()) != null) { + System.out.println(output); + } + + httpClient.getConnectionManager().shutdown(); + return output; + + } catch (MalformedURLException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public String doGet(String resourcePath, String userName, String passWord) { + try { + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpGet getRequest = new HttpGet(resourcePath); + getRequest.addHeader("Content-Type", "application/json"); + + String userPass = userName + ":" + passWord; + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8")); + getRequest.addHeader("Authorization", basicAuth); + + HttpResponse response = httpClient.execute(getRequest); + + if (response.getStatusLine().getStatusCode() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); + } + + BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); + + String output; + String result = ""; + System.out.println("Output from Server .... \n"); + while ((output = br.readLine()) != null) { + //System.out.println(output); + result += output; + } + + httpClient.getConnectionManager().shutdown(); + return result; + + } catch (ClientProtocolException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public void doDelete() { + //To change body of implemented methods use File | Settings | File Templates. + } + + public void doPut() { + //To change body of implemented methods use File | Settings | File Templates. + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java new file mode 100644 index 0000000..8017ad0 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java @@ -0,0 +1,104 @@ +/** + * 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.cli; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.apache.stratos.cli.exception.CommandException; +import org.apache.stratos.cli.utils.CliConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import org.apache.stratos.cli.utils.RowMapper; +import org.apache.stratos.cli.utils.CommandLineUtils; + +public class RestCommandLineService { + + private static final Logger logger = LoggerFactory.getLogger(RestCommandLineService.class); + + RestClient restClientService = new RestClient(); + + RestCommandLineService() { + } + + private static class SingletonHolder { + private final static RestCommandLineService INSTANCE = new RestCommandLineService(); + } + + public static RestCommandLineService getInstance() { + return SingletonHolder.INSTANCE; + } + + public void listAvailableCartridges() throws CommandException { + try { + + String stratosURL = System.getenv(CliConstants.STRATOS_URL_ENV_PROPERTY); + String username = System.getenv(CliConstants.STRATOS_USERNAME_ENV_PROPERTY); + String password = System.getenv(CliConstants.STRATOS_PASSWORD_ENV_PROPERTY); + + System.out.println(stratosURL + " : " + username + " : " + password); + String resultString = restClientService.doGet("http://ec2-54-254-71-178.ap-southeast-1.compute.amazonaws.com:9765/stratos/admin/cartridge/list", "[email protected]", "admin123"); + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + CartridgeList cartridgeList = gson.fromJson(resultString, CartridgeList.class); + + if (cartridgeList == null) { + System.out.println("Object is null"); + } + + + + RowMapper<Cartridge> cartridgeMapper = new RowMapper<Cartridge>() { + + @Override + public String[] getData(Cartridge cartridge) { + String[] data = new String[3]; + data[0] = cartridge.getCartridgeType(); + data[1] = cartridge.getDisplayName(); + data[2] = cartridge.getVersion(); + return data; + } + }; + + Cartridge[] cartridges = new Cartridge[cartridgeList.getCartridge().size()]; + cartridges = cartridgeList.getCartridge().toArray(cartridges); + + CommandLineUtils.printTable(cartridges, cartridgeMapper, "Type", "Name", "Version"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private class CartridgeList { + private ArrayList<Cartridge> cartridge; + + public ArrayList<Cartridge> getCartridge() { + return cartridge; + } + + public void setCartridge(ArrayList<Cartridge> cartridge) { + this.cartridge = cartridge; + } + + CartridgeList() { + cartridge = new ArrayList<Cartridge>(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5fc89c38/service-stubs/org.apache.stratos.adc.mgt.stub/pom.xml ---------------------------------------------------------------------- diff --git a/service-stubs/org.apache.stratos.adc.mgt.stub/pom.xml b/service-stubs/org.apache.stratos.adc.mgt.stub/pom.xml index e7ec021..c12cdb2 100644 --- a/service-stubs/org.apache.stratos.adc.mgt.stub/pom.xml +++ b/service-stubs/org.apache.stratos.adc.mgt.stub/pom.xml @@ -51,8 +51,11 @@ <pathelement location="${settings.localRepository}/org/apache/axis2/wso2/axis2/${axis2.wso2.version}/axis2-${axis2.wso2.version}.jar"/> </path> <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true"> - <arg line="-uri src/main/resources/ApplicationManagementService.wsdl -u -uw -o target/generated-code -p org.apache.stratos.adc.mgt.stub"/> + <arg line="-uri src/main/resources/ApplicationManagementService.wsdl -u -uw -o target/generated-code -p org.apache.stratos.adc.mgt.stub -ns2p http://exception.mgt.adc.stratos.apache.org/xsd=org.apache.stratos.adc.mgt.exception,http://dto.mgt.adc.stratos.apache.org/xsd=org.apache.stratos.adc.mgt.dto"/> <classpath refid="wsdl2java.classpath"/> + <classpath refid="maven.dependency.classpath"/> + <classpath refid="maven.compile.classpath"/> + <classpath refid="maven.runtime.classpath"/> </java> </tasks> </configuration> @@ -93,8 +96,8 @@ <Export-Package> org.apache.stratos.adc.mgt.stub.*; version="${project.version}", org.apache.stratos.adc.mgt.service.*; version="${project.version}", - org.apache.stratos.adc.mgt.dto.xsd*; version="${project.version}", - org.apache.stratos.adc.mgt.exception.xsd.*; version="${project.version}" + org.apache.stratos.adc.mgt.dto.*; version="${project.version}", + org.apache.stratos.adc.mgt.exception.*; version="${project.version}" </Export-Package> <Import-Package> !org.apache.stratos.adc.mgt.stub.*
