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 *******************************************************************************/ 018/** 019 * 020 */ 021package org.mitre.discovery.view; 022 023import java.io.IOException; 024import java.io.Writer; 025import java.util.Map; 026 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.mitre.openid.connect.view.HttpCodeView; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 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.gson.ExclusionStrategy; 039import com.google.gson.FieldAttributes; 040import com.google.gson.Gson; 041import com.google.gson.GsonBuilder; 042import com.google.gson.JsonArray; 043import com.google.gson.JsonObject; 044 045/** 046 * @author jricher 047 * 048 */ 049@Component("webfingerView") 050public class WebfingerView extends AbstractView { 051 052 /** 053 * Logger for this class 054 */ 055 private static final Logger logger = LoggerFactory.getLogger(WebfingerView.class); 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 .create(); 079 080 @Override 081 protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { 082 083 response.setContentType("application/jrd+json"); 084 085 086 HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE); 087 if (code == null) { 088 code = HttpStatus.OK; // default to 200 089 } 090 091 response.setStatus(code.value()); 092 093 try { 094 095 String resource = (String)model.get("resource"); 096 String issuer = (String)model.get("issuer"); 097 098 JsonObject obj = new JsonObject(); 099 obj.addProperty("subject", resource); 100 101 JsonArray links = new JsonArray(); 102 JsonObject link = new JsonObject(); 103 link.addProperty("rel", "http://openid.net/specs/connect/1.0/issuer"); 104 link.addProperty("href", issuer); 105 links.add(link); 106 107 obj.add("links", links); 108 109 Writer out = response.getWriter(); 110 gson.toJson(obj, out); 111 112 } catch (IOException e) { 113 114 logger.error("IOException in JsonEntityView.java: ", e); 115 116 } 117 } 118 119}