001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the 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
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018/**
019 *
020 */
021package org.mitre.openid.connect.view;
022
023import java.io.IOException;
024import java.io.Writer;
025import java.lang.reflect.Type;
026import java.util.Map;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.mitre.oauth2.model.PKCEAlgorithm;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034import org.springframework.http.HttpStatus;
035import org.springframework.http.MediaType;
036import org.springframework.web.servlet.view.AbstractView;
037
038import com.google.gson.ExclusionStrategy;
039import com.google.gson.Gson;
040import com.google.gson.GsonBuilder;
041import com.google.gson.JsonElement;
042import com.google.gson.JsonParser;
043import com.google.gson.JsonPrimitive;
044import com.google.gson.JsonSerializationContext;
045import com.google.gson.JsonSerializer;
046import com.nimbusds.jose.EncryptionMethod;
047import com.nimbusds.jose.JWEAlgorithm;
048import com.nimbusds.jose.JWSAlgorithm;
049import com.nimbusds.jose.jwk.JWKSet;
050import com.nimbusds.jwt.JWT;
051
052/**
053 *
054 * Abstract superclass for client entity view, used with the ClientApi.
055 *
056 * @see ClientEntityViewForUsers
057 * @see ClientEntityViewForAdmins
058 *
059 * @author jricher
060 *
061 */
062public abstract class AbstractClientEntityView extends AbstractView {
063        /**
064         * Logger for this class
065         */
066        private static final Logger logger = LoggerFactory.getLogger(AbstractClientEntityView.class);
067
068        private JsonParser parser = new JsonParser();
069
070        private Gson gson = new GsonBuilder()
071                        .setExclusionStrategies(getExclusionStrategy())
072                        .registerTypeAdapter(JWSAlgorithm.class, new JsonSerializer<JWSAlgorithm>() {
073                                @Override
074                                public JsonElement serialize(JWSAlgorithm src, Type typeOfSrc, JsonSerializationContext context) {
075                                        if (src != null) {
076                                                return new JsonPrimitive(src.getName());
077                                        } else {
078                                                return null;
079                                        }
080                                }
081                        })
082                        .registerTypeAdapter(JWEAlgorithm.class, new JsonSerializer<JWEAlgorithm>() {
083                                @Override
084                                public JsonElement serialize(JWEAlgorithm src, Type typeOfSrc, JsonSerializationContext context) {
085                                        if (src != null) {
086                                                return new JsonPrimitive(src.getName());
087                                        } else {
088                                                return null;
089                                        }
090                                }
091                        })
092                        .registerTypeAdapter(EncryptionMethod.class, new JsonSerializer<EncryptionMethod>() {
093                                @Override
094                                public JsonElement serialize(EncryptionMethod src, Type typeOfSrc, JsonSerializationContext context) {
095                                        if (src != null) {
096                                                return new JsonPrimitive(src.getName());
097                                        } else {
098                                                return null;
099                                        }
100                                }
101                        })
102                        .registerTypeAdapter(JWKSet.class, new JsonSerializer<JWKSet>() {
103                                @Override
104                                public JsonElement serialize(JWKSet src, Type typeOfSrc, JsonSerializationContext context) {
105                                        if (src != null) {
106                                                return parser.parse(src.toString());
107                                        } else {
108                                                return null;
109                                        }
110                                }
111                        })
112                        .registerTypeAdapter(JWT.class, new JsonSerializer<JWT>() {
113                                @Override
114                                public JsonElement serialize(JWT src, Type typeOfSrc, JsonSerializationContext context) {
115                                        if (src != null) {
116                                                return new JsonPrimitive(src.serialize());
117                                        } else {
118                                                return null;
119                                        }
120                                }
121
122                        })
123                        .registerTypeAdapter(PKCEAlgorithm.class, new JsonSerializer<PKCEAlgorithm>() {
124                                @Override
125                                public JsonPrimitive serialize(PKCEAlgorithm src, Type typeOfSrc, JsonSerializationContext context) {
126                                        if (src != null) {
127                                                return new JsonPrimitive(src.getName());
128                                        } else {
129                                                return null;
130                                        }
131                                }
132                        })
133                        .serializeNulls()
134                        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
135                        .create();
136
137
138        /**
139         * @return
140         */
141        protected abstract ExclusionStrategy getExclusionStrategy();
142
143
144        @Override
145        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
146
147                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
148
149
150                HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
151                if (code == null) {
152                        code = HttpStatus.OK; // default to 200
153                }
154
155                response.setStatus(code.value());
156
157                try {
158
159                        Writer out = response.getWriter();
160                        Object obj = model.get(JsonEntityView.ENTITY);
161                        gson.toJson(obj, out);
162
163                } catch (IOException e) {
164
165                        logger.error("IOException in JsonEntityView.java: ", e);
166
167                }
168        }
169
170}