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
020package org.apache.isis.core.commons.authentication;
021
022import java.io.IOException;
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031import org.apache.isis.core.commons.encoding.DataInputExtended;
032import org.apache.isis.core.commons.encoding.DataOutputExtended;
033import org.apache.isis.core.commons.util.ToString;
034
035public abstract class AuthenticationSessionAbstract implements AuthenticationSession, Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    private final String name;
040    private final List<String> roles = new ArrayList<String>();
041    private final String validationCode;
042
043    private final Map<String, Object> attributeByName = new HashMap<String, Object>();
044
045    // ///////////////////////////////////////////////////////
046    // Constructor, encode
047    // ///////////////////////////////////////////////////////
048
049    @SuppressWarnings("unchecked")
050    public AuthenticationSessionAbstract(final String name, final String code) {
051        this(name, Collections.EMPTY_LIST, code);
052    }
053
054    public AuthenticationSessionAbstract(final String name, final List<String> roles, final String validationCode) {
055        this.name = name;
056        this.roles.addAll(roles);
057        this.validationCode = validationCode;
058        initialized();
059    }
060
061    public AuthenticationSessionAbstract(final DataInputExtended input) throws IOException {
062        this.name = input.readUTF();
063        this.roles.addAll(Arrays.asList(input.readUTFs()));
064        this.validationCode = input.readUTF();
065        initialized();
066    }
067
068    @Override
069    public void encode(final DataOutputExtended output) throws IOException {
070        output.writeUTF(getUserName());
071        output.writeUTFs(roles.toArray(new String[] {}));
072        output.writeUTF(validationCode);
073    }
074
075    private void initialized() {
076        // nothing to do
077    }
078
079    // ///////////////////////////////////////////////////////
080    // User Name
081    // ///////////////////////////////////////////////////////
082
083    @Override
084    public String getUserName() {
085        return name;
086    }
087
088    @Override
089    public boolean hasUserNameOf(final String userName) {
090        return userName == null ? false : userName.equals(getUserName());
091    }
092
093    // ///////////////////////////////////////////////////////
094    // Roles
095    // ///////////////////////////////////////////////////////
096
097    /**
098     * Can be overridden.
099     */
100    @Override
101    public List<String> getRoles() {
102        return Collections.unmodifiableList(roles);
103    }
104
105    // ///////////////////////////////////////////////////////
106    // Validation Code
107    // ///////////////////////////////////////////////////////
108
109    @Override
110    public String getValidationCode() {
111        return validationCode;
112    }
113
114    // ///////////////////////////////////////////////////////
115    // Attributes
116    // ///////////////////////////////////////////////////////
117
118    @Override
119    public Object getAttribute(final String attributeName) {
120        return attributeByName.get(attributeName);
121    }
122
123    @Override
124    public void setAttribute(final String attributeName, final Object attribute) {
125        attributeByName.put(attributeName, attribute);
126    }
127
128
129    // ///////////////////////////////////////////////////////
130    // MessageBroker
131    // ///////////////////////////////////////////////////////
132
133    private MessageBroker messageBroker;
134    @Override
135    public MessageBroker getMessageBroker() {
136        return messageBroker;
137    }
138    @Override
139    public void setMessageBroker(MessageBroker messageBroker) {
140        this.messageBroker = messageBroker;
141    }
142
143    // ///////////////////////////////////////////////////////
144    // toString
145    // ///////////////////////////////////////////////////////
146
147    @Override
148    public String toString() {
149        return new ToString(this).append("name", getUserName()).append("code", getValidationCode()).toString();
150    }
151
152}