Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Right click on JavaResources:src and select New->Package (or Ctrl+N, start typing package and select Package)





  2. Name the package to org.apache.geronimo.samples.jaxws.rest and click Finish





  3. Right click on the new package and select New->Class (or Ctrl+N, start typing class and select Class)





  4. Name the class as ConverterService and click Finish





  5. Add the following code to the ConverterService class
    Code Block
    titleConverterService.java
    borderStylesolid
    
    package org.apache.geronimo.samples.jaxws.rest;
    
    import java.io.ByteArrayInputStream;
    import java.math.BigDecimal;
    
    import javax.annotation.Resource;
    import javax.servlet.ServletRequest;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Source;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.ws.BindingType;
    import javax.xml.ws.Provider;
    import javax.xml.ws.WebServiceContext;
    import javax.xml.ws.WebServiceProvider;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.http.HTTPBinding;
    import javax.xml.ws.http.HTTPException;
    
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    @WebServiceProvider
    @BindingType(value = HTTPBinding.HTTP_BINDING)
    public class ConverterService implements Provider<Source> {
    
        @Resource
        protected WebServiceContext wsContext;
    
        private BigDecimal rupeeRate = new BigDecimal("40.58");
        private BigDecimal euroRate = new BigDecimal("0.018368");
    
        public Source invoke(Source source) {
            try {
                String amount = null;
    
                if (source == null) {
                    System.out.println("Getting input from query string");
                    MessageContext mc = wsContext.getMessageContext();
                    String query = (String) mc.get(MessageContext.QUERY_STRING);
                    System.out.println("Query String = " + query);
                    ServletRequest req = (ServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
                    amount = req.getParameter("amount");
                } else {
                    System.out.println("Getting input from input message");
                    Node n = null;
                    if (source instanceof DOMSource) {
                        n = ((DOMSource) source).getNode();
                    } else if (source instanceof StreamSource) {
                        StreamSource streamSource = (StreamSource) source;
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        InputSource inputSource = null;
                        if (streamSource.getInputStream() != null) {
                            inputSource = new InputSource(streamSource.getInputStream());
                        } else if (streamSource.getReader() != null) {
                            inputSource = new InputSource(streamSource.getReader());
                        }
                        n = db.parse(inputSource);
                    } else {
                        throw new RuntimeException("Unsupported source: " + source);
                    }
                    NodeList children = n.getChildNodes();
                    for (int i = 0; i < children.getLength(); i++) {
                        Node child = children.item(i);
                        if (child.getNodeName().equals("adddollars")) {
                            amount = child.getAttributes().getNamedItem("amount").getNodeValue();
                            break;
                        }
                    }
                }
                BigDecimal dollars = new BigDecimal(amount);
                BigDecimal rupees = dollarToRupees(dollars);
                BigDecimal euros = rupeesToEuro(rupees);
                return createResultSource(rupees, euros);
            } catch (Exception e) {
                e.printStackTrace();
                throw new HTTPException(500);
            }
        }
    
        public BigDecimal dollarToRupees(BigDecimal dollars) {
            BigDecimal result = dollars.multiply(rupeeRate);
            return result.setScale(2, BigDecimal.ROUND_UP);
        }
    
        public BigDecimal rupeesToEuro(BigDecimal rupees) {
            BigDecimal result = rupees.multiply(euroRate);
            return result.setScale(2, BigDecimal.ROUND_UP);
        }
    
        private Source createResultSource(BigDecimal rupees, BigDecimal euros) {
            String body = "<ns:return xmlns:ns=\"http://rest.jaxws.samples.geronimo.apache.org\">"
                    + "<ns:dollarToRupeesResponse>" + rupees + "</ns:dollarToRupeesResponse><ns:rupeesToEurosResponse>"
                    + euros + "</ns:rupeesToEurosResponse></ns:return>";
            Source source = new StreamSource(new ByteArrayInputStream(body.getBytes()));
            return source;
        }
    }
    
    

...