Author: jim.ma
Date: 2015-03-17 05:45:27 -0400 (Tue, 17 Mar 2015)
New Revision: 19578
Added:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetInterceptor.java
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetOutInterceptor.java
Modified:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/configuration/BusHolder.java
stack/cxf/branches/management/modules/testsuite/shared-tests/src/test/java/org/jboss/test/ws/jaxws/samples/context/WebServiceContextJSETestCase.java
Log:
[JBWS-3881]:Add interceptor to query endpoint metrics
Modified:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/configuration/BusHolder.java
===================================================================
---
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/configuration/BusHolder.java 2015-03-16
16:47:51 UTC (rev 19577)
+++
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/configuration/BusHolder.java 2015-03-17
09:45:27 UTC (rev 19578)
@@ -73,6 +73,7 @@
import org.jboss.wsf.stack.cxf.interceptor.EndpointAssociationInterceptor;
import org.jboss.wsf.stack.cxf.interceptor.EndpointConfigsGetInterceptor;
import org.jboss.wsf.stack.cxf.interceptor.EndpointConfigsPutInterceptor;
+import org.jboss.wsf.stack.cxf.interceptor.EndpointMetricsGetInterceptor;
import org.jboss.wsf.stack.cxf.interceptor.HandlerAuthInterceptor;
import org.jboss.wsf.stack.cxf.interceptor.NsCtxSelectorStoreInterceptor;
import org.jboss.wsf.stack.cxf.interceptor.WSDLSoapAddressRewriteInterceptor;
@@ -208,6 +209,7 @@
bus.getInInterceptors().add(new EnableDecoupledFaultInterceptor());
bus.getInInterceptors().add(new EndpointConfigsGetInterceptor());
bus.getInInterceptors().add(new EndpointConfigsPutInterceptor());
+ bus.getInInterceptors().add(new EndpointMetricsGetInterceptor());
bus.getInInterceptors().add(new NsCtxSelectorStoreInterceptor());
final String p = (props != null) ?
props.get(Constants.JBWS_CXF_DISABLE_HANDLER_AUTH_CHECKS) : null;
Added:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetInterceptor.java
===================================================================
---
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetInterceptor.java
(rev 0)
+++
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetInterceptor.java 2015-03-17
09:45:27 UTC (rev 19578)
@@ -0,0 +1,111 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.interceptor;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.interceptor.OutgoingChainInterceptor;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+import org.jboss.wsf.spi.deployment.Endpoint;
+import org.jboss.wsf.spi.management.EndpointMetrics;
+
+/**
+ * Interceptor to get json format endpoint metrics. This interceptor is only
+ * responds to get url like
http://localhost:8080/context/wsendpoint/management?metrics
+ *@author <a href="mailto:ema@redhat.com>Jim Ma</a>
+ *
+ */
+public class EndpointMetricsGetInterceptor extends AbstractEndpintManagementInterceptor
+{
+ public static final EndpointMetricsGetInterceptor INSTANCE = new
EndpointMetricsGetInterceptor();
+ public static final String ENDPOINT_METRICS =
EndpointMetricsGetInterceptor.class.getName() + ".EndpointMetrics";
+ public static final Set<String> httpMethods = new HashSet<String>(4);
+ private Interceptor<Message> metricsOutInteceptor =
EndpointMetricsGetOutInterceptor.INSTANCE;
+ private static final String TRANSFORM_SKIP = "transform.skip";
+ static
+ {
+ httpMethods.add("GET");
+ httpMethods.add("POST");
+ }
+
+ public EndpointMetricsGetInterceptor()
+ {
+ super(Phase.READ);
+ getAfter().add(EndpointSelectionInterceptor.class.getName());
+ }
+
+ public EndpointMetricsGetInterceptor(Interceptor<Message> outInterceptor)
+ {
+ this();
+ // Let people override the EndpointConfigsGetOutIntercetpor
+ //configsOutInteceptor = outInterceptor;
+ }
+
+ public void handleMessage(Message message) throws Fault
+ {
+ if (!isAllowed(message))
+ {
+ return;
+ }
+ if (isRecognizedQuery(getQueryMap(message)))
+ {
+
+ Endpoint endpoint = message.getExchange().get(Endpoint.class);
+ EndpointMetrics endpointMetrics = endpoint.getEndpointMetrics();
+ if (endpointMetrics == null) {
+ return;
+ }
+ Message mout = this.createOutMessage(message);
+ mout.put(ENDPOINT_METRICS, endpointMetrics);
+ mout.put(Message.CONTENT_TYPE, "text/xml");
+ // just remove the interceptor which should not be used
+ cleanUpOutInterceptors(mout);
+ // notice this is being added after the purge above, don't swap the order!
+
+ mout.getInterceptorChain().add(metricsOutInteceptor);
+ message.getExchange().put(TRANSFORM_SKIP, Boolean.TRUE);
+ // skip the service executor and goto the end of the chain.
+ message.getInterceptorChain().doInterceptStartingAt(message,
OutgoingChainInterceptor.class.getName());
+ }
+ }
+
+ private boolean isRecognizedQuery(Map<String, String> map)
+ {
+ if (map.containsKey("metrics") && map.size() == 1) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ Set<String> getAllowedMethod()
+ {
+ return httpMethods;
+ }
+
+}
Property changes on:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetInterceptor.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Added:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetOutInterceptor.java
===================================================================
---
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetOutInterceptor.java
(rev 0)
+++
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetOutInterceptor.java 2015-03-17
09:45:27 UTC (rev 19578)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.interceptor;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.StaxOutInterceptor;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.codehaus.jettison.AbstractXMLStreamWriter;
+import org.codehaus.jettison.mapped.MappedNamespaceConvention;
+import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
+import org.jboss.wsf.spi.management.EndpointMetrics;
+
+/**
+ * Out Interceptor to write json format endpoint metrics result. This interceptor is
added to interceptorchain by
+ * @see org.jboss.wsf.stack.cxf.interceptor.EndpointMetricsGetInterceptor
+ * @author <a href="mailto:ema@redhat.com">Jim Ma</a>
+ *
+ */
+public class EndpointMetricsGetOutInterceptor extends
AbstractPhaseInterceptor<Message>
+{
+ public static final EndpointMetricsGetOutInterceptor INSTANCE = new
EndpointMetricsGetOutInterceptor();
+
+ public EndpointMetricsGetOutInterceptor()
+ {
+ super(Phase.PRE_STREAM);
+ getAfter().add(StaxOutInterceptor.class.getName());
+ }
+
+ public void handleMessage(Message message) throws Fault
+ {
+ EndpointMetrics metrics =
(EndpointMetrics)message.get(EndpointMetricsGetInterceptor.ENDPOINT_METRICS);
+ message.remove(EndpointMetricsGetInterceptor.ENDPOINT_METRICS);
+ OutputStream out = message.getContent(OutputStream.class);
+ if (out == null)
+ {
+ return;
+ }
+ message.put(Message.CONTENT_TYPE, "text/xml");
+ OutputStreamWriter writer = null;
+ AbstractXMLStreamWriter mappedWriter = null;
+ try
+ {
+ writer = new OutputStreamWriter(out, getEncoding(message));
+ mappedWriter = new MappedXMLStreamWriter(new MappedNamespaceConvention(),
writer);
+ mappedWriter.writeStartDocument();
+ writeElement(mappedWriter, "requestCount",
metrics.getRequestCount());
+ writeElement(mappedWriter, "responseCount",
metrics.getResponseCount());
+ writeElement(mappedWriter, "faultCount", metrics.getFaultCount());
+ writeElement(mappedWriter, "totalProcessingTime",
metrics.getTotalProcessingTime());
+ writeElement(mappedWriter, "maxProcessingTime",
metrics.getMaxProcessingTime());
+ writeElement(mappedWriter, "minProcessingTime",
metrics.getMinProcessingTime());
+ writeElement(mappedWriter, "averageProcessingTime",
metrics.getAverageProcessingTime());
+ mappedWriter.writeEndDocument();
+ out.flush();
+ }
+ catch (Exception e)
+ {
+ throw new Fault(e);
+ }
+ finally
+ {
+ StaxUtils.close(mappedWriter);
+ }
+ }
+
+ private void writeElement(AbstractXMLStreamWriter writter, String key, long value)
throws Exception
+ {
+ writter.writeStartElement(key);
+ writter.writeCharacters(String.valueOf(value));
+ writter.writeEndElement();
+
+ }
+ private String getEncoding(Message message)
+ {
+ Exchange ex = message.getExchange();
+ String encoding = (String)message.get(Message.ENCODING);
+ if (encoding == null && ex.getInMessage() != null)
+ {
+ encoding = (String)ex.getInMessage().get(Message.ENCODING);
+ message.put(Message.ENCODING, encoding);
+ }
+
+ if (encoding == null)
+ {
+ encoding = "UTF-8";
+ message.put(Message.ENCODING, encoding);
+ }
+ return encoding;
+ }
+
+}
Property changes on:
stack/cxf/branches/management/modules/server/src/main/java/org/jboss/wsf/stack/cxf/interceptor/EndpointMetricsGetOutInterceptor.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Modified:
stack/cxf/branches/management/modules/testsuite/shared-tests/src/test/java/org/jboss/test/ws/jaxws/samples/context/WebServiceContextJSETestCase.java
===================================================================
---
stack/cxf/branches/management/modules/testsuite/shared-tests/src/test/java/org/jboss/test/ws/jaxws/samples/context/WebServiceContextJSETestCase.java 2015-03-16
16:47:51 UTC (rev 19577)
+++
stack/cxf/branches/management/modules/testsuite/shared-tests/src/test/java/org/jboss/test/ws/jaxws/samples/context/WebServiceContextJSETestCase.java 2015-03-17
09:45:27 UTC (rev 19578)
@@ -137,7 +137,7 @@
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtils.copy(connenction.getInputStream(), bout);
assertTrue("Unexpected response",
bout.toString().contains("\"type\":\"JAXWS_JSE\",\"securityDomain\":\"JBossWS\""));
-
+ //enable statistics
url = new URL(baseURL +
"/jaxws-samples-context-jse/management?statistics-enabled=true");
connenction = (HttpURLConnection)url.openConnection();
connenction.setRequestProperty("Authorization", "Basic " +
encoding);
@@ -146,6 +146,18 @@
bout = new ByteArrayOutputStream();
IOUtils.copy(connenction.getInputStream(), bout);
assertTrue("Unexpected response",
bout.toString().contains("Successfully set endpoint runtime configurations"));
+ //call webservice
+ port.testMessageContextProperties();
+ //get metrics
+ url = new URL(baseURL +
"/jaxws-samples-context-jse/management?metrics");
+ connenction = (HttpURLConnection)url.openConnection();
+ connenction.setRequestProperty("Authorization", "Basic " +
encoding);
+ connenction.connect();
+ assertEquals(200, connenction.getResponseCode());
+ bout = new ByteArrayOutputStream();
+ IOUtils.copy(connenction.getInputStream(), bout);
+ assertTrue("Unexpected response",
bout.toString().contains("\"requestCount\":3"));
+
}
}