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.JsonEntityView;
027import org.mitre.uma.model.ResourceSet;
028import org.mitre.util.JsonUtils;
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(ResourceSetEntityView.VIEWNAME)
047public class ResourceSetEntityView extends AbstractView {
048        private static Logger logger = LoggerFactory.getLogger(JsonEntityView.class);
049
050        public static final String VIEWNAME = "resourceSetEntityView";
051
052        @Autowired
053        private ConfigurationPropertiesBean config;
054
055        private Gson gson = new GsonBuilder()
056                        .setExclusionStrategies(new ExclusionStrategy() {
057
058                                @Override
059                                public boolean shouldSkipField(FieldAttributes f) {
060
061                                        return false;
062                                }
063
064                                @Override
065                                public boolean shouldSkipClass(Class<?> clazz) {
066                                        // skip the JPA binding wrapper
067                                        if (clazz.equals(BeanPropertyBindingResult.class)) {
068                                                return true;
069                                        }
070                                        return false;
071                                }
072
073                        })
074                        .serializeNulls()
075                        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
076                        .setLongSerializationPolicy(LongSerializationPolicy.STRING)
077                        .create();
078
079        @Override
080        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
081
082                response.setContentType("application/json");
083
084
085                HttpStatus code = (HttpStatus) model.get("code");
086                if (code == null) {
087                        code = HttpStatus.OK; // default to 200
088                }
089
090                response.setStatus(code.value());
091
092                String location = (String) model.get("location");
093                if (!Strings.isNullOrEmpty(location)) {
094                        response.setHeader(HttpHeaders.LOCATION, location);
095                }
096
097                try {
098
099                        Writer out = response.getWriter();
100                        ResourceSet rs = (ResourceSet) model.get("entity");
101
102                        JsonObject o = new JsonObject();
103
104                        o.addProperty("_id", rs.getId().toString()); // send the id as a string
105                        o.addProperty("user_access_policy_uri", config.getIssuer() + "manage/resource/" + rs.getId());
106                        o.addProperty("name", rs.getName());
107                        o.addProperty("uri", rs.getUri());
108                        o.addProperty("type", rs.getType());
109                        o.add("scopes", JsonUtils.getAsArray(rs.getScopes()));
110                        o.addProperty("icon_uri", rs.getIconUri());
111
112                        gson.toJson(o, out);
113
114                } catch (IOException e) {
115
116                        logger.error("IOException in ResourceSetEntityView.java: ", e);
117
118                }
119        }
120
121}