JsonErrorView.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Portions copyright 2011-2013 The MITRE Corporation
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *******************************************************************************/
  18. package org.mitre.openid.connect.view;

  19. import java.io.IOException;
  20. import java.io.Writer;
  21. import java.util.Map;

  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;

  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.http.HttpStatus;
  27. import org.springframework.http.MediaType;
  28. import org.springframework.stereotype.Component;
  29. import org.springframework.validation.BeanPropertyBindingResult;
  30. import org.springframework.web.servlet.view.AbstractView;

  31. import com.google.common.base.Strings;
  32. import com.google.gson.ExclusionStrategy;
  33. import com.google.gson.FieldAttributes;
  34. import com.google.gson.Gson;
  35. import com.google.gson.GsonBuilder;
  36. import com.google.gson.JsonObject;

  37. /**
  38.  * @author aanganes, jricher
  39.  *
  40.  */
  41. @Component(JsonErrorView.VIEWNAME)
  42. public class JsonErrorView extends AbstractView {

  43.     /**
  44.      *
  45.      */
  46.     public static final String ERROR_MESSAGE = "errorMessage";

  47.     /**
  48.      *
  49.      */
  50.     public static final String ERROR = "error";

  51.     /**
  52.      * Logger for this class
  53.      */
  54.     private static final Logger logger = LoggerFactory.getLogger(JsonErrorView.class);

  55.     public static final String VIEWNAME = "jsonErrorView";

  56.     private Gson gson = new GsonBuilder()
  57.             .setExclusionStrategies(new ExclusionStrategy() {

  58.                 @Override
  59.                 public boolean shouldSkipField(FieldAttributes f) {

  60.                     return false;
  61.                 }

  62.                 @Override
  63.                 public boolean shouldSkipClass(Class<?> clazz) {
  64.                     // skip the JPA binding wrapper
  65.                     if (clazz.equals(BeanPropertyBindingResult.class)) {
  66.                         return true;
  67.                     }
  68.                     return false;
  69.                 }

  70.             })
  71.             .serializeNulls()
  72.             .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
  73.             .create();

  74.     @Override
  75.     protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {

  76.         response.setContentType(MediaType.APPLICATION_JSON_VALUE);


  77.         HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
  78.         if (code == null) {
  79.             code = HttpStatus.INTERNAL_SERVER_ERROR; // default to 500
  80.         }

  81.         response.setStatus(code.value());

  82.         try {

  83.             Writer out = response.getWriter();

  84.             String errorTitle = (String) model.get(ERROR);
  85.             if (Strings.isNullOrEmpty(errorTitle)) {
  86.                 errorTitle = "mitreid_error";
  87.             }
  88.             String errorMessage = (String) model.get(ERROR_MESSAGE);
  89.             JsonObject obj = new JsonObject();
  90.             obj.addProperty("error", errorTitle);
  91.             obj.addProperty("error_description", errorMessage);
  92.             gson.toJson(obj, out);

  93.         } catch (IOException e) {

  94.             logger.error("IOException in JsonErrorView.java: ", e);

  95.         }
  96.     }

  97. }