001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.driver.services.impl;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: import javax.portlet.PortletRequest;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.pluto.PortletContainerException;
030: import org.apache.pluto.PortletWindow;
031: import org.apache.pluto.spi.optional.UserInfoAttributesService;
032: import org.apache.pluto.spi.optional.P3PAttributes;
033:
034: /**
035: * FIXME: Only include attributes that are defined in portlet.xml
036: *
037: * This is a default implementation of that gets user information attributes
038: * from a properties file where the user information attribute name (as defined
039: * in PLT.D of the JSR-168 spec) is prefixed by the user name (e.g. craig.user.name.given=Craig).
040: *
041: */
042: public class UserInfoAttributesServiceImpl implements
043: UserInfoAttributesService {
044:
045: private static UserInfoAttributesServiceImpl instance = new UserInfoAttributesServiceImpl();
046:
047: private static final String USER_INFO_ATTR_FILE = "/user-info-attributes.properties";
048: /** Logger. */
049: private static final Log LOG = LogFactory
050: .getLog(UserInfoAttributesServiceImpl.class);
051: private static Properties props = new Properties();
052: private static Map cache = new HashMap();
053:
054: private UserInfoAttributesServiceImpl() {
055:
056: }
057:
058: public static UserInfoAttributesServiceImpl getInstance()
059: throws IOException {
060: loadProperties();
061: return instance;
062: }
063:
064: public static UserInfoAttributesServiceImpl getInstance(
065: Properties inprops) throws IOException {
066: props = inprops;
067: return instance;
068: }
069:
070: /**
071: * Implementation of PLT.17.2 used to access user information attributes.
072: *
073: * @see UserInfoAttributesService#getUserInfo(javax.portlet.PortletRequest)
074: * @return As per the spec, return null if the user is not authenticated or an empty Map if there are
075: * no attributes in the properties file or a Map containing only those attributes found in the attribute
076: * data store (properties file).
077: */
078: public Map getUserInfo(PortletRequest request)
079: throws PortletContainerException {
080: Map map = null;
081: String user = request.getRemoteUser();
082: if (user == null) {
083: return null;
084: }
085: map = (Map) cache.get(user);
086: if (map == null) {
087: map = new HashMap();
088: int len = P3PAttributes.ATTRIBUTE_ARRAY.length;
089: StringBuffer prefix = new StringBuffer();
090: prefix.append(user);
091: prefix.append('.');
092: StringBuffer name = null;
093: for (int i = 0; i < len; i++) {
094: name = new StringBuffer();
095: name.append(prefix);
096: String attr = P3PAttributes.ATTRIBUTE_ARRAY[i];
097: name.append(attr);
098: String prop = props.getProperty(name.toString());
099: //spec says that Map only attributes that have data
100: if (prop != null) {
101: //TODO: convert user.bdate to milliseconds since January 1, 1970, 00:00:00 GMT.
102: map.put(attr, prop);
103: }
104: }
105: cache.put(user, map);
106: }
107: return map;
108: }
109:
110: public Map getUserInfo(PortletRequest request, PortletWindow window)
111: throws PortletContainerException {
112: return getUserInfo(request);
113: }
114:
115: private static void loadProperties() throws IOException {
116: //get the properties from prop file
117: if (props.isEmpty()) {
118: InputStream stream = UserInfoAttributesServiceImpl.class
119: .getResourceAsStream(USER_INFO_ATTR_FILE);
120: if (stream == null) {
121: String msg = "The properties file '"
122: + USER_INFO_ATTR_FILE
123: + "' cannot be found."
124: + " Please make sure this file exists and is in the classpath (i.e. WEB-INF/classes).";
125: LOG.error(msg);
126: throw new IOException(msg);
127: }
128: props.load(stream);
129: }
130: }
131:
132: }
|