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.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.PrintWriter;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.fosstrak.epcis.model.ImplementationException;
34  import org.fosstrak.epcis.model.QueryResults;
35  import org.fosstrak.epcis.model.QueryTooLargeException;
36  import org.fosstrak.epcis.utils.QueryResultsParser;
37  
38  /**
39   * @author Marco Steybe
40   */
41  public class QueryCallbackClient extends HttpServlet implements QueryCallbackInterface {
42  
43      private static final long serialVersionUID = 6250815925403597265L;
44      private static String callbackResults = null;
45  
46      /**
47       * {@inheritDoc}
48       * 
49       * @see org.fosstrak.epcis.queryclient.QueryCallbackInterface#callbackResults(org.fosstrak.epcis.soapapi.QueryResults)
50       */
51      public void callbackResults(QueryResults result) {
52          InputStream is = new ByteArrayInputStream(callbackResults.getBytes());
53          try {
54              result = QueryResultsParser.parseResults(is);
55          } catch (IOException e) {
56              // TODO auto-generated catch block
57              e.printStackTrace();
58          }
59      }
60  
61      /**
62       * Performs a callback for a standing query. When the callback returns, the
63       * given String will be populated with an XML representation of a standing
64       * query result.
65       * 
66       * @param result
67       *            The String to be populated.
68       */
69      public static void callbackQueryResults(String result) {
70          result = callbackResults;
71      }
72  
73      /**
74       * {@inheritDoc}
75       * 
76       * @see org.fosstrak.epcis.queryclient.QueryCallbackInterface#callbackImplementationException(org.fosstrak.epcis.soapapi.ImplementationException)
77       */
78      public void callbackImplementationException(final ImplementationException e) {
79          // TODO implement
80          throw new UnsupportedOperationException();
81      }
82  
83      /**
84       * {@inheritDoc}
85       * 
86       * @see org.fosstrak.epcis.queryclient.QueryCallbackInterface#callbackQueryTooLargeException(org.fosstrak.epcis.soapapi.QueryTooLargeException)
87       */
88      public void callbackQueryTooLargeException(final QueryTooLargeException e) {
89          // TODO implement
90          throw new UnsupportedOperationException();
91      }
92  
93      /**
94       * {@inheritDoc}
95       * 
96       * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
97       *      javax.servlet.http.HttpServletResponse)
98       */
99      public void doPost(final HttpServletRequest req, final HttpServletResponse rsp) throws ServletException,
100             IOException {
101         rsp.setContentType("text/plain");
102         final PrintWriter out = rsp.getWriter();
103 
104         // get POST data
105         try {
106             callbackResults = (String) req.getParameterValues("callbackResults")[0];
107         } catch (NullPointerException e) {
108             throw new IOException("POST argument \"callbackResults=\" not found");
109         }
110 
111         out.println("Callback OK.");
112         out.flush();
113     }
114 
115     /**
116      * {@inheritDoc}
117      * 
118      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
119      *      javax.servlet.http.HttpServletResponse)
120      */
121     public void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws ServletException, IOException {
122         rsp.setContentType("text/xml");
123         final PrintWriter out = rsp.getWriter();
124         out.print(callbackResults);
125     }
126 }