Store scenario using JAX-RS from Apache Wink

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@903450 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
lresende 2010-01-26 22:10:11 +00:00
parent a03fb0ee87
commit e320d6586e
12 changed files with 539 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sample-store-jaxrs-webapp</name>
<comment>The Apache Software Foundation provides support for the Apache community of open-source software projects.
The Apache projects are characterized by a collaborative, consensus based development process, an open and
pragmatic software license, and a desire to create high quality software that leads the way in its field.
We consider ourselves not simply a group of projects sharing a server, but rather a community of developers
and users.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
</natures>
</projectDescription>

View file

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca</artifactId>
<version>2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>sample-store-jaxrs-webapp</artifactId>
<packaging>war</packaging>
<name>Apache Tuscany SCA Sample Getting Started Online Store as WebApp</name>
<repositories>
<repository>
<id>Codehaus</id>
<name>Codehaus Repository</name>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>http://repository.codehaus.org/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-common</artifactId>
<version>1.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-server</artifactId>
<version>1.0-incubating</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>store-jaxrs</finalName>
</build>
</project>

View file

@ -0,0 +1,40 @@
/*
* 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.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Produces(MediaType.APPLICATION_JSON)
public interface Catalog {
@GET
@Produces(MediaType.APPLICATION_JSON)
List<Item> get();
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
Item get(@PathParam("id") String id);
}

View file

@ -0,0 +1,27 @@
/*
* 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 CurrencyConverter {
public double getConversion(String fromCurrenycCode, String toCurrencyCode, double amount);
public String getCurrencySymbol(String currencyCode);
}

View file

@ -0,0 +1,39 @@
/*
* 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 CurrencyConverterImpl implements CurrencyConverter {
public double getConversion(String fromCurrencyCode, String toCurrencyCode, double amount) {
if (toCurrencyCode.equals("USD"))
return amount;
else if (toCurrencyCode.equals("EUR"))
return ((double)Math.round(amount * 0.7256 * 100)) /100;
return 0;
}
public String getCurrencySymbol(String currencyCode) {
if (currencyCode.equals("USD"))
return "$";
else if (currencyCode.equals("EUR"))
return "E"; //"";
return "?";
}
}

View file

@ -0,0 +1,53 @@
/*
* 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 javax.ws.rs.Path;
@Path("catalog")
public class FruitsCatalogImpl implements Catalog {
public String currencyCode = "USD";
public CurrencyConverter currencyConverter = new CurrencyConverterImpl();
private List<Item> catalog = new ArrayList<Item>();
public FruitsCatalogImpl() {
init();
}
public void init() {
String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
catalog.add(new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99)));
catalog.add(new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55)));
catalog.add(new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55)));
}
public List<Item> get() {
return catalog;
}
public Item get(String id) {
throw new UnsupportedOperationException("Not yet implemented.");
}
}

View file

@ -0,0 +1,50 @@
/*
* 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;
public Item() {
}
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 String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}

View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View file

@ -0,0 +1,2 @@
services.FruitsCatalogImpl
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>store-jaxrs</display-name>
<!--
Wink SDK servlet configuration. This servlet handles HTTP requests
of SDK web service on application server.
-->
<servlet>
<servlet-name>restSdkService</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>applicationConfigLocation</param-name>
<param-value>/WEB-INF/application</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restSdkService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>store.html</welcome-file>
</welcome-file-list>
</web-app>

View file

@ -0,0 +1,105 @@
/*
* 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.
*/
function HTTPClient(uri) {
this.msxmlNames = [ "MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP" ];
this.uri=uri;
this.get = function(id, responseFunction) {
var xhr = this.createXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var strDocument = xhr.responseText;
if (responseFunction != null) responseFunction(strDocument);
} else {
alert("get - Error getting data from the server");
}
}
}
xhr.open("GET", uri + '/' + id, true);
xhr.send(null);
}
this.post = function (entry, responseFunction) {
var xhr = this.createXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 201) {
var strDocument = xhr.responseText;
if (responseFunction != null) responseFunction(strDocument);
} else {
alert("post - Error getting data from the server");
}
}
}
xhr.open("POST", uri, true);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.send(entry);
}
this.put = function (id, entry, responseFunction) {
var xhr = this.createXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var strDocument = xhr.responseText;
if (responseFunction != null) responseFunction(strDocument);
} else {
alert("put - Error getting data from the server");
}
}
}
xhr.open("PUT", uri + '/' + id, true);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.send(entry);
}
this.del = function (id, responseFunction) {
var xhr = this.createXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (responseFunction != null) responseFunction();
} else {
alert("delete - Error getting data from the server");
}
}
}
xhr.open("DELETE", uri + '/' + id, true);
xhr.send(null);
}
this.createXMLHttpRequest = function () {
/* Mozilla XMLHttpRequest */
try {return new XMLHttpRequest();} catch(e) {}
/* Microsoft MSXML ActiveX */
for (var i=0;i < this.msxmlNames.length; i++) {
try {return new ActiveXObject(this.msxmlNames[i]);} catch (e) {}
}
alert("XML http request not supported");
return null;
}
}

View file

@ -0,0 +1,86 @@
<!--
* 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.
-->
<html>
<head>
<title>Store</title>
<script type="text/javascript" src="binding-http.js"></script>
<script language="JavaScript">
var catalog = new HTTPClient('http://localhost:8080/store-jaxrs/services/catalog');
var catalogItems;
function catalog_getResponse(items,exception) {
if(exception){
alert(exception.message);
return;
}
var catalog = "";
items = eval(items);
for (var i=0; i<items.length; i++) {
var item = items[i].name + ' - ' + items[i].price;
catalog += '<input name="items" type="checkbox" value="' +
item + '">' + item + ' <br>';
}
document.getElementById('catalog').innerHTML=catalog;
catalogItems = items;
}
function init() {
try {
catalog.get('',catalog_getResponse);
}
catch(e){
alert(e);
}
}
</script>
</head>
<body onload="init()">
<h1>Store</h1>
<div id="store">
<h2>Catalog</h2>
<form name="catalogForm">
<div id="catalog" ></div>
<br>
<input type="button" onClick="addToCart()" value="Add to Cart">
</form>
<br>
<h2>Your Shopping Cart</h2>
<form name="shoppingCartForm">
<div id="shoppingCart"></div>
<br>
<div id="total"></div>
<br>
<input type="button" onClick="checkoutCart()" value="Checkout">
<input type="button" onClick="deleteCart()" value="Empty">
<a href="../ShoppingCart/Cart/">(feed)</a>
</form>
</div>
</body>
</html>