/** * Create a composite interface Class for the given interfaces, * implementing the given interfaces in one single Class. * <p>This implementation builds a JDK proxy class for the given interfaces. * @param interfaces the interfaces to merge * @param classLoader the ClassLoader to create the composite Class in * @return the merged interface as Class * @see java.lang.reflect.Proxy#getProxyClass */ public static Class<?> createCompositeInterface(Class<?>[] interfaces, ClassLoader classLoader) { Assert.notEmpty(interfaces, "Interfaces must not be empty"); Assert.notNull(classLoader, "ClassLoader must not be null"); return Proxy.getProxyClass(classLoader, interfaces); }
public DisconnectionFrame(byte magic, FrameType type, byte version, byte code, byte[] data) { super(magic, type, version); this.code = code; this.data = data; if (data != null) { Assert.isTrue(data.length <= MAX_PAYLOAD_LENGTH, "The data length must be not greater than the max payload length"); } }
@Override public boolean verifyEchoString(EchoRequest request) { Assert.hasText(request.getEchostr(), "The echo string must be not empty"); Assert.hasText(request.getSignature(), "The signature string must be not empty"); Assert.hasText(request.getNonce(), "The nonce string must be not empty"); Assert.notNull(request.getTimestamp(), "The timestamp must be not null"); Assert.isTrue(request.getTimestamp() > 0, "The timestamp string must be greater than 0"); TreeSet<String> set = new TreeSet<>(Arrays.asList(request.getNonce(), request.getTimestamp().toString(), wechatToken)); StringBuilder sign = new StringBuilder(); set.forEach(sign::append); try { String originSign = sign.toString(); String hexSign = HexUtils.bytesToHex(MessageDigest.getInstance("SHA-1").digest(originSign.getBytes(StandardCharsets.US_ASCII))); log.info("verify echo string. {} | {} | {}", originSign, hexSign, request.getSignature()); return request.getSignature().equals(hexSign); } catch (Exception e) { log.error("verify echo string exception", e); return false; } }
@Override public void setListener(Listener listener) { Assert.notNull(listener, "The stream listener must be not null"); this.listener = listener; }
/** * Create a new {@code ConcurrentReferenceHashMap} instance. * * @param initialCapacity the initial capacity of the map * @param loadFactor the load factor. When the average number of references per * table exceeds this value, resize will be attempted. * @param concurrencyLevel the expected number of threads that will concurrently write to * the map * @param referenceType the reference type used for entries (soft or weak) */ @SuppressWarnings("unchecked") public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ReferenceType referenceType) { Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative"); Assert.isTrue(loadFactor > 0f, "Load factor must be positive"); Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive"); Assert.notNull(referenceType, "Reference type must not be null"); this.loadFactor = loadFactor; this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL); int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapacity = (int) ((initialCapacity + size - 1L) / size); this.segments = (Segment[]) Array.newInstance(Segment.class, size); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(roundedUpSegmentCapacity); } }
/** * Assert a boolean expression, throwing {@link IllegalStateException} if * the test result is {@code false}. * <p> * Call {@link #isTrue(boolean)} if you wish to throw * {@link IllegalArgumentException} on an assertion failure. * * <pre class="code"> * Assert.state(id == null); * </pre> * * @param expression a boolean expression * @throws IllegalStateException if the supplied expression is {@code false} */ public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
private void checkWrite(Request request, HTTP1ClientResponseHandler handler) { Assert.notNull(request, "The http client request is null."); Assert.notNull(handler, "The http1 client response handler is null."); Assert.state(isOpen(), "The current connection " + tcpSession.getSessionId() + " has been closed."); Assert.state(!upgradeHTTP2Complete.get(), "The current connection " + tcpSession.getSessionId() + " has upgraded HTTP2."); Assert.state(!upgradeWebSocketComplete.get(), "The current connection " + tcpSession.getSessionId() + " has upgraded WebSocket."); if (wrap.writing.compareAndSet(null, handler)) { request.getFields().put(HttpHeader.HOST, tcpSession.getRemoteAddress().getHostString()); handler.connection = this; handler.request = request; } else { throw new WritePendingException(); } }
/** * Assert that a Map has entries; that is, it must not be {@code null} and * must have at least one entry. * * <pre class="code"> * Assert.notEmpty(map); * </pre> * * @param map the map to check * @throws IllegalArgumentException if the map is {@code null} or has no entries */ public static void notEmpty(Map<?, ?> map) { notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); }
/** * Assert that the given String has valid text content; that is, it must not * be {@code null} and must contain at least one non-whitespace character. * * <pre class="code"> * Assert.hasText(name, "'name' must not be empty"); * </pre> * * @param text the String to check * @throws IllegalArgumentException if the text does not contain valid text content * @see StringUtils#hasText */ public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); }
/** * Assert that the given String is not empty; that is, it must not be * {@code null} and not the empty String. * * <pre class="code"> * Assert.hasLength(name); * </pre> * * @param text the String to check * @throws IllegalArgumentException if the text is empty * @see StringUtils#hasLength */ public static void hasLength(String text) { hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); }
/** * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}. * * <pre class="code"> * Assert.isAssignable(Number.class, myClass); * </pre> * * @param superType the super type to check * @param subType the sub type to check * @throws IllegalArgumentException if the classes are not assignable */ public static void isAssignable(Class<?> superType, Class<?> subType) { isAssignable(superType, subType, ""); }
/** * Assert that the given text does not contain the given substring. * * <pre class="code"> * Assert.doesNotContain(name, "rod"); * </pre> * * @param textToSearch the text to search * @param substring the substring to find within the text * @throws IllegalArgumentException if the text contains the substring */ public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); }
Assert.notNull(settingsField, "The http2 setting field must be not null."); Assert.isTrue(HttpMethod.GET.is(request.getMethod()), "The method of the request MUST be GET in the websocket handshake."); Assert.isTrue(request.getHttpVersion() == HttpVersion.HTTP_1_1, "The http version MUST be HTTP/1.1"); Assert.hasText(key, "Missing request header 'Sec-WebSocket-Key'");
public FlexContext(Request request, Stream stream, FlexConnection connection) { Assert.notNull(request, "The request must be not null."); this.request = request; this.connection = connection; this.stream = stream; }
/** * Create a new {@code ConcurrentReferenceHashMap} instance. * * @param initialCapacity the initial capacity of the map * @param loadFactor the load factor. When the average number of references per * table exceeds this value, resize will be attempted. * @param concurrencyLevel the expected number of threads that will concurrently write to * the map * @param referenceType the reference type used for entries (soft or weak) */ @SuppressWarnings("unchecked") public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ReferenceType referenceType) { Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative"); Assert.isTrue(loadFactor > 0f, "Load factor must be positive"); Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive"); Assert.notNull(referenceType, "Reference type must not be null"); this.loadFactor = loadFactor; this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL); int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapacity = (int) ((initialCapacity + size - 1L) / size); this.segments = (Segment[]) Array.newInstance(Segment.class, size); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(roundedUpSegmentCapacity); } }
/** * Assert a boolean expression, throwing {@link IllegalStateException} if * the test result is {@code false}. * <p> * Call {@link #isTrue(boolean)} if you wish to throw * {@link IllegalArgumentException} on an assertion failure. * * <pre class="code"> * Assert.state(id == null); * </pre> * * @param expression a boolean expression * @throws IllegalStateException if the supplied expression is {@code false} */ public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
private void checkWrite(Request request, HTTP1ClientResponseHandler handler) { Assert.notNull(request, "The http client request is null."); Assert.notNull(handler, "The http1 client response handler is null."); Assert.state(isOpen(), "The current connection " + tcpSession.getSessionId() + " has been closed."); Assert.state(!upgradeHTTP2Complete.get(), "The current connection " + tcpSession.getSessionId() + " has upgraded HTTP2."); Assert.state(!upgradeWebSocketComplete.get(), "The current connection " + tcpSession.getSessionId() + " has upgraded WebSocket."); if (wrap.writing.compareAndSet(null, handler)) { request.getFields().put(HttpHeader.HOST, tcpSession.getRemoteAddress().getHostString()); handler.connection = this; handler.request = request; } else { throw new WritePendingException(); } }
/** * Assert that a Map has entries; that is, it must not be {@code null} and * must have at least one entry. * * <pre class="code"> * Assert.notEmpty(map); * </pre> * * @param map the map to check * @throws IllegalArgumentException if the map is {@code null} or has no entries */ public static void notEmpty(Map<?, ?> map) { notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); }
/** * Assert that the given String has valid text content; that is, it must not * be {@code null} and must contain at least one non-whitespace character. * * <pre class="code"> * Assert.hasText(name, "'name' must not be empty"); * </pre> * * @param text the String to check * @throws IllegalArgumentException if the text does not contain valid text content * @see StringUtils#hasText */ public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); }