方槍槍的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

          主站蜘蛛池模板: 忻城县| 同仁县| 来凤县| 眉山市| 乡宁县| 宝坻区| 沙坪坝区| 马边| 龙泉市| 宁德市| 宜章县| 天水市| 阆中市| 闽侯县| 永平县| 东乌珠穆沁旗| 汉川市| 肇源县| 鄢陵县| 定结县| 科技| 白河县| 乌兰浩特市| 诸城市| 浮梁县| 常州市| 尚义县| 海原县| 娄底市| 建瓯市| 永吉县| 普格县| 阜新市| 白水县| 克拉玛依市| 安达市| 邹城市| 莎车县| 新乐市| 新建县| 云阳县|