Author: nicolas
Date: Tue Jan 26 09:13:43 2010
New Revision: 903133

URL: http://svn.apache.org/viewvc?rev=903133&view=rev
Log:
draft JAR-RS repporting endpoint

Added:
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/RepositoryResource.java
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/HttpUtils.java
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringServlet.java
Modified:
    commons/sandbox/monitoring/trunk/reporting/pom.xml
    
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java
    
commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java

Modified: commons/sandbox/monitoring/trunk/reporting/pom.xml
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/pom.xml?rev=903133&r1=903132&r2=903133&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/pom.xml (original)
+++ commons/sandbox/monitoring/trunk/reporting/pom.xml Tue Jan 26 09:13:43 2010
@@ -18,6 +18,18 @@
       <artifactId>commons-lang</artifactId>
       <version>2.4</version>
     </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>[2.3,)</version>
+    </dependency>
+    <dependency>
+      <groupId>javax.ws.rs</groupId>
+      <artifactId>jsr311-api</artifactId>
+      <version>1.1</version>
+      <scope>optional</scope>
+    </dependency>   
+    
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
@@ -37,4 +49,11 @@
          <scope>test</scope>
     </dependency>
   </dependencies>
+  
+  <repositories>
+    <repository>
+        <id>java.net</id>
+        <url>http://download.java.net/maven/2</url>
+     </repository>
+  </repositories>
 </project>
\ No newline at end of file

Modified: 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java?rev=903133&r1=903132&r2=903133&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java
 (original)
+++ 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java
 Tue Jan 26 09:13:43 2010
@@ -37,20 +37,11 @@
 
     public FormattingVisitor( Format format, PrintWriter writer )
     {
-        this( format, writer, Locale.getDefault() );
-    }
-
-    public FormattingVisitor( Format format, PrintWriter writer, Locale locale 
)
-    {
-        this( format, writer, DecimalFormat.getNumberInstance( locale ) );
-    }
-
-    public FormattingVisitor( Format format, PrintWriter writer, NumberFormat 
numberFormat )
-    {
         super();
         this.format = format;
         this.writer = writer;
-        this.numberFormat = numberFormat;
+        this.numberFormat = DecimalFormat.getNumberInstance( Locale.US );
+        this.numberFormat.setMinimumFractionDigits( 1 );
     }
 
     protected void doVisit( Repository repository )

Added: 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/RepositoryResource.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/RepositoryResource.java?rev=903133&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/RepositoryResource.java
 (added)
+++ 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/jaxrs/RepositoryResource.java
 Tue Jan 26 09:13:43 2010
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.monitoring.reporting.jaxrs;
+
+import static javax.ws.rs.core.HttpHeaders.ACCEPT_LANGUAGE;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static javax.ws.rs.core.MediaType.TEXT_XML;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Locale;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.Visitor;
+import org.apache.commons.monitoring.reporting.Format;
+import org.apache.commons.monitoring.reporting.FormattingVisitor;
+import org.apache.commons.monitoring.reporting.web.HttpUtils;
+
+...@path( "/repository" )
+public class RepositoryResource
+{
+    private Repository repository;
+
+    @GET
+    @Produces( TEXT_XML )
+    public String asXML( @Context ServletContext context )
+    {
+        this.repository = (Repository) context.getAttribute( 
Repository.class.getName() );
+        return asFormat( Format.XML );
+    }
+
+    @GET
+    @Produces( APPLICATION_JSON )
+    @Path( "/monitoring" )
+    public String asJSONP( @Context ServletContext context,
+                           @QueryParam( "callback" ) String callback )
+    {
+        this.repository = (Repository) context.getAttribute( 
Repository.class.getName() );
+        String json = asFormat( Format.JSON );
+        if ( callback != null )
+        {
+            return callback + "(" + json + ")";
+        }
+        return json;
+    }
+
+    public String asFormat( Format format )
+    {
+        PrintWriter writer = new PrintWriter( new StringWriter() );
+        Visitor visitor = new FormattingVisitor( format, writer );
+        repository.accept( visitor );
+        return writer.toString();
+    }
+}

Added: 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/HttpUtils.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/HttpUtils.java?rev=903133&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/HttpUtils.java
 (added)
