Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 2013-12-5 */ package ext.paycenter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.ArrayUtils; import common.Global; import play.Logger; import play.Logger.ALogger; import play.Play; import play.libs.F.Promise; import play.libs.WS.Response; import utils.WSUtil; import ext.config.ConfigFactory; /** * * * @ClassName: PCClient * @Description: * @date 2013-12-5 ?3:55:35 * @author ShenTeng * */ public class PCClient { private static final ALogger LOGGER = Logger.of(PCClient.class); /** * 10000ms */ private static int REQUEST_TIMEOUT = ConfigFactory.getInt("payCenter.client.timeout"); /** * ? */ private PCClient() { } /** * * * @param token token * @param email ? * @param payment ???? * @param serveduration ? * @param isIgnoreLocked ??true - ? false - ?? * @return */ public static PCResult<Void> transferax(String token, String email, String payment, String serveduration, boolean ignoreLocked) { String post = postNullIgnore("/pay/payment/transferax", "pay_user.token", token, "incom_user.email", email, "payment", payment, "serveduration", serveduration, "ignoreLocked", Boolean.toString(ignoreLocked)); return PCResult.fromJsonString(post, Void.class, false); } /** * * * @param email * @param toEmail ? * @param payment ???? * @param serveduration ? * @param isIgnoreLocked ??true - ? false - ?? * @return */ public static PCResult<Void> transferByEmail(String email, String toEmail, String payment, String serveduration, boolean ignoreLocked) { String post = postNullIgnore("/pay/payment/transferByEmail", "pay_user.email", email, "incom_user.email", toEmail, "payment", payment, "serveduration", serveduration, "ignoreLocked", Boolean.toString(ignoreLocked)); return PCResult.fromJsonString(post, Void.class, false); } /** * ?? * * @param token token * @return PCResult.data - String ????? */ public static PCResult<String> getBalance(String token) { String post = postNullIgnore("/pay/account/getBalance", "token", token); return PCResult.fromJsonString(post, String.class, true); } /** * ?? * * @param email email * @return PCResult.data - String ????? */ public static PCResult<String> getBalanceBySystem(String email) { String post = postNullIgnore("/pay/account/getBalanceBySystem", "email", email); return PCResult.fromJsonString(post, String.class, true); } /** * ? * @param email email * @return PCResult.data - String ????? */ public static PCResult<String> lock(String email) { String post = postNullIgnore("/pay/account/lock", "email", email); return PCResult.fromJsonString(post, String.class, true); } /** * ? * @param email email * @return PCResult.data - String ????? */ public static PCResult<String> unlock(String email) { String post = postNullIgnore("/pay/account/unlock", "email", email); return PCResult.fromJsonString(post, String.class, true); } /** * ??? * @param email email * @return PCResult.data - Boolean true - ?, false - ? */ public static PCResult<Boolean> getLockStatus(String email) { String post = postNullIgnore("/pay/account/getLockStatus", "email", email); return PCResult.fromJsonString(post, Boolean.class, true); } /** * ?post?null? */ private static String postNullIgnore(String relativeUrl, String... params) { if (null == relativeUrl || (params != null && params.length % 2 != 0)) { throw new IllegalArgumentException("illegal method input param. param: relativeUrl=" + relativeUrl + ", params=" + Arrays.toString(params)); } String[] paramArray = filterNullParams(params); String uri = Play.application().configuration().getString("payCenter.client.serverUrl") + relativeUrl; Promise<Response> post = WSUtil.postFormURLEncoded(uri, paramArray); LOGGER.debug("payCenter " + relativeUrl + " post : " + Arrays.toString(params)); Response response; try { response = post.get(REQUEST_TIMEOUT); } catch (Exception e) { throw new PayCenterException("??.URI = " + relativeUrl, e); } String body = response.getBody(); LOGGER.debug("payCenter " + relativeUrl + " post response : " + body); return body; } private static String[] filterNullParams(String[] params) { if (ArrayUtils.isNotEmpty(params)) { List<String> paramList = new ArrayList<>(); for (int i = 0; i < params.length; i += 2) { if (null == params[i + 1]) { continue; } paramList.add(params[i]); paramList.add(params[i + 1]); } return paramList.toArray(new String[0]); } return null; } /** * ?get?null? */ private static String getNullIgnore(String relativeUrl, String... params) { if (null == relativeUrl || (params != null && params.length % 2 != 0)) { throw new IllegalArgumentException("illegal method input param. param: relativeUrl=" + relativeUrl + ", params=" + Arrays.toString(params)); } String[] paramArray = filterNullParams(params); String uri = Play.application().configuration().getString("payCenter.client.serverUrl") + relativeUrl; Promise<Response> get = WSUtil.get(uri, paramArray); LOGGER.debug("payCenter " + relativeUrl + " get : " + Arrays.toString(params)); Response response = null; try { response = get.get(REQUEST_TIMEOUT); } catch (Exception e) { throw new PayCenterException("??.URI = " + relativeUrl, e); } String body = response.getBody(); LOGGER.debug("payCenter " + relativeUrl + " get response : " + body); return body; } /** * * getKeyWordCountBySystem */ public static String postKeyWordCountBySystem(String relativeUrl, String... params) { if (null == relativeUrl || (params != null && params.length % 2 != 0)) { throw new IllegalArgumentException("illegal method input param. param: relativeUrl=" + relativeUrl + ", params=" + Arrays.toString(params)); } String[] paramArray = filterNullParams(params); String uri = ConfigFactory.getString("keyWordCount.client.serverUrl") + relativeUrl; Promise<Response> post = WSUtil.postFormURLEncoded(uri, paramArray); LOGGER.debug("keyWordCount " + relativeUrl + " post : " + Arrays.toString(params)); Response response; try { response = post.get(REQUEST_TIMEOUT); } catch (Exception e) { throw new PayCenterException("???.URI = " + relativeUrl, e); } String body = response.getBody(); LOGGER.debug("keyWordCount " + relativeUrl + " post response : " + body); return body; } public static String getKeyWordCountBySystem(String relativeUrl, String... params) { if (null == relativeUrl || (params != null && params.length % 2 != 0)) { throw new IllegalArgumentException("illegal method input param. param: relativeUrl=" + relativeUrl + ", params=" + Arrays.toString(params)); } String[] paramArray = filterNullParams(params); String uri = ConfigFactory.getString("keyWordCount.client.serverUrl") + relativeUrl; Promise<Response> post = WSUtil.get(uri, paramArray); LOGGER.debug("keyWordCount " + relativeUrl + " post : " + Arrays.toString(params)); Response response; try { response = post.get(REQUEST_TIMEOUT); } catch (Exception e) { throw new PayCenterException("???.URI = " + relativeUrl, e); } String body = response.getBody(); LOGGER.debug("keyWordCount " + relativeUrl + " post response : " + body); return body; } }