001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.resources; 017 018import java.util.Arrays; 019import java.util.Objects; 020import java.util.function.UnaryOperator; 021 022import org.apache.commons.lang3.StringUtils; 023 024/** 025 * Helper for web resource dependencies list components 026 */ 027public final class WebResourceDependenciesHelper 028{ 029 private static final String[] __WEB_PREFIXES = new String[] {"/preview", "/live"}; 030 031 private WebResourceDependenciesHelper() 032 { 033 // static helper 034 } 035 036 /** 037 * Remove the web prefix from the uri, if present 038 * @param uri The uri 039 * @return The URI without the prefix, or the original URI if no prefix were found 040 */ 041 public static String removeWebPrefix(String uri) 042 { 043 UnaryOperator<String> removeUriPrefix = prefix -> uri != null && uri.startsWith(prefix) ? StringUtils.removeStart(uri, prefix) : null; 044 045 return Arrays.stream(__WEB_PREFIXES) 046 .map(removeUriPrefix) 047 .filter(Objects::nonNull) 048 .findFirst() 049 .orElse(uri); 050 } 051 052}