001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, 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.config;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.jwt.SignedJWT;
023import com.nimbusds.oauth2.sdk.ParseException;
024import com.nimbusds.oauth2.sdk.http.HTTPResponse;
025import com.nimbusds.oauth2.sdk.util.StringUtils;
026import com.nimbusds.openid.connect.sdk.federation.entities.EntityStatement;
027
028
029/**
030 * Federation entity configuration success response.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OpenID Connect Federation 1.0, section 6.2.
036 * </ul>
037 */
038public class FederationEntityConfigurationSuccessResponse extends FederationEntityConfigurationResponse {
039
040
041        /**
042         * The content type.
043         */
044        private static final ContentType CONTENT_TYPE = EntityStatement.CONTENT_TYPE;
045
046
047        /**
048         * The entity statement.
049         */
050        private final EntityStatement entityStatement;
051
052
053        /**
054         * Creates a new federation entity configuration success response.
055         *
056         * @param entityStatement The entity statement. Must not be
057         *                        {@code null}.
058         */
059        public FederationEntityConfigurationSuccessResponse(final EntityStatement entityStatement) {
060
061                if (entityStatement == null) {
062                        throw new IllegalArgumentException("The federation entity statement must not be null");
063                }
064                this.entityStatement = entityStatement;
065        }
066
067
068        /**
069         * Returns the entity statement. No signature or expiration validation
070         * is performed.
071         *
072         * @return The entity statement.
073         */
074        public EntityStatement getEntityStatement() {
075
076                return entityStatement;
077        }
078
079
080        @Override
081        public boolean indicatesSuccess() {
082                return true;
083        }
084
085
086        @Override
087        public HTTPResponse toHTTPResponse() {
088
089                HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
090                httpResponse.setEntityContentType(CONTENT_TYPE);
091                httpResponse.setContent(entityStatement.getSignedStatement().serialize());
092                return httpResponse;
093        }
094
095
096        /**
097         * Parses a federation entity configuration success response from the
098         * specified HTTP response.
099         *
100         * @param httpResponse The HTTP response. Must not be {@code null}.
101         *
102         * @return The federation entity configuration success response.
103         *
104         * @throws ParseException If HTTP response couldn't be parsed to a
105         *                        federation entity configuration success
106         *                        response.
107         */
108        public static FederationEntityConfigurationSuccessResponse parse(final HTTPResponse httpResponse)
109                throws ParseException {
110
111                httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
112                httpResponse.ensureEntityContentType(CONTENT_TYPE);
113
114                String content = httpResponse.getContent();
115
116                if (StringUtils.isBlank(content)) {
117                        throw new ParseException("Empty HTTP entity body");
118                }
119
120                SignedJWT signedJWT;
121                try {
122                        signedJWT = SignedJWT.parse(httpResponse.getContent());
123                } catch (java.text.ParseException e) {
124                        throw new ParseException(e.getMessage(), e);
125                }
126
127                return new FederationEntityConfigurationSuccessResponse(EntityStatement.parse(signedJWT));
128        }
129}