/*
 *  Copyright 2017-2022 Adobe.
 *
 *  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.adobe.testing.s3mock;

import com.adobe.testing.s3mock.dto.Tag;
import com.adobe.testing.s3mock.util.AwsHttpHeaders;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.core.convert.converter.Converter;

/**
 * Converts values of the {@link AwsHttpHeaders#X_AMZ_TAGGING}
 * which is sent by the Amazon client like this:
 * x-amz-tagging: tag1=value1&tag2=value2
 * https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
 */
class TaggingHeaderConverter implements Converter<String, List<Tag>> {

  private static final Pattern TAGGING_PATTERN = Pattern.compile(".*=.*(&.*=.*)?");

  @Override
  public List<Tag> convert(String source) {
    List<Tag> tags = null;
    Matcher matcher = TAGGING_PATTERN.matcher(source);
    if (matcher.matches()) {
      tags = new ArrayList<>();
      String[] split = source.split("&");
      for (String tag : split) {
        tags.add(new Tag(tag));
      }
    }
    return tags;
  }
}
