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

se.swedenconnect.signservice.context.DefaultSignServiceContext Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 * Copyright 2022-2024 Sweden Connect
 *
 * 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 se.swedenconnect.signservice.context;

import java.io.Serializable;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.lang3.SerializationUtils;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

/**
 * Default implementation of the {@link SignServiceContext} interface.
 */
public class DefaultSignServiceContext implements SignServiceContext {

  /** For serializing. */
  private static final long serialVersionUID = -5637851951212011897L;

  /** The context ID. */
  private final String id;

  /** The context data. */
  private final Map data = new HashMap<>();

  /**
   * Constructor.
   *
   * @param id the context ID
   */
  public DefaultSignServiceContext(@Nonnull final String id) {
    this.id = Objects.requireNonNull(id, "id must not be null");
  }

  /** {@inheritDoc} */
  @Override
  @Nonnull
  public String getId() {
    return this.id;
  }

  /** {@inheritDoc} */
  @Override
  public  void put(@Nonnull final String name, @Nullable final T data) {
    this.data.put(Objects.requireNonNull(name, "name must not be null"), data);
  }

  /** {@inheritDoc} */
  @Override
  @Nullable
  @SuppressWarnings("unchecked")
  public  T get(@Nonnull final String name) {
    return (T) this.data.get(Objects.requireNonNull(name, "name must not be null"));
  }

  /** {@inheritDoc} */
  @Override
  @Nonnull
  public  T get(@Nonnull final String name, @Nonnull final Class type)
      throws ClassCastException {
    return Optional.ofNullable(this.data.get(Objects.requireNonNull(name, "name must not be null")))
        .map(type::cast)
        .orElse(null);
  }

  /** {@inheritDoc} */
  @Override
  public  void remove(@Nonnull final String name) {
    this.data.remove(Objects.requireNonNull(name, "name must not be null"));
  }

  /** {@inheritDoc} */
  @Override
  @Nonnull
  public String serialize() {
    return Base64.getEncoder().encodeToString(SerializationUtils.serialize(this));
  }

  /**
   * Deserializes an encoding to a {@code DefaultSignServiceContext} object.
   *
   * @param encoding the string encoding
   * @return a DefaultSignServiceContext object
   */
  @Nonnull
  public static DefaultSignServiceContext deserialize(@Nonnull final String encoding) {
    return (DefaultSignServiceContext) SerializationUtils.deserialize(
        Base64.getDecoder().decode(Objects.requireNonNull(encoding, "encoding must not be null")));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy