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 *******************************************************************************/
018package org.mitre.openid.connect.view;
019
020import java.io.IOException;
021import java.io.Writer;
022import java.util.Map;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.springframework.http.HttpStatus;
030import org.springframework.http.MediaType;
031import org.springframework.stereotype.Component;
032import org.springframework.validation.BeanPropertyBindingResult;
033import org.springframework.web.servlet.view.AbstractView;
034
035import com.google.common.base.Strings;
036import com.google.gson.ExclusionStrategy;
037import com.google.gson.FieldAttributes;
038import com.google.gson.Gson;
039import com.google.gson.GsonBuilder;
040import com.google.gson.JsonObject;
041
042/**
043 * @author aanganes, jricher
044 *
045 */
046@Component(JsonErrorView.VIEWNAME)
047public class JsonErrorView extends AbstractView {
048
049        /**
050         *
051         */
052        public static final String ERROR_MESSAGE = "errorMessage";
053
054        /**
055         *
056         */
057        public static final String ERROR = "error";
058
059        /**
060         * Logger for this class
061         */
062        private static final Logger logger = LoggerFactory.getLogger(JsonErrorView.class);
063
064        public static final String VIEWNAME = "jsonErrorView";
065
066        private Gson gson = new GsonBuilder()
067                        .setExclusionStrategies(new ExclusionStrategy() {
068
069                                @Override
070                                public boolean shouldSkipField(FieldAttributes f) {
071
072                                        return false;
073                                }
074
075                                @Override
076                                public boolean shouldSkipClass(Class<?> clazz) {
077                                        // skip the JPA binding wrapper
078                                        if (clazz.equals(BeanPropertyBindingResult.class)) {
079                                                return true;
080                                        }
081                                        return false;
082                                }
083
084                        })
085                        .serializeNulls()
086                        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
087                        .create();
088
089        @Override
090        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
091
092                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
093
094
095                HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
096                if (code == null) {
097                        code = HttpStatus.INTERNAL_SERVER_ERROR; // default to 500
098                }
099
100                response.setStatus(code.value());
101
102                try {
103
104                        Writer out = response.getWriter();
105
106                        String errorTitle = (String) model.get(ERROR);
107                        if (Strings.isNullOrEmpty(errorTitle)) {
108                                errorTitle = "mitreid_error";
109                        }
110                        String errorMessage = (String) model.get(ERROR_MESSAGE);
111                        JsonObject obj = new JsonObject();
112                        obj.addProperty("error", errorTitle);
113                        obj.addProperty("error_description", errorMessage);
114                        gson.toJson(obj, out);
115
116                } catch (IOException e) {
117
118                        logger.error("IOException in JsonErrorView.java: ", e);
119
120                }
121        }
122
123}