de.cosmocode.palava.ipc.legacy.LegacyHttpSessionAdapter.java Source code

Java tutorial

Introduction

Here is the source code for de.cosmocode.palava.ipc.legacy.LegacyHttpSessionAdapter.java

Source

/**
 * Copyright 2010 CosmoCode GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.cosmocode.palava.ipc.legacy;

import java.text.Collator;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;

import com.google.common.base.Preconditions;

import de.cosmocode.palava.bridge.session.HttpSession;
import de.cosmocode.palava.ipc.IpcSession;
import de.cosmocode.rendering.Renderer;
import de.cosmocode.rendering.RenderingException;
import de.cosmocode.rendering.RenderingLevel;

/**
 * Adapter for {@link IpcSession} to {@link HttpSession}.
 *
 * @since 1.0
 * @author Willi Schoenborn
 */
@SuppressWarnings("deprecation")
final class LegacyHttpSessionAdapter implements HttpSession {

    private final IpcSession session;

    private transient Locale locale;

    private transient NumberFormat format;

    private transient Collator collator;

    public LegacyHttpSessionAdapter(IpcSession session) {
        this.session = Preconditions.checkNotNull(session, "Session");
    }

    @Override
    public String getIdentifier() {
        return session.getIdentifier();
    }

    @Override
    public String getSessionId() {
        return session.getSessionId();
    }

    @Override
    public long getTimeout(TimeUnit unit) {
        return session.getTimeout(unit);
    }

    @Override
    public boolean isExpired() {
        return session.isExpired();
    }

    @Override
    public Date lastAccessTime() {
        return session.lastAccessTime();
    }

    @Override
    public void setTimeout(long timeout, TimeUnit unit) {
        session.setTimeout(timeout, unit);
    }

    @Override
    public Date startedAt() {
        return session.startedAt();
    }

    @Override
    public void touch() {
        session.touch();
    }

    @Override
    public Date getAccessTime() {
        return lastAccessTime();
    }

    @Override
    public Locale getLocale() {
        touch();
        final Object langValue = get(LANGUAGE);
        if (locale == null || !locale.getLanguage().equals(langValue)) {

            format = null;
            collator = null;

            final Object countryValue = get(COUNTRY);

            if (langValue instanceof String && StringUtils.isNotBlank(String.class.cast(langValue))) {
                if (countryValue instanceof String && StringUtils.isNotBlank(String.class.cast(countryValue))) {
                    locale = new Locale(String.class.cast(langValue), String.class.cast(countryValue));
                } else {
                    locale = new Locale(String.class.cast(langValue));
                }
            } else {
                throw new IllegalStateException("No language found in session");
            }
        }
        return locale;
    }

    @Override
    public NumberFormat getNumberFormat() {
        touch();
        if (format == null) {
            format = NumberFormat.getInstance(getLocale());
        }
        return format;
    }

    @Override
    public Collator getCollator() {
        touch();
        if (collator == null) {
            collator = Collator.getInstance(getLocale());
        }
        return collator;
    }

    @Override
    public void updateAccessTime() {
        session.touch();
    }

    @Override
    public void clear() {
        session.clear();
    }

    @Override
    public Object putIfAbsent(Object key, Object value) {
        return session.putIfAbsent(key, value);
    }

    @Override
    public Object get(Object key) {
        return session.get(key);
    }

    @Override
    public Object remove(Object key) {
        return session.remove(key);
    }

    @Override
    public boolean remove(Object key, Object value) {
        return session.remove(key, value);
    }

    @Override
    public boolean replace(Object key, Object oldValue, Object newValue) {
        return session.replace(key, oldValue, newValue);
    }

    @Override
    public Object replace(Object key, Object value) {
        return session.replace(key, value);
    }

    @Override
    public int size() {
        return session.size();
    }

    @Override
    public boolean isEmpty() {
        return session.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return session.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return session.containsValue(value);
    }

    @Override
    public Object put(Object key, Object value) {
        return session.put(key, value);
    }

    @Override
    public void putAll(Map<? extends Object, ? extends Object> m) {
        session.putAll(m);
    }

    @Override
    public Set<Object> keySet() {
        return session.keySet();
    }

    @Override
    public Collection<Object> values() {
        return session.values();
    }

    @Override
    public Set<java.util.Map.Entry<Object, Object>> entrySet() {
        return session.entrySet();
    }

    @Override
    public boolean equals(Object o) {
        return session.equals(o);
    }

    @Override
    public int hashCode() {
        return session.hashCode();
    }

    public IpcSession getSession() {
        return session;
    }

    @Override
    public void render(Renderer renderer, RenderingLevel level) throws RenderingException {
        renderer.key("id").value(getSessionId()).key("accesstime").value(lastAccessTime()).key("data")
                .value(getSession());
    }

}