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 */
019 package org.apache.wiki.tags;
020
021 import java.io.IOException;
022 import java.security.Principal;
023 import java.util.regex.Pattern;
024
025 import javax.servlet.http.HttpServletRequest;
026
027 import org.apache.wiki.WikiEngine;
028 import org.apache.wiki.WikiSession;
029 import org.apache.wiki.i18n.InternationalizationManager;
030 import org.apache.wiki.preferences.Preferences;
031 import org.apache.wiki.util.TextUtil;
032
033 /**
034 * Returns the current user name, or empty, if the user has not been
035 * validated.
036 *
037 * @since 2.0
038 */
039 public class UserNameTag extends WikiTagBase
040 {
041 private static final long serialVersionUID = 0L;
042
043 private static String notStartWithBlankOrColon = "^[^( |:)]";
044
045 private static String noColons = "[^:]*";
046
047 private static final Pattern VALID_USER_NAME_PATTERN = Pattern.compile(notStartWithBlankOrColon + noColons);
048
049 public final int doWikiStartTag() throws IOException
050 {
051 WikiEngine engine = this.m_wikiContext.getEngine();
052 WikiSession wikiSession = WikiSession.getWikiSession(engine, (HttpServletRequest) pageContext.getRequest());
053 Principal user = wikiSession.getUserPrincipal();
054
055 if (user != null)
056 {
057 if (VALID_USER_NAME_PATTERN.matcher(user.getName()).matches())
058 {
059 pageContext.getOut().print(TextUtil.replaceEntities(user.getName()));
060 }
061 else
062 {
063 pageContext.getOut().print( Preferences.getBundle( m_wikiContext, InternationalizationManager.CORE_BUNDLE )
064 .getString( "security.user.fullname.invalid" ) );
065 }
066 }
067
068 return SKIP_BODY;
069 }
070 }