001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.connector.transport; 020 021import java.net.URI; 022 023/** 024 * A utility class for transporters. 025 * 026 * @since 2.0.25 027 */ 028public final class TransporterUtils { 029 private TransporterUtils() {} 030 031 /** 032 * Checks whether two URIs have the same origin (scheme, host and effective port). 033 * 034 * @param origin the repository origin 035 * @param target the URI to compare with the origin 036 * @return {@code true} when both URIs have the same origin 037 * @since 2.0.25 038 */ 039 public static boolean isSameOrigin(URI origin, URI target) { 040 return isSameEndpoint(origin, target, true, false); 041 } 042 043 /** 044 * Checks whether two URIs have the same authority (user, host and port). 045 * 046 * @param origin the URI to compare 047 * @param target the URI to compare with the origin 048 * @return {@code true} when both URIs have the same authority 049 * @since 2.0.25 050 */ 051 public static boolean isSameAuthority(URI origin, URI target) { 052 return isSameEndpoint(origin, target, false, true); 053 } 054 055 private static boolean isSameEndpoint( 056 URI origin, URI target, boolean requireSameScheme, boolean requireSameUserInfo) { 057 if (origin.getHost() == null || target.getHost() == null) { 058 return false; 059 } 060 if (requireSameScheme && !equalsIgnoreCase(origin.getScheme(), target.getScheme())) { 061 return false; 062 } 063 if (requireSameUserInfo && !equalsIgnoreCase(origin.getUserInfo(), target.getUserInfo())) { 064 return false; 065 } 066 return origin.getHost().equalsIgnoreCase(target.getHost()) && effectivePort(origin) == effectivePort(target); 067 } 068 069 private static boolean equalsIgnoreCase(String left, String right) { 070 return left != null ? left.equalsIgnoreCase(right) : right == null; 071 } 072 073 private static int effectivePort(URI uri) { 074 if (uri.getPort() >= 0) { 075 return uri.getPort(); 076 } 077 if ("https".equalsIgnoreCase(uri.getScheme())) { 078 return 443; 079 } 080 return "http".equalsIgnoreCase(uri.getScheme()) ? 80 : -1; 081 } 082}