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 */ 018package org.fcrepo.kernel.modeshape.utils; 019 020import static com.google.common.base.Strings.isNullOrEmpty; 021import static org.slf4j.LoggerFactory.getLogger; 022 023import java.net.URI; 024 025import org.slf4j.Logger; 026 027import com.google.common.annotations.VisibleForTesting; 028 029/** 030 * @author Daniel Bernstein 031 * @since Sep 25, 2017 032 */ 033public class FedoraSessionUserUtil { 034 035 private static final Logger LOGGER = getLogger(FedoraSessionUserUtil.class); 036 037 @VisibleForTesting 038 public static final String USER_AGENT_BASE_URI_PROPERTY = "fcrepo.auth.webac.userAgent.baseUri"; 039 @VisibleForTesting 040 public static final String DEFAULT_USER_AGENT_BASE_URI = "info:fedora/local-user#"; 041 042 private FedoraSessionUserUtil() { 043 } 044 045 /** 046 * Returns the user agent based on the session user id. 047 * @param sessionUserId the acting user's id for this session 048 * @return the uri of the user agent 049 */ 050 public static URI getUserURI(final String sessionUserId) { 051 // user id could be in format <anonymous>, remove < at the beginning and the > at the end in this case. 052 final String userId = (sessionUserId == null ? "anonymous" : sessionUserId).replaceAll("^<|>$", ""); 053 try { 054 final URI uri = URI.create(userId); 055 // return useId if it's an absolute URI or an opaque URI 056 if (uri.isAbsolute() || uri.isOpaque()) { 057 return uri; 058 } else { 059 return buildDefaultURI(userId); 060 } 061 } catch (final IllegalArgumentException e) { 062 return buildDefaultURI(userId); 063 } 064 } 065 066 /** 067 * Build default URI with the configured base uri for agent 068 * @param userId of which a URI will be created 069 * @return URI 070 */ 071 private static URI buildDefaultURI(final String userId) { 072 // Construct the default URI for the user ID that is not a URI. 073 String userAgentBaseUri = System.getProperty(USER_AGENT_BASE_URI_PROPERTY); 074 if (isNullOrEmpty(userAgentBaseUri)) { 075 // use the default local user agent base uri 076 userAgentBaseUri = DEFAULT_USER_AGENT_BASE_URI; 077 } 078 079 final String userAgentUri = userAgentBaseUri + userId; 080 081 LOGGER.trace("Default URI is created for user {}: {}", userId, userAgentUri); 082 return URI.create(userAgentUri); 083 } 084 085}