| 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 | |
|
| 26 | |
|
| 27 | 0 | public class AppUtils { |
| 28 | |
|
| 29 | |
public static String urlDecode(String str) { |
| 30 | |
try { |
| 31 | 0 | str = URLDecoder.decode(str, "UTF-8"); |
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 63 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 165 | |
|
| 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 | |
|
| 174 | |
|
| 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 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 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 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
} |