class CsrfProtectionRefererFilter implements javax.servlet.Filter {

    public static List<URI> parseAllowedReferers(String allowedReferersConfigProperty) {
        if (allowedReferersConfigProperty == null || allowedReferersConfigProperty.isEmpty()) {
            throw new IllegalAllowedReferersException("must not be null or empty", allowedReferersConfigProperty);
        }
        String[] split = allowedReferersConfigProperty.split("\\,");
        List<URI> allowedReferers = new ArrayList<>();
        for (String allowedReferer : split) {
            if (allowedReferer == null || allowedReferer.isEmpty()) {
                continue;
            } else {
                if (allowedReferer.contains("*")) {
                    throw new IllegalAllowedReferersException("do not support regexs (*)", allowedReferer);
                }
                URI uri;
                try {
                    uri = new URI(allowedReferer);
                } catch (NullPointerException | IllegalArgumentException | URISyntaxException e) {
                    throw new IllegalAllowedReferersException("includes invalid referer", allowedReferer, e);
                }
                if (uri == null) {
                    throw new IllegalAllowedReferersException("must not be null", allowedReferer);
                }
                if (uri.getUserInfo() != null) {
                    throw new IllegalAllowedReferersException("must not contain user information", allowedReferer);
                }
                if (uri.getPath() != null && !uri.getPath().isEmpty() && !uri.getPath().equals("/")) {
                    throw new IllegalAllowedReferersException("do not support restricting by path", allowedReferer);
                }
                if (uri.getFragment() != null) {
                    throw new IllegalAllowedReferersException("do not support restricting by fragment", allowedReferer);
                }
                if (uri.getScheme() == null || (!uri.getScheme().equals("http") && !uri.getScheme().equals("https"))) {
                    throw new IllegalAllowedReferersException("must be http or https", allowedReferer);
                }
                if (uri.getAuthority() == null || uri.getHost() == null) {
                    throw new IllegalAllowedReferersException("must be hierarchical (e.g. http://example.com)", allowedReferer);
                }
                allowedReferers.add(uri);
            }
        }
        if (allowedReferers.isEmpty()) {
            throw new IllegalAllowedReferersException("Invalid List of allowed csrf referers must not be null or empty",
                    allowedReferersConfigProperty);
        }
        return allowedReferers;
    }

}