Author: donsez
Date: Mon Oct 22 07:49:05 2007
New Revision: 587116
URL: http://svn.apache.org/viewvc?rev=587116&view=rev
Log:
add a RESTFulClient class
process XML responses with DOM
Added:
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
(with props)
Modified:
felix/sandbox/donsez/bunny/pom.xml
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
Modified: felix/sandbox/donsez/bunny/pom.xml
URL:
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/pom.xml?rev=587116&r1=587115&r2=587116&view=diff
==============================================================================
--- felix/sandbox/donsez/bunny/pom.xml (original)
+++ felix/sandbox/donsez/bunny/pom.xml Mon Oct 22 07:49:05 2007
@@ -64,8 +64,8 @@
<!-- docs in
http://cwiki.apache.org/FELIX/bundle-plugin-for-maven-bnd.html and
http://cwiki.apache.org/FELIX/osgi-plugin-for-maven-2.html -->
-
<Private-Package>${pom.artifactId}.impl,${pom.artifactId}.activator</Private-Package>
-
<Import-Package>org.osgi.framework,org.osgi.service.cm,org.apache.felix.shell</Import-Package>
+
<Private-Package>${pom.artifactId}.impl,${pom.artifactId}.activator,org.apache.felix.sandbox.rest</Private-Package>
+
<Import-Package>org.osgi.framework,org.osgi.service.cm,org.apache.felix.shell,javax.xml.parsers,
org.w3c.dom, org.xml.sax, javax.xml.transform, javax.xml.transform.dom,
javax.xml.transform.stream</Import-Package>
<Export-Package>${pom.artifactId}.model</Export-Package>
<Bundle-Activator>${pom.artifactId}.activator.Activator</Bundle-Activator>
Modified:
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java?rev=587116&r1=587115&r2=587116&view=diff
==============================================================================
---
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
(original)
+++
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
Mon Oct 22 07:49:05 2007
@@ -18,21 +18,21 @@
*/
package org.apache.felix.sandbox.bunny.impl;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.net.URLEncoder;
import java.text.MessageFormat;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.felix.sandbox.bunny.model.BunnyServiceModel;
+import org.apache.felix.sandbox.rest.RESTFulClient;
import org.osgi.framework.BundleContext;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* this class provides implementation of a bunny service.
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a>
*/
@@ -50,16 +50,15 @@
private String voice = "julie22k";
- private boolean trace=true;
-
- private BundleContext bundleContext=null;
+ private boolean trace = true;
+ private BundleContext bundleContext = null;
public BunnyServiceModelImpl(BundleContext bundleContext) {
- this.bundleContext=bundleContext;
+ this.bundleContext = bundleContext;
loadConfiguration();
}
-
+
public BunnyServiceModelImpl() {
loadConfiguration();
}
@@ -80,147 +79,109 @@
// TODO
}
-
- public void play(
- long newLeftEarPosition,
- long newRightEarPosition,
- String textToSpeech,
- String chor,
- String voice
- ) {
-
- Object[] arguments = new Object[5];
+ public void play(long newLeftEarPosition, long newRightEarPosition,
+ String textToSpeech, String chor, String voice) {
String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";
- urlString +=
"key={0}&sn={1}&token={2}&posleft={3}&posright={4}";
- // urlString+="&ears=ok"; // send the ears position
- arguments[0] = key;
- arguments[1] = serialNumber;
- arguments[2] = token;
- arguments[3] = new Long(newLeftEarPosition);
- arguments[4] = new Long(newRightEarPosition);
+ Map parameters=new HashMap();
+ parameters.put("key",key);
+ parameters.put("sn",serialNumber);
+ parameters.put("token",token);
- MessageFormat messageFormat = new MessageFormat(urlString);
- urlString = messageFormat.format(arguments);
+ parameters.put("posleft",new Long(newLeftEarPosition));
+ parameters.put("posright",new Long(newRightEarPosition));
+
- if (textToSpeech!=null && !textToSpeech.equals("")) {
- urlString += "&tts="+URLEncoder.encode(textToSpeech);
+ if (textToSpeech != null && !textToSpeech.equals("")) {
+ parameters.put("tts",textToSpeech);
}
-
- if (voice!=null && !voice.equals("")) {
- urlString += "&voice=" + voice;
- } else if (this.voice!=null) {
- urlString += "&voice=" + this.voice;
+
+ if (voice != null && !voice.equals("")) {
+ parameters.put("voice",voice);
+ } else if (this.voice != null) {
+ parameters.put("voice",this.voice);
}
- if (chor!=null && !chor.equals("")) {
- urlString += "&chor="+chor;
+ if (chor != null && !chor.equals("")) {
+ parameters.put("chor",chor);
}
- sendUrl(urlString);
-
- leftEarPosition = newLeftEarPosition;
- rightEarPosition = newRightEarPosition;
- // update();
- }
+ Document document=RESTFulClient.invoke(urlString,parameters);
+ if(document!=null) {
+ NodeList nodeList;
+ nodeList=document.getElementsByTagName("leftposition");
+ if(nodeList.getLength()==1){
+ Node node=nodeList.item(0);
+
leftEarPosition=Long.parseLong(node.getTextContent());
+ }
+ nodeList=document.getElementsByTagName("rightposition");
+ if(nodeList.getLength()==1){
+ Node node=nodeList.item(0);
+
rightEarPosition=Long.parseLong(node.getTextContent());
+ }
+ // update();
+ }
+ }
- public void sendRawURL(String encodedExtraURLParameters){
+ public void sendRawURL(String encodedExtraURLParameters) {
Object[] arguments = new Object[3];
String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";
- urlString += "key={0}&sn={1}&token={2}&";
-
- arguments[0] = key;
- arguments[1] = serialNumber;
- arguments[2] = token;
+ urlString += encodedExtraURLParameters;
- MessageFormat messageFormat = new MessageFormat(urlString);
- urlString = messageFormat.format(arguments);
+ Map parameters=new HashMap();
+ parameters.put("key",key);
+ parameters.put("sn",serialNumber);
+ parameters.put("token",token);
- urlString += encodedExtraURLParameters;
-
- sendUrl(urlString);
-
+ RESTFulClient.invokeAndPrint(urlString,parameters);
}
-
-
+
public void getEarPositions() {
- Object[] arguments = new Object[3];
String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";
- urlString += "key={0}&sn={1}&token={2}&ears=ok";
- // urlString+="&ears=ok"; // send the ears position
- arguments[0] = key;
- arguments[1] = serialNumber;
- arguments[2] = token;
+ Map parameters=new HashMap();
+ parameters.put("key",key);
+ parameters.put("sn",serialNumber);
+ parameters.put("token",token);
- MessageFormat messageFormat = new MessageFormat(urlString);
- urlString = messageFormat.format(arguments);
-
- sendUrl(urlString);
- }
-
-
- void sendUrl(String urlString){
- if(trace) System.out.println("Bunny Request:" + urlString);
-
- URL url;
- try {
- url = new URL(urlString);
- } catch (MalformedURLException e) {
- if(trace) e.printStackTrace();
- return;
- }
+
+ parameters.put("ears","ok"); // send the ears position
- try {
- HttpURLConnection httpURLConnection =
(HttpURLConnection) url
- .openConnection();
- httpURLConnection.connect();
- InputStream in = httpURLConnection.getInputStream();
- int length = httpURLConnection.getContentLength();
- if(length==-1) {
- if(trace) System.out.print("Bunny Reply
(length=unknown):");
- BufferedReader br = new BufferedReader(new
InputStreamReader(in));
- String line;
- while((line=br.readLine())!=null){
- if(trace) System.out.println(line);
- }
- br.close();
-
- } else {
- if(trace) System.out.print("Bunny Reply
(length="+length+"):");
-
- for (; length != 0; --length) {
- if(trace) System.out.print((char)
in.read());
- }
- if(trace) System.out.println();
- in.close();
+ Document document=RESTFulClient.invoke(urlString,parameters);
+ if(document!=null) {
+ NodeList nodeList;
+ nodeList=document.getElementsByTagName("leftposition");
+ if(nodeList.getLength()==1){
+ Node node=nodeList.item(0);
+
leftEarPosition=Long.parseLong(node.getTextContent());
}
-
- } catch (IOException e) {
- if(trace) e.printStackTrace();
+ nodeList=document.getElementsByTagName("rightposition");
+ if(nodeList.getLength()==1){
+ Node node=nodeList.item(0);
+
rightEarPosition=Long.parseLong(node.getTextContent());
+ }
+ // update();
}
+
+
//System.out.println("left:"+leftEarPosition+",right:"+rightEarPosition);
}
-
- public void configure(
- String newSerialNumber,
- String newToken,
+ public void configure(String newSerialNumber, String newToken,
String newVoice) {
- if(newSerialNumber!=null)
+ if (newSerialNumber != null && !newSerialNumber.equals(""))
serialNumber = newSerialNumber;
- if(newToken!=null)
+ if (newToken != null && !newToken.equals(""))
token = newToken;
- if (newVoice!=null && !voice.equals(""))
+ if (newVoice != null && !voice.equals(""))
voice = newVoice;
saveConfiguration();
}
-
public java.lang.Long getLeftEarPositionStateVariableValue() {
return new Long(leftEarPosition);
}
@@ -230,6 +191,7 @@
}
public void setTrace(boolean trace) {
- this.trace=trace;
+ this.trace = trace;
+ RESTFulClient.setTrace(trace);
}
}
Added:
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java?rev=587116&view=auto
==============================================================================
---
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
(added)
+++
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
Mon Oct 22 07:49:05 2007
@@ -0,0 +1,153 @@
+package org.apache.felix.sandbox.rest;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+public class RESTFulClient {
+
+ private static boolean trace=true;
+
+ /**
+ * @return the trace
+ */
+ public static synchronized final boolean isTrace() {
+ return trace;
+ }
+
+ /**
+ * @param trace the trace to set
+ */
+ public static synchronized final void setTrace(boolean trace) {
+ RESTFulClient.trace = trace;
+ }
+
+ public static void invokeAndPrint(String urlString, Map parameters) {
+ if(parameters!=null){
+ for (Iterator iterator =
parameters.keySet().iterator(); iterator.hasNext();) {
+ Object key = iterator.next();
+ Object value=parameters.get(key);
+ urlString += '&' +
URLEncoder.encode(key.toString()) + '=' + URLEncoder.encode(value.toString());
+ }
+ }
+ if (trace)
+ System.out.println("Bunny Request:" + urlString);
+
+ URL url;
+ try {
+ url = new URL(urlString);
+ } catch (MalformedURLException e) {
+ if (trace)
+ e.printStackTrace();
+ return;
+ }
+
+ try {
+ HttpURLConnection httpURLConnection =
(HttpURLConnection) url
+ .openConnection();
+ httpURLConnection.connect();
+ InputStream in = httpURLConnection.getInputStream();
+ int length = httpURLConnection.getContentLength();
+ if (length == -1) {
+ if (trace)
+ System.out.print("Response
(length=unknown):");
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader(in));
+ String line;
+ while ((line = br.readLine()) != null) {
+ if (trace)
+ System.out.println(line);
+ }
+ br.close();
+
+ } else {
+ if (trace)
+ System.out.print("Response (length=" +
length + "):");
+
+ for (; length != 0; --length) {
+ if (trace)
+ System.out.print((char)
in.read());
+ }
+ if (trace)
+ System.out.println();
+ in.close();
+ }
+
+ } catch (IOException e) {
+ if (trace)
+ e.printStackTrace();
+ }
+ }
+
+ public static Document invoke(String urlString, Map parameters) {
+ if(parameters!=null){
+ for (Iterator iterator =
parameters.keySet().iterator(); iterator.hasNext();) {
+ Object key = iterator.next();
+ Object value=parameters.get(key);
+ urlString += '&' +
URLEncoder.encode(key.toString()) + '=' + URLEncoder.encode(value.toString());
+ }
+ }
+
+ if (trace)
+ System.out.println("Request:" + urlString);
+
+ DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
+ DocumentBuilder db=null;
+ try {
+ db = dbf.newDocumentBuilder();
+ // Read the entire document into memory
+ Document document = db.parse(urlString);
+ if (trace)
+ System.out.println("Bunny Response:" +
toText(document));
+ return document;
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace(System.err);
+ } catch (SAXException e) {
+ System.err.println(e);
+ } catch (IOException e) {
+ System.err.println(e);
+ } catch (TransformerException e) {
+ System.err.println(e);
+ }
+ return null;
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ invoke(args[0],null);
+ }
+
+ private static String toText(Document document) throws
TransformerException{
+ DOMSource domSource = new DOMSource(document);
+ StringWriter writer = new StringWriter();
+ StreamResult result = new StreamResult(writer);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer = tf.newTransformer();
+ transformer.transform(domSource, result);
+ return writer.toString();
+ }
+
+}
Propchange:
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
------------------------------------------------------------------------------
svn:eol-style = native