+++ 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/HttpUtils.java
 Tue Jan 26 09:13:43 2010
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.monitoring.reporting.web;
+
+import java.util.Locale;
+
+public class HttpUtils
+{
+
+    public static String parseAccept( String header )
+    {
+        return getPrefered( header );
+    }
+
+    private static String getPrefered( String header )
+    {
+        if ( header == null )
+        {
+            return null;
+        }
+        String[] languages = header.split( "," );
+        String prefered;
+        double preference = 0.0D;
+        for ( String language : languages )
+        {
+            int idx = language.indexOf( ';' );
+            if ( idx > 0 )
+            {
+                String paramString = language.substring( idx + 1 );
+                double d = getQuality( paramString );
+                if ( d > preference )
+                {
+                    preference = d;
+                    prefered = language.substring( 0, idx );
+                }
+            }
+            else
+            {
+                return language;
+            }
+        }
+        return null;
+    }
+
+    private static double getQuality( String paramString )
+    {
+        String[] params = paramString.split( ";" );
+        for ( String param : params )
+        {
+            if ( param.startsWith( "q=" ) )
+            {
+                return Double.parseDouble( param.substring( 2 ) );
+            }
+        }
+        return 1;
+    }
+
+}

Added: 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringServlet.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringServlet.java?rev=903133&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringServlet.java
 (added)
+++ 
commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringServlet.java
 Tue Jan 26 09:13:43 2010
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.monitoring.reporting.web;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.Visitor;
+import org.apache.commons.monitoring.reporting.Format;
+import org.apache.commons.monitoring.reporting.FormattingVisitor;
+
+public class MonitoringServlet
+    extends HttpServlet
+{
+    private Repository repository;
+
+    @Override
+    public void init( ServletConfig config )
+        throws ServletException
+    {
+        repository = (Repository) config.getServletContext().getAttribute( 
Repository.class.getName() );
+    }
+
+    private static Map<String, Format> formats = new HashMap<String, Format>();
+    static
+    {
+        formats.put( "application/json", Format.JSON );
+        formats.put( "text/javascript", Format.JSON );
+        formats.put( "application/xml", Format.XML );
+        formats.put( "text/xml", Format.XML );
+    }
+
+    private static Map<String, Format> extensions = new HashMap<String, 
Format>();
+    static
+    {
+        extensions.put( "json", Format.JSON );
+        extensions.put( "js", Format.JSON );
+        extensions.put( "xml", Format.XML );
+    }
+
+    @Override
+    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
+        throws ServletException, IOException
+    {
+        String mime = HttpUtils.parseAccept( req.getHeader( "Accept" ) );
+        Format format = formats.get( mime );
+           
+        if ( format == null )
+        {
+            String path = req.getRequestURI();
+            String extension = path.substring( path.lastIndexOf( '.' ) );
+            format = formats.get( extension );
+        }
+        
+        Visitor visitor = new FormattingVisitor( format, resp.getWriter() );
+        repository.accept( visitor );
+    }
+}

Modified: 
commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java?rev=903133&r1=903132&r2=903133&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java
 (original)
+++ 
commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java
 Tue Jan 26 09:13:43 2010
@@ -27,7 +27,6 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.text.NumberFormat;
-import java.util.Locale;
 
 import org.apache.commons.monitoring.Repository;
 import org.apache.commons.monitoring.Visitor;
@@ -54,9 +53,6 @@
         repository = new DefaultRepository();
         repository.getMonitor( "RendererTest", "unit", "test" ).getCounter( 
FAILURES ).add( 1.0 );
         repository.getMonitor( "RendererTest", "unit", "test" ).getGauge( 
CONCURRENCY ).increment( UNARY );
-
-        format = NumberFormat.getNumberInstance( Locale.US );
-        format.setMinimumFractionDigits( 1 );
     }
 
     @Test
@@ -64,7 +60,7 @@
         throws Exception
     {
         StringWriter out = new StringWriter();
-        Visitor v = new FormattingVisitor( Format.XML_PRETTY, new PrintWriter( 
out ), format );
+        Visitor v = new FormattingVisitor( Format.XML_PRETTY, new PrintWriter( 
out ) );
         repository.accept( v );
 
         System.out.println( out.toString() );
@@ -77,7 +73,7 @@
         throws Exception
     {
         StringWriter out = new StringWriter();
-        Visitor v = new FormattingVisitor( Format.JSON_PRETTY, new 
PrintWriter( out ), format );
+        Visitor v = new FormattingVisitor( Format.JSON_PRETTY, new 
PrintWriter( out ) );
         repository.accept( v );
 
         System.out.println( out.toString() );


Reply via email to