/** * Calls the given consumer with <tt>this</tt> if the value is filled. * * @param consumer the consumer to call with this object if it is filled * @return the value itself for fluent method calls */ public Value ifFilled(Consumer<Value> consumer) { if (isFilled()) { consumer.accept(this); } return this; }
/** * Returns the internal value wrapped as Optional while expecting it to be an integer number. * <p> * If the value is empty or not an integer, an empty Optional will be returned. * * @return the internal value wrapped as Optional or an empty Optional if the value is not filled or non-integer */ public Optional<Integer> asOptionalInt() { return isFilled() ? Optional.of(getInteger()) : Optional.empty(); }
/** * Returns the internal value wrapped as Optional. * <p> * If the value is empty, an empty Optional will be returned. * * @return the internal value wrapped as Optional or an empty Optional if the value is not filled */ public Optional<String> asOptionalString() { return isFilled() ? Optional.of(asString()) : Optional.empty(); }
/** * Returns a new <tt>Value</tt> which will wrap the given value, if the current value is empty. * Otherwise, the current value will be returned. * * @param replacement the value which is used as replacement if this value is empty * @return a new Value wrapping the given value or the current value if this is not empty. */ @Nonnull public Value replaceEmptyWith(@Nullable Object replacement) { if (isFilled()) { return this; } return Value.of(replacement); }
/** * Returns an optional value computed by the given mapper. If this value is <tt>empty</tt> the mapper will not * be called, but an empty optional will be returned. * * @param mapper the function used to convert the value into the desired object * @param <R> the type of the desired result * @return an Optional object wrapping the result of the computation or an empty Optional, if the value wasn't * filled */ @Nonnull public <R> Optional<R> map(@Nonnull Function<Value, R> mapper) { if (isFilled()) { return Optional.ofNullable(mapper.apply(this)); } else { return Optional.empty(); } }
/** * Returns a new <tt>Value</tt> which will wrap the value produced by the given supplier, if the current * value is empty. Otherwise, the current value will be returned. * * @param supplier the supplier used to compute a replacement value if this value is empty * @return a new Value wrapping the produced value of the given supplier or the current value if this is not empty. */ @Nonnull public Value replaceIfEmpty(@Nonnull Supplier<?> supplier) { if (isFilled()) { return this; } return Value.of(supplier.get()); }
@SuppressWarnings({"unchecked", "raw", "rawtypes"}) @Override public Object transformValue(Value value) { if (value.isFilled()) { return value.asEnum((Class<Enum>) field.getType()); } if (this.isNullable() || Strings.isEmpty(defaultValue)) { return null; } return Value.of(defaultValue).asEnum((Class<Enum>) field.getType()); }
@Override public Object transformValue(Value value) { if (value.isFilled()) { return value.asString(); } if (this.isNullable() || Strings.isEmpty(defaultValue)) { return null; } return defaultValue; }
/** * Extracts the given hash from the given request. Returns null if no hash was given. */ private String getAuthHash(WebContext ctx) { Value authorizationHeaderValue = ctx.getHeaderValue(HttpHeaderNames.AUTHORIZATION); if (!authorizationHeaderValue.isFilled()) { return ctx.get("Signature").getString(); } String authentication = Strings.isEmpty(authorizationHeaderValue.getString()) ? "" : authorizationHeaderValue.getString(); Matcher m = AWS_AUTH_PATTERN.matcher(authentication); if (m.matches()) { return m.group(2); } m = AWS_AUTH4_PATTERN.matcher(authentication); if (m.matches()) { return m.group(7); } return null; }
/** * Returns a <tt>Value</tt> containing a translated value using the string representation * of the wrapped value as key. * * @param lang a two-letter language code for which the translation is requested * @return a <tt>Value</tt> containing a translated value by calling {@link NLS#get(String, String)} * if the string representation of the wrapped value starts with {@code $}. * The dollar sign is skipped when passing the key to <tt>NLS</tt>. Otherwise <tt>this</tt> is returned. * @see NLS#get(String, String) */ @Nonnull @CheckReturnValue public Value translate(String lang) { if (isFilled() && is(String.class)) { return Value.of(NLS.smartGet(asString(), lang)); } else { return this; } } }
@Override public void execute(Output output, String... params) throws Exception { boolean withTraces = Value.indexOf(0, params).isFilled(); boolean includeWaiting = "Y".equals(Value.indexOf(1, params).asString()); if (withTraces) { outputThreadInfos(output, includeWaiting, params[0]); } else { output.line("Usage: threads [<filter> (or 'all')] [Y=include WAITING/NATIVE]"); output.separator(); output.apply("%-15s %10s %53s", "STATE", "ID", "NAME"); output.separator(); for (ThreadInfo info : t.dumpAllThreads(false, false)) { output.apply("%-15s %10s %53s", info.isInNative() ? "NATIVE" : info.getThreadState().name(), info.getThreadId(), info.getThreadName()); } output.separator(); } }
@Override public Object transformValue(Value value) { if (value.isFilled()) { return NLS.parseUserString(Amount.class, value.asString()); } if (this.isNullable() || Strings.isEmpty(defaultValue)) { return Amount.NOTHING; } return Value.of(defaultValue).getAmount(); }
@Override public Object transformValue(Value value) { if (value.isFilled()) { Integer result = value.getInteger(); if (result == null) { throw illegalFieldValue(value); } return result; } if (this.isNullable() || Strings.isEmpty(defaultValue)) { return null; } return Value.of(defaultValue).getInteger(); }
@Override public Object transformValue(Value value) { if (value.isFilled()) { Long result = value.getLong(); if (result == null) { throw illegalFieldValue(value); } return result; } if (this.isNullable() || Strings.isEmpty(defaultValue)) { return null; } return Value.of(defaultValue).getLong(); }
/** * Handles requests to / * * @param ctx the context describing the current request */ @Routed("/ui") public void index(WebContext ctx) { if (ctx.isUnsafePOST() && ctx.get("bucketName").isFilled()) { storage.getBucket(ctx.get("bucketName").asString()).create(); UserContext.message(Message.info("Bucket successfully created.")); } onError(ctx, null); }
private boolean objectCheckAuth(WebContext ctx, Bucket bucket) { String hash = getAuthHash(ctx); if (hash != null) { String expectedHash = hashCalculator.computeHash(ctx, ""); String alternativeHash = hashCalculator.computeHash(ctx, "/s3"); if (!expectedHash.equals(hash) && !alternativeHash.equals(hash)) { ctx.respondWith() .error(HttpResponseStatus.UNAUTHORIZED, Strings.apply("Invalid Hash (Expected: %s, Found: %s)", expectedHash, hash)); log.log(ctx.getRequest().method().name(), ctx.getRequestedURI(), APILog.Result.REJECTED, CallContext.getCurrent().getWatch()); return false; } } if (bucket.isPrivate() && !ctx.get("noAuth").isFilled() && hash == null) { ctx.respondWith().error(HttpResponseStatus.UNAUTHORIZED, "Authentication required"); log.log(ctx.getRequest().method().name(), ctx.getRequestedURI(), APILog.Result.REJECTED, CallContext.getCurrent().getWatch()); return false; } return true; }
if (PUT == method) { Value copy = ctx.getHeaderValue("x-amz-copy-source"); if (copy.isFilled()) { copyObject(ctx, bucket, id, copy.asString()); } else if (ctx.hasParameter("partNumber") && Strings.isFilled(uploadId)) {
Formatter.create(profile.get(KEY_PASSWORD).asString()).set(ctx).format() : ext.get(KEY_PASSWORD).asString(); this.initialSize = ext.get(KEY_INITIAL_SIZE).isFilled() ? ext.get(KEY_INITIAL_SIZE).asInt(0) : profile.get(KEY_INITIAL_SIZE).asInt(0); this.maxActive = ext.get(KEY_MAX_ACTIVE).isFilled() ? ext.get(KEY_MAX_ACTIVE).asInt(10) : profile.get(KEY_MAX_ACTIVE).asInt(10); this.maxIdle = ext.get(KEY_MAX_IDLE).isFilled() ? ext.get(KEY_MAX_IDLE).asInt(1) : profile.get(KEY_MAX_IDLE).asInt(1); this.validationQuery = ext.get(KEY_VALIDATION_QUERY).isEmptyString() ? Formatter.create(profile.get(KEY_VALIDATION_QUERY).asString()).set(ctx).format() :