001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.scim;
018
019import org.jspecify.annotations.NonNull;
020
021import javax.annotation.concurrent.Immutable;
022import java.util.Collections;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027/** A fully bounded HTTP response prepared by a service provider. Intentional body emission may contain personal data.
028 * @since 1.0.0
029 */
030@Immutable
031public final class ScimResponse {
032    private final int statusCode;
033    private final @NonNull Map<@NonNull String, @NonNull List<@NonNull String>> headers;
034    private final byte @NonNull [] body;
035
036    ScimResponse(int statusCode, @NonNull Map<@NonNull String, @NonNull List<@NonNull String>> headers, byte @NonNull [] body) {
037        this.statusCode = statusCode;
038        LinkedHashMap<String, List<String>> copy = new LinkedHashMap<>();
039        headers.forEach((name, values) -> copy.put(name, List.copyOf(values)));
040        this.headers = Collections.unmodifiableMap(copy);
041        this.body = body.clone();
042    }
043
044    /** Returns the prepared HTTP status.
045     * @return the status
046     * @since 1.0.0
047     */
048    public @NonNull Integer getStatusCode() { return statusCode; }
049
050    /** Returns the immutable prepared header occurrences.
051     * @return the headers
052     * @since 1.0.0
053     */
054    public @NonNull Map<@NonNull String, @NonNull List<@NonNull String>> getHeaders() { return headers; }
055
056    /** Intentionally emits a defensive copy of the HTTP body, which may contain personal data.
057     * @return the complete prepared body
058     * @since 1.0.0
059     */
060    public byte @NonNull [] toHttpBodyWithPersonalData() { return body.clone(); }
061
062    /** Returns a fixed diagnostic without provisioning data or header contents.
063     * @return the diagnostic
064     * @since 1.0.0
065     */
066    @Override public @NonNull String toString() { return "ScimResponse{<redacted>}"; }
067}