001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance 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, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.fcrepo.kernel.impl.util; 020 021import com.google.common.annotations.VisibleForTesting; 022import org.slf4j.Logger; 023 024import java.net.URI; 025 026import static com.google.common.base.Strings.isNullOrEmpty; 027import static org.slf4j.LoggerFactory.getLogger; 028 029/** 030 * @author Daniel Bernstein 031 * @since Sep 25, 2017 032 */ 033public class UserUtil { 034 035 private static final Logger LOGGER = getLogger(UserUtil.class); 036 037 @VisibleForTesting 038 public static final String DEFAULT_USER_AGENT_BASE_URI = "info:fedora/local-user#"; 039 040 private UserUtil() { 041 } 042 043 /** 044 * Returns the user agent based on the session user id. 045 * @param sessionUserId the acting user's id for this session 046 * @param userAgentBaseUri the user agent base uri, optional 047 * @return the uri of the user agent 048 */ 049 public static URI getUserURI(final String sessionUserId, final String userAgentBaseUri) { 050 // user id could be in format <anonymous>, remove < at the beginning and the > at the end in this case. 051 final String userId = (sessionUserId == null ? "anonymous" : sessionUserId).replaceAll("^<|>$", ""); 052 try { 053 final URI uri = URI.create(userId); 054 // return useId if it's an absolute URI or an opaque URI 055 if (uri.isAbsolute() || uri.isOpaque()) { 056 return uri; 057 } else { 058 return buildDefaultURI(userId, userAgentBaseUri); 059 } 060 } catch (final IllegalArgumentException e) { 061 return buildDefaultURI(userId, userAgentBaseUri); 062 } 063 } 064 065 /** 066 * Build default URI with the configured base uri for agent 067 * @param userId of which a URI will be created 068 * @return URI 069 */ 070 private static URI buildDefaultURI(final String userId, final String userAgentBaseUri) { 071 var baseUri = userAgentBaseUri; 072 // Construct the default URI for the user ID that is not a URI. 073 if (isNullOrEmpty(userAgentBaseUri)) { 074 // use the default local user agent base uri 075 baseUri = DEFAULT_USER_AGENT_BASE_URI; 076 } 077 078 final String userAgentUri = baseUri + userId; 079 080 LOGGER.trace("Default URI is created for user {}: {}", userId, userAgentUri); 081 return URI.create(userAgentUri); 082 } 083 084}