반응형

package com.collabi.red.sys.openapi.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.collabi.red.ext.common.utils.StringUtil;

import net.sf.json.JSONObject;

public class OpenApiTest {

 private static final Logger logger = LoggerFactory.getLogger(OpenApiTest.class);

 public OpenApiTest() {

 }

 public Map<String, Object> setParamMap() {
  Map<String, Object> dataMap = new ConcurrentHashMap<String, Object>();
  dataMap.put("PARAM1", "APPLE");
  dataMap.put("PARAM2", "WOOD");
  return dataMap;
 }

 public void callApiCommon() {
  String defUrl = "subinto.test.com";
  String encodType = "UTF-8";
  StringBuffer sb = new StringBuffer();
  sb.append(defUrl);
  Map<String, Object> dataMap = this.setParamMap();
  this.getRedundancyInterLook(sb.toString(), this.setParamMapToJson(dataMap), encodType);
 }

 public String setParamMapToJson(Map<String, Object> dataMap) {
  JSONObject jsonParam = new JSONObject();
  jsonParam.putAll(dataMap);
  StringBuffer sbEqParam = new StringBuffer();
  try {
   sbEqParam.append("paramData=").append(URLEncoder.encode(jsonParam.toString().replaceAll("%", "%25"), "UTF-8"));
  } catch (UnsupportedEncodingException e) {
   logger.error(e.getMessage());
  }
  return sbEqParam.toString();
 }

 private void getRedundancyInterLook(String interLookUrl, String urlParameters, String encodType) {
  System.out.println("############################ : URL " + interLookUrl);
  System.out.println("############################ : urlParameters " + urlParameters);
  JSONObject jsonObj = this.concertFromHTTPtoJSON(interLookUrl, urlParameters, encodType);
  System.out.println(" :::::::::: interLookUrl : " + interLookUrl);
  System.out.println(" :::::::::: viewType   : " + encodType);
  System.out.println(" :::::::::: jsonObj   : " + ToStringBuilder.reflectionToString(jsonObj));
 }

 private JSONObject concertFromHTTPtoJSON(String xmlUrl, String urlParameters, String encodType) {
  URL url = null;
  HttpURLConnection httpUrlConnection = null;
  BufferedWriter bw = null;
  InputStream is = null;
  JSONObject jsonObj = null;

  try {
   url = new URL(xmlUrl);
   URLConnection urlConnection = url.openConnection();
   httpUrlConnection = (HttpURLConnection) urlConnection;

   String postData = urlParameters;

   httpUrlConnection.setDoInput(true);
   httpUrlConnection.setDoOutput(true);
   httpUrlConnection.setRequestMethod("POST");
   httpUrlConnection.setUseCaches(false);
   httpUrlConnection.setDefaultUseCaches(false);
   httpUrlConnection.setRequestProperty("Content-Length", "0");
   httpUrlConnection.setRequestProperty("Content-Language", encodType);

   bw = new BufferedWriter(new OutputStreamWriter(httpUrlConnection.getOutputStream(), "UTF-8"));
   bw.write(postData);
   bw.flush();

   is = httpUrlConnection.getInputStream();

   BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
   String jsonText = this.readAll(rd);
   jsonObj = JSONObject.fromObject(StringUtil.converterDecoding(jsonText, encodType));

   bw.close();
   is.close();
   is = null;
  } catch (MalformedURLException e) {
   logger.error(e.getMessage());
  } catch (IOException e) {
   logger.error(e.getMessage());
  } finally {

   if (httpUrlConnection != null)
    try {
     httpUrlConnection.disconnect();
     httpUrlConnection = null;
    } catch (Exception e) {
     logger.error(e.getMessage());
    }

   if (bw != null)
    try {
     bw.close();
    } catch (Exception e) {
     logger.error(e.getMessage());
    }

   if (is != null)
    try {
     is.close();
     is = null;
    } catch (Exception e) {
     logger.error(e.getMessage());
    }
  }

  return jsonObj;
 }

 private String readAll(BufferedReader rd) throws IOException {
  StringBuilder sb = new StringBuilder();

  String inputStr;
  while ((inputStr = rd.readLine()) != null) {
   sb.append(inputStr);
  }

  return sb.toString();
 }

 public static void main(String[] args) {
  new OpenApiTest().callApiCommon();
 }

}

반응형

+ Recent posts