View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.epcis.queryclient;
22  
23  import java.net.URL;
24  import java.util.List;
25  
26  /**
27   * A simple test utility class which demonstrates how to send an EPCIS query
28   * subscription to the Fosstrak EPCIS repository.
29   * 
30   * @author Marco Steybe
31   */
32  public class SimpleSubscriptionsTest {
33  
34      protected static final String LOCAL_EPCIS_QUERY_URL = "http://localhost:8080/epcis-repository/query";
35      protected static final String DEMO_EPCIS_QUERY_URL = "http://demo.fosstrak.org/epcis/query";
36  
37      // Note: keep the methods in this class static in order to prevent them from
38      // being executed when building the project with Maven.
39  
40      public static void main(String[] args) throws Exception {
41          // configure the query service
42          String queryUrl = LOCAL_EPCIS_QUERY_URL;
43          QueryControlClient client = new QueryControlClient();
44          client.configureService(new URL(queryUrl), null);
45  
46          // subscribe a query
47          System.out.println("Sending subscription:");
48          String mySubscrId = "mySubscription";
49          String xml = createSubscriptionXml(mySubscrId);
50          System.out.println(xml);
51          client.subscribe(xml);
52  
53          // list subscription IDs
54          System.out.println("Listing all subscribed queries:");
55          List<String> subscrIds = client.getSubscriptionIds("SimpleEventQuery");
56          for (String subscrId : subscrIds) {
57              System.out.println(" - " + subscrId);
58          }
59  
60          // TODO you should wait here and listen for callbacks on the specified address
61  
62          // unsubscribe a query
63          System.out.println("Unsubscribing query subscription with ID: " + mySubscrId);
64          client.unsubscribe(mySubscrId);
65  
66          // list subscription IDs ('mySubscription' should not be present anymore)
67          System.out.println("Listing all subscribed queries:");
68          subscrIds = client.getSubscriptionIds("SimpleEventQuery");
69          for (String subscrId : subscrIds) {
70              System.out.println(" - " + subscrId);
71          }
72      }
73  
74      private static String createSubscriptionXml(String subscriptionId) {
75          StringBuilder sb = new StringBuilder();
76          sb.append("<epcisq:Subscribe xmlns:epcisq=\"urn:epcglobal:epcis-query:xsd:1\">\n");
77          sb.append("<queryName>SimpleEventQuery</queryName>\n");
78          sb.append("<params>\n");
79          sb.append("  <param>\n");
80          sb.append("    <name>eventType</name>\n");
81          sb.append("    <value>\n");
82          sb.append("      <string>ObjectEvent</string>\n");
83          sb.append("    </value>\n");
84          sb.append("  </param>\n");
85          sb.append("  <param>\n");
86          sb.append("    <name>MATCH_epc</name>\n");
87          sb.append("    <value>\n");
88          sb.append("      <string>urn:epc:id:sgtin:0614141.107346.2017</string>\n");
89          sb.append("    </value>\n");
90          sb.append("  </param>\n");
91          sb.append("</params>\n");
92          sb.append("<dest>http://localhost:8888/</dest> <!-- this is where query results will be delivered to -->\n");
93          sb.append("<controls>\n");
94          sb.append("  <schedule>\n");
95          sb.append("    <second>0</second> <!-- every full minute -->\n");
96          sb.append("  </schedule>\n");
97          sb.append("  <initialRecordTime>2008-03-16T00:00:00+01:00</initialRecordTime>\n");
98          sb.append("  <reportIfEmpty>false</reportIfEmpty>\n");
99          sb.append("</controls>\n");
100         sb.append("<subscriptionID>").append(subscriptionId).append("</subscriptionID>\n");
101         sb.append("</epcisq:Subscribe>");
102         return sb.toString();
103     }
104 }