
Before you begin, you need to install the software mentioned in the class website on your computer. You also need download and unzip the hands-on lab zip file below (if you have not done so yet).























| <?xml version="1.0"
encoding="UTF-8"?> <!-- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language governing permissions and limitations under the License. When distributing the software, include this License Header Notice in each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this particular file as subject to the "Classpath" exception as provided by Sun in the GPL Version 2 section of the License file that accompanied this code. If applicable, add the following below the License Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" Contributor(s): The Original Software is NetBeans. The Initial Developer of the Original Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, indicate your decision by adding "[Contributor] elects to include this software in this distribution under the [CDDL or GPL Version 2] license." If you do not indicate a single choice of license, a recipient has the option to distribute your version of this file under either the CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the option applies only if the new code is made subject to such option by the copyright holder. --> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ServletAdaptor</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |

| /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common * Development and Distribution License("CDDL") (collectively, the * "License"). You may not use this file except in compliance with the * License. You can obtain a copy of the License at * http://www.netbeans.org/cddl-gplv2.html * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the * specific language governing permissions and limitations under the * License. When distributing the software, include this License Header * Notice in each file and include the License file at * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the * License Header, with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL * or only the GPL Version 2, indicate your decision by adding * "[Contributor] elects to include this software in this distribution * under the [CDDL or GPL Version 2] license." If you do not indicate a * single choice of license, a recipient has the option to distribute * your version of this file under either the CDDL, the GPL Version 2 or * to extend the choice of license to its licensees as provided above. * However, if you add GPL Version 2 code and therefore, elected the GPL * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. */ package helloworld; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; /** * REST Web Service * * @author __USER__ */ @Path("/helloWorld") public class HelloWorldResource { @Context private UriInfo context; /** Creates a new instance of HelloWorldResource */ public HelloWorldResource() { } /** * Retrieves representation of an instance of helloworld.HelloWorldResource * @return an instance of java.lang.String */ @GET @Produces("text/html") public String getXml() { return "<html><body><h1>Hello World!</body></h1></html>"; } /** * PUT method for updating or creating an instance of HelloWorldResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("application/xml") public void putXml(String content) { } } |
















