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.util.Map;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.mitre.oauth2.model.RegisteredClient;
031import org.mitre.openid.connect.ClientDetailsEntityJsonProcessor;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034import org.springframework.http.HttpStatus;
035import org.springframework.http.MediaType;
036import org.springframework.stereotype.Component;
037import org.springframework.web.servlet.view.AbstractView;
038
039import com.google.gson.Gson;
040import com.google.gson.JsonIOException;
041import com.google.gson.JsonObject;
042
043/**
044 *
045 * Provides representation of a client's registration metadata, to be shown from the dynamic registration endpoint
046 * on the client_register and rotate_secret operations.
047 *
048 * @author jricher
049 *
050 */
051@Component(ClientInformationResponseView.VIEWNAME)
052public class ClientInformationResponseView extends AbstractView {
053
054        /**
055         * Logger for this class
056         */
057        private static final Logger logger = LoggerFactory.getLogger(ClientInformationResponseView.class);
058
059        public static final String VIEWNAME = "clientInformationResponseView";
060
061        // note that this won't serialize nulls by default
062        private Gson gson = new Gson();
063
064        /* (non-Javadoc)
065         * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
066         */
067        @Override
068        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
069
070                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
071
072                RegisteredClient c = (RegisteredClient) model.get("client");
073                //OAuth2AccessTokenEntity token = (OAuth2AccessTokenEntity) model.get("token");
074                //String uri = (String)model.get("uri"); //request.getRequestURL() + "/" + c.getClientId();
075
076                HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
077                if (code == null) {
078                        code = HttpStatus.OK;
079                }
080                response.setStatus(code.value());
081
082                JsonObject o = ClientDetailsEntityJsonProcessor.serialize(c);
083
084                try {
085                        Writer out = response.getWriter();
086                        gson.toJson(o, out);
087                } catch (JsonIOException e) {
088
089                        logger.error("JsonIOException in ClientInformationResponseView.java: ", e);
090
091                } catch (IOException e) {
092
093                        logger.error("IOException in ClientInformationResponseView.java: ", e);
094
095                }
096
097        }
098
099}