001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.monitoring.reporting.web.plugin.report; 018 019 import org.apache.commons.monitoring.MonitoringException; 020 import org.apache.commons.monitoring.reporting.web.handler.api.Regex; 021 import org.apache.commons.monitoring.reporting.web.handler.api.Template; 022 import org.apache.commons.monitoring.reporting.web.plugin.report.format.Format; 023 import org.apache.commons.monitoring.repositories.Repository; 024 025 import javax.servlet.http.HttpServletRequest; 026 import javax.servlet.http.HttpServletResponse; 027 import java.io.IOException; 028 029 public class ReportEndpoints { 030 @Regex 031 public Template html(final HttpServletRequest request, final HttpServletResponse response) { 032 return renderFormat(request, response, Format.Defaults.HTML); 033 } 034 035 @Regex(".csv") 036 public Template csv(final HttpServletRequest request, final HttpServletResponse response) { 037 return renderFormat(request, response, Format.Defaults.CSV); 038 } 039 040 @Regex(".json") 041 public Template json(final HttpServletRequest request, final HttpServletResponse response) { 042 return renderFormat(request, response, Format.Defaults.JSON); 043 } 044 045 @Regex(".xml") 046 public Template xml(final HttpServletRequest request, final HttpServletResponse response) { 047 return renderFormat(request, response, Format.Defaults.XML); 048 } 049 050 @Regex("/clear") 051 public void clear(final HttpServletRequest request, final HttpServletResponse response) { 052 Repository.INSTANCE.clear(); 053 try { 054 response.sendRedirect(request.getRequestURI().substring(0, request.getRequestURI().length() - "/clear".length())); 055 } catch (final IOException e) { 056 throw new MonitoringException(e); 057 } 058 } 059 060 private Template renderFormat(final HttpServletRequest request, final HttpServletResponse response, final Format format) { 061 response.setContentType(format.type()); 062 return format.render(request.getParameterMap()); 063 } 064 }