Package com.jetdrone.vertx.yoke.core

Examples of com.jetdrone.vertx.yoke.core.YokeException


                return request.getParameter(path);
            case 1:
                return request.getFormParameter(path);
            case 2:
                if (!request.hasBody()) {
                    throw new YokeException(400, "No Body");
                }

                Object obj = request.body();
                if (!(obj instanceof JsonObject)) {
                    throw new YokeException(400, "Body is not JSON");
                }

                JsonObject json = (JsonObject) obj;

                String[] keys = path.split("\\.");

                for (int i = 0; i < keys.length - 1; i++) {
                    boolean optional = keys[i].charAt(0) == '?';

                    if (json == null) {
                        if (optional) {
                            json = EMPTY;
                        } else {
                            throw new YokeException(400, "Parameter '" + keys[i] + "' is not present or is null");
                        }
                    }

                    json = json.getObject(optional ? keys[i].substring(1) : keys[i]);
                }

                boolean optional = keys[keys.length - 1].charAt(0) == '?';

                if (json == null) {
                    if (optional) {
                        json = EMPTY;
                    } else {
                        throw new YokeException(400, "Parameter '" + keys[keys.length - 1] + "' is not present or is null");
                    }
                }

                return json.getField(optional ? keys[keys.length - 1].substring(1) : keys[keys.length - 1]);
            case 3:
                return request.get(path);
            case 4:
                return request.getHeader(path);
            default:
                throw new YokeException(400, "Unknown source " + type);
        }
    }
View Full Code Here


                // null is handled as a special case
                if (field == null) {
                    if (optional || type == Type.Null) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                switch (type) {
                    case Any:
                        return;
                    // base json types
                    case JsonObject:
                        if (field instanceof JsonObject || field instanceof Map) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case JsonArray:
                        if (field instanceof JsonArray || field instanceof List) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case String:
                        if (field instanceof String) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Number:
                        if (field instanceof Number) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Boolean:
                        if (field instanceof Boolean) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                        // specific types
                    case Integer:
                        if (field instanceof Integer) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Long:
                        if (field instanceof Long) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Double:
                        if (field instanceof Double) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                        // json schema validations
                    case DateTime:
                        if (field instanceof CharSequence && DATETIME.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Date:
                        if (field instanceof CharSequence && DATE.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Time:
                        if (field instanceof CharSequence && TIME.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Email:
                        if (field instanceof CharSequence && EMAIL.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case IPAddress:
                        if (field instanceof CharSequence && IPADDRESS.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case IPV6Address:
                        if (field instanceof CharSequence && IPV6ADDRESS.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case URI:
                        if (field instanceof CharSequence && URI.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Hostname:
                        if (field instanceof CharSequence && HOSTNAME.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Alpha:
                        if (field instanceof CharSequence && ALPHA.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                    case Alphanumeric:
                        if (field instanceof CharSequence && ALPHANUMERIC.matcher((CharSequence) field).matches()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
                }

                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    int len = ((String) field).length();
                    if (len >= min.intValue() && len <= max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                }
                if (field instanceof Number) {
                    if (NUMBERCOMPARATOR.compare((Number) field, min) >= 0 && NUMBERCOMPARATOR.compare((Number) field, max) <= 0) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                }

                if (field instanceof List) {
                    int len = ((List) field).size();
                    if (len >= min.intValue() && len <= max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                }

                if (field instanceof JsonArray) {
                    int len = ((JsonArray) field).size();
                    if (len >= min.intValue() && len <= max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                }

                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    if (DATETIME.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse((String) field).getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis >= min.getTime() && millis <= max.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                    }
                    if (DATE.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse(field + "T00:00:00Z").getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis >= min.getTime() && millis <= max.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is outside the range [" + min + ":" + max + "] be NULL");
                    }
                }
                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    int len = ((String) field).length();
                    if (len < max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside greater than [" + max + "] be NULL");
                }
                if (field instanceof Number) {
                    if (NUMBERCOMPARATOR.compare((Number) field, max) < 0) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside greater than [" + max + "] be NULL");
                }

                if (field instanceof List) {
                    int len = ((List) field).size();
                    if (len < max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside greater than [" + max + "] be NULL");
                }

                if (field instanceof JsonArray) {
                    int len = ((JsonArray) field).size();
                    if (len < max.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is outside greater than [" + max + "] be NULL");
                }

                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    int len = ((String) field).length();
                    if (len > min.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is less than [" + min + "] be NULL");
                }
                if (field instanceof Number) {
                    if (NUMBERCOMPARATOR.compare((Number) field, min) > 0) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is less than [" + min + "] be NULL");
                }

                if (field instanceof List) {
                    int len = ((List) field).size();
                    if (len > min.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is less than [" + min + "] be NULL");
                }

                if (field instanceof JsonArray) {
                    int len = ((JsonArray) field).size();
                    if (len > min.intValue()) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' is less than [" + min + "] be NULL");
                }

                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    if (DATETIME.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse((String) field).getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis < max.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is after [" + max + "] be NULL");
                    }
                    if (DATE.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse(field + "T00:00:00Z").getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis < max.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is after [" + max + "] be NULL");
                    }
                }
                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

            public void ok(final YokeRequest request) throws YokeException {

                final Object field = get(request);

                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    if (DATETIME.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse((String) field).getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis > min.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is before [" + min + "] be NULL");
                    }
                    if (DATE.matcher((CharSequence) field).matches()) {
                        long millis;
                        try {
                            millis = DATEFORMAT.parse(field + "T00:00:00Z").getTime();
                        } catch (ParseException e) {
                            throw new YokeException(errorCode, "Failed to validate", e);
                        }
                        if (millis > min.getTime()) {
                            return;
                        }
                        throw new YokeException(errorCode, "'" + path + "' is before [" + min + "] be NULL");
                    }
                }
                // unknown
                throw new YokeException(errorCode, "Failed to validate");
            }
        };
    }
View Full Code Here

                final Object field = get(request);
                final boolean optional = isOptional();

                // null is handled as a special case
                if (field == null) {
                    throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
                }

                if (field instanceof String) {
                    if (value.equals(field)) {
                        return;
                    }
                    throw new YokeException(errorCode, "'" + path + "' does not equal [" + value + "] be NULL");
                }
            }
        };
    }
View Full Code Here

TOP

Related Classes of com.jetdrone.vertx.yoke.core.YokeException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.