ClientInformationResponseView.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. /**
  19.  *
  20.  */
  21. package org.mitre.openid.connect.view;

  22. import java.io.IOException;
  23. import java.io.Writer;
  24. import java.util.Map;

  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;

  27. import org.mitre.oauth2.model.RegisteredClient;
  28. import org.mitre.openid.connect.ClientDetailsEntityJsonProcessor;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.http.HttpStatus;
  32. import org.springframework.http.MediaType;
  33. import org.springframework.stereotype.Component;
  34. import org.springframework.web.servlet.view.AbstractView;

  35. import com.google.gson.Gson;
  36. import com.google.gson.JsonIOException;
  37. import com.google.gson.JsonObject;

  38. /**
  39.  *
  40.  * Provides representation of a client's registration metadata, to be shown from the dynamic registration endpoint
  41.  * on the client_register and rotate_secret operations.
  42.  *
  43.  * @author jricher
  44.  *
  45.  */
  46. @Component(ClientInformationResponseView.VIEWNAME)
  47. public class ClientInformationResponseView extends AbstractView {

  48.     /**
  49.      * Logger for this class
  50.      */
  51.     private static final Logger logger = LoggerFactory.getLogger(ClientInformationResponseView.class);

  52.     public static final String VIEWNAME = "clientInformationResponseView";

  53.     // note that this won't serialize nulls by default
  54.     private Gson gson = new Gson();

  55.     /* (non-Javadoc)
  56.      * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  57.      */
  58.     @Override
  59.     protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {

  60.         response.setContentType(MediaType.APPLICATION_JSON_VALUE);

  61.         RegisteredClient c = (RegisteredClient) model.get("client");
  62.         //OAuth2AccessTokenEntity token = (OAuth2AccessTokenEntity) model.get("token");
  63.         //String uri = (String)model.get("uri"); //request.getRequestURL() + "/" + c.getClientId();

  64.         HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
  65.         if (code == null) {
  66.             code = HttpStatus.OK;
  67.         }
  68.         response.setStatus(code.value());

  69.         JsonObject o = ClientDetailsEntityJsonProcessor.serialize(c);

  70.         try {
  71.             Writer out = response.getWriter();
  72.             gson.toJson(o, out);
  73.         } catch (JsonIOException e) {

  74.             logger.error("JsonIOException in ClientInformationResponseView.java: ", e);

  75.         } catch (IOException e) {

  76.             logger.error("IOException in ClientInformationResponseView.java: ", e);

  77.         }

  78.     }

  79. }