All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.http.simple.cookies.SimpleCookies Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2020 original authors
 *
 * 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
 *
 * https://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 io.micronaut.http.simple.cookies;

import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.ConversionServiceAware;
import io.micronaut.http.cookie.Cookie;
import io.micronaut.http.cookie.Cookies;

import java.util.*;

/**
 * Simple {@link Cookies} implementation.
 *
 * @author Graeme Rocher
 * @author Vladimir Orany
 * @since 1.0
 */
public class SimpleCookies implements Cookies, ConversionServiceAware {

    private ConversionService conversionService;
    private final Map cookies;

    /**
     * @param conversionService The conversion service
     */
    public SimpleCookies(ConversionService conversionService) {
        this.conversionService = conversionService;
        this.cookies = new LinkedHashMap<>();
    }

    @Override
    public Set getAll() {
        return new HashSet<>(cookies.values());
    }

    @Override
    public Optional findCookie(CharSequence name) {
        Cookie cookie = cookies.get(name);
        return cookie != null ? Optional.of(cookie) : Optional.empty();
    }

    @Override
    public  Optional get(CharSequence name, Class requiredType) {
        if (requiredType == Cookie.class || requiredType == Object.class) {
            //noinspection unchecked
            return (Optional) findCookie(name);
        } else {
            return findCookie(name).flatMap((cookie -> conversionService.convert(cookie.getValue(), requiredType)));
        }
    }

    @Override
    public  Optional get(CharSequence name, ArgumentConversionContext conversionContext) {
        return findCookie(name).flatMap((cookie -> conversionService.convert(cookie.getValue(), conversionContext)));
    }

    @Override
    public Collection values() {
        return Collections.unmodifiableCollection(cookies.values());
    }

    /**
     * Put a new cookie.
     * @param name      the name of the cookie
     * @param cookie    the cookie itself
     * @return previous value for given name
     */
    public Cookie put(CharSequence name, Cookie cookie) {
        return this.cookies.put(name, cookie);
    }

    /**
     * Put a set of new cookies.
     * @param cookies   Map of cookie names and cookies
     */
    public void putAll(Map cookies) {
        this.cookies.putAll(cookies);
    }

    @Override
    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy