001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 *******************************************************************************/ 016package org.mitre.uma.view; 017 018import java.io.IOException; 019import java.io.Writer; 020import java.util.Map; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025import org.mitre.openid.connect.config.ConfigurationPropertiesBean; 026import org.mitre.openid.connect.view.HttpCodeView; 027import org.mitre.openid.connect.view.JsonEntityView; 028import org.mitre.uma.model.ResourceSet; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031import org.springframework.beans.factory.annotation.Autowired; 032import org.springframework.http.HttpHeaders; 033import org.springframework.http.HttpStatus; 034import org.springframework.stereotype.Component; 035import org.springframework.validation.BeanPropertyBindingResult; 036import org.springframework.web.servlet.view.AbstractView; 037 038import com.google.common.base.Strings; 039import com.google.gson.ExclusionStrategy; 040import com.google.gson.FieldAttributes; 041import com.google.gson.Gson; 042import com.google.gson.GsonBuilder; 043import com.google.gson.JsonObject; 044import com.google.gson.LongSerializationPolicy; 045 046@Component(ResourceSetEntityAbbreviatedView.VIEWNAME) 047public class ResourceSetEntityAbbreviatedView extends AbstractView { 048 private static Logger logger = LoggerFactory.getLogger(JsonEntityView.class); 049 050 public static final String VIEWNAME = "resourceSetEntityAbbreviatedView"; 051 052 public static final String LOCATION = "location"; 053 054 @Autowired 055 private ConfigurationPropertiesBean config; 056 057 private Gson gson = new GsonBuilder() 058 .setExclusionStrategies(new ExclusionStrategy() { 059 060 @Override 061 public boolean shouldSkipField(FieldAttributes f) { 062 063 return false; 064 } 065 066 @Override 067 public boolean shouldSkipClass(Class<?> clazz) { 068 // skip the JPA binding wrapper 069 if (clazz.equals(BeanPropertyBindingResult.class)) { 070 return true; 071 } 072 return false; 073 } 074 075 }) 076 .serializeNulls() 077 .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") 078 .setLongSerializationPolicy(LongSerializationPolicy.STRING) 079 .create(); 080 081 @Override 082 protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { 083 084 response.setContentType("application/json"); 085 086 087 HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE); 088 if (code == null) { 089 code = HttpStatus.OK; // default to 200 090 } 091 092 response.setStatus(code.value()); 093 094 String location = (String) model.get(LOCATION); 095 if (!Strings.isNullOrEmpty(location)) { 096 response.setHeader(HttpHeaders.LOCATION, location); 097 } 098 099 try { 100 101 Writer out = response.getWriter(); 102 ResourceSet rs = (ResourceSet) model.get(JsonEntityView.ENTITY); 103 104 JsonObject o = new JsonObject(); 105 106 o.addProperty("_id", rs.getId().toString()); // set the ID to a string 107 o.addProperty("user_access_policy_uri", config.getIssuer() + "manage/user/policy/" + rs.getId()); 108 109 110 gson.toJson(o, out); 111 112 } catch (IOException e) { 113 114 logger.error("IOException in ResourceSetEntityView.java: ", e); 115 116 } 117 } 118 119}