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.systest.rest;
019:
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.annotation.Resource;
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.ws.BindingType;
031: import javax.xml.ws.Provider;
032: import javax.xml.ws.Service;
033: import javax.xml.ws.ServiceMode;
034: import javax.xml.ws.WebServiceContext;
035: import javax.xml.ws.WebServiceProvider;
036: import javax.xml.ws.handler.MessageContext;
037:
038: import org.w3c.dom.Document;
039:
040: import org.apache.cxf.helpers.CastUtils;
041:
042: @WebServiceProvider()
043: @ServiceMode(value=Service.Mode.PAYLOAD)
044: @BindingType("http://cxf.apache.org/bindings/xformat")
045: public class RestSourcePayloadProviderHttpBinding implements
046: Provider<DOMSource> {
047:
048: @Resource
049: protected WebServiceContext wsContext;
050:
051: public RestSourcePayloadProviderHttpBinding() {
052: }
053:
054: public DOMSource invoke(DOMSource request) {
055: MessageContext mc = wsContext.getMessageContext();
056: String path = (String) mc.get(MessageContext.PATH_INFO);
057: String query = (String) mc.get(MessageContext.QUERY_STRING);
058: String httpMethod = (String) mc
059: .get(MessageContext.HTTP_REQUEST_METHOD);
060:
061: Map<String, List<String>> responseHeader = CastUtils
062: .cast((Map) mc
063: .get(MessageContext.HTTP_RESPONSE_HEADERS));
064: if (responseHeader == null) {
065: responseHeader = new HashMap<String, List<String>>();
066: mc
067: .put(MessageContext.HTTP_RESPONSE_HEADERS,
068: responseHeader);
069: }
070:
071: List<String> values = new ArrayList<String>();
072: values.add("hello1");
073: values.add("hello2");
074: responseHeader.put("REST", values);
075: // System.out.println("--path--- " + path);
076: // System.out.println("--query--- " + query);
077: // System.out.println("--httpMethod--- " + httpMethod);
078:
079: if (httpMethod.equalsIgnoreCase("POST")) {
080: // TBD: parse query info from DOMSource
081: // System.out.println("--POST: getAllCustomers--- ");
082: return getCustomer(null);
083: } else if (httpMethod.equalsIgnoreCase("GET")) {
084: if ("/XMLService/RestProviderPort/Customer".equals(path)
085: && query == null) {
086: // System.out.println("--GET:getAllCustomers--- ");
087: return getAllCustomers();
088: } else if ("/XMLService/RestProviderPort/Customer"
089: .equals(path)
090: && query != null) {
091: // System.out.println("--GET:getCustomer--- ");
092: return getCustomer(query);
093: }
094: }
095:
096: return null;
097: }
098:
099: private DOMSource getAllCustomers() {
100: return createDOMSource("resources/CustomerAllResp.xml");
101: }
102:
103: private DOMSource getCustomer(String customerID) {
104: return createDOMSource("resources/CustomerJohnResp.xml");
105: }
106:
107: private DOMSource createDOMSource(String fileName) {
108: DocumentBuilderFactory factory;
109: DocumentBuilder builder;
110: Document document = null;
111: DOMSource response = null;
112:
113: try {
114: factory = DocumentBuilderFactory.newInstance();
115: factory.setNamespaceAware(true);
116: builder = factory.newDocumentBuilder();
117: InputStream greetMeResponse = getClass()
118: .getResourceAsStream(fileName);
119:
120: document = builder.parse(greetMeResponse);
121: response = new DOMSource(document);
122: } catch (Exception e) {
123: e.printStackTrace();
124: }
125: return response;
126: }
127: }
|