Coverage Report - jp.co.y_net.amm.common.AppUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AppUtils
0%
0/112
0%
0/60
0
 
 1  
 package jp.co.y_net.amm.common;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.UnsupportedEncodingException;
 5  
 import java.net.HttpURLConnection;
 6  
 import java.net.MalformedURLException;
 7  
 import java.net.ProtocolException;
 8  
 import java.net.URL;
 9  
 import java.net.URLDecoder;
 10  
 import java.net.URLEncoder;
 11  
 import java.text.DateFormat;
 12  
 import java.text.SimpleDateFormat;
 13  
 import java.util.ArrayList;
 14  
 import java.util.Calendar;
 15  
 import java.util.Date;
 16  
 import java.util.HashMap;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 
 20  
 import jp.co.y_net.amm.dao.AppDef;
 21  
 import jp.co.y_net.amm.dao.Usr;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 
 25  
 // TODO メッセージ取得部品の作成と利用
 26  
 
 27  0
 public class AppUtils {
 28  
 
 29  
     public static String urlDecode(String str) {
 30  
         try {
 31  0
             str = URLDecoder.decode(str, "UTF-8"); // URLデコード
 32  0
         } catch (UnsupportedEncodingException e) {
 33  0
             throw new RuntimeException(e);
 34  
         }
 35  0
         return str;
 36  
     }
 37  
     
 38  
     
 39  0
     private static final DateFormat DF_LONG = new SimpleDateFormat("yyyyMMddHHmmss");
 40  0
     private static final DateFormat DF_SHORT = new SimpleDateFormat("yyyyMMdd");
 41  
 
 42  
 
 43  
     /**
 44  
      * @return 現在日付
 45  
      */
 46  
     public static Integer createNowShort() {
 47  0
         Date d = new Date();
 48  0
         String s = DF_SHORT.format(d);
 49  0
         Integer n = Integer.parseInt(s);
 50  0
         return n;
 51  
     }
 52  
     /**
 53  
      * @return 現在日時
 54  
      */
 55  
     public static Long createNowLong() {
 56  0
         Date d = new Date();
 57  0
         String s = DF_LONG.format(d);
 58  0
         Long n = Long.parseLong(s);
 59  0
         return n;
 60  
     }
 61  
     /**
 62  
      * @param offset 加算する[秒]
 63  
      * @return 現在日時に offset を加算した時間
 64  
      */
 65  
     public static Long createNowLong(int offset) {
 66  0
         Calendar c = Calendar.getInstance();
 67  0
         c.add(Calendar.SECOND, offset);
 68  0
         Date d = c.getTime();
 69  0
         String s = DF_LONG.format(d);
 70  0
         Long n = Long.parseLong(s);
 71  0
         return n;
 72  
     }
 73  
     
 74  
     public static Date toDate(Long longDate) {
 75  
         try {
 76  0
             String s = String.valueOf(longDate);
 77  0
             Date d = DF_LONG.parse(s);
 78  0
             return d;
 79  0
         } catch (Exception e) {
 80  0
             return null;
 81  
         }
 82  
     }
 83  
 
 84  
     public static Integer toInteger(String str) {
 85  
         try {
 86  0
             return Integer.parseInt(str);
 87  0
         } catch (NumberFormatException e) {
 88  0
             return -1;
 89  
         }
 90  
     }
 91  
     public static boolean toBoolean(Integer integer) {
 92  0
         return AppDef.TRUE.equals(integer);
 93  
     }
 94  
     public static String toStr(Number num) {
 95  0
         if(num == null) {
 96  0
             return "";
 97  
         }
 98  0
         return String.valueOf(num);
 99  
     }
 100  
     public static String toStrYyyyMMdd(Integer integer) {
 101  0
         String str = toStr(integer);
 102  0
         if(str.length() == 8) {
 103  0
             str = str.substring(0, 4) + "/" + str.substring(4, 6) + "/" + str.substring(6, 8);
 104  
         }
 105  0
         return str;
 106  
     }
 107  
     public static String toStrYyyyMMdd(Long integer) {
 108  0
         String str = toStr(integer);
 109  0
         if(str.length() >= 8) {
 110  0
             str = str.substring(0, 4) + "/" + str.substring(4, 6) + "/" + str.substring(6, 8);
 111  
         }
 112  0
         return str;
 113  
     }
 114  
     public static String toStrYyyyMMdd_HHmmss(Long integer) {
 115  0
         String str = toStr(integer);
 116  0
         if(str.length() >= 14) {
 117  0
             str = str.substring(0, 4) + "/" + str.substring(4, 6) + "/" + str.substring(6, 8)
 118  0
                     + " " + str.substring(8, 10) + ":" + str.substring(10, 12) + ":" + str.substring(12, 14);
 119  
         }
 120  0
         return str;
 121  
     }
 122  0
     private static final DateFormat DF_YYYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
 123  
     public static String toStrYyyyMMdd(Date date) {
 124  0
         if(date == null) return "";
 125  0
         return DF_YYYYMMDD.format(date);
 126  
     }
 127  
     
 128  
     /** ID-04900,ID-05242 二つのBooleanを比較する */
 129  
     public static boolean equals(Boolean a, Boolean b) {
 130  0
         if(a == null && b == null) return true;
 131  0
         if(a == null && b != null) return false;
 132  0
         if(a != null && b == null) return false;
 133  0
         return a.equals(b);
 134  
     }
 135  
     /** ID-04900,ID-05242 二つのIntegerを比較する */
 136  
     public static boolean equals(Integer a, Integer b) {
 137  0
         if(a == null && b == null) return true;
 138  0
         if(a == null && b != null) return false;
 139  0
         if(a != null && b == null) return false;
 140  0
         return a.equals(b);
 141  
     }
 142  
     /** ID-04900,ID-05242 二つのStringを比較する */
 143  
     public static boolean equals(String a, String b) {
 144  0
         if(a == null && b == null) return true;
 145  0
         if(a == null && b != null) return false;
 146  0
         if(a != null && b == null) return false;
 147  0
         return a.equals(b);
 148  
     }
 149  
     
 150  
     public static void main(String[] args) {
 151  0
         Map<String, String> param = new HashMap<String, String>();
 152  0
         param.put("loginid", "test@example.com");
 153  0
         param.put("parame2", "()=~");
 154  0
         int result = httpRequest("http://www.y-net.co.jp/abc", param);
 155  
         
 156  0
         if(result == 200) {
 157  0
             AppLogger.debug("送信成功");
 158  0
         } else {
 159  0
             AppLogger.error("送信失敗 " + result);
 160  
         }
 161  0
     }
 162  
     
 163  
     /**
 164  
      * 外部APIに退会者のログインIDを通知
 165  
      * @param usr 
 166  
      */
 167  
     public static void callDeleteNotice(Usr usr) {
 168  0
         List<Usr> list = new ArrayList<Usr>();
 169  0
         list.add(usr);
 170  0
         callDeleteNotice(list);
 171  0
     }
 172  
     /**
 173  
      * 外部APIに退会者のログインIDを通知
 174  
      * @param users 
 175  
      */
 176  
     public static void callDeleteNotice(List<Usr> users) {
 177  0
         Map<String, String> param = new HashMap<String, String>();
 178  0
         param.put("size", String.valueOf(users.size()));
 179  0
         for (int i = 0; i < users.size(); i++) {
 180  0
             param.put("loginid" + i, users.get(i).getLoginid());
 181  
         }
 182  0
         String urlApiDeletecall = ResourceReader.getStringQuick("api.deletenotice");
 183  0
         int r = AppUtils.httpRequest(urlApiDeletecall, param);
 184  0
         if(r >= 400) {
 185  0
             AppLogger.error(
 186  0
                     "外部APIに退会者のログインIDを通知する処理が失敗しました。"
 187  0
                     + " URL=" + urlApiDeletecall 
 188  0
                     + " HTTPステータスコード=" + r);
 189  
         }
 190  0
     }
 191  
     public static int httpRequest(String url, Map<String, String> params) {
 192  
         
 193  0
         if(params != null && params.isEmpty() == false) {
 194  0
             String queryString = "";
 195  0
             for(Map.Entry<String, String> param: params.entrySet()) {
 196  0
                 if(StringUtils.isEmpty(queryString) == false) {
 197  0
                     queryString += "&";
 198  
                 }
 199  
                 String value;
 200  
                 try {
 201  0
                     value = URLEncoder.encode(param.getValue(), "UTF-8");
 202  0
                 } catch (UnsupportedEncodingException e) {
 203  0
                     throw new RuntimeException(e);
 204  
                 }
 205  0
                 queryString += param.getKey() + "=" + value;
 206  
             }
 207  0
             url += "?" + queryString;
 208  
         }
 209  0
         AppLogger.debug("http request:" + url);
 210  
         try {
 211  0
             URL urlObj = new URL(url);
 212  0
             HttpURLConnection urlCon = (HttpURLConnection) urlObj.openConnection();
 213  0
             urlCon.setRequestMethod("GET");
 214  0
             urlCon.connect();
 215  0
             int status = urlCon.getResponseCode();
 216  
             
 217  
 //            InputStream is = urlCon.getInputStream();
 218  
 //            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 219  
 //            String s;
 220  
 //            while ((s = reader.readLine()) != null) {
 221  
 //                System.out.println(s);
 222  
 //            }
 223  
 //            reader.close();
 224  
             
 225  0
             return status;
 226  
             
 227  0
         } catch (MalformedURLException e) {
 228  0
             AppLogger.error(e);
 229  0
             return -1;
 230  0
         } catch (ProtocolException e) {
 231  0
             AppLogger.error(e);
 232  0
             return -1;
 233  0
         } catch (IOException e) {
 234  0
             AppLogger.error(e);
 235  0
             return -1;
 236  
         }
 237  
         
 238  
     }
 239  
 //    /** 
 240  
 //     * 入力されたURLが実在するかチェックする処理
 241  
 //     * 実在するとき、エラーコードブランクでリターン 実在しないときエラーコードにエラーの理由をセットしてリターン
 242  
 //     * @param url url
 243  
 //     * @return errorCode エラーの理由
 244  
 //     *  */
 245  
 //    public static String urlChecker(String url) {
 246  
 //        String errorCode = "";
 247  
 //
 248  
 //        try {
 249  
 //            /* httpsのページは表示できないため、httpで目的のページを閲覧する */
 250  
 //            if(url.startsWith("https") == true) {
 251  
 //                url.replace("https", "http");
 252  
 //            }
 253  
 //            URL u = new URL(url);
 254  
 //            HttpURLConnection urlCon = null;
 255  
 //            try {
 256  
 //                urlCon = (HttpURLConnection) u.openConnection();
 257  
 //                try {
 258  
 //                    urlCon.setRequestMethod("GET");
 259  
 //                    try {
 260  
 //                        urlCon.connect();
 261  
 //                        int status = urlCon.getResponseCode();
 262  
 //                        if(status == 200) {
 263  
 //                            errorCode = "";
 264  
 //                        } else {
 265  
 //                            errorCode = "リンクが切れています。";
 266  
 //                        }
 267  
 //                    } catch (UnknownHostException e) {
 268  
 //                        /* ホスト名に誤り */
 269  
 //                        errorCode = "入力したURLに誤りがあります。";
 270  
 //                    } catch (NullPointerException e) {
 271  
 //                        /* コネクションなどでエラー */
 272  
 //                        errorCode = "入力したURLに誤りがあります。";
 273  
 //                        e.printStackTrace();
 274  
 //                    }
 275  
 //                } catch (ProtocolException e) {
 276  
 //                    /* プロトコルでエラー */
 277  
 //                    errorCode = "ネットワークに問題があります。";
 278  
 //                } finally {
 279  
 //                    /* コネクションの切断 */
 280  
 //                    urlCon.disconnect();
 281  
 //                }
 282  
 //            } catch (IOException e) {
 283  
 //                /* コネクションなどでエラー */
 284  
 //                errorCode = "入力したURLに誤りがあります。";
 285  
 //                e.printStackTrace();
 286  
 //            }
 287  
 //        } catch (MalformedURLException e) {
 288  
 //            /* URLの構文が不正 */
 289  
 //            errorCode = "入力したURLに誤りがあります。";
 290  
 //        }
 291  
 //        return errorCode;
 292  
 //    }
 293  
 
 294  
 
 295  
 
 296  
 }