| package helloworld; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; /** * REST Web Service * * @author __USER__ */ @Path("/myOwnWorld") public class MyOwnResource { @Context private UriInfo context; /** Creates a new instance of HelloWorldResource */ public MyOwnResource() { } /** * Retrieves representation of an instance of helloworld.HelloWorldResource * @return an instance of java.lang.String */ @GET @Produces("text/html") public String getXml() { return "<html><body><h1>My Own World!</body></h1></html>"; } /** * PUT method for updating or creating an instance of HelloWorldResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("application/xml") public void putXml(String content) { } } |







| C:\download\curl>curl
http://localhost:8080/HelloWorld/resources/helloWorld <html><body><h1>Hello World!</body></h1></html> |









| package
hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; /** * REST Web Service * * @author sang */ @Path("helloworld") public class HelloworldResource { @Context private UriInfo context; /** Creates a new instance of HelloworldResource */ public HelloworldResource() { } /** * Retrieves representation of an instance of hello.world.HelloworldResource * @return an instance of java.lang.String */ @GET @Produces("text/plain") public String getText() { //TODO return proper representation object throw new UnsupportedOperationException(); } /** * PUT method for updating or creating an instance of HelloworldResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("text/plain") public void putText(String content) { } } |
| /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; /** * REST Web Service * * @author sang */ @Path("helloworld") public class HelloworldResource { @Context private UriInfo context; /** Creates a new instance of HelloworldResource */ public HelloworldResource() { } /** * Retrieves representation of an instance of hello.world.HelloworldResource * @return an instance of java.lang.String */ @GET @Produces("text/plain") public String getText() { //TODO return proper representation object //throw new UnsupportedOperationException(); return ("Hello World! Here is " + context.getAbsolutePath()); } /** * PUT method for updating or creating an instance of HelloworldResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("text/plain") public void putText(String content) { } } |

Learning points: The HelloWorldResource
class is a very simple Web
resource. The URI path of the resource is "/helloworld", it
supports the HTTP GET method (line 35) with getText() method, which produces a
textual
content of the MIME media type "text/plain".
Notice the use of Java annotations to declare the URI path(@Path),
the HTTP
method (@GET) and the MIME type
(@Produces). Also note the @Context annotation,
which acts as a marker to say "Please inject an instance of the Java
type, in this case UriInfo,
after the class has been constructed."
4. Verify that the project is already configured with JAX-RS
libraries.








| package hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import javax.ws.rs.POST; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; @Path("/Items") public class ItemsResource { @Context private UriInfo context; /** Creates a new instance of ItemsResource */ public ItemsResource() { } /** * Retrieves representation of an instance of hello.world.ItemsResource * @return an instance of java.lang.String */ @GET @Produces("application/xml") public String getXml() { //TODO return proper representation object throw new UnsupportedOperationException(); } /** * POST method for creating an instance of ItemResource * @param content representation for the new resource * @return an HTTP response with content of the created resource */ @POST @Consumes("application/xml") @Produces("application/xml") public Response postXml(String content) { //TODO return Response.created(context.getAbsolutePath()).build(); } /** * Sub-resource locator method for {id} */ @Path("{id}") public ItemResource getItemResource() { return new ItemResource(); } } |
| package hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.ws.rs.DELETE; public class ItemResource { @Context private UriInfo context; /** Creates a new instance of ItemResource */ public ItemResource() { } /** * Retrieves representation of an instance of hello.world.ItemResource * @param id resource URI parameter * @return an instance of java.lang.String */ @GET @Produces("application/xml") public String getXml(@PathParam("id") String id) { //TODO return proper representation object throw new UnsupportedOperationException(); } /** * PUT method for updating or creating an instance of ItemResource * @param id resource URI parameter * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("application/xml") public void putXml(@PathParam("id") String id, String content) { } /** * DELETE method for resource ItemResource * @param id resource URI parameter */ @DELETE public void delete(@PathParam("id") String id) { } } |





| package hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; /** * REST Web Service * * @author sang */ @Path("/Items") public class ItemsResource { @Context private UriInfo context; /** Creates a new instance of ItemsResource */ public ItemsResource() { } /** * Retrieves representation of an instance of hello.world.ItemsResource * @return an instance of java.lang.String */ @GET @Produces("application/xml") public String getXml() { //TODO return proper representation object throw new UnsupportedOperationException(); } /** * Sub-resource locator method for {name} */ @Path("{name}") public ItemResource getItemResource() { return new ItemResource(); } } |
| package hello.world; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.ws.rs.DELETE; /** * REST Web Service * * @author sang */ public class ItemResource { @Context private UriInfo context; /** Creates a new instance of ItemResource */ public ItemResource() { } /** * Retrieves representation of an instance of hello.world.ItemResource * @param name resource URI parameter * @return an instance of java.lang.String */ @GET @Produces("application/xml") public String getXml(@PathParam("name") String name) { //TODO return proper representation object throw new UnsupportedOperationException(); } /** * PUT method for updating or creating an instance of ItemResource * @param name resource URI parameter * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("application/xml") public void putXml(@PathParam("name") String name, String content) { } /** * DELETE method for resource ItemResource * @param name resource URI parameter */ @DELETE public void delete(@PathParam("name") String name) { } } |





























| <?xml version="1.0"
encoding="UTF-8"?> <customer uri="http://localhost:8080/CustomerDB/resources/customers/2/"> <addressline1>9754 Main Street</addressline1> <addressline2>P.O. Box 567</addressline2> <city>Miami</city> <creditLimit>50000</creditLimit> <customerId>2</customerId> <discountCode uri="http://localhost:8080/CustomerDB/resources/customers/2/discountCode/"/> <email>www.tsoftt.com</email> <fax>305-456-8889</fax> <name>Sang Shin</name> <phone>305-456-8888</phone> <state>FL</state> <zip>33055</zip> </customer> |






| /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package customerdb.service; import customerdb.Customer; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.DELETE; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; import javax.ws.rs.DefaultValue; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import com.sun.jersey.api.core.ResourceContext; import javax.ws.rs.WebApplicationException; import javax.persistence.NoResultException; import javax.persistence.EntityManager; import customerdb.DiscountCode; import customerdb.converter.CustomerConverter; /** * * @author sang */ public class CustomerResource { @Context protected UriInfo uriInfo; @Context protected ResourceContext resourceContext; protected Integer id; /** Creates a new instance of CustomerResource */ public CustomerResource() { } public void setId(Integer id) { this.id = id; } @GET @Produces("text/html") public String getGoogleMap() { // Drag and drop the getGoogleMap operation here return null; } //Gets invoked by going to browser and accessing any customer (ie /customers/1/googleMap) /** * Returns sub-resource. */ @Path("googleMap/") @GET @Produces("text/html") public String getGoogleMapResource() { return getGoogleMap(); } /** * Get method for retrieving an instance of Customer identified by id in XML format. * * @param id identifier for the entity * @return an instance of CustomerConverter */ @GET @Produces({"application/xml", "application/json"}) public CustomerConverter get(@QueryParam("expandLevel") @DefaultValue("1") int expandLevel) { PersistenceService persistenceSvc = PersistenceService.getInstance(); try { persistenceSvc.beginTx(); return new CustomerConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel); } finally { PersistenceService.getInstance().close(); } } ... |




| package customerdb.service; import customerdb.Customer; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.DELETE; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; import javax.ws.rs.DefaultValue; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import com.sun.jersey.api.core.ResourceContext; import javax.ws.rs.WebApplicationException; import javax.persistence.NoResultException; import javax.persistence.EntityManager; import customerdb.DiscountCode; import customerdb.converter.CustomerConverter; import org.netbeans.saas.google.GoogleMapService; import org.netbeans.saas.RestResponse; /** * * @author sang */ public class CustomerResource { @Context protected UriInfo uriInfo; @Context protected ResourceContext resourceContext; protected Integer id; /** Creates a new instance of CustomerResource */ public CustomerResource() { } public void setId(Integer id) { this.id = id; } @GET @Produces("text/html") public String getGoogleMap() { // Drag and drop the getGoogleMap operation here try { //String address = "16 Network Circle, Menlo Park"; //java.lang.Integer zoom = 15; //String iframe = "false"; //RestResponse result = GoogleMapService.getGoogleMap(address, zoom, iframe); Customer c = getEntity(); String address = c.getAddressline1() + " " + c.getAddressline2() + " " + c.getCity() + " " + c.getState() + " " + c.getZip(); java.lang.Integer zoom = 15; String iframe = "false"; RestResponse result = GoogleMapService.getGoogleMap(address, zoom, iframe); return result.getDataAsString(); //TODO - Uncomment the print Statement below to print result. //System.out.println("The SaasService returned: "+result.getDataAsString()); } catch (Exception ex) { ex.printStackTrace(); } return null; } //Gets invoked by going to browser and accessing any customer (ie /customers/1/googleMap) /** * Returns sub-resource. */ @Path("googleMap/") @GET @Produces("text/html") public String getGoogleMapResource() { return getGoogleMap(); } ... |








