Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.authserver.web.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.apigw.appmanagement.ApplicationManagementService; import org.apigw.appmanagement.domain.Application; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.ModelAndView; /** * List all actions for a user * * @author albert * */ @Controller @SessionAttributes @RequestMapping(value = "developer/monitoring") public class MonitoringController { private static final Logger log = LoggerFactory.getLogger(MonitoringController.class); private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Autowired private ApplicationManagementService appManagement; @Value("${monitoring.api.location}") private String location;// = "http://localhost:9000"; @RequestMapping(method = RequestMethod.GET, params = { "clientId", "state" }) public ModelAndView list(@RequestParam("clientId") String clientId, @RequestParam("state") String state, Authentication authentication) { Application application = appManagement.getApplicationByClientId(clientId); UserDetails user = (UserDetails) authentication.getPrincipal(); if (application == null) { throw new IllegalArgumentException("No application found with client id " + clientId); } if (!user.getUsername().equals(application.getDeveloper().getResidentIdentificationNumber())) { throw new IllegalArgumentException("Application developer is not the same as the logged in user"); } if (!state.toUpperCase().equals("SERVER_FAILURE") && !state.toUpperCase().equals("CLIENT_FAILURE") && !state.toUpperCase().equals("SUCCESS")) { throw new IllegalArgumentException("Provided state not recogonized: " + state); } TreeMap<String, Object> model = new TreeMap<String, Object>(); RestTemplate template = new RestTemplate(); long from = System.currentTimeMillis() - 24 * 3600 * 1000; Map<String, Object> params = new HashMap<String, Object>(); params.put("from", from); params.put("client", clientId); params.put("state", state); // Load list of last 100 failed requests List<Map<String, Object>> requests = template.getForObject( location + "/api/timeline/resourceRequest?from={from}&state={state}&client={client}", List.class, params); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (Map<String, Object> request : requests) { long timestamp = (Long) request.get("timestamp"); Date date = new Date(timestamp); request.put("datetime", format.format(date)); } model.put("requests", requests); model.put("client", clientId); model.put("state", state); return new ModelAndView("monitoring", model); } // @RequestMapping(value="", method = RequestMethod.GET, params={"clientId", "logId"}) // public ModelAndView findByLogId(@RequestParam("clientId") String clientId, // @RequestParam("logId") String logId){ // // TreeMap<String, Object> model = new TreeMap<String, Object>(); // // RestTemplate template = new RestTemplate(); // // return new ModelAndView("actions", model); // } }