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

com.networknt.openapi.parameter.CookieParameterDeserializer Maven / Gradle / Ivy

Go to download

An OpenAPI Specification 3.0 meta handler module that cache the spec and attached context related operation to the request based on uri and method.

There is a newer version: 2.1.37
Show newest version
package com.networknt.openapi.parameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.networknt.oas.model.Parameter;
import com.networknt.openapi.OpenApiHandler;
import com.networknt.utility.StringUtils;

import io.undertow.UndertowOptions;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.Cookie;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Headers;

public class CookieParameterDeserializer implements ParameterDeserializer {
	private static final String FORM="form";

	@Override
	public AttachmentKey> getAttachmentKey(){
		return OpenApiHandler.DESERIALIZED_COOKIE_PARAMETERS;
	}	
	
	@Override
	public StyleParameterDeserializer getStyleDeserializer(String style) {
		if (StringUtils.isNotBlank(style) && !FORM.equalsIgnoreCase(style)) {
			return null;
		}
		
		return new StyleParameterDeserializer() {
			@Override
			public boolean isApplicable(ValueType valueType, boolean exploade) {
				return !exploade || StyleParameterDeserializer.super.isApplicable(valueType, exploade);
			}

			@Override
			public Object deserialize(HttpServerExchange exchange, Parameter parameter, ValueType valueType,
					boolean exploade) {
				List rawCookies = exchange.getRequestHeaders().get(Headers.COOKIE);
				
				
				Map cookies = CookieHelper.parseRequestCookies(
						exchange.getConnection().getUndertowOptions().get(UndertowOptions.MAX_COOKIES, 200),
						exchange.getConnection().getUndertowOptions().get(UndertowOptions.ALLOW_EQUALS_IN_COOKIE_VALUE, false),
						rawCookies);
						
				
				Cookie cookie = cookies.get(parameter.getName());
				
				String value = cookie.getValue();
				
				if (ValueType.ARRAY == valueType) {
					List valueList = new ArrayList<>();
					
					valueList.addAll(asList(value, Delimiters.COMMA));
					
					return valueList;			
				}else if (ValueType.OBJECT == valueType) {
					Map valueMap = new HashMap<>();
					valueMap.putAll(asMap(value, Delimiters.COMMA));
					
					return valueMap;
				}
				
				return null;
			}
			
		};
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy