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.Provider;
031: import javax.xml.ws.Service;
032: import javax.xml.ws.ServiceMode;
033: import javax.xml.ws.WebServiceContext;
034: import javax.xml.ws.WebServiceProvider;
035: import javax.xml.ws.handler.MessageContext;
036:
037: import org.w3c.dom.Document;
038:
039: import org.apache.cxf.helpers.CastUtils;
040:
041: @WebServiceProvider()
042: @ServiceMode(value=Service.Mode.PAYLOAD)
043: @javax.xml.ws.BindingType(value="http://cxf.apache.org/bindings/xformat")
044: public class RestSourcePayloadProvider implements Provider<DOMSource> {
045:
046: @Resource
047: protected WebServiceContext wsContext;
048:
049: public RestSourcePayloadProvider() {
050: }
051:
052: public DOMSource invoke(DOMSource request) {
053: MessageContext mc = wsContext.getMessageContext();
054: String path = (String) mc.get(MessageContext.PATH_INFO);
055: String query = (String) mc.get(MessageContext.QUERY_STRING);
056: String httpMethod = (String) mc
057: .get(MessageContext.HTTP_REQUEST_METHOD);
058:
059: /*Map<String, List<String>> requestHeader =
060: (Map<String, List<String>>)mc.get(MessageContext.HTTP_REQUEST_HEADERS);*/
061:
062: Map<String, List<String>> responseHeader = CastUtils
063: .cast((Map) mc
064: .get(MessageContext.HTTP_RESPONSE_HEADERS));
065: if (responseHeader == null) {
066: responseHeader = new HashMap<String, List<String>>();
067: mc
068: .put(MessageContext.HTTP_RESPONSE_HEADERS,
069: responseHeader);
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: // setup return context
093: return getCustomer(query);
094: }
095: }
096:
097: return null;
098: }
099:
100: private DOMSource getAllCustomers() {
101: return createDOMSource("resources/CustomerAllResp.xml");
102: }
103:
104: private DOMSource getCustomer(String customerID) {
105: return createDOMSource("resources/CustomerJohnResp.xml");
106: }
107:
108: private DOMSource createDOMSource(String fileName) {
109: DocumentBuilderFactory factory;
110: DocumentBuilder builder;
111: Document document = null;
112: DOMSource response = null;
113:
114: try {
115: factory = DocumentBuilderFactory.newInstance();
116: factory.setNamespaceAware(true);
117: builder = factory.newDocumentBuilder();
118: InputStream greetMeResponse = getClass()
119: .getResourceAsStream(fileName);
120:
121: document = builder.parse(greetMeResponse);
122: response = new DOMSource(document);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: return response;
127: }
128:
129: }
|