Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.groupbuy.plugin; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import net.groupbuy.Setting; import net.groupbuy.entity.Payment; import net.groupbuy.entity.PluginConfig; import net.groupbuy.service.PaymentService; import net.groupbuy.service.PluginConfigService; import net.groupbuy.util.SettingUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.CompareToBuilder; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** * Plugin - * * @author SHOP++ Team * @version 3.0 */ public abstract class PaymentPlugin implements Comparable<PaymentPlugin> { /** ????? */ public static final String PAYMENT_NAME_ATTRIBUTE_NAME = "paymentName"; /** ?? */ public static final String FEE_TYPE_ATTRIBUTE_NAME = "feeType"; /** ?? */ public static final String FEE_ATTRIBUTE_NAME = "fee"; /** LOGO?? */ public static final String LOGO_ATTRIBUTE_NAME = "logo"; /** ???? */ public static final String DESCRIPTION_ATTRIBUTE_NAME = "description"; /** * */ public enum FeeType { /** */ scale, /** */ fixed } /** * */ public enum RequestMethod { /** POST */ post, /** GET */ get } /** * */ public enum NotifyMethod { /** */ general, /** ? */ sync, /** */ async } @Resource(name = "pluginConfigServiceImpl") private PluginConfigService pluginConfigService; @Resource(name = "paymentServiceImpl") private PaymentService paymentService; /** * ?ID * * @return ID */ public final String getId() { return getClass().getAnnotation(Component.class).value(); } /** * ??? * * @return ?? */ public abstract String getName(); /** * ? * * @return */ public abstract String getVersion(); /** * ? * * @return */ public abstract String getAuthor(); /** * ?? * * @return ? */ public abstract String getSiteUrl(); /** * ?URL * * @return URL */ public abstract String getInstallUrl(); /** * ?unURL * * @return unURL */ public abstract String getUninstallUrl(); /** * ?URL * * @return URL */ public abstract String getSettingUrl(); /** * ?? * * @return ? */ public boolean getIsInstalled() { return pluginConfigService.pluginIdExists(getId()); } /** * ??? * * @return ?? */ public PluginConfig getPluginConfig() { return pluginConfigService.findByPluginId(getId()); } /** * ??? * * @return ?? */ public boolean getIsEnabled() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getIsEnabled() : false; } /** * ? * * @param name * ?? * @return */ public String getAttribute(String name) { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getAttribute(name) : null; } /** * ?? * * @return ? */ public Integer getOrder() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getOrder() : null; } /** * ???? * * @return ??? */ public String getPaymentName() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getAttribute(PAYMENT_NAME_ATTRIBUTE_NAME) : null; } /** * ? * * @return */ public FeeType getFeeType() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? FeeType.valueOf(pluginConfig.getAttribute(FEE_TYPE_ATTRIBUTE_NAME)) : null; } /** * ? * * @return */ public BigDecimal getFee() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? new BigDecimal(pluginConfig.getAttribute(FEE_ATTRIBUTE_NAME)) : null; } /** * ?LOGO * * @return LOGO */ public String getLogo() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getAttribute(LOGO_ATTRIBUTE_NAME) : null; } /** * ??? * * @return ?? */ public String getDescription() { PluginConfig pluginConfig = getPluginConfig(); return pluginConfig != null ? pluginConfig.getAttribute(DESCRIPTION_ATTRIBUTE_NAME) : null; } /** * ?URL * * @return URL */ public abstract String getRequestUrl(); /** * ? * * @return */ public abstract RequestMethod getRequestMethod(); /** * ?? * * @return ? */ public abstract String getRequestCharset(); /** * ?? * * @param sn * ? * @param description * ?? * @param request * httpServletRequest * @return ? */ public abstract Map<String, Object> getParameterMap(String sn, String description, HttpServletRequest request); /** * ??? * * @param sn * ? * @param notifyMethod * * @param request * httpServletRequest * @return ?? */ public abstract boolean verifyNotify(String sn, NotifyMethod notifyMethod, HttpServletRequest request); /** * ?? * * @param sn * ? * @param notifyMethod * * @param request * httpServletRequest * @return ? */ public abstract String getNotifyMessage(String sn, NotifyMethod notifyMethod, HttpServletRequest request); /** * ? * * @return */ public abstract Integer getTimeout(); /** * * * @param amount * ? * @return */ public BigDecimal calculateFee(BigDecimal amount) { Setting setting = SettingUtils.get(); BigDecimal fee; if (getFeeType() == FeeType.scale) { fee = amount.multiply(getFee()); } else { fee = getFee(); } return setting.setScale(fee); } /** * ? * * @param amount * ? * @return ? */ public BigDecimal calculateAmount(BigDecimal amount) { return amount.add(calculateFee(amount)).setScale(2, RoundingMode.UP); } /** * ??? * * @param sn * ?(?) * @return ??null */ protected Payment getPayment(String sn) { return paymentService.findBySn(sn); } /** * ?URL * * @param sn * ? * @param notifyMethod * * @return URL */ protected String getNotifyUrl(String sn, NotifyMethod notifyMethod) { Setting setting = SettingUtils.get(); if (notifyMethod == null) { return setting.getSiteUrl() + "/payment/notify/" + NotifyMethod.general + "/" + sn + ".jhtml"; } return setting.getSiteUrl() + "/payment/notify/" + notifyMethod + "/" + sn + ".jhtml"; } /** * Map * * @param map * Map * @param prefix * ? * @param suffix * ? * @param separator * * @param ignoreEmptyValue * * @param ignoreKeys * Key * @return */ protected String joinKeyValue(Map<String, Object> map, String prefix, String suffix, String separator, boolean ignoreEmptyValue, String... ignoreKeys) { List<String> list = new ArrayList<String>(); if (map != null) { for (Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); String value = ConvertUtils.convert(entry.getValue()); if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key) && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) { list.add(key + "=" + (value != null ? value : "")); } } } return (prefix != null ? prefix : "") + StringUtils.join(list, separator) + (suffix != null ? suffix : ""); } /** * Map * * @param map * Map * @param prefix * ? * @param suffix * ? * @param separator * * @param ignoreEmptyValue * * @param ignoreKeys * Key * @return */ protected String joinValue(Map<String, Object> map, String prefix, String suffix, String separator, boolean ignoreEmptyValue, String... ignoreKeys) { List<String> list = new ArrayList<String>(); if (map != null) { for (Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); String value = ConvertUtils.convert(entry.getValue()); if (StringUtils.isNotEmpty(key) && !ArrayUtils.contains(ignoreKeys, key) && (!ignoreEmptyValue || StringUtils.isNotEmpty(value))) { list.add(value != null ? value : ""); } } } return (prefix != null ? prefix : "") + StringUtils.join(list, separator) + (suffix != null ? suffix : ""); } /** * POST * * @param url * URL * @param parameterMap * ? * @return */ protected String post(String url, Map<String, Object> parameterMap) { Assert.hasText(url); String result = null; HttpClient httpClient = new DefaultHttpClient(); try { HttpPost httpPost = new HttpPost(url); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (parameterMap != null) { for (Entry<String, Object> entry : parameterMap.entrySet()) { String name = entry.getKey(); String value = ConvertUtils.convert(entry.getValue()); if (StringUtils.isNotEmpty(name)) { nameValuePairs.add(new BasicNameValuePair(name, value)); } } } httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity); EntityUtils.consume(httpEntity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return result; } /** * GET * * @param url * URL * @param parameterMap * ? * @return */ protected String get(String url, Map<String, Object> parameterMap) { Assert.hasText(url); String result = null; HttpClient httpClient = new DefaultHttpClient(); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (parameterMap != null) { for (Entry<String, Object> entry : parameterMap.entrySet()) { String name = entry.getKey(); String value = ConvertUtils.convert(entry.getValue()); if (StringUtils.isNotEmpty(name)) { nameValuePairs.add(new BasicNameValuePair(name, value)); } } } HttpGet httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?") + EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"))); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity); EntityUtils.consume(httpEntity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return result; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } if (this == obj) { return true; } PaymentPlugin other = (PaymentPlugin) obj; return new EqualsBuilder().append(getId(), other.getId()).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder(17, 37).append(getId()).toHashCode(); } public int compareTo(PaymentPlugin paymentPlugin) { return new CompareToBuilder().append(getOrder(), paymentPlugin.getOrder()) .append(getId(), paymentPlugin.getId()).toComparison(); } }