
com.threewks.thundr.bind.http.request.CookieBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thundr-servlet-25 Show documentation
Show all versions of thundr-servlet-25 Show documentation
Servlet 2.5 support for the thundr web framework
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://3wks.github.io/thundr/
* Copyright (C) 2015 3wks,
*
* 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 com.threewks.thundr.bind.http.request;
import static com.atomicleopard.expressive.Expressive.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import com.atomicleopard.expressive.ETransformer;
import com.atomicleopard.expressive.Expressive;
import com.atomicleopard.expressive.transform.CollectionTransformer;
import com.threewks.thundr.bind.Binder;
import com.threewks.thundr.bind.parameter.ParameterBinderRegistry;
import com.threewks.thundr.introspection.ParameterDescription;
import com.threewks.thundr.request.Request;
import com.threewks.thundr.request.Response;
public class CookieBinder implements Binder {
private static final ETransformer toValue = Expressive.Transformers.toProperty("value", Cookie.class);
private static final CollectionTransformer toValues = Expressive.Transformers.transformAllUsing(toValue);
private static final ETransformer, Map>> beanLookup = Expressive.Transformers.toBeanLookup("name", Cookie.class);
public static final List> BoundTypes = Expressive.> list(Cookie.class);
private ParameterBinderRegistry parameterBinderRegistry;
public CookieBinder(ParameterBinderRegistry parameterBinderRegistry) {
super();
this.parameterBinderRegistry = parameterBinderRegistry;
}
@Override
public void bindAll(Map bindings, Request req, Response resp) {
HttpServletRequest rawRequest = req.getRawRequest(HttpServletRequest.class);
if (rawRequest != null && bindings.values().contains(null) && rawRequest.getCookies() != null) {
Map> cookieMap = beanLookup.from(list(rawRequest.getCookies()));
Map> valuesMap = createCookieValueMap(cookieMap);
parameterBinderRegistry.bind(bindings, valuesMap, null);
for (Map.Entry binding : bindings.entrySet()) {
ParameterDescription key = binding.getKey();
if (binding.getValue() == null && key.isA(Cookie.class)) {
String name = key.name();
List namedCookies = cookieMap.get(name);
Cookie cookie = isNotEmpty(namedCookies) ? namedCookies.get(0) : null;
bindings.put(key, cookie);
}
}
}
}
private Map> createCookieValueMap(Map> lookup) {
Map> result = new HashMap>();
for (Map.Entry> entry : lookup.entrySet()) {
result.put(entry.getKey(), toValues.from(entry.getValue()));
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy