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

io.inverno.mod.http.server.internal.GenericRequestCookies Maven / Gradle / Ivy

There is a newer version: 1.11.0
Show newest version
/*
 * Copyright 2020 Jeremy KUHN
 *
 * 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 io.inverno.mod.http.server.internal;

import io.inverno.mod.base.converter.ObjectConverter;
import io.inverno.mod.http.base.InboundCookies;
import io.inverno.mod.http.base.InboundRequestHeaders;
import io.inverno.mod.http.base.header.Cookie;
import io.inverno.mod.http.base.header.CookieParameter;
import io.inverno.mod.http.base.header.Headers;
import io.inverno.mod.http.base.internal.header.GenericCookieParameter;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 

* Generic {@link InboundCookies} implementation. *

* * @author Jeremy Kuhn * @since 1.0 */ public class GenericRequestCookies implements InboundCookies { private final Map> pairs; /** *

* Creates request cookies with the specified request headers and parameter value converter. *

* * @param requestHeaders the request headers * @param parameterConverter an string object converter */ public GenericRequestCookies(InboundRequestHeaders requestHeaders, ObjectConverter parameterConverter) { this.pairs = requestHeaders.getAllHeader(Headers.NAME_COOKIE) .stream() .flatMap(cookieHeader -> cookieHeader.getPairs().values().stream().flatMap(List::stream)) .map(cookie -> { if(cookie instanceof CookieParameter) { return (CookieParameter)cookie; } else { return new GenericCookieParameter(parameterConverter, cookie.getName(), cookie.getValue()); } }) .collect(Collectors.groupingBy(Cookie::getName)); } @Override public boolean contains(String name) { return this.pairs.containsKey(name); } @Override public Set getNames() { return this.pairs.keySet(); } @Override public Optional get(String name) { return Optional.ofNullable(this.getAll(name)).map(cookies -> { if(!cookies.isEmpty()) { return cookies.get(0); } return null; }); } @Override public List getAll(String name) { List cookiePairs = this.pairs.get(name); return cookiePairs != null ? cookiePairs : List.of(); } @Override public Map> getAll() { return this.pairs; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy