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

net.codestory.http.Cookies Maven / Gradle / Ivy

Go to download

Fluent-http is the simplest fastest full fledged web server we could come up with

There is a newer version: 2.105
Show newest version
/**
 * Copyright (C) 2013-2015 [email protected]
 *
 * 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 net.codestory.http;

import java.util.*;

import net.codestory.http.convert.*;
import net.codestory.http.internal.Unwrappable;

public interface Cookies extends Iterable, Unwrappable {
  Cookie get(String name);

  default Map keyValues() {
    Map keyValues = new HashMap<>();
    forEach(cookie -> keyValues.put(cookie.name(), cookie.value()));
    return keyValues;
  }

  default String value(String name) {
    Cookie cookie = get(name);
    return (cookie == null) ? null : cookie.value();
  }

  default String value(String name, String defaultValue) {
    String value = value(name);
    return (value == null) ? defaultValue : value;
  }

  default int value(String name, int defaultValue) {
    String value = value(name);
    return (value == null) ? defaultValue : Integer.parseInt(value);
  }

  default long value(String name, long defaultValue) {
    String value = value(name);
    return (value == null) ? defaultValue : Long.parseLong(value);
  }

  default boolean value(String name, boolean defaultValue) {
    String value = value(name);
    return (value == null) ? defaultValue : Boolean.parseBoolean(value);
  }

  @SuppressWarnings("unchecked")
  default  T value(String name, T defaultValue) {
    T value = value(name, (Class) defaultValue.getClass());
    return (value == null) ? defaultValue : value;
  }

  @SuppressWarnings("unchecked")
  default  T value(String name, Class type) {
    String value = value(name);
    if (value == null) {
      return null;
    }

    // fix for https://github.com/ariya/phantomjs/issues/12160
    if (value.indexOf('\\') != -1) {
      value = value.replace("\\", "");
    }

    return TypeConvert.fromJson(value, type);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy