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.calculator;
019:
020: import java.io.ByteArrayInputStream;
021:
022: import javax.annotation.Resource;
023: import javax.servlet.ServletRequest;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.dom.DOMSource;
028: import javax.xml.transform.stream.StreamSource;
029: import javax.xml.ws.BindingType;
030: import javax.xml.ws.Provider;
031: import javax.xml.ws.WebServiceContext;
032: import javax.xml.ws.WebServiceProvider;
033: import javax.xml.ws.handler.MessageContext;
034: import javax.xml.ws.http.HTTPBinding;
035: import javax.xml.ws.http.HTTPException;
036:
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import org.xml.sax.InputSource;
040:
041: @WebServiceProvider
042: @BindingType(value=HTTPBinding.HTTP_BINDING)
043: public class CalculatorImpl implements Provider<Source> {
044:
045: @Resource
046: protected WebServiceContext wsContext;
047:
048: public Source invoke(Source source) {
049: try {
050: String num1 = null;
051: String num2 = null;
052:
053: if (source == null) {
054: System.out.println("Getting input from query string");
055: MessageContext mc = wsContext.getMessageContext();
056: String query = (String) mc
057: .get(MessageContext.QUERY_STRING);
058: System.out.println("Query String = " + query);
059: ServletRequest req = (ServletRequest) mc
060: .get(MessageContext.SERVLET_REQUEST);
061: num1 = req.getParameter("num1");
062: num2 = req.getParameter("num2");
063: } else {
064: System.out.println("Getting input from input message");
065: Node n = null;
066: if (source instanceof DOMSource) {
067: n = ((DOMSource) source).getNode();
068: } else if (source instanceof StreamSource) {
069: StreamSource streamSource = (StreamSource) source;
070: DocumentBuilderFactory dbf = DocumentBuilderFactory
071: .newInstance();
072: DocumentBuilder db = dbf.newDocumentBuilder();
073: InputSource inputSource = null;
074: if (streamSource.getInputStream() != null) {
075: inputSource = new InputSource(streamSource
076: .getInputStream());
077: } else if (streamSource.getReader() != null) {
078: inputSource = new InputSource(streamSource
079: .getReader());
080: }
081: n = db.parse(inputSource);
082: } else {
083: throw new RuntimeException("Unsupported source: "
084: + source);
085: }
086: NodeList children = n.getChildNodes();
087: for (int i = 0; i < children.getLength(); i++) {
088: Node child = children.item(i);
089: if (child.getNodeName().equals("add")) {
090: num1 = child.getAttributes().getNamedItem(
091: "num1").getNodeValue();
092: num2 = child.getAttributes().getNamedItem(
093: "num2").getNodeValue();
094: break;
095: }
096: }
097: }
098:
099: int n1 = Integer.parseInt(num1);
100: int n2 = Integer.parseInt(num2);
101: return createResultSource(n1 + n2);
102: } catch (Exception e) {
103: e.printStackTrace();
104: throw new HTTPException(500);
105: }
106: }
107:
108: private Source createResultSource(int sum) {
109: String body = "<ns:addResponse xmlns:ns=\"http://geronimo.apache.org\"><ns:return>"
110: + sum + "</ns:return></ns:addResponse>";
111: Source source = new StreamSource(new ByteArrayInputStream(body
112: .getBytes()));
113: return source;
114: }
115: }
|