/** * Assert that the given text does not contain the given substring. * * <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre> * * @param textToSearch the text to search * @param substring the substring to find within the text * @param message the exception message to use if the assertion fails */ public static void doesNotContain(String textToSearch, String substring, String message) { if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) { throw new AppException(message); } }
/** * Assert that the given text does not contain the given substring. * * <pre class="code"> * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'"); * </pre> * * @param textToSearch the text to search * @param substring the substring to find within the text * @param message the exception message to use if the assertion fails */ public static void doesNotContain( final String textToSearch, final String substring, final String message) { if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && (textToSearch.indexOf(substring) != -1)) { throw new IllegalArgumentException(message); } }
/** * Assert that the provided object is an instance of the provided class. * * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * * @param type the type to check against * @param obj the object to check * @param message a message which will be prepended to the message produced by the function * itself, and which may be used to provide context. It should normally end in a ": " or ". " * so that the function generate message looks ok when prepended to it. * @throws AppException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class<?> type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throw new AppException( (StringUtils.hasLength(message) ? message + " " : "") + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } }
/** * Parse the given, comma-separated string into a list of {@code MimeType} objects. * * @param mimeTypes the string to parse * @return the list of mime types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MimeType> parseMimeTypes(String mimeTypes) { if (!StringUtils.hasLength(mimeTypes)) { return Collections.emptyList(); } String[] tokens = StringUtils.tokenizeToStringArray(mimeTypes, ","); List<MimeType> result = new ArrayList<>(tokens.length); for (String token : tokens) { result.add(parseMimeType(token)); } return result; }
/** * Parse the given String into a single {@code MimeType}. * * @param mimeType the string to parse * @return the mime type * @throws InvalidMimeTypeException if the string cannot be parsed */ public static MimeType parseMimeType(String mimeType) { if (!StringUtils.hasLength(mimeType)) { throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); } String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";"); if (parts.length == 0) { throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); } String fullType = parts[0].trim(); // java.net.HttpURLConnection returns a *; q=.2 Accept header if (MimeType.WILDCARD_TYPE.equals(fullType)) { fullType = "*/*"; } int subIndex = fullType.indexOf('/'); if (subIndex == -1) { throw new InvalidMimeTypeException(mimeType, "does not contain '/'"); } if (subIndex == fullType.length() - 1) { throw new InvalidMimeTypeException(mimeType, "does not contain subtype after '/'"); } String type = fullType.substring(0, subIndex); String subtype = fullType.substring(subIndex + 1, fullType.length()); if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) { throw new InvalidMimeTypeException( mimeType, "wildcard type is legal only in '*/*' (all mime types)"); } Map<String, String> parameters = null; if (parts.length > 1) { parameters = new LinkedHashMap<>(parts.length - 1); for (int i = 1; i < parts.length; i++) { String parameter = parts[i]; int eqIndex = parameter.indexOf('='); if (eqIndex != -1) { String attribute = parameter.substring(0, eqIndex); String value = parameter.substring(eqIndex + 1, parameter.length()); parameters.put(attribute, value); } } } try { return new MimeType(type, subtype, parameters); } catch (UnsupportedCharsetException ex) { throw new InvalidMimeTypeException( mimeType, "unsupported charset '" + ex.getCharsetName() + "'"); } catch (IllegalArgumentException ex) { throw new InvalidMimeTypeException(mimeType, ex.getMessage()); } }
public static String encodeUri(String uri) { if (!StringUtils.hasLength(uri)) { return uri; } return UriComponentsBuilder.fromUriString(uri).build().encode().toUriString(); }
/** * 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, "Name must not be empty");</pre> * * @param text the String to check * @param message the exception message to use if the assertion fails * @see StringUtils#hasLength */ public static void hasLength(String text, String message) { if (!StringUtils.hasLength(text)) { throw new AppException(message); } }
/** * Assert that the given String is not empty; that is, it must not be <code>null</code> and not * the empty String. * * <pre class="code"> * Assert.hasLength(name, "Name must not be empty"); * </pre> * * @param text the String to check * @param message the exception message to use if the assertion fails * @see StringUtils#hasLength */ public static void hasLength(final String text, final String message) { if (!StringUtils.hasLength(text)) { throw new IllegalArgumentException(message); } }