| @@ -5,9 +5,9 @@ | |||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |||
| * (the "License"); you may not use this file except in compliance with | |||
| * the License. You may obtain a copy of the License at | |||
| * | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| @@ -23,9 +23,6 @@ package me.chanjar.weixin.common.session; | |||
| * | |||
| * @author Craig R. McClanahan | |||
| */ | |||
| public class Constants { | |||
| public static final String Package = "me.chanjar.weixin.common.session"; | |||
| public static final String PACKAGE = "me.chanjar.weixin.common.session"; | |||
| } | |||
| @@ -11,13 +11,12 @@ public class StandardSession implements WxSession, InternalSession { | |||
| /** | |||
| * The string manager for this package. | |||
| */ | |||
| protected static final StringManager sm = StringManager.getManager(Constants.Package); | |||
| protected static final StringManager SM = StringManager.getManager(Constants.PACKAGE); | |||
| /** | |||
| * Type array. | |||
| */ | |||
| private static final String[] EMPTY_ARRAY = new String[0]; | |||
| // ------------------------------ WxSession | |||
| protected Map<String, Object> attributes = new ConcurrentHashMap<>(); | |||
| /** | |||
| * The session identifier of this Session. | |||
| @@ -73,7 +72,7 @@ public class StandardSession implements WxSession, InternalSession { | |||
| if (!isValidInternal()) { | |||
| throw new IllegalStateException | |||
| (sm.getString("sessionImpl.getAttribute.ise")); | |||
| (SM.getString("sessionImpl.getAttribute.ise")); | |||
| } | |||
| if (name == null) { | |||
| @@ -86,7 +85,7 @@ public class StandardSession implements WxSession, InternalSession { | |||
| @Override | |||
| public Enumeration<String> getAttributeNames() { | |||
| if (!isValidInternal()) { | |||
| throw new IllegalStateException(sm.getString("sessionImpl.getAttributeNames.ise")); | |||
| throw new IllegalStateException(SM.getString("sessionImpl.getAttributeNames.ise")); | |||
| } | |||
| Set<String> names = new HashSet<>(); | |||
| @@ -98,7 +97,7 @@ public class StandardSession implements WxSession, InternalSession { | |||
| public void setAttribute(String name, Object value) { | |||
| // Name cannot be null | |||
| if (name == null) { | |||
| throw new IllegalArgumentException(sm.getString("sessionImpl.setAttribute.namenull")); | |||
| throw new IllegalArgumentException(SM.getString("sessionImpl.setAttribute.namenull")); | |||
| } | |||
| // Null value is the same as removeAttribute() | |||
| @@ -109,7 +108,7 @@ public class StandardSession implements WxSession, InternalSession { | |||
| // Validate our current state | |||
| if (!isValidInternal()) { | |||
| throw new IllegalStateException(sm.getString("sessionImpl.setAttribute.ise", getIdInternal())); | |||
| throw new IllegalStateException(SM.getString("sessionImpl.setAttribute.ise", getIdInternal())); | |||
| } | |||
| this.attributes.put(name, value); | |||
| @@ -123,8 +122,9 @@ public class StandardSession implements WxSession, InternalSession { | |||
| @Override | |||
| public void invalidate() { | |||
| if (!isValidInternal()) | |||
| throw new IllegalStateException(sm.getString("sessionImpl.invalidate.ise")); | |||
| if (!isValidInternal()) { | |||
| throw new IllegalStateException(SM.getString("sessionImpl.invalidate.ise")); | |||
| } | |||
| // Cause this session to expire | |||
| expire(); | |||
| @@ -13,8 +13,7 @@ import java.util.concurrent.atomic.AtomicBoolean; | |||
| */ | |||
| public class StandardSessionManager implements WxSessionManager, InternalSessionManager { | |||
| protected static final StringManager sm = | |||
| StringManager.getManager(Constants.Package); | |||
| protected static final StringManager SM = StringManager.getManager(Constants.PACKAGE); | |||
| /** | |||
| * The descriptive name of this Manager implementation (for logging). | |||
| */ | |||
| @@ -82,7 +81,7 @@ public class StandardSessionManager implements WxSessionManager, InternalSession | |||
| public WxSession getSession(String sessionId, boolean create) { | |||
| if (sessionId == null) { | |||
| throw new IllegalStateException | |||
| (sm.getString("sessionManagerImpl.getSession.ise")); | |||
| (SM.getString("sessionManagerImpl.getSession.ise")); | |||
| } | |||
| InternalSession session = findSession(sessionId); | |||
| @@ -124,25 +123,24 @@ public class StandardSessionManager implements WxSessionManager, InternalSession | |||
| @Override | |||
| public InternalSession findSession(String id) { | |||
| if (id == null) | |||
| if (id == null) { | |||
| return (null); | |||
| } | |||
| return this.sessions.get(id); | |||
| } | |||
| @Override | |||
| public InternalSession createSession(String sessionId) { | |||
| if (sessionId == null) { | |||
| throw new IllegalStateException | |||
| (sm.getString("sessionManagerImpl.createSession.ise")); | |||
| (SM.getString("sessionManagerImpl.createSession.ise")); | |||
| } | |||
| if ((this.maxActiveSessions >= 0) && | |||
| (getActiveSessions() >= this.maxActiveSessions)) { | |||
| this.rejectedSessions++; | |||
| throw new TooManyActiveSessionsException( | |||
| sm.getString("sessionManagerImpl.createSession.tmase"), | |||
| SM.getString("sessionManagerImpl.createSession.tmase"), | |||
| this.maxActiveSessions); | |||
| } | |||
| @@ -230,8 +228,9 @@ public class StandardSessionManager implements WxSessionManager, InternalSession | |||
| @Override | |||
| public void backgroundProcess() { | |||
| this.count = (this.count + 1) % this.processExpiresFrequency; | |||
| if (this.count == 0) | |||
| if (this.count == 0) { | |||
| processExpires(); | |||
| } | |||
| } | |||
| /** | |||
| @@ -243,16 +242,18 @@ public class StandardSessionManager implements WxSessionManager, InternalSession | |||
| InternalSession sessions[] = findSessions(); | |||
| int expireHere = 0; | |||
| if (this.log.isDebugEnabled()) | |||
| if (this.log.isDebugEnabled()) { | |||
| this.log.debug("Start expire sessions {} at {} sessioncount {}", getName(), timeNow, sessions.length); | |||
| for (int i = 0; i < sessions.length; i++) { | |||
| if (sessions[i] != null && !sessions[i].isValid()) { | |||
| } | |||
| for (InternalSession session : sessions) { | |||
| if (session != null && !session.isValid()) { | |||
| expireHere++; | |||
| } | |||
| } | |||
| long timeEnd = System.currentTimeMillis(); | |||
| if (this.log.isDebugEnabled()) | |||
| if (this.log.isDebugEnabled()) { | |||
| this.log.debug("End expire sessions {} processingTime {} expired sessions: {}", getName(), timeEnd - timeNow, expireHere); | |||
| } | |||
| this.processingTime += (timeEnd - timeNow); | |||
| } | |||
| @@ -28,10 +28,10 @@ import java.util.Random; | |||
| */ | |||
| public class WxCryptUtil { | |||
| private static final Base64 base64 = new Base64(); | |||
| private static final Base64 BASE64 = new Base64(); | |||
| private static final Charset CHARSET = StandardCharsets.UTF_8; | |||
| private static final ThreadLocal<DocumentBuilder> builderLocal = new ThreadLocal<DocumentBuilder>() { | |||
| private static final ThreadLocal<DocumentBuilder> BUILDER_LOCAL = new ThreadLocal<DocumentBuilder>() { | |||
| @Override | |||
| protected DocumentBuilder initialValue() { | |||
| try { | |||
| @@ -65,7 +65,7 @@ public class WxCryptUtil { | |||
| static String extractEncryptPart(String xml) { | |||
| try { | |||
| DocumentBuilder db = builderLocal.get(); | |||
| DocumentBuilder db = BUILDER_LOCAL.get(); | |||
| Document document = db.parse(new InputSource(new StringReader(xml))); | |||
| Element root = document.getDocumentElement(); | |||
| @@ -192,7 +192,7 @@ public class WxCryptUtil { | |||
| byte[] encrypted = cipher.doFinal(unencrypted); | |||
| // 使用BASE64对加密后的字符串进行编码 | |||
| return base64.encodeToString(encrypted); | |||
| return BASE64.encodeToString(encrypted); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| @@ -4,5 +4,16 @@ package me.chanjar.weixin.common.util.http; | |||
| * Created by ecoolper on 2017/4/28. | |||
| */ | |||
| public enum HttpType { | |||
| JODD_HTTP, APACHE_HTTP, OK_HTTP; | |||
| /** | |||
| * jodd-http. | |||
| */ | |||
| JODD_HTTP, | |||
| /** | |||
| * apache httpclient. | |||
| */ | |||
| APACHE_HTTP, | |||
| /** | |||
| * okhttp. | |||
| */ | |||
| OK_HTTP | |||
| } | |||
| @@ -46,8 +46,7 @@ import java.util.*; | |||
| */ | |||
| public class StringManager { | |||
| private static final Map<String, Map<Locale, StringManager>> managers = | |||
| new Hashtable<>(); | |||
| private static final Map<String, Map<Locale, StringManager>> MANAGERS = new Hashtable<>(); | |||
| private static int LOCALE_CACHE_SIZE = 10; | |||
| /** | |||
| * The ResourceBundle for this StringManager. | |||
| @@ -118,16 +117,16 @@ public class StringManager { | |||
| public static final synchronized StringManager getManager( | |||
| String packageName, Locale locale) { | |||
| Map<Locale, StringManager> map = managers.get(packageName); | |||
| Map<Locale, StringManager> map = MANAGERS.get(packageName); | |||
| if (map == null) { | |||
| /* | |||
| * Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE. | |||
| * Expansion occurs when size() exceeds capacity. Therefore keep | |||
| * size at or below capacity. | |||
| * removeEldestEntry() executes after insertion therefore the test | |||
| * for removal needs to use one less than the maximum desired size | |||
| * | |||
| */ | |||
| /* | |||
| * Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE. | |||
| * Expansion occurs when size() exceeds capacity. Therefore keep | |||
| * size at or below capacity. | |||
| * removeEldestEntry() executes after insertion therefore the test | |||
| * for removal needs to use one less than the maximum desired size | |||
| * | |||
| */ | |||
| map = new LinkedHashMap<Locale, StringManager>(LOCALE_CACHE_SIZE, 1, true) { | |||
| private static final long serialVersionUID = 1L; | |||
| @@ -137,7 +136,7 @@ public class StringManager { | |||
| return size() > (LOCALE_CACHE_SIZE - 1); | |||
| } | |||
| }; | |||
| managers.put(packageName, map); | |||
| MANAGERS.put(packageName, map); | |||
| } | |||
| StringManager mgr = map.get(locale); | |||
| @@ -16,8 +16,14 @@ import java.util.List; | |||
| @Data | |||
| public class WxCpUser implements Serializable { | |||
| public enum Gender { | |||
| /** | |||
| * 男 | |||
| */ | |||
| MALE("男", "1"), | |||
| FEMAIL("女", "2"); | |||
| /** | |||
| * 女 | |||
| */ | |||
| FEMALE("女", "2"); | |||
| private String genderName; | |||
| private String code; | |||
| @@ -40,7 +46,7 @@ public class WxCpUser implements Serializable { | |||
| return Gender.MALE; | |||
| } | |||
| if ("2".equals(code)) { | |||
| return Gender.FEMAIL; | |||
| return Gender.FEMALE; | |||
| } | |||
| return null; | |||
| @@ -37,7 +37,7 @@ public class WxCpUserServiceImplTest { | |||
| user.setName("Some Woman"); | |||
| user.setDepartIds(new Integer[]{2}); | |||
| user.setEmail("none@none.com"); | |||
| user.setGender(WxCpUser.Gender.FEMAIL); | |||
| user.setGender(WxCpUser.Gender.FEMALE); | |||
| user.setMobile("13560084979"); | |||
| user.setPosition("woman"); | |||
| user.setTelephone("3300393"); | |||
| @@ -213,7 +213,7 @@ public class WxMpCardServiceImpl implements WxMpCardService { | |||
| WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
| new TypeToken<WxMpCardResult>() { | |||
| }.getType()); | |||
| if (!cardResult.getErrorCode().equals("0")) { | |||
| if (!"0".equals(cardResult.getErrorCode())) { | |||
| this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); | |||
| } | |||
| } | |||
| @@ -227,6 +227,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ | |||
| /** | |||
| * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | |||
| */ | |||
| @Override | |||
| public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||
| int retryTimes = 0; | |||
| do { | |||
| @@ -30,7 +30,7 @@ import java.util.Map; | |||
| * @author <a href="https://github.com/007gzs">007</a> | |||
| */ | |||
| public class WxOpenComponentServiceImpl implements WxOpenComponentService { | |||
| private static final Map<String, WxMpService> wxOpenMpServiceMap = new Hashtable<>(); | |||
| private static final Map<String, WxMpService> WX_OPEN_MP_SERVICE_MAP = new Hashtable<>(); | |||
| protected final Logger log = LoggerFactory.getLogger(this.getClass()); | |||
| private WxOpenService wxOpenService; | |||
| @@ -40,14 +40,14 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | |||
| @Override | |||
| public WxMpService getWxMpServiceByAppid(String appId) { | |||
| WxMpService wxMpService = wxOpenMpServiceMap.get(appId); | |||
| WxMpService wxMpService = WX_OPEN_MP_SERVICE_MAP.get(appId); | |||
| if (wxMpService == null) { | |||
| synchronized (wxOpenMpServiceMap) { | |||
| wxMpService = wxOpenMpServiceMap.get(appId); | |||
| synchronized (WX_OPEN_MP_SERVICE_MAP) { | |||
| wxMpService = WX_OPEN_MP_SERVICE_MAP.get(appId); | |||
| if (wxMpService == null) { | |||
| wxMpService = new WxOpenMpServiceImpl(this, appId, getWxOpenConfigStorage().getWxMpConfigStorage(appId)); | |||
| wxOpenMpServiceMap.put(appId, wxMpService); | |||
| WX_OPEN_MP_SERVICE_MAP.put(appId, wxMpService); | |||
| } | |||
| } | |||
| } | |||
| @@ -30,6 +30,7 @@ public abstract class WxOpenServiceAbstractImpl<H, P> implements WxOpenService, | |||
| return wxOpenConfigStorage; | |||
| } | |||
| @Override | |||
| public void setWxOpenConfigStorage(WxOpenConfigStorage wxOpenConfigStorage) { | |||
| this.wxOpenConfigStorage = wxOpenConfigStorage; | |||
| } | |||