Java tutorial
/* * M2M ServiceFOTA ONM version 1.0 * * Copyright 2014 kt corp. All rights reserved. * * This is a proprietary software of kt corp, and you may not use this file except in * compliance with license agreement with kt corp. Any redistribution or use of this * software, with or without modification shall be strictly prohibited without prior written * approval of kt corp, and the copyright notice above does not evidence any actual or * intended publication of such software. */ package com.fota.Link; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.codec.binary.Base64; import org.jdom2.JDOMException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import com.fota.batch.vo.SDPInfoVO; import com.fota.comm.util.PropUtil; public class sdpApi { private static final Logger logger = LoggerFactory.getLogger(sdpApi.class); public SDPInfoVO getSpecificSubscpnInfo(String CTN) { //String resultStr = null; SDPInfoVO resVO = new SDPInfoVO(); String strIMEI = null; String Model_Name = null; String return_cd = null; try { String endPointUrl = PropUtil.getPropValue("sdp.oif114.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:getSpecificSubscpnInfoRequest>" + " <!--You may enterthe following 6 items in any order-->" // + " <sdp:Credt_Id></sdp:Credt_Id>" + " <sdp:User_Name>" + CTN + "</sdp:User_Name>" + "<sdp:Credt_Type_Cd>" + "05" + "</sdp:Credt_Type_Cd>" + " </sdp:getSpecificSubscpnInfoRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = null; String parseStr = null; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; } //resultStr = line; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; // sdp:returnCode while (list.item(i) != null) { element = (Element) list.item(i); //System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //System.out.println(element.getNodeName()); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); //System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55"); //System.out.println(element.getNodeName()); //System.out.println(element.getFirstChild().getNodeName()); if (element.getNodeName().equals("n1:Resource_Unique_Id")) { if (element.getNextSibling().getFirstChild().getNodeValue().equals(("06"))) { //System.out.println("%%%%%%%%%%%%%%%%% FINDFIND!!! %%%%%%%%%%%%%%%%%%%%%%%%%%%55"); strIMEI = element.getFirstChild().getNodeValue(); //System.out.println("%%%%%%%%%%%%%%%%% FINDFIND!!! " + strIMEI + "%%%%%%%%%%%%%%%%%%%%%%%%%%%55"); } //resultStr = element.getFirstChild().getNodeName(); } if (element.getNodeName().equals("n1:Resource_Model_Name")) { Model_Name = element.getFirstChild().getNodeValue(); } if (element.getNodeName().equals("sdp:returnCode")) { return_cd = element.getFirstChild().getNodeValue(); } //n1:Resource_Model_Name System.out.println(contents); } i++; } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } resVO.setModem_model_nm(Model_Name); resVO.setImei(strIMEI); resVO.setReturn_cd(return_cd); return resVO; } public String GetBasicUserInfoAndMarketInfo(String CTN) throws JDOMException { String resultStr = null; StringBuffer logSb = new StringBuffer(); try { String endPointUrl = PropUtil.getPropValue("sdp.oif516.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:getBasicUserInfoAndMarketInfoRequest>" + " <!--You may enterthe following 6 items in any order-->" + " <sdp:CALL_CTN>" + CTN + "</sdp:CALL_CTN>" + " </sdp:getBasicUserInfoAndMarketInfoRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; logSb.append("\r\n---------- GetBasicUserInfoAndMarketInfo(" + CTN + ") ----------"); logSb.append("\r\nendPointUrl : " + endPointUrl); logSb.append("\r\nrequest msg : \r\n==============================\r\n" + strRequest + "\r\n===============================\r\n"); logger.info(logSb.toString()); logSb.setLength(0); // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = null; String resValue = null; String parseStr = null; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; while (list.item(i) != null) { element = (Element) list.item(i); // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); // System.out.println(element.getNodeName()); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); // System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55"); // System.out.println(element.getNodeName()); // System.out.println(element.getFirstChild().getNodeName()); if (element.getNodeName().equals("sdp:PREPAID")) { resultStr = contents; } System.out.println(contents); } i++; } //resultStr = resValue; connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return resultStr; } public String GetBasicUserInfoAndMarketInfo2(String CTN) throws JDOMException { String resultStr = null; try { String endPointUrl = PropUtil.getPropValue("sdp.oif516.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:getBasicUserInfoAndMarketInfoRequest>" + " <!--You may enterthe following 6 items in any order-->" + " <sdp:CALL_CTN>" + CTN + "</sdp:CALL_CTN>" + " </sdp:getBasicUserInfoAndMarketInfoRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = null; String resValue = null; String parseStr = null; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; while (list.item(i) != null) { element = (Element) list.item(i); // System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); // System.out.println(element.getNodeName()); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); // System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55"); // System.out.println(element.getNodeName()); // System.out.println(element.getFirstChild().getNodeName()); if (element.getNodeName().equals("sdp:CUSTOMER_LINK_NAME")) { resultStr = element.getFirstChild().getNodeValue(); } System.out.println(contents); } i++; } //resultStr = resValue; resultStr = java.net.URLDecoder.decode(resultStr, "euc-kr"); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return resultStr; } public static String getNcnInfo(String CTN) throws JDOMException { String resultStr = ""; String logData = ""; try { String endPointUrl = PropUtil.getPropValue("sdp.oif516.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:getBasicUserInfoAndMarketInfoRequest>" + " <!--You may enterthe following 6 items in any order-->" + " <sdp:CALL_CTN>" + CTN + "</sdp:CALL_CTN>" + " </sdp:getBasicUserInfoAndMarketInfoRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; logData = "\r\n---------- Get Ncn Req Info start ----------\r\n"; logData += " get Ncn Req Info - endPointUrl : " + endPointUrl; logData += "\r\n get Ncn Req Info - Content-type : text/xml;charset=utf-8"; logData += "\r\n get Ncn Req Info - RequestMethod : POST"; logData += "\r\n get Ncn Req Info - xml : " + strRequest; logData += "\r\n---------- Get Ncn Req Info end ----------"; logger.info(logData); // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = ""; String resValue = ""; String parseStr = ""; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; String contractNum = ""; String customerId = ""; while (list.item(i) != null) { element = (Element) list.item(i); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); System.out.println(element.getNodeName() + " / " + element.getFirstChild().getNodeName()); if (element.getNodeName().equals("sdp:NS_CONTRACT_NUM")) { // resultStr = element.getFirstChild().getNodeValue(); contractNum = element.getFirstChild().getNodeValue(); } if (element.getNodeName().equals("sdp:NS_CUSTOMER_ID")) { customerId = element.getFirstChild().getNodeValue(); } // System.out.println(" >>>>> " + contents); } i++; } // System.out.println("contractNum : " + contractNum + " / cusomerId : " + customerId); resultStr = getNcnFromContractnumAndCustomerId(contractNum, customerId); // System.out.println("ncn : " + resultStr); //resultStr = resValue; // resultStr = java.net.URLDecoder.decode(resultStr, "euc-kr"); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return resultStr; } public static String getNcnFromContractnumAndCustomerId(String contractNum, String cusomerId) { String retNcn = ""; // contractNum ncn ? int totalOddNum = 0; int totalEvenNum = 0; for (int i = 0; i < contractNum.length(); i++) { if (i % 2 == 0) { totalOddNum += Integer.parseInt(contractNum.substring(i, i + 1)); } else { totalEvenNum += Integer.parseInt(contractNum.substring(i, i + 1)); } } totalOddNum *= 2; totalEvenNum *= 4; retNcn = contractNum + ((totalOddNum + totalEvenNum) % 10); // cusomerId ncn ? totalOddNum = 0; totalEvenNum = 0; for (int i = 0; i < cusomerId.length(); i++) { if (i % 2 == 0) { totalOddNum += Integer.parseInt(cusomerId.substring(i, i + 1)); } else { totalEvenNum += Integer.parseInt(cusomerId.substring(i, i + 1)); } } totalOddNum *= 2; totalEvenNum *= 4; retNcn += cusomerId + ((totalOddNum + totalEvenNum) % 10); return retNcn; } public static String getCtnInfo(String ncn) throws JDOMException { String resultStr = ""; String logData = ""; try { String cpId = PropUtil.getPropValue("sdp3g.id"); String cpPw = PropUtil.getPropValue("sdp3g.pw"); String authorization = cpId + ":" + cpPw; logData = "\r\n---------- Get Ctn Req Info start ----------\r\n"; logData += " getCtnRequestInfo - authorization : " + authorization; byte[] encoded = Base64.encodeBase64(authorization.getBytes()); authorization = new String(encoded); String contractType = "0"; String contractNum = ncn.substring(0, 9); String customerId = ncn.substring(10, ncn.length() - 1); JSONObject reqJObj = new JSONObject(); reqJObj.put("transactionid", ""); reqJObj.put("sequenceno", ""); reqJObj.put("userid", ""); reqJObj.put("screenid", ""); reqJObj.put("CONTRACT_NUM", contractNum); reqJObj.put("CUSTOMER_ID", customerId); reqJObj.put("CONTRACT_TYPE", contractType); authorization = "Basic " + authorization; String endPointUrl = PropUtil.getPropValue("sdp.oif555.url"); logData += "\r\n getCtnRequestInfo - endPointUrl : " + endPointUrl; logData += "\r\n getCtnRequestInfo - authorization(encode) : " + authorization; logData += "\r\n getCtnRequestInfo - Content-type : application/json"; logData += "\r\n getCtnRequestInfo - RequestMethod : POST"; logData += "\r\n getCtnRequestInfo - json : " + reqJObj.toString(); logData += "\r\n---------- Get Ctn Req Info end ----------"; logger.info(logData); // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", authorization); connection.setRequestProperty("Content-type", "application/json"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(reqJObj.toJSONString()); wr.flush(); wr.close(); // input BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine = ""; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONParser jsonParser = new JSONParser(); JSONObject respJsonObject = (JSONObject) jsonParser.parse(response.toString()); // String respTransactionId = (String) respJsonObject.get("transactionid"); // String respSequenceNo = (String) respJsonObject.get("sequenceno"); // String respReturnCode = (String) respJsonObject.get("returncode"); // String respReturnDescription = (String) respJsonObject.get("returndescription"); // String respErrorCode = (String) respJsonObject.get("errorcode"); // String respErrorDescription = (String) respJsonObject.get("errordescription"); String respCtn = (String) respJsonObject.get("ctn"); // String respSubStatus = (String) respJsonObject.get("sub_status"); // String respSubStatusDate = (String) respJsonObject.get("sub_status_date"); resultStr = respCtn; //resultStr = resValue; // resultStr = java.net.URLDecoder.decode(resultStr, "euc-kr"); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return resultStr; } public String CheckNumberPortabilityByPhoneNumber(String CTN) throws SAXException, IOException, ParserConfigurationException { String resultStr = null; try { String endPointUrl = PropUtil.getPropValue("sdp.oif552.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:checkNumberPortabilityByPhoneNumberRequest>" + " <!--You may enterthe following 6 items in any order-->" + " <sdp:CALL_CTN>" + CTN + "</sdp:CALL_CTN>" + " </sdp:checkNumberPortabilityByPhoneNumberRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; // phone_number + telco_code + party_name + birth_date + [gender + // foreigner](?) // + auth_code + random_no + action_type // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); // connection.setRequestProperty("Content-Length", // String.valueOf(strRequest.length())); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = null; String resValue = null; String parseStr = null; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; //int nIndex = resultStr.indexOf("<sdp:ROUTING_DIGIT>"); //resValue = resultStr.substring(nIndex,nIndex+1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; while (list.item(i) != null) { element = (Element) list.item(i); System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println(element.getNodeName()); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55"); System.out.println(element.getNodeName()); System.out.println(element.getFirstChild().getNodeName()); if (element.getNodeName().equals("sdp:ROUTING_DIGIT")) { //nero17 //resultStr = element.getFirstChild().getNodeName(); resultStr = contents; } System.out.println(contents); } i++; } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } if (resultStr.length() < 2) { resultStr = ""; } return resultStr; } }