Author: jeff.yuchang
Date: 2011-04-03 08:49:32 -0400 (Sun, 03 Apr 2011)
New Revision: 1347
Added:
trunk/samples/quickstart/management/
trunk/samples/quickstart/management/build.xml
trunk/samples/quickstart/management/readme.txt
trunk/samples/quickstart/management/src/
trunk/samples/quickstart/management/src/org/
trunk/samples/quickstart/management/src/org/jboss/
trunk/samples/quickstart/management/src/org/jboss/riftsaw/
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/GsonFactory.java
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/ManagementClient.java
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/SQLDateTypeAdapter.java
Modified:
trunk/samples/common/base-build.xml
Log:
*RIFTSAW-365, add example for the RESTful web services.
Modified: trunk/samples/common/base-build.xml
===================================================================
--- trunk/samples/common/base-build.xml 2011-04-01 03:48:04 UTC (rev 1346)
+++ trunk/samples/common/base-build.xml 2011-04-03 12:49:32 UTC (rev 1347)
@@ -27,6 +27,12 @@
<fileset dir="${org_jboss_as_home}/lib/endorsed">
<include name="**/*.jar"/>
</fileset>
+ <fileset dir="../../../modules/bpel-console">
+ <include name="**/*.jar" />
+ </fileset>
+ <fileset
dir="${deploy.dir}/bpel-console/gwt-console-server.war/WEB-INF/lib">
+ <include name="**/gson-*.jar"/>
+ </fileset>
</path>
</project>
Added: trunk/samples/quickstart/management/build.xml
===================================================================
--- trunk/samples/quickstart/management/build.xml (rev 0)
+++ trunk/samples/quickstart/management/build.xml 2011-04-03 12:49:32 UTC (rev 1347)
@@ -0,0 +1,32 @@
+<project name="management" default="runclient"
basedir=".">
+
+ <description>
+ ${ant.project.name}
+ ${line.separator}
+ </description>
+
+ <property file="../../../install/deployment.properties" />
+ <import file="../../common/base-build.xml"/>
+
+ <property name="build.dir"
location="target/build/classes"/>
+
+ <target name="compile">
+ <mkdir dir="${build.dir}" />
+ <javac srcdir="src" destdir="${build.dir}"
debug="true">
+ <classpath>
+ <path refid="rs-exec-classpath" />
+ </classpath>
+ </javac>
+
+ </target>
+
+ <target name="runclient" depends="compile">
+ <echo>Send test message to: ${ant.project.name}</echo>
+ <java classname="org.jboss.riftsaw.management.ManagementClient">
+ <classpath>
+ <pathelement location="${build.dir}" />
+ <path refid="rs-exec-classpath" />
+ </classpath>
+ </java>
+ </target>
+</project>
Added: trunk/samples/quickstart/management/readme.txt
===================================================================
--- trunk/samples/quickstart/management/readme.txt (rev 0)
+++ trunk/samples/quickstart/management/readme.txt 2011-04-03 12:49:32 UTC (rev 1347)
@@ -0,0 +1,24 @@
+Quickstart Examples - Management
+===================================
+
+This is a simple example to demonstrate our management APIs that are exposed as Restful
services.
+
+This example shows three restful services:
+
+1). all deployments: This service returns all of deployed artifacts on the server.
+2). all process definitions: This services returns all of process definitions that are
deployed on server.
+3). active process instances for simple_correlation example process definition.
+
+By default, all of restful services are secured by form authentication, except the status
service and the deployments
+service.
+
+In the example, firstly, we've invoked the restful service, and then with the JSON
result, we've used the
+third party library (Gson) to marshall the JSON data into Java Object, where you can find
it on the gwt-console-rpc.jar.
+
+These all restful services are built from the bpm console project, not on the riftsaw
core components.
+for details on bpm console, please refer to
https://github.com/bpmc/bpm-console/
+
+
+some handy URLs:
+
+http://localhost:8080/gwt-console-server/rs/server/resources/riftsaw RiftSaw Restful
Services document
\ No newline at end of file
Added:
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/GsonFactory.java
===================================================================
--- trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/GsonFactory.java
(rev 0)
+++
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/GsonFactory.java 2011-04-03
12:49:32 UTC (rev 1347)
@@ -0,0 +1,20 @@
+package org.jboss.riftsaw.management;
+
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+/**
+ * @author Heiko.Braun <heiko.braun(a)jboss.com>
+ */
+public class GsonFactory
+{
+ public static Gson createInstance()
+ {
+ Gson gson = new GsonBuilder()
+ .setDateFormat("yyyy-MM-dd HH:mm:ss")
+ .registerTypeAdapter(java.sql.Timestamp.class, new SQLDateTypeAdapter())
+ .create();
+ return gson;
+ }
+}
\ No newline at end of file
Added:
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/ManagementClient.java
===================================================================
---
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/ManagementClient.java
(rev 0)
+++
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/ManagementClient.java 2011-04-03
12:49:32 UTC (rev 1347)
@@ -0,0 +1,134 @@
+package org.jboss.riftsaw.management;
+
+import org.apache.commons.httpclient.*;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.jboss.bpm.console.client.model.*;
+
+import java.io.IOException;
+import org.jboss.soa.bpel.console.ModelAdaptor;
+
+import com.google.gson.*;
+
+
+/**
+ * User: Jeff Yu
+ * Date: 1/04/11
+ */
+public class ManagementClient {
+
+ private static final String deployment_url =
"http://localhost:8080/gwt-console-server/rs/engine/deployments";
+
+ private static final String definitions_url =
"http://localhost:8080/gwt-console-server/rs/process/definitions";
+
+ private static final String authentication_url =
"http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check";
+
+ public void showAllDeployments() throws Exception {
+ System.out.println("-----------------------------");
+ System.out.println("Get all of deployments: ");
+ String result = getDataFromService(deployment_url, "GET", null, null);
+
+ System.out.println("-----------------------------");
+ System.out.println("Marshall the Json data into java class.");
+
+ Gson gson = GsonFactory.createInstance();
+ DeploymentRefWrapper wrapper = gson.fromJson(result,
DeploymentRefWrapper.class);
+
+ for (DeploymentRef ref : wrapper.getDeployments()) {
+ System.out.println("deployment name is: " + ref.getName());
+ }
+
+ }
+
+ public void showAllDefinitions() throws Exception {
+ System.out.println("-----------------------------");
+ System.out.println("Get all of process definitions: ");
+ String result = getDataFromService(definitions_url, "GET",
"admin", "password");
+
+ System.out.println("-----------------------------");
+ System.out.println("Marshall the Json data into java class.");
+
+ Gson gson = GsonFactory.createInstance();
+ ProcessDefinitionRefWrapper wrapper = gson.fromJson(result,
ProcessDefinitionRefWrapper.class);
+
+ for (ProcessDefinitionRef ref : wrapper.getDefinitions()) {
+ System.out.println("process name is: " + ref.getName());
+ }
+ }
+
+ public void getActiveProcessInstance() throws Exception {
+
+ String processId =
"{http://www.jboss.org/bpel/examples}HelloGoodbye-1";
+ String encodedId = ModelAdaptor.encodeId(processId);
+ String instances_url =
"http://localhost:8080/gwt-console-server/rs/process/definition" + "/"
+ encodedId + "/instances";
+ System.out.println("-----------------------------");
+ System.out.println("Get active process instances from process definition of
: " + processId );
+ String result = getDataFromService(instances_url, "GET",
"admin", "password");
+
+ System.out.println("-----------------------------");
+ System.out.println("Marshall the Json data into java class.");
+
+ Gson gson = GsonFactory.createInstance();
+ ProcessInstanceRefWrapper wrapper = gson.fromJson(result,
ProcessInstanceRefWrapper.class);
+
+ for (ProcessInstanceRef ref : wrapper.getInstances()) {
+ System.out.println("instance id is: " + ref.getId() + "
definition key is: " + ModelAdaptor.decodeId(ref.getDefinitionId()));
+ }
+ }
+
+ private String getDataFromService(String urlpath, String method, String username,
String password ) throws Exception{
+ HttpClient httpclient = new HttpClient();
+
+ HttpMethod theMethod = null;
+ StringBuffer sb = new StringBuffer();
+
+ if ("GET".equalsIgnoreCase(method)) {
+ theMethod = new GetMethod(urlpath);
+ } else if ("POST".equalsIgnoreCase(method)) {
+ theMethod = new PostMethod(urlpath);
+ }
+
+ if (username != null && password != null) {
+
+ try {
+ httpclient.executeMethod(theMethod);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ theMethod.releaseConnection();
+ }
+ PostMethod authMethod = new PostMethod(authentication_url);
+ NameValuePair[] data = {new NameValuePair("j_username", username), new
NameValuePair("j_password", password)};
+ authMethod.setRequestBody(data);
+ try {
+ httpclient.executeMethod(authMethod);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ authMethod.releaseConnection();
+ }
+ }
+
+ try {
+ httpclient.executeMethod(theMethod);
+ sb.append(theMethod.getResponseBodyAsString());
+ System.out.println("JSon Result: => " + sb.toString());
+ return sb.toString();
+
+ }catch (Exception e) {
+ throw e;
+ }finally {
+ theMethod.releaseConnection();
+ }
+
+ }
+
+ public static void main(String[] args) throws Exception {
+ ManagementClient client = new ManagementClient();
+ client.showAllDeployments();
+ client.showAllDefinitions();
+ client.getActiveProcessInstance();
+ }
+
+
+}
Added:
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/SQLDateTypeAdapter.java
===================================================================
---
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/SQLDateTypeAdapter.java
(rev 0)
+++
trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/SQLDateTypeAdapter.java 2011-04-03
12:49:32 UTC (rev 1347)
@@ -0,0 +1,69 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, 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.riftsaw.management;
+
+import com.google.gson.*;
+
+import java.util.Date;
+import java.text.*;
+import java.lang.reflect.Type;
+
+
+/**
+ * A default type adapter for a {@link java.util.Date} object.<br>
+ * Create a GSON instance that can serialize/deserialize "java.util.Date"
objects:
+ * <pre>
+ * Gson gson = new GsonBuilder()
+ * .registerTypeAdapter(new DateTypeAdapter())
+ * .create();
+ * </pre>
+ *
+ * @author Joel Leitch
+ */
+public class SQLDateTypeAdapter implements JsonSerializer<java.sql.Timestamp>,
JsonDeserializer<Date>
+{
+ private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
+
+ public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc,
JsonSerializationContext context)
+ {
+ String dateFormatAsString = format.format(src);
+ return new JsonPrimitive(dateFormatAsString);
+ }
+
+ public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
context)
+ throws JsonParseException
+ {
+ if (!(json instanceof JsonPrimitive)) {
+ throw new JsonParseException("The date should be a string value");
+ }
+
+ try
+ {
+ return format.parse(json.getAsString());
+ }
+ catch (ParseException e)
+ {
+ throw new JsonParseException(e);
+ }
+
+ }
+}