001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.customer.bare;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.annotation.Resource;
024: import javax.jws.WebMethod;
025: import javax.jws.WebParam;
026: import javax.jws.WebService;
027: import javax.xml.ws.WebServiceContext;
028: import javax.xml.ws.WebServiceException;
029:
030: import org.apache.cxf.customer.Customer;
031: import org.apache.cxf.customer.CustomerNotFoundDetails;
032: import org.apache.cxf.customer.CustomerNotFoundFault;
033: import org.apache.cxf.customer.Customers;
034: import org.codehaus.jra.Delete;
035: import org.codehaus.jra.Get;
036: import org.codehaus.jra.HttpResource;
037: import org.codehaus.jra.Post;
038: import org.codehaus.jra.Put;
039:
040: // END SNIPPET: service
041: @WebService(targetNamespace="http://cxf.apache.org/jra")
042: public class CustomerService {
043: long currentId = 1;
044: Map<Long, Customer> customers = new HashMap<Long, Customer>();
045:
046: @Resource
047: private WebServiceContext context;
048:
049: public CustomerService() {
050: Customer customer = createCustomer();
051: customers.put(customer.getId(), customer);
052: }
053:
054: @Get
055: @HttpResource(location="/customers")
056: @WebMethod
057: public Customers getCustomers(@WebParam(name="GetCustomers")
058: GetCustomers req) {
059: Customers cbean = new Customers();
060: cbean.setCustomer(customers.values());
061:
062: if (context == null || context.getMessageContext() == null) {
063: throw new WebServiceException("WebServiceContext is null!");
064: }
065:
066: return cbean;
067: }
068:
069: @Get
070: @HttpResource(location="/customers/{id}")
071: @WebMethod
072: public Customer getCustomer(@WebParam(name="GetCustomer")
073: GetCustomer getCustomer) throws CustomerNotFoundFault {
074: Customer c = customers.get(getCustomer.getId());
075: if (c == null) {
076: CustomerNotFoundDetails details = new CustomerNotFoundDetails();
077: details.setId(getCustomer.getId());
078: throw new CustomerNotFoundFault(details);
079: }
080: return c;
081: }
082:
083: @Get
084: @HttpResource(location="/customers/details/{id}")
085: @WebMethod
086: public String getSomeDetails(@WebParam(name="GetCustomer")
087: GetCustomer getCustomer) throws CustomerNotFoundFault {
088: return "some details";
089: }
090:
091: @Put
092: @HttpResource(location="/customers/{id}")
093: @WebMethod
094: public void updateCustomer(@WebParam(name="customer")
095: Customer c) {
096: customers.put(c.getId(), c);
097: }
098:
099: @Post
100: @HttpResource(location="/customers")
101: @WebMethod
102: public void addCustomer(@WebParam(name="customer")
103: Customer c) {
104: long id = ++currentId;
105: c.setId(id);
106:
107: customers.put(id, c);
108: }
109:
110: @Delete
111: @HttpResource(location="/customers/{id}")
112: @WebMethod
113: public void deleteCustomer(long id) {
114: customers.remove(id);
115: }
116:
117: final Customer createCustomer() {
118: Customer c = new Customer();
119: c.setName("Dan Diephouse");
120: c.setId(123);
121: return c;
122: }
123: }
124: // END SNIPPET: service
|