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 *******************************************************************************/
018package org.mitre.openid.connect.web;
019
020import java.util.Map;
021
022import org.mitre.openid.connect.model.ClientStat;
023import org.mitre.openid.connect.service.StatsService;
024import org.mitre.openid.connect.view.JsonEntityView;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.http.MediaType;
029import org.springframework.security.access.prepost.PreAuthorize;
030import org.springframework.stereotype.Controller;
031import org.springframework.ui.ModelMap;
032import org.springframework.web.bind.annotation.PathVariable;
033import org.springframework.web.bind.annotation.RequestMapping;
034
035@Controller
036@RequestMapping("/" + StatsAPI.URL)
037public class StatsAPI {
038
039        public static final String URL = RootController.API_URL + "/stats";
040
041        // Logger for this class
042        private static final Logger logger = LoggerFactory.getLogger(StatsAPI.class);
043
044        @Autowired
045        private StatsService statsService;
046
047        @RequestMapping(value = "summary", produces = MediaType.APPLICATION_JSON_VALUE)
048        public String statsSummary(ModelMap m) {
049
050                Map<String, Integer> e = statsService.getSummaryStats();
051
052                m.put(JsonEntityView.ENTITY, e);
053
054                return JsonEntityView.VIEWNAME;
055
056        }
057
058        //      @PreAuthorize("hasRole('ROLE_USER')")
059        //      @RequestMapping(value = "byclientid", produces = MediaType.APPLICATION_JSON_VALUE)
060        //      public String statsByClient(ModelMap m) {
061        //              Map<Long, Integer> e = statsService.getByClientId();
062        //
063        //              m.put(JsonEntityView.ENTITY, e);
064        //
065        //              return JsonEntityView.VIEWNAME;
066        //      }
067        //
068        @PreAuthorize("hasRole('ROLE_USER')")
069        @RequestMapping(value = "byclientid/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
070        public String statsByClientId(@PathVariable("id") String clientId, ModelMap m) {
071                ClientStat e = statsService.getCountForClientId(clientId);
072
073                m.put(JsonEntityView.ENTITY, e);
074
075                return JsonEntityView.VIEWNAME;
076        }
077
078}