方槍槍的java世界

          不要因為風雨飄落就停止了你的腳步,真正的得失就在你的心中。 做喜歡做的事,不輕言放棄!

          03 一個簡單的HttpServer_Command_CommandImpl

          package com.tianhe.frm.http;

          public interface Command
          {
              public byte[] execute(String resource, char[] postData, String charset);
          }


          package com.tianhe.frm.http;

          import java.io.UnsupportedEncodingException;
          import java.util.Properties;

          import com.tianhe.frm.context.GlobalConfig;
          import com.tianhe.frm.utils.PropertiesUtil;
          import com.tianhe.frm.utils.StringUtil;

          public class CommandImpl implements Command
          {
              private String DEFAULT_RESPONSE;
             
              private String resultConfig = "response.properties";
             
              private Properties resultMapping;
             
              public CommandImpl()
              {
                  init(resultConfig);
              }
             
              private void init(String resultConfig2)
              {
                  try
                  {
                      if (StringUtil.isNotEmpty(resultConfig))
                      {
                          this.resultMapping = PropertiesUtil.loadProperties(ProcessorImpl.class, resultConfig);
                      }
                     
                      this.DEFAULT_RESPONSE = createDefaultResponse();
                  }
                  catch (Exception e)
                  {
                      System.err.println("ProcessorImpl.init failed, ignored!");
                  }
              }
             
              protected String createDefaultResponse()
              {
                  String defaultResponse = GlobalConfig.getString("ProcessorImpl.DEFAULT_RESPONSE",
                          "request url[%REQUEST_URL%] is not configured in reponse config!");
                  return defaultResponse;
              }
             
              public byte[] execute(String resource, char[] postData, String charset)
              {
                  String resKey = resource;
                  resKey = resKey.replace("=", "__");
                  resKey = resKey.replace(":", "--");
                  resKey = resKey.replace("\"", "");
                 
                  byte[] resVal = createResultData(resource, resKey, charset);
                 
                  return resVal;
              }
             
              protected byte[] createResultData(String resource, String resKey, String charset)
              {
                  String resVal = resultMapping.getProperty(resKey);
                  if (StringUtil.isEmpty(resVal))
                  {
                      resVal = DEFAULT_RESPONSE.replaceAll("%REQUEST_URL%", resource);
                  }
                 
                  byte[] resData = null;
                  try
                  {
                      // resVal = URLEncoder.encode(resVal, charset);
                      resData = resVal.getBytes(charset);
                  }
                  catch (UnsupportedEncodingException e)
                  {
                      e.printStackTrace();
                  }
                 
                  return resData;
              }
             
              public static void main(String[] args)
              {
                  CommandImpl s = new CommandImpl();
                  Properties p = s.resultMapping;
                  for (Object key : p.keySet())
                  {
                      System.out.println(key + "<->" + p.getProperty(key.toString()));
                  }
              }
          }

          posted on 2012-07-09 22:14 做強大的自己 閱讀(185) 評論(0)  編輯  收藏 所屬分類: Socket

          主站蜘蛛池模板: 惠州市| 伊川县| 长丰县| 武宣县| 阳东县| 正镶白旗| 吉木萨尔县| 剑河县| 卓尼县| 和静县| 肇州县| 龙岩市| 依兰县| 东至县| 衡山县| 监利县| 全南县| 闵行区| 新源县| 筠连县| 龙川县| 赣榆县| 青海省| 万载县| 涞水县| 凯里市| 宁德市| 左贡县| 兴和县| 合江县| 辉南县| 布拖县| 隆尧县| 微山县| 精河县| 明星| 金坛市| 洪江市| 马尔康县| 瓮安县| 钟山县|