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.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.http.HttpStatus;
033import org.springframework.http.MediaType;
034import org.springframework.stereotype.Component;
035import org.springframework.validation.BeanPropertyBindingResult;
036import org.springframework.web.servlet.view.AbstractView;
037
038import com.google.gson.ExclusionStrategy;
039import com.google.gson.FieldAttributes;
040import com.google.gson.Gson;
041import com.google.gson.GsonBuilder;
042
043/**
044 * @author jricher
045 *
046 */
047@Component(JsonEntityView.VIEWNAME)
048public class JsonEntityView extends AbstractView {
049
050        public static final String ENTITY = "entity";
051
052        /**
053         * Logger for this class
054         */
055        private static final Logger logger = LoggerFactory.getLogger(JsonEntityView.class);
056
057        public static final String VIEWNAME = "jsonEntityView";
058
059        private Gson gson = new GsonBuilder()
060                        .setExclusionStrategies(new ExclusionStrategy() {
061
062                                @Override
063                                public boolean shouldSkipField(FieldAttributes f) {
064
065                                        return false;
066                                }
067
068                                @Override
069                                public boolean shouldSkipClass(Class<?> clazz) {
070                                        // skip the JPA binding wrapper
071                                        if (clazz.equals(BeanPropertyBindingResult.class)) {
072                                                return true;
073                                        }
074                                        return false;
075                                }
076
077                        })
078                        .serializeNulls()
079                        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
080                        .create();
081
082        @Override
083        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
084
085                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
086                response.setCharacterEncoding("UTF-8");
087
088
089                HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
090                if (code == null) {
091                        code = HttpStatus.OK; // default to 200
092                }
093
094                response.setStatus(code.value());
095
096                try {
097
098                        Writer out = response.getWriter();
099                        Object obj = model.get(ENTITY);
100                        gson.toJson(obj, out);
101
102                } catch (IOException e) {
103
104                        logger.error("IOException in JsonEntityView.java: ", e);
105
106                }
107        }
108
109}