diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-12-21 11:58:17 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-12-21 11:58:17 +0000 |
commit | 23d5587f8a4a4dfa5fb2f5d9497d04733cdfe8df (patch) | |
tree | 2bfca23b82c9c9a3909cfe7019c1ffdedaa1e63b /sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services | |
parent | 1d9d90639d2b679dc82443dd1b2011effc34261b (diff) |
Copy 1.x trunk to branches for start of 1.6 release
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@892786 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
8 files changed, 786 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Catalog.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Catalog.java new file mode 100644 index 0000000000..9105f1047c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Catalog.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package services; + +public interface Catalog { + Item[] get(); +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/CatalogProxy.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/CatalogProxy.java new file mode 100644 index 0000000000..7851ac6713 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/CatalogProxy.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package services; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import services.json.rpc.JSONRpc; + +public class CatalogProxy implements Catalog { + // see http://developer.android.com/guide/developing/tools/emulator.html + private static final String jsonRPCServiceURI = "http://10.0.2.2:8080/Catalog"; + private static final String jsonRPCRequest = "{\"id\": 3, \"method\": \"Service.get\", \"params\": []}"; + + private List<Item> catalog = new ArrayList<Item>(); + + public CatalogProxy() { + initialize(); + } + + public void initialize() { + JSONObject json = null; + + try { + json = JSONRpc.invoke(jsonRPCServiceURI, jsonRPCRequest); + + if(json == null) { + return; + } + + JSONArray result = json.getJSONArray("result"); + for(int i = 0; i < result.length(); i++) { + Item item = new Item(); + item.setName(result.getJSONObject(i).getString("name")); + item.setPrice(result.getJSONObject(i).getString("price")); + + catalog.add(item); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Commons.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Commons.java new file mode 100644 index 0000000000..9da89c8fec --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Commons.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package services; + +/** + * + */ +public class Commons { + + public static final String TAG="Tuscany"; + public static final String DEL="HTTP DELETE "; + public static final String PST="HTTP POST "; + + +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Item.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Item.java new file mode 100644 index 0000000000..1881dd4d11 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/Item.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package services; + + +public class Item { + private String name; + private String price; + private String key; + + /** + * @return the key + */ + public String getKey() { + return key; + } + + /** + * @param key the key to set + */ + public void setKey(String key) { + this.key = key; + } + + + + + + + + + + public Item() { + } + + + + + + + + + + /** + * Parses a string entry to an Item object + * @param s + * @return Item + */ + public static Item parseItem(String s) + { + Item i=new Item(); + i.setName(s.split("-")[0].trim()); + i.setPrice(s.split("-")[1].trim()); + return i; + + } + + public Item(String name, String price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Item(String name, String price, String key) { + super(); + this.name = name; + this.price = price; + this.key = key; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public String toString() + { + return name + " - " + price; + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/ShoppingCartProxy.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/ShoppingCartProxy.java new file mode 100644 index 0000000000..c01edfbe2f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/ShoppingCartProxy.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package services; + +import org.json.JSONException; +import org.json.JSONObject; + +import services.atom.xml.AtomXML; +import services.json.rpc.JSONRpc; +import android.util.Log; + +public class ShoppingCartProxy { + private static final String jsonRPCTotalServiceURI = "http://10.0.2.2:8080/ShoppingCart/Total"; + private static final String jsonRPCTotalRequest = "{\"id\": 4, \"method\": \"Service.getTotal\", \"params\": []}"; + private static final String atomXMLCartServiceURI="http://10.0.2.2:8080/ShoppingCart/Cart"; + + + public Item[] getItems() { + return AtomXML.getItems(atomXMLCartServiceURI); + } + + public boolean addItem(Item item) { + String content="<entry xmlns=\"http://www.w3.org/2005/Atom\">" + + "<title>item</title>" + + "<content type=\"text/xml\">" + + "<Item xmlns=\"http://services/\">" + + "<name xmlns=\"\">" + item.getName()+ "</name>" + + "<price xmlns=\"\">" +item.getPrice()+"</price>" + + "</Item></content></entry>"; + + String key =AtomXML.postItem(atomXMLCartServiceURI, content); + if(key==null) { + return false; + } + item.setKey(key); + Log.i("TUSCANY shopping cart proxy", key); + return true; + } + + public boolean removeItem(Item item) { + Log.e("Sent key",item.getKey()); + String uri=atomXMLCartServiceURI+"/"+item.getKey(); + return AtomXML.performdelete(uri); + } + + public boolean clearCartContent() { + return AtomXML.performdelete(atomXMLCartServiceURI); + } + + public void checkOut() { + + } + + public String getTotal() { + String total = ""; + JSONObject json = null; + Log.e("TUSC", "getting total"); + + try { + json = JSONRpc.invoke(jsonRPCTotalServiceURI, jsonRPCTotalRequest); + Log.e("TUSC", "Request OK"); + + if (json != null) { + total = json.getString("result"); + Log.e("TUSC", "Total: " + total); + } + } catch (JSONException e) { + Log.e("TUSC", e.getMessage()); + } + return total; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/AtomXML.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/AtomXML.java new file mode 100644 index 0000000000..69f4409ed9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/AtomXML.java @@ -0,0 +1,257 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package services.atom.xml; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.apache.http.Header; +import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.ParseException; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +import services.Commons; +import services.Item; +import android.util.Log; + +/** + * + */ +public class AtomXML { + + public static String postItem(String ServiceURI, final String content) + { + DefaultHttpClient client=new DefaultHttpClient(); + HttpPost httpost = new HttpPost(ServiceURI); + + httpost.setEntity(new HttpEntity(){ + + String entry=content; + + class mHeader implements Header + { + public HeaderElement[] getElements() throws ParseException { + // TODO Auto-generated method stub + return null; + } + + public String getName() { + // TODO Auto-generated method stub + return "Content-type"; + } + + public String getValue() { + // TODO Auto-generated method stub + return "application/atom+xml;type=entry"; + } + } + + public void consumeContent() throws IOException { + // TODO Auto-generated method stub + + } + + public InputStream getContent() throws IOException, + IllegalStateException { + // TODO Auto-generated method stub + return new InputStream(){ + + public int read() throws IOException { + // TODO Auto-generated method stub + return this.available(); + } + + }; + } + + public Header getContentEncoding() { + // TODO Auto-generated method stub + return new mHeader(); + } + + public long getContentLength() { + // TODO Auto-generated method stub + return entry.length(); + } + + public Header getContentType() { + // TODO Auto-generated method stub + return new mHeader(); + } + + public boolean isChunked() { + // TODO Auto-generated method stub + return false; + } + + public boolean isRepeatable() { + // TODO Auto-generated method stub + return true; + } + + public boolean isStreaming() { + // TODO Auto-generated method stub + return false; + } + + public void writeTo(OutputStream arg0) throws IOException { + // TODO Auto-generated method stub + + arg0.write(entry.getBytes()); + arg0.flush(); + //Log.i("Tuscany", "Entry posted via atom/xml"); + } + + }); + + try { + HttpResponse response = client.execute(httpost); + InputStream is =response.getEntity().getContent(); + + //Human readable atom response from servlet + int read; + StringBuffer sb=new StringBuffer(); + while((read=is.read())>0) + { + sb.append((char)read); + } + Log.i("Tuscany", "Atom entry post status: "+response.getStatusLine().toString()); + //Log.i("Tuscany", "Response: "+sb.toString()); + //Try now to parse the consumed data + try { + SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParser sp; + sp = spf.newSAXParser(); + XMLReader xr = sp.getXMLReader(); + CartItemHandler cih=new CartItemHandler(); + xr.setContentHandler(cih); + + xr.parse(new InputSource(new ByteArrayInputStream(sb.toString().getBytes()))); + is.close(); + + return cih.getCurrentKey(); + + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (SAXException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getLocalizedMessage()); + } + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (IOException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } + + return null; + } + + public static boolean performdelete(String uri) + { + DefaultHttpClient client=new DefaultHttpClient(); + Log.i(Commons.TAG,Commons.DEL+uri); + HttpDelete del=new HttpDelete(uri); + + try { + client.execute(del); + return true; + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (IOException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } + + return false; + + + } + + public static Item[] getItems(String uri) + { + DefaultHttpClient client=new DefaultHttpClient(); + HttpGet hg=new HttpGet(uri); + HttpResponse hr; + HttpEntity he; + try { + hr=client.execute(hg); + InputStream is =hr.getEntity().getContent(); + + //Human readable atom response from servlet + int read; + StringBuffer sb=new StringBuffer(); + while((read=is.read())>0) + { + sb.append((char)read); + } + Log.i("Tuscany", "Atom get content: "+sb.toString()); + + SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParser sp; + sp = spf.newSAXParser(); + XMLReader xr = sp.getXMLReader(); + CartItemHandler cih=new CartItemHandler(); + xr.setContentHandler(cih); + + xr.parse(new InputSource(new ByteArrayInputStream(sb.toString().getBytes()))); + is.close(); + Log.e(Commons.TAG,String.valueOf(cih.getItemsCollection().length)); + return cih.getItemsCollection(); + + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (IOException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } catch (SAXException e) { + // TODO Auto-generated catch block + Log.e(Commons.TAG,e.getMessage()); + } + return null; + } + + + + +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/CartItemHandler.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/CartItemHandler.java new file mode 100644 index 0000000000..ed29839795 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/atom/xml/CartItemHandler.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package services.atom.xml; + +import java.util.ArrayList; +import java.util.List; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import android.util.Log; + +import services.Item; + + +/** + * @author Lookouster + * + */ +public class CartItemHandler extends DefaultHandler { + + private boolean in_entry = false; + private boolean in_id = false; + private boolean in_title = false; + private boolean in_content=false; + private boolean in_item=false; + private boolean in_link=false; + private boolean in_name=false; + private boolean in_price=false; + private String currentKey,currentName,currentPrice; + private List<Item> items=new ArrayList<Item>(); + + /** + * @return the currentName + */ + public String getCurrentName() { + return currentName; + } + + + /** + * @return the currentPrice + */ + public String getCurrentPrice() { + return currentPrice; + } + + + public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { + if (localName.equalsIgnoreCase("entry")) { + this.in_entry = true; + } else if (localName.equalsIgnoreCase("id")) { + this.in_id = true; + } else if (localName.equalsIgnoreCase("title")) { + this.in_title = true; + } else if (localName.equalsIgnoreCase("content")) { + this.in_content = true; + } else if (localName.equalsIgnoreCase("item")) { + this.in_item = true; + } else if (localName.equals("link")) { + this.in_link = true; + } else if (localName.equalsIgnoreCase("name")) { + this.in_name = true; + } else if (localName.equalsIgnoreCase("price")) { + this.in_price = true; + } + } + + + public void endElement(String namespaceURI, String localName, String qName) throws SAXException { + if (localName.equalsIgnoreCase("id")) + this.in_id = false; + if (localName.equalsIgnoreCase("entry")) { + this.in_entry = false; + } else if (localName.equalsIgnoreCase("id")) { + this.in_id = false; + } else if (localName.equalsIgnoreCase("title")) { + this.in_title = false; + } else if (localName.equalsIgnoreCase("content")) { + this.in_content = false; + } else if (localName.equalsIgnoreCase("item")) { + this.in_item = false; + items.add(new Item(currentName, currentPrice, currentKey)); + } else if (localName.equalsIgnoreCase("link")) { + this.in_link = false; + } else if (localName.equalsIgnoreCase("name")) { + this.in_name = false; + } else if (localName.equalsIgnoreCase("price")) { + this.in_price = false; + } + } + + + public void characters(char ch[], int start, int length) { + if (this.in_id) { + if (this.in_entry) { + currentKey = new String(ch, start, length); + Log.e("kjhkh", currentKey); + } + + } + if (this.in_name) { + currentName = new String(ch, start, length); + } + if (this.in_price) { + currentPrice = new String(ch, start, length); + } + + } + + /** + * @return the key + */ + public String getCurrentKey() { + return currentKey; + } + + public Item[] getItemsCollection() + { + return items.toArray(new Item[items.size()]); + } + + + +} diff --git a/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/json/rpc/JSONRpc.java b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/json/rpc/JSONRpc.java new file mode 100644 index 0000000000..b803ac6470 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/samples/store-android/src/services/json/rpc/JSONRpc.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package services.json.rpc; + +import java.io.IOException; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; +import org.json.JSONException; +import org.json.JSONObject; + +public class JSONRpc { + + protected JSONRpc() { + + } + + public static JSONObject invoke(String serviceURI, String rpcRequest) throws JSONException{ + HttpClient httpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost(serviceURI); + + JSONObject result = null; + try { + httpPost.setHeader("Content-Type", "text/xml"); + httpPost.setEntity(new StringEntity(rpcRequest)); + + HttpResponse httpResponse = httpClient.execute(httpPost); + if (httpResponse.getStatusLine().getStatusCode() == 200) { + String jsonResult = EntityUtils.toString(httpResponse.getEntity()); + result = new JSONObject(jsonResult); + } else { + String errorMessage = httpResponse.getStatusLine() + .getReasonPhrase(); + System.out.println(errorMessage); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return result; + } +} |