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

io.micronaut.http.netty.cookies.NettyCookies Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show 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.netty.cookies;

import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.http.cookie.Cookie;
import io.micronaut.http.cookie.Cookies;
import io.micronaut.http.cookie.ServerCookieDecoder;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * Delegates to {@link Cookie}.
 *
 * @author Graeme Rocher
 * @since 1.0
 */
public class NettyCookies implements Cookies {

    private static final Logger LOG = LoggerFactory.getLogger(NettyCookies.class);

    private final ConversionService conversionService;
    private final Map cookies;

    /**
     * @param path              The path
     * @param nettyHeaders      The Netty HTTP headers
     * @param conversionService The conversion service
     */
    public NettyCookies(String path, HttpHeaders nettyHeaders, ConversionService conversionService) {
        this.conversionService = conversionService;
        String value = nettyHeaders.get(HttpHeaderNames.COOKIE);
        if (value != null) {
            cookies = new LinkedHashMap<>();
            ServerCookieDecoder.INSTANCE.decode(value)
                    .stream()
                    .filter(cookie -> cookie.getPath() == null || path.startsWith(cookie.getPath()))
                    .forEach(cookie -> cookies.put(cookie.getName(), cookie));
        } else {
            cookies = Collections.emptyMap();
        }
    }

    /**
     * @param nettyHeaders      The Netty HTTP headers
     * @param conversionService The conversion service
     */
    public NettyCookies(HttpHeaders nettyHeaders, ConversionService conversionService) {
        this.conversionService = conversionService;
        if (nettyHeaders != null) {
            List values = nettyHeaders.getAll(HttpHeaderNames.SET_COOKIE);
            if (values != null && !values.isEmpty()) {
                cookies = new LinkedHashMap<>();
                for (String value: values) {
                    io.netty.handler.codec.http.cookie.Cookie nettyCookie = ClientCookieDecoder.STRICT.decode(value);
                    if (nettyCookie != null) {
                        cookies.put(nettyCookie.name(), new NettyCookie(nettyCookie));
                    } else {
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Failed to decode cookie value [{}]", value);
                        }
                    }
                }
            } else {
                cookies = Collections.emptyMap();
            }
        } else {
            cookies = Collections.emptyMap();
        }
    }

    @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());
    }

    @Override
    public ConversionService getConversionService() {
        return conversionService;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy