ResourceSetEntityView.java

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

  17. import java.io.IOException;
  18. import java.io.Writer;
  19. import java.util.Map;

  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;

  22. import org.mitre.openid.connect.config.ConfigurationPropertiesBean;
  23. import org.mitre.openid.connect.view.JsonEntityView;
  24. import org.mitre.uma.model.ResourceSet;
  25. import org.mitre.util.JsonUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.http.HttpHeaders;
  30. import org.springframework.http.HttpStatus;
  31. import org.springframework.stereotype.Component;
  32. import org.springframework.validation.BeanPropertyBindingResult;
  33. import org.springframework.web.servlet.view.AbstractView;

  34. import com.google.common.base.Strings;
  35. import com.google.gson.ExclusionStrategy;
  36. import com.google.gson.FieldAttributes;
  37. import com.google.gson.Gson;
  38. import com.google.gson.GsonBuilder;
  39. import com.google.gson.JsonObject;
  40. import com.google.gson.LongSerializationPolicy;

  41. @Component(ResourceSetEntityView.VIEWNAME)
  42. public class ResourceSetEntityView extends AbstractView {
  43.     private static Logger logger = LoggerFactory.getLogger(JsonEntityView.class);

  44.     public static final String VIEWNAME = "resourceSetEntityView";

  45.     @Autowired
  46.     private ConfigurationPropertiesBean config;

  47.     private Gson gson = new GsonBuilder()
  48.             .setExclusionStrategies(new ExclusionStrategy() {

  49.                 @Override
  50.                 public boolean shouldSkipField(FieldAttributes f) {

  51.                     return false;
  52.                 }

  53.                 @Override
  54.                 public boolean shouldSkipClass(Class<?> clazz) {
  55.                     // skip the JPA binding wrapper
  56.                     if (clazz.equals(BeanPropertyBindingResult.class)) {
  57.                         return true;
  58.                     }
  59.                     return false;
  60.                 }

  61.             })
  62.             .serializeNulls()
  63.             .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
  64.             .setLongSerializationPolicy(LongSerializationPolicy.STRING)
  65.             .create();

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

  68.         response.setContentType("application/json");


  69.         HttpStatus code = (HttpStatus) model.get("code");
  70.         if (code == null) {
  71.             code = HttpStatus.OK; // default to 200
  72.         }

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

  74.         String location = (String) model.get("location");
  75.         if (!Strings.isNullOrEmpty(location)) {
  76.             response.setHeader(HttpHeaders.LOCATION, location);
  77.         }

  78.         try {

  79.             Writer out = response.getWriter();
  80.             ResourceSet rs = (ResourceSet) model.get("entity");

  81.             JsonObject o = new JsonObject();

  82.             o.addProperty("_id", rs.getId().toString()); // send the id as a string
  83.             o.addProperty("user_access_policy_uri", config.getIssuer() + "manage/resource/" + rs.getId());
  84.             o.addProperty("name", rs.getName());
  85.             o.addProperty("uri", rs.getUri());
  86.             o.addProperty("type", rs.getType());
  87.             o.add("scopes", JsonUtils.getAsArray(rs.getScopes()));
  88.             o.addProperty("icon_uri", rs.getIconUri());

  89.             gson.toJson(o, out);

  90.         } catch (IOException e) {

  91.             logger.error("IOException in ResourceSetEntityView.java: ", e);

  92.         }
  93.     }

  94. }