a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The contents of this file are subject to the terms
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of the Common Development and Distribution License
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * (the License). You may not use this file except in
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * compliance with the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * You can obtain a copy of the License at
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * opensso/legal/CDDLv1.0.txt
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * See the License for the specific language governing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * permission and limitations under the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * When distributing Covered Code, include this CDDL
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Header Notice in each file and include the License file
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * at opensso/legal/CDDLv1.0.txt.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If applicable, add the following below the CDDL Header,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * with the fields enclosed by brackets [] replaced by
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * your own identifying information:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * $Id: URLPatternMatcher.java,v 1.1 2009/11/24 21:42:35 madan_ranganath Exp $
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2014-2015 ForgeRock AS.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpackage com.sun.identity.shared.whitelist;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Majorimport com.sun.identity.shared.debug.Debug;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.net.MalformedURLException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.net.URL;
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Majorimport java.util.Collection;
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Majorimport java.util.HashMap;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The class provides pattern matching for notenforced URIs/URLs.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpublic class URLPatternMatcher {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major private static final Debug DEBUG = Debug.getInstance("patternMatching");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private HttpURLResourceName resourceName = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Constructor
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major public URLPatternMatcher() {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster resourceName = new HttpURLResourceName();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster resourceName.initialize(new HashMap());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major * Matches the URL against the provided URL patterns.
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major *
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major * @param requestedURL The URL to be matched.
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major * @param patterns The patterns to match the URL against.
b1b9ed659b5ba78c15d544b1a76134a6eba06a55David Luna * @param wildcard Flag for wildcard comparison
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major * @return <code>true</code> if matched, <code>false</code> otherwise.
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major * @throws MalformedURLException If the URL or one of the patterns is invalid.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major public boolean match(String requestedURL, Collection<String> patterns, boolean wildcard)
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major throws MalformedURLException {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major boolean result = false;
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major String patternLower;
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major for (String pattern : patterns) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster patternLower = pattern.toLowerCase();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major requestedURL = resourceName.canonicalize(requestedURL);
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
b1b9ed659b5ba78c15d544b1a76134a6eba06a55David Luna // convert URI to URL if required (but not if pattern is relative)
b1b9ed659b5ba78c15d544b1a76134a6eba06a55David Luna if (patternLower.startsWith("http")) {
b1b9ed659b5ba78c15d544b1a76134a6eba06a55David Luna pattern = resourceName.canonicalize(pattern);
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major if (DEBUG.messageEnabled()) {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major DEBUG.message("URLPatternMatcher.match(" + requestedURL + "): matching by pattern: " + pattern);
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major ResourceMatch res = resourceName.compare(requestedURL, pattern, wildcard);
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major if (res == ResourceMatch.WILDCARD_MATCH || res == ResourceMatch.EXACT_MATCH) {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major if (DEBUG.messageEnabled()) {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major DEBUG.message("URLPatternMatcher.match(" + requestedURL + "): matched by pattern: " + pattern
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major + " result = " + res);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major result = true;
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return result;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /*
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * convert pattern's URI to URL format.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private String convertToURL(String pattern, String requestedURL) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuilder builder = new StringBuilder();
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster URL url = new URL(requestedURL);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster builder.append(url.getProtocol()).append("://");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster builder.append(url.getHost()).append(":");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster builder.append(url.getPort());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster builder.append(pattern);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster pattern = builder.toString();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (MalformedURLException ex) {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major if (DEBUG.warningEnabled()) {
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major DEBUG.warning("URLPatternMatcher.convertToURL()- pattern:" + pattern + " requestedURL:" + requestedURL
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major + " Exception:", ex);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
f015d695b84915f1c2c3fcf81f339548af1852c4Peter Major }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return pattern;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster}