1 /* 2 * Copyright 2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.openehealth.ipf.commons.ihe.xds.core.validate; 17 18 import java.net.MalformedURLException; 19 import java.net.URI; 20 import java.net.URISyntaxException; 21 import java.net.URL; 22 23 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*; 24 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert; 25 26 /** 27 * Validates a value list for compliance with a URI (RFC 2616). 28 * @author Jens Riemschneider 29 */ 30 public class UriValidator implements ValueValidator { 31 32 @Override 33 public void validate(String uri) throws XDSMetaDataException { 34 metaDataAssert(uri != null, NULL_URI); 35 metaDataAssert(!uri.isEmpty(), EMPTY_URI); 36 37 // Accept anything that the classes URI or URL accept. This is done to 38 // avoid e.g. "http://" to fail. The XDSToolKit is using this URI 39 // for some tests and the RFCs do not clearly state if this is a valid 40 // URI or not. The URL class seems to accept it, the URI class doesn't. 41 try { 42 new URI(uri); 43 } 44 catch (URISyntaxException eUri) { 45 try { 46 new URL(uri); 47 } catch (MalformedURLException eUrl) { 48 throw new XDSMetaDataException(INVALID_URI, uri); 49 } 50 } 51 } 52 53 }