/* ** Copyright [2012] [Megam Systems] ** ** Licensed 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.megam.deccanplato.provider.twilio.handler; import static org.megam.deccanplato.provider.Constants.*; import static org.megam.deccanplato.provider.twilio.Constants.*; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.message.BasicNameValuePair; import org.megam.deccanplato.http.TransportMachinery; import org.megam.deccanplato.http.TransportResponse; import org.megam.deccanplato.http.TransportTools; import org.megam.deccanplato.provider.BusinessActivity; import org.megam.deccanplato.provider.core.BusinessActivityInfo; /** * This class deals with the Application business functionalities, * such as create, list, update and delete applications. * Application contains application SID, this id use to make calls. * @author pandiyaraja * */ public class ApplicationImpl implements BusinessActivity{ private Map<String, String> args; private BusinessActivityInfo bizInfo; /* (non-Javadoc) * @see org.megam.deccanplato.provider.BusinessActivity#setArguments(org.megam.deccanplato.provider.core.BusinessActivityInfo, java.util.Map) */ @Override public void setArguments(BusinessActivityInfo tempBizInfo, Map<String, String> tempArgs) { this.args = tempArgs; this.bizInfo = tempBizInfo; } /* (non-Javadoc) * @see org.megam.deccanplato.provider.BusinessActivity#run() */ @Override public Map<String, String> run() { Map<String, String> outMap = new HashMap<String, String>(); switch (bizInfo.getActivityFunction()) { case CREATE: outMap=create(outMap); break; case LIST: outMap=list(outMap); break; case VIEW: outMap=view(outMap); break; case UPDATE: outMap=update(outMap); break; case DELETE: outMap=delete(outMap); break; } return outMap; } /** * Delete method deletes an application by based on application SID value * @param outMap * @return */ private Map<String, String> delete(Map<String, String> outMap) { final String account_view_url = TWILIO_URL + "Accounts/" + args.get(ACCOUNT_SID) + "/Applications/"+args.get(APPLICATION_SID)+".json"; Map<String, String> header = new HashMap<>(); header.put(PROVIDER, args.get(PROVIDER)); header.put(ACCOUNT_SID, args.get(ACCOUNT_SID)); header.put(OAUTH_TOKEN, args.get(OAUTH_TOKEN)); TransportTools tst = new TransportTools(account_view_url, null, header); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.delete(tst); responseBody = response.entityToString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; } /** * update method updates a particular application details by using application * SID, we can change application name and api version. * @param outMap * @return */ private Map<String, String> update(Map<String, String> outMap) { final String account_view_url = TWILIO_URL + "Accounts/" + args.get(ACCOUNT_SID) + "/Applications/"+args.get(APPLICATION_SID)+".json"; Map<String, String> header = new HashMap<>(); header.put(PROVIDER, args.get(PROVIDER)); header.put(ACCOUNT_SID, args.get(ACCOUNT_SID)); header.put(OAUTH_TOKEN, args.get(OAUTH_TOKEN)); List<NameValuePair> appsList=new ArrayList<NameValuePair>(); appsList.add(new BasicNameValuePair("FriendlyName", args.get(FRIENDLY_NAME))); //appsList.add(new BasicNameValuePair("ApiVersion", args.get(API_VERSION))); TransportTools tst = new TransportTools(account_view_url, appsList, header); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.post(tst); responseBody = response.entityToString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; } /** * This method returns a particular application details by using application SID. * @param outMap * @return */ private Map<String, String> view(Map<String, String> outMap) { final String account_view_url = TWILIO_URL + "Accounts/" + args.get(ACCOUNT_SID) + "/Applications.json?ApplicationSid="+args.get(APPLICATION_SID); Map<String, String> header = new HashMap<>(); header.put(PROVIDER, args.get(PROVIDER)); header.put(ACCOUNT_SID, args.get(ACCOUNT_SID)); header.put(OAUTH_TOKEN, args.get(OAUTH_TOKEN)); TransportTools tst = new TransportTools(account_view_url, null, header); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.get(tst); responseBody = response.entityToString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; } /** * This method lists all applications created by a user by using account SID * @param outMap * @return */ private Map<String, String> list(Map<String, String> outMap) { final String account_view_url = TWILIO_URL + "Accounts/" + args.get(ACCOUNT_SID) + "/Applications.json"; Map<String, String> header = new HashMap<>(); header.put(PROVIDER, args.get(PROVIDER)); header.put(ACCOUNT_SID, args.get(ACCOUNT_SID)); header.put(OAUTH_TOKEN, args.get(OAUTH_TOKEN)); TransportTools tst = new TransportTools(account_view_url, null, header); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.get(tst); responseBody = response.entityToString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; } /** * This method creates an application in twilio by using two * arguments are FriendlyName and ApiVersion. * @param outMap * @return */ private Map<String, String> create(Map<String, String> outMap) { final String account_view_url = TWILIO_URL + "Accounts/" + args.get(ACCOUNT_SID) + "/Applications.json"; Map<String, String> header = new HashMap<>(); header.put(PROVIDER, args.get(PROVIDER)); header.put(ACCOUNT_SID, args.get(ACCOUNT_SID)); header.put(OAUTH_TOKEN, args.get(OAUTH_TOKEN)); List<NameValuePair> appsList=new ArrayList<NameValuePair>(); appsList.add(new BasicNameValuePair("FriendlyName", args.get(FRIENDLY_NAME))); appsList.add(new BasicNameValuePair("ApiVersion", args.get(API_VERSION))); TransportTools tst = new TransportTools(account_view_url, appsList, header); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.post(tst); responseBody = response.entityToString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; } /* (non-Javadoc) * @see org.megam.deccanplato.provider.BusinessActivity#name() */ @Override public String name() { // TODO Auto-generated method stub return "application"; } }