001/* 002 * Copyright 2011-2016 UnboundID Corp. 003 * 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of the GNU General Public License (GPLv2 only) 006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 007 * as published by the Free Software Foundation. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, see <http://www.gnu.org/licenses>. 016 */ 017 018package com.unboundid.scim.wink; 019 020import com.unboundid.scim.sdk.Debug; 021import com.unboundid.scim.sdk.Version; 022import org.json.JSONException; 023import org.json.JSONStringer; 024import org.json.JSONWriter; 025 026import javax.ws.rs.GET; 027import javax.ws.rs.Path; 028import javax.ws.rs.Produces; 029import javax.ws.rs.core.MediaType; 030import javax.ws.rs.core.Response; 031import java.util.Map; 032 033 034/** 035 * This class is a JAX-RS resource to allow monitor data to be fetched and 036 * reset. 037 */ 038@Path("monitor") 039public class MonitorResource 040{ 041 private static final String RESOURCE_NAME = "monitor"; 042 private final SCIMApplication application; 043 044 /** 045 * Create a new JAX-RS resource. 046 * 047 * @param application The SCIM JAX-RS application associated with this 048 * resource. 049 */ 050 public MonitorResource(final SCIMApplication application) { 051 this.application = application; 052 } 053 054 /** 055 * Implement the GET operation on the monitor resource to fetch the monitor 056 * data in JSON format. 057 * 058 * @return The response to the request. 059 */ 060 @GET 061 @Produces(MediaType.APPLICATION_JSON) 062 public Response doJsonGet() 063 { 064 try 065 { 066 final JSONStringer writer = new JSONStringer(); 067 writeMonitorData(writer); 068 application.getStatsForResource(RESOURCE_NAME).incrementStat( 069 ResourceStats.GET_RESPONSE_JSON); 070 application.getStatsForResource(RESOURCE_NAME).incrementStat( 071 ResourceStats.GET_OK); 072 return Response.ok(writer.toString(), MediaType.APPLICATION_JSON).build(); 073 } 074 catch (JSONException e) 075 { 076 Debug.debugException(e); 077 application.getStatsForResource(RESOURCE_NAME).incrementStat( 078 ResourceStats.GET_INTERNAL_SERVER_ERROR); 079 return Response.serverError().entity(e.getMessage()).build(); 080 } 081 } 082 083 084 085 /** 086 * Write the monitor data in JSON format. 087 * 088 * @param writer A JSON writer where the monitor data is to be written. 089 * 090 * @throws JSONException If an error occurs while formatting the data. 091 */ 092 private void writeMonitorData(final JSONWriter writer) 093 throws JSONException 094 { 095 writer.object(); 096 writer.key("version"); 097 writer.value(Version.VERSION); 098 writer.key("build"); 099 writer.value(Version.BUILD_TIMESTAMP); 100 writer.key("revision"); 101 writer.value(Version.REVISION_ID); 102 103 writer.key("resources"); 104 writer.array(); 105 for(ResourceStats stats : application.getResourceStats()) 106 { 107 writer.object(); 108 writer.key("name"); 109 writer.value(stats.getName()); 110 for(Map.Entry<String, Long> stat : stats.getStats().entrySet()) 111 { 112 writer.key(stat.getKey()); 113 writer.value(stat.getValue()); 114 } 115 writer.endObject(); 116 } 117 writer.endArray(); 118 writer.endObject(); 119 } 120}