public static String removeSlashesAtBothEnds(String path) { ObjectUtil.checkNotNull(path, "path"); if (path.isEmpty()) { return path; } int beginIndex = 0; while (beginIndex < path.length() && path.charAt(beginIndex) == '/') { beginIndex++; } if (beginIndex == path.length()) { return ""; } int endIndex = path.length() - 1; while (endIndex > beginIndex && path.charAt(endIndex) == '/') { endIndex--; } return path.substring(beginIndex, endIndex + 1); }
/** * Creates a new instance * * @param sslCtx an OpenSSL {@code SSL_CTX} object * @param alloc the {@link ByteBufAllocator} that will be used by this engine * @param clientMode {@code true} if this is used for clients, {@code false} otherwise * @param sessionContext the {@link OpenSslSessionContext} this {@link SSLEngine} belongs to. */ OpenSslEngine(long sslCtx, ByteBufAllocator alloc, String fallbackApplicationProtocol, boolean clientMode, OpenSslSessionContext sessionContext, OpenSslEngineMap engineMap) { OpenSsl.ensureAvailability(); if (sslCtx == 0) { throw new NullPointerException("sslCtx"); } this.alloc = ObjectUtil.checkNotNull(alloc, "alloc"); ssl = SSL.newSSL(sslCtx, !clientMode); networkBIO = SSL.makeNetworkBIO(ssl); this.fallbackApplicationProtocol = fallbackApplicationProtocol; this.clientMode = clientMode; this.sessionContext = sessionContext; this.engineMap = engineMap; }
@Override public void putValue(String name, Object value) { ObjectUtil.checkNotNull(name, "name"); ObjectUtil.checkNotNull(value, "value"); Map<String, Object> values = this.values; if (values == null) { // Use size of 2 to keep the memory overhead small values = this.values = new HashMap<String, Object>(2); } Object old = values.put(name, value); if (value instanceof SSLSessionBindingListener) { ((SSLSessionBindingListener) value).valueBound(new SSLSessionBindingEvent(this, name)); } notifyUnbound(old, name); }
/** * Creates a new instance. * * @param byteOrder the {@link ByteOrder} of the length field * @param lengthFieldLength the length of the prepended length field. * Only 1, 2, 3, 4, and 8 are allowed. * @param lengthAdjustment the compensation value to add to the value * of the length field * @param lengthIncludesLengthFieldLength * if {@code true}, the length of the prepended * length field is added to the value of the * prepended length field. * * @throws IllegalArgumentException * if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 */ public LengthFieldPrepender( ByteOrder byteOrder, int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength) { if (lengthFieldLength != 1 && lengthFieldLength != 2 && lengthFieldLength != 3 && lengthFieldLength != 4 && lengthFieldLength != 8) { throw new IllegalArgumentException( "lengthFieldLength must be either 1, 2, 3, 4, or 8: " + lengthFieldLength); } ObjectUtil.checkNotNull(byteOrder, "byteOrder"); this.byteOrder = byteOrder; this.lengthFieldLength = lengthFieldLength; this.lengthIncludesLengthFieldLength = lengthIncludesLengthFieldLength; this.lengthAdjustment = lengthAdjustment; }
@Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (period <= 0) { throw new IllegalArgumentException( String.format("period: %d (expected: > 0)", period)); } return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), unit.toNanos(period))); }
@Override public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (delay <= 0) { throw new IllegalArgumentException( String.format("delay: %d (expected: > 0)", delay)); } return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), -unit.toNanos(delay))); }
/** * The maps will be wrapped in Collections.unmodifiableMap. */ public RouteResult( String uri, String decodedPath, Map<String, String> pathParams, Map<String, List<String>> queryParams, T target ) { this.uri = ObjectUtil.checkNotNull(uri, "uri"); this.decodedPath = ObjectUtil.checkNotNull(decodedPath, "decodedPath"); this.pathParams = Collections.unmodifiableMap(ObjectUtil.checkNotNull(pathParams, "pathParams")); this.queryParams = Collections.unmodifiableMap(ObjectUtil.checkNotNull(queryParams, "queryParams")); this.target = ObjectUtil.checkNotNull(target, "target"); }
/** * The pattern must not contain query, example: * {@code constant1/constant2?foo=bar}. * * <p>The pattern will be stored without slashes at both ends. */ public PathPattern(String pattern) { if (pattern.contains("?")) { throw new IllegalArgumentException("Path pattern must not contain query"); } this.pattern = removeSlashesAtBothEnds(ObjectUtil.checkNotNull(pattern, "pattern")); this.tokens = this.pattern.split("/"); }
/** * Removes all routes leading to the target. */ public void removeTarget(T target) { Set<PathPattern> patterns = reverseRoutes.remove(ObjectUtil.checkNotNull(target, "target")); if (patterns == null) { return; } // A pattern can only point to one target. // A target can have multiple patterns. // Remove all patterns leading to this target. for (PathPattern pattern : patterns) { routes.remove(pattern); } }
public MqttFixedHeader( MqttMessageType messageType, boolean isDup, MqttQoS qosLevel, boolean isRetain, int remainingLength) { this.messageType = ObjectUtil.checkNotNull(messageType, "messageType"); this.isDup = isDup; this.qosLevel = ObjectUtil.checkNotNull(qosLevel, "qosLevel"); this.isRetain = isRetain; this.remainingLength = remainingLength; }
@Override public void setEnabledCipherSuites(String[] cipherSuites) { ObjectUtil.checkNotNull(cipherSuites, "cipherSuites"); final StringBuilder buf = new StringBuilder(); for (String c: cipherSuites) { if (c == null) { break; } String converted = CipherSuiteConverter.toOpenSsl(c); if (converted == null) { converted = c; } if (!OpenSsl.isCipherSuiteAvailable(converted)) { throw new IllegalArgumentException("unsupported cipher suite: " + c + '(' + converted + ')'); } buf.append(converted); buf.append(':'); } if (buf.length() == 0) { throw new IllegalArgumentException("empty cipher suites"); } buf.setLength(buf.length() - 1); final String cipherSuiteSpec = buf.toString(); try { SSL.setCipherSuites(ssl, cipherSuiteSpec); } catch (Exception e) { throw new IllegalStateException("failed to enable cipher suites: " + cipherSuiteSpec, e); } }
@Override public Object getValue(String name) { ObjectUtil.checkNotNull(name, "name"); if (values == null) { return null; } return values.get(name); }
@Override public void removeValue(String name) { ObjectUtil.checkNotNull(name, "name"); Map<String, Object> values = this.values; if (values == null) { return; } Object old = values.remove(name); notifyUnbound(old, name); }
@Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (delay < 0) { throw new IllegalArgumentException( String.format("delay: %d (expected: >= 0)", delay)); } return schedule(new ScheduledFutureTask<Void>( this, command, null, ScheduledFutureTask.deadlineNanos(unit.toNanos(delay)))); }
@Override public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { ObjectUtil.checkNotNull(callable, "callable"); ObjectUtil.checkNotNull(unit, "unit"); if (delay < 0) { throw new IllegalArgumentException( String.format("delay: %d (expected: >= 0)", delay)); } return schedule(new ScheduledFutureTask<V>( this, callable, ScheduledFutureTask.deadlineNanos(unit.toNanos(delay)))); }
public static void addHook(final int priority, final Runnable shutdownHook) { ObjectUtil.checkNotNull(shutdownHook, "Shutdown hook should not be null"); synchronized (shutdownHooks) { shutdownHooks.add(new ShutdownHookEntry(priority, shutdownHook)); } }