001/*
002 * Copyright (c) 2010-2026 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb;
023
024import java.io.Serializable;
025import java.time.Duration;
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.LinkedHashMap;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * Immutable carrier for HTTP response metadata.
034 */
035public final class ResponseMetadata implements Serializable {
036
037  private static final long serialVersionUID = 1L;
038
039  private final DebugHeaderInfo debugHeaderInfo;
040
041  private final Map<String, List<String>> responseHeaders;
042
043  private final Duration duration;
044
045  private final String httpMethod;
046
047  private final String requestUrl;
048
049  private ResponseMetadata(DebugHeaderInfo debugHeaderInfo, Map<String, List<String>> responseHeaders,
050      Duration duration, String httpMethod, String requestUrl) {
051    this.debugHeaderInfo = debugHeaderInfo;
052    this.responseHeaders = responseHeaders;
053    this.duration = duration;
054    this.httpMethod = httpMethod;
055    this.requestUrl = requestUrl;
056  }
057
058  public static ResponseMetadata of(DebugHeaderInfo debugHeaderInfo, Map<String, List<String>> responseHeaders,
059      Duration duration, String httpMethod, String requestUrl) {
060    return new ResponseMetadata(debugHeaderInfo, copyHeaders(responseHeaders), duration, httpMethod, requestUrl);
061  }
062
063  public DebugHeaderInfo getDebugHeaderInfo() {
064    return debugHeaderInfo;
065  }
066
067  public Map<String, List<String>> getResponseHeaders() {
068    return responseHeaders;
069  }
070
071  public Duration getDuration() {
072    return duration;
073  }
074
075  public String getHttpMethod() {
076    return httpMethod;
077  }
078
079  public String getRequestUrl() {
080    return requestUrl;
081  }
082
083  private static Map<String, List<String>> copyHeaders(Map<String, List<String>> headers) {
084    if (headers == null || headers.isEmpty()) {
085      return Collections.emptyMap();
086    }
087
088    Map<String, List<String>> copy = new LinkedHashMap<>();
089    headers.forEach((key, value) -> {
090      if (value == null) {
091        copy.put(key, null);
092      } else {
093        copy.put(key, Collections.unmodifiableList(new ArrayList<>(value)));
094      }
095    });
096    return Collections.unmodifiableMap(copy);
097  }
098}