View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.reporting.web.plugin.report;
18  
19  import org.apache.commons.monitoring.MonitoringException;
20  import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
21  import org.apache.commons.monitoring.reporting.web.handler.api.Template;
22  import org.apache.commons.monitoring.reporting.web.plugin.report.format.Format;
23  import org.apache.commons.monitoring.repositories.Repository;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.io.IOException;
28  
29  public class ReportEndpoints {
30      @Regex
31      public Template html(final HttpServletRequest request, final HttpServletResponse response) {
32          return renderFormat(request, response, Format.Defaults.HTML);
33      }
34  
35      @Regex(".csv")
36      public Template csv(final HttpServletRequest request, final HttpServletResponse response) {
37          return renderFormat(request, response, Format.Defaults.CSV);
38      }
39  
40      @Regex(".json")
41      public Template json(final HttpServletRequest request, final HttpServletResponse response) {
42          return renderFormat(request, response, Format.Defaults.JSON);
43      }
44  
45      @Regex(".xml")
46      public Template xml(final HttpServletRequest request, final HttpServletResponse response) {
47          return renderFormat(request, response, Format.Defaults.XML);
48      }
49  
50      @Regex("/clear")
51      public void clear(final HttpServletRequest request, final HttpServletResponse response) {
52          Repository.INSTANCE.clear();
53          try {
54              response.sendRedirect(request.getRequestURI().substring(0, request.getRequestURI().length() - "/clear".length()));
55          } catch (final IOException e) {
56              throw new MonitoringException(e);
57          }
58      }
59  
60      private Template renderFormat(final HttpServletRequest request, final HttpServletResponse response, final Format format) {
61          response.setContentType(format.type());
62          return format.render(request.getParameterMap());
63      }
64  }