001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.openid.connect.sdk.federation.api;
019
020
021import net.jcip.annotations.Immutable;
022import net.minidev.json.JSONObject;
023
024import com.nimbusds.common.contenttype.ContentType;
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.http.HTTPResponse;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028
029
030/**
031 * Trust mark status success response.
032 *
033 * <p>Related specifications:
034 *
035 * <ul>
036 *     <li>OpenID Connect Federation 1.0, section 7.4.2.
037 * </ul>
038 */
039@Immutable
040public class TrustMarkStatusSuccessResponse extends TrustMarkStatusResponse {
041        
042        
043        /**
044         * The trust mark active status.
045         */
046        private final boolean active;
047        
048        
049        /**
050         * Creates a new trust mark status success response.
051         *
052         * @param active {@code true} if the trust mark is active,
053         *               {@code false} if invalid.
054         */
055        public TrustMarkStatusSuccessResponse(final boolean active) {
056                this.active = active;
057        }
058        
059        
060        /**
061         * Returns the trust mark active status.
062         *
063         * @return {@code true} if the trust mark is active, {@code false} if
064         *         invalid.
065         */
066        public boolean isActive() {
067                return active;
068        }
069        
070        
071        @Override
072        public boolean indicatesSuccess() {
073                return true;
074        }
075        
076        
077        @Override
078        public HTTPResponse toHTTPResponse() {
079                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
080                httpResponse.setEntityContentType(ContentType.APPLICATION_JSON);
081                JSONObject jsonObject = new JSONObject();
082                jsonObject.put("active", isActive());
083                httpResponse.setContent(jsonObject.toJSONString());
084                return httpResponse;
085        }
086        
087        
088        /**
089         * Parses a trust mark status success response from the specified HTTP
090         * response.
091         *
092         * @param httpResponse The HTTP response. Must not be {@code null}.
093         *
094         * @return The trust mark status success response.
095         *
096         * @throws ParseException If parsing failed.
097         */
098        public static TrustMarkStatusSuccessResponse parse(final HTTPResponse httpResponse)
099                throws ParseException {
100                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
101                JSONObject jsonObject = httpResponse.getContentAsJSONObject();
102                boolean active = JSONObjectUtils.getBoolean(jsonObject, "active");
103                return new TrustMarkStatusSuccessResponse(active);
104        }
105}