Java tutorial
/* * ==================================================================== * 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. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package de.dtag.tlabs.cbclient; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.Timer; import java.util.TimerTask; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; /** * Elemental example for executing multiple GET requests sequentially. */ public class CBClient { /** * ANDSF Rule String to switch to WIFI, check subscription_id_data and ip addresses for the right IMSI */ private String toWiFi = "id=18&subscription_id_type=1&subscription_id_data=001011234567890&status=0&udp_alert_ip=192.168.103.63&udp_alert_port=10005&refresh_time=0&access_id=OpenEPCWLAN&location_xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22ISO-8859-1%22%3F%3E%0D%0A%3CUE%3E%3CSubscriptionId%3E%3CData%3E001011234567890%3C%2FData%3E%3CType%3E1%3C%2FType%3E%3C%2FSubscriptionId%3E%3CName%3EOpenEPC-MM%3C%2FName%3E%3CDiscoveryInformation%3E%3COpenEPCWLAN%3E%3CAccessNetworkType%3E3%3C%2FAccessNetworkType%3E%3CAccessNetworkArea%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CSSID%3EOpenEPCWLAN%3C%2FSSID%3E%3CBSSID%3E00%3A21%3A63%3Aa4%3A49%3Af5%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3C%2FWLAN_Location%3E%3C%2FAccessNetworkArea%3E%3CAccessNetworkInformationRef%3EOpenEPCWLAN%3C%2FAccessNetworkInformationRef%3E%3C%2FOpenEPCWLAN%3E%3C%2FDiscoveryInformation%3E%3CUE_Location%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CSSID%3EOpenEPCWLAN%3C%2FSSID%3E%3CBSSID%3E00%3A21%3A63%3Aa4%3A49%3Af5%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3C%2FWLAN_Location%3E%3C%2FUE_Location%3E%3CS14Alert%3E%3CAlertIP%3E192.168.103.63%3C%2FAlertIP%3E%3CAlertPort%3E10005%3C%2FAlertPort%3E%3C%2FS14Alert%3E%3C%2FUE%3E%0D%0A&push_xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22ISO-8859-1%22%3F%3E%0D%0A%3CANDSF%3E%3CSubscriptionId%3E%3CData%3E001011234567890%3C%2FData%3E%3CType%3E1%3C%2FType%3E%3C%2FSubscriptionId%3E%3CPolicy%3E%3CToLTE%3E%3CRulePriority%3E0%3C%2FRulePriority%3E%3CPrioritizedAccess%3E%3CN31%3E%3CAccessTechnology%3E1%3C%2FAccessTechnology%3E%3CAccessId%3ELTE%3C%2FAccessId%3E%3CAccessNetworkPriority%3E12%3C%2FAccessNetworkPriority%3E%3C%2FN31%3E%3C%2FPrioritizedAccess%3E%3CRoaming%3E0%3C%2FRoaming%3E%3CUpdatePolicy%3E0%3C%2FUpdatePolicy%3E%3C%2FToLTE%3E%3C%2FPolicy%3E%3CDiscoveryInformation%3E%3CFirst_AN_Area%3E%3CAccessNetworkType%3E103%3C%2FAccessNetworkType%3E%3CAccessNetworkArea%3E%3CTGPP_Location%3E%3CN3G_Cell_Number_1%3E%3CPLMN%3E1234%3C%2FPLMN%3E%3CTAC%3E5678%3C%2FTAC%3E%3CLAC%3E030%3C%2FLAC%3E%3CGERAN_CI%3E1%3C%2FGERAN_CI%3E%3CUTRAN_CI%3E2%3C%2FUTRAN_CI%3E%3CEUTRA_CI%3E2%3C%2FEUTRA_CI%3E%3C%2FN3G_Cell_Number_1%3E%3CN3G_Cell_Number_2%3E%3CPLMN%3E5678%3C%2FPLMN%3E%3C%2FN3G_Cell_Number_2%3E%3C%2FTGPP_Location%3E%3CTGPP2_Location%3E%3COnex%3E%3CN3GPP2_First_1x%3E%3CSID%3E2%3C%2FSID%3E%3CNID%3E3%3C%2FNID%3E%3CBase_ID%3E4%3C%2FBase_ID%3E%3C%2FN3GPP2_First_1x%3E%3CN3GPP2_Second_1x%3E%3CSID%3E1%3C%2FSID%3E%3CNID%3E2%3C%2FNID%3E%3CBase_ID%3E3%3C%2FBase_ID%3E%3C%2FN3GPP2_Second_1x%3E%3C%2FOnex%3E%3CHRPD%3E%3CN3GPP2_First_HRPD%3E%3CSector_ID%3E123%3C%2FSector_ID%3E%3CNetmask%3E123123%3C%2FNetmask%3E%3C%2FN3GPP2_First_HRPD%3E%3C%2FHRPD%3E%3C%2FTGPP2_Location%3E%3CWiMAX_Location%3E%3CWiMAX_Location_1%3E%3CNAP-ID%3E1%3C%2FNAP-ID%3E%3CBS-ID%3E2%3C%2FBS-ID%3E%3C%2FWiMAX_Location_1%3E%3CWiMAX_Location_2%3E%3CNAP-ID%3E3%3C%2FNAP-ID%3E%3CBS-ID%3E4%3C%2FBS-ID%3E%3C%2FWiMAX_Location_2%3E%3C%2FWiMAX_Location%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CHESSID%3E4%3C%2FHESSID%3E%3CSSID%3E5%3C%2FSSID%3E%3CBSSID%3E123%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3CWiFi_Location_2%3E%3CBSSID%3E456%3C%2FBSSID%3E%3C%2FWiFi_Location_2%3E%3C%2FWLAN_Location%3E%3CGeo_Location%3E%3CCircular%3E%3CFirst_Location%3E%3CAnchorLatitude%3E1.000000%3C%2FAnchorLatitude%3E%3CAnchorLongitude%3E2.000000%3C%2FAnchorLongitude%3E%3CRadius%3E3%3C%2FRadius%3E%3C%2FFirst_Location%3E%3C%2FCircular%3E%3C%2FGeo_Location%3E%3C%2FAccessNetworkArea%3E%3CAccessNetworkInformationRef%3Etest%3C%2FAccessNetworkInformationRef%3E%3C%2FFirst_AN_Area%3E%3C%2FDiscoveryInformation%3E%3C%2FANDSF%3E%0D%0A&push_4=6&key=18&src_id=&src_subscription_id_type=-1&src_subscription_id_data=&src_status=-1&src_access_id=&src_do=push_is_policies&src_page=0&src_order=subscription_id_data&push_cnt=8"; /** * ANDSF Rule String to switch to LTE, check subscription_id_data and ip addresses for the right IMSI */ private String toLTE = "id=18&subscription_id_type=1&subscription_id_data=001011234567890&status=0&udp_alert_ip=192.168.103.63&udp_alert_port=10005&refresh_time=0&access_id=OpenEPCWLAN&location_xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22ISO-8859-1%22%3F%3E%0D%0A%3CUE%3E%3CSubscriptionId%3E%3CData%3E001011234567890%3C%2FData%3E%3CType%3E1%3C%2FType%3E%3C%2FSubscriptionId%3E%3CName%3EOpenEPC-MM%3C%2FName%3E%3CDiscoveryInformation%3E%3COpenEPCWLAN%3E%3CAccessNetworkType%3E3%3C%2FAccessNetworkType%3E%3CAccessNetworkArea%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CSSID%3EOpenEPCWLAN%3C%2FSSID%3E%3CBSSID%3E00%3A21%3A63%3Aa4%3A49%3Af5%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3C%2FWLAN_Location%3E%3C%2FAccessNetworkArea%3E%3CAccessNetworkInformationRef%3EOpenEPCWLAN%3C%2FAccessNetworkInformationRef%3E%3C%2FOpenEPCWLAN%3E%3C%2FDiscoveryInformation%3E%3CUE_Location%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CSSID%3EOpenEPCWLAN%3C%2FSSID%3E%3CBSSID%3E00%3A21%3A63%3Aa4%3A49%3Af5%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3C%2FWLAN_Location%3E%3C%2FUE_Location%3E%3CS14Alert%3E%3CAlertIP%3E192.168.103.63%3C%2FAlertIP%3E%3CAlertPort%3E10005%3C%2FAlertPort%3E%3C%2FS14Alert%3E%3C%2FUE%3E%0D%0A&push_xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22ISO-8859-1%22%3F%3E%0D%0A%3CANDSF%3E%3CSubscriptionId%3E%3CData%3E001011234567890%3C%2FData%3E%3CType%3E1%3C%2FType%3E%3C%2FSubscriptionId%3E%3CPolicy%3E%3CWiFi_LTE_GSM_IS_Rule%3E%3CRulePriority%3E0%3C%2FRulePriority%3E%3CPrioritizedAccess%3E%3CN20%3E%3CAccessTechnology%3E3%3C%2FAccessTechnology%3E%3CAccessId%3EOpenEPCWLAN%3C%2FAccessId%3E%3CAccessNetworkPriority%3E1%3C%2FAccessNetworkPriority%3E%3C%2FN20%3E%3CN18%3E%3CAccessTechnology%3E1%3C%2FAccessTechnology%3E%3CAccessId%3ELTE%3C%2FAccessId%3E%3CAccessNetworkPriority%3E10%3C%2FAccessNetworkPriority%3E%3C%2FN18%3E%3CN25%3E%3CAccessTechnology%3E1%3C%2FAccessTechnology%3E%3CAccessId%3EGSM%3C%2FAccessId%3E%3CAccessNetworkPriority%3E15%3C%2FAccessNetworkPriority%3E%3C%2FN25%3E%3C%2FPrioritizedAccess%3E%3CRoaming%3E0%3C%2FRoaming%3E%3CUpdatePolicy%3E0%3C%2FUpdatePolicy%3E%3C%2FWiFi_LTE_GSM_IS_Rule%3E%3C%2FPolicy%3E%3CDiscoveryInformation%3E%3CFirst_AN_Area%3E%3CAccessNetworkType%3E103%3C%2FAccessNetworkType%3E%3CAccessNetworkArea%3E%3CTGPP_Location%3E%3CN3G_Cell_Number_1%3E%3CPLMN%3E1234%3C%2FPLMN%3E%3CTAC%3E5678%3C%2FTAC%3E%3CLAC%3E030%3C%2FLAC%3E%3CGERAN_CI%3E1%3C%2FGERAN_CI%3E%3CUTRAN_CI%3E2%3C%2FUTRAN_CI%3E%3CEUTRA_CI%3E2%3C%2FEUTRA_CI%3E%3C%2FN3G_Cell_Number_1%3E%3CN3G_Cell_Number_2%3E%3CPLMN%3E5678%3C%2FPLMN%3E%3C%2FN3G_Cell_Number_2%3E%3C%2FTGPP_Location%3E%3CTGPP2_Location%3E%3COnex%3E%3CN3GPP2_First_1x%3E%3CSID%3E2%3C%2FSID%3E%3CNID%3E3%3C%2FNID%3E%3CBase_ID%3E4%3C%2FBase_ID%3E%3C%2FN3GPP2_First_1x%3E%3CN3GPP2_Second_1x%3E%3CSID%3E1%3C%2FSID%3E%3CNID%3E2%3C%2FNID%3E%3CBase_ID%3E3%3C%2FBase_ID%3E%3C%2FN3GPP2_Second_1x%3E%3C%2FOnex%3E%3CHRPD%3E%3CN3GPP2_First_HRPD%3E%3CSector_ID%3E123%3C%2FSector_ID%3E%3CNetmask%3E123123%3C%2FNetmask%3E%3C%2FN3GPP2_First_HRPD%3E%3C%2FHRPD%3E%3C%2FTGPP2_Location%3E%3CWiMAX_Location%3E%3CWiMAX_Location_1%3E%3CNAP-ID%3E1%3C%2FNAP-ID%3E%3CBS-ID%3E2%3C%2FBS-ID%3E%3C%2FWiMAX_Location_1%3E%3CWiMAX_Location_2%3E%3CNAP-ID%3E3%3C%2FNAP-ID%3E%3CBS-ID%3E4%3C%2FBS-ID%3E%3C%2FWiMAX_Location_2%3E%3C%2FWiMAX_Location%3E%3CWLAN_Location%3E%3COpenEPCWLAN%3E%3CHESSID%3E4%3C%2FHESSID%3E%3CSSID%3E5%3C%2FSSID%3E%3CBSSID%3E123%3C%2FBSSID%3E%3C%2FOpenEPCWLAN%3E%3CWiFi_Location_2%3E%3CBSSID%3E456%3C%2FBSSID%3E%3C%2FWiFi_Location_2%3E%3C%2FWLAN_Location%3E%3CGeo_Location%3E%3CCircular%3E%3CFirst_Location%3E%3CAnchorLatitude%3E1.000000%3C%2FAnchorLatitude%3E%3CAnchorLongitude%3E2.000000%3C%2FAnchorLongitude%3E%3CRadius%3E3%3C%2FRadius%3E%3C%2FFirst_Location%3E%3C%2FCircular%3E%3C%2FGeo_Location%3E%3C%2FAccessNetworkArea%3E%3CAccessNetworkInformationRef%3Etest%3C%2FAccessNetworkInformationRef%3E%3C%2FFirst_AN_Area%3E%3C%2FDiscoveryInformation%3E%3C%2FANDSF%3E%0D%0A&push_3=8&key=18&src_id=&src_subscription_id_type=-1&src_subscription_id_data=&src_status=-1&src_access_id=&src_do=push_is_policies&src_page=0&src_order=subscription_id_data&push_cnt=8"; /** * ANDSF Host Address */ private String httpHostAddr = "192.168.100.100"; /** * ANDSF Host Port */ private int httpHostPort = 80; /** * ANDSF Login UserName */ private String adminName = "admin"; /** * ANDSF Login Password */ private String adminPassword = "admin"; /** * ANDSF Resource */ private String andsfResource = "/andsf/s14_clients.php"; /** * Context Broker IP Address */ private String contextBrokerIPAddr = "192.168.100.100"; /** * Context Broker Port */ private int contextBrokerPort = 80; /** * Context Broker Resource Address */ private String contextBrokerRequestLine = "/fuseco/answercontextrequest.xml"; private final int pollingSeconds = 1; private enum accessModes { NONE, LTE, WIFI }; private CloseableHttpClient httpClient; private PoolingHttpClientConnectionManager cm; private accessModes currentPolicy = accessModes.NONE; private accessModes newPolicy = accessModes.NONE; private String sendingPolicy = ""; private Timer pollingTimer; public static void main(String[] args) throws Exception { CBClient myInstance = new CBClient(); myInstance.start(); } /** * Is called by the polling Timer */ private void performPolling() { HttpHost targetHost = new HttpHost(contextBrokerIPAddr, contextBrokerPort, "http"); HttpClientContext context = HttpClientContext.create(); // HttpGetWithEntity httpGet = new HttpGetWithEntity(contextBrokerRequestLine); //httpGet.setHeader("Content-Type", "application/xml"); HttpPost httpPostContext = new HttpPost(contextBrokerRequestLine); String getXMLInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<queryContextRequest>" + "<entityIdList>" + "<entityId type=\"User_Context\" isPattern=\"false\">" + "<id>User1</id>" + "</entityId>" + "</entityIdList>" + "<attributeList/>" + "</queryContextRequest>"; StringEntity getEntity = new StringEntity(getXMLInfo, ContentType.APPLICATION_XML); httpPostContext.setEntity(getEntity); try { CloseableHttpResponse response = httpClient.execute(targetHost, httpPostContext, context); HttpEntity entity = response.getEntity(); // System.out.println("Response: " + response.getStatusLine().toString()); // System.out.println("Response Body-Type: " + entity.getContentType()); StringWriter writer = new StringWriter(); InputStream is = entity.getContent(); IOUtils.copy(is, writer); String responseContent = writer.toString(); System.out.println(responseContent); checkForHandOver(responseContent); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void checkForHandOver(String responseContent) { if (null == responseContent) { return; } /* * Check for access technology */ if (responseContent.contains("WIFI")) { setNewPolicy(accessModes.WIFI); } if (responseContent.contains("LTE")) { setNewPolicy(accessModes.LTE); } } private void setNewPolicy(accessModes newMode) { if (!currentPolicy.equals(newMode)) { newPolicy = newMode; performHO(); } else { System.out.println("Current Policy: " + currentPolicy + " == New Policy : " + newPolicy); } } public void start() { /* * Run the Polling stuff */ pollingTimer = new Timer(); pollingTimer.schedule(new TimerTask() { @Override public void run() { performPolling(); } }, 0L, pollingSeconds * 1000L); } public CBClient() { cm = new PoolingHttpClientConnectionManager(); httpClient = HttpClients.custom().setConnectionManager(cm).build(); System.out.println("CM: " + cm); System.out.println("httpClient: " + httpClient); } public void performHO() { try { System.out.println("Perform HO"); HttpHost targetHost = new HttpHost(httpHostAddr, httpHostPort, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "epc")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); HttpPost httpPost = new HttpPost(andsfResource); switch (newPolicy) { case NONE: break; case LTE: //Switch over to LTE sendingPolicy = toLTE; currentPolicy = accessModes.LTE; System.out.println("SWITCHING OVER TO LTE"); break; case WIFI: //Switch over to WIFI sendingPolicy = toWiFi; currentPolicy = accessModes.WIFI; System.out.println("SWITCHING OVER TO WIFI"); break; default: break; } StringEntity postEntity = new StringEntity(sendingPolicy, ContentType.APPLICATION_FORM_URLENCODED); httpPost.setEntity(postEntity); StringWriter writer = new StringWriter(); System.out.println("httpPost: " + httpPost); CloseableHttpResponse response = httpClient.execute(targetHost, httpPost, context); try { HttpEntity entity = response.getEntity(); System.out.println("Response: " + response.getStatusLine().toString()); InputStream is = entity.getContent(); IOUtils.copy(is, writer); String responseContent = writer.toString(); if (responseContent.contains("Success")) { System.out.println("Response content: Success"); } else { System.out.println("Response content: No Success found"); } } finally { response.close(); } } catch (IOException e) { System.out.println("IOException found"); e.printStackTrace(); } catch (IllegalStateException e) { System.out.println("Exception found"); e.printStackTrace(); } } }