You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by Thilini <th...@wso2.com> on 2005/11/24 13:56:49 UTC

REST support for Axis2 client side

Hi all,
I've started to implement REST for Axis2 client side using the rules found on WSDL 2.0. I completed implementing parts within the CommonsHTTPTransportSender with several test cases. The test cases however run against the yahoo endpoint so I've excluded these test cases from the build for the time.
I would appreciate if somebody can review this and apply this attached patch.

Thanks,
Thilini


Re: REST support for Axis2 client side

Posted by Davanum Srinivas <da...@gmail.com>.
Thilini,

could you please open a JIRA issue and upload a zip?

thanks,
dims

On 11/24/05, Thilini <th...@wso2.com> wrote:
> Hi all,
> I've started to implement REST for Axis2 client side using the rules found on WSDL 2.0. I completed implementing parts within the CommonsHTTPTransportSender with several test cases. The test cases however run against the yahoo endpoint so I've excluded these test cases from the build for the time.
> I would appreciate if somebody can review this and apply this attached patch.
>
> Thanks,
> Thilini
>
>
>
> Index: modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java
> ===================================================================
> --- modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java    (revision 348719)
> +++ modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java    (working copy)
> @@ -28,6 +28,7 @@
>  import org.apache.axis2.description.TransportOutDescription;
>  import org.apache.axis2.handlers.AbstractHandler;
>  import org.apache.axis2.i18n.Messages;
> +import org.apache.axis2.om.OMAbstractFactory;
>  import org.apache.axis2.om.OMAttribute;
>  import org.apache.axis2.om.OMElement;
>  import org.apache.axis2.om.impl.OMOutputImpl;
> @@ -59,12 +60,14 @@
>  import javax.xml.stream.XMLOutputFactory;
>  import javax.xml.stream.XMLStreamException;
>  import javax.xml.stream.XMLStreamWriter;
> +
>  import java.io.ByteArrayOutputStream;
>  import java.io.IOException;
>  import java.io.InputStream;
>  import java.io.OutputStream;
>  import java.net.MalformedURLException;
>  import java.net.URL;
> +import java.util.ArrayList;
>  import java.util.Iterator;
>
>  public class CommonsHTTPTransportSender
> @@ -104,7 +107,69 @@
>
>      public CommonsHTTPTransportSender() {
>      } //default
> -
> +
> +    public RequestData createRequest(MessageContext msgContext) {
> +       //This used to obtain two strings to go with the url and to pass in the body when doing
> +       //POST with application/x-www-form-urlencoded
> +       RequestData data = new RequestData();
> +       String contentType = findContentType(true,msgContext);
> +       OMElement dataOut = msgContext.getEnvelope().getBody().getFirstElement();
> +
> +       Iterator iter1 = dataOut.getChildElements();
> +               ArrayList paraList = new ArrayList();
> +               ArrayList urlList = new ArrayList();
> +
> +       //String[] s  = new String[] {"abc","def","pqr"};
> +               String[] s = new String[] {};
> +            String ns = "http:/rwrfr";
> +            OMElement bodypara = OMAbstractFactory.getOMFactory().createOMElement("dummy", null);
> +
> +               while(iter1.hasNext()){
> +                       OMElement ele = (OMElement)iter1.next();
> +                        boolean has = false;
> +
> +                       for(int i = 0;i<s.length; i++)
> +                       {
> +                               if(s[i].equals(ele.getLocalName())){
> +                                       has = true;
> +                                       break;
> +                               }
> +                       }
> +                   String parameter1;
> +
> +                       if(has){
> +                               parameter1 = ele.getLocalName() + "=" + ele.getText();
> +                               urlList.add(parameter1);
> +
> +                       }else {
> +                               bodypara.addChild(ele);
> +                       }
> +               }
> +
> +               String urlString= "";
> +               for(int i= 0; i<urlList.size() ; i++){
> +                       String c = (String)urlList.get(i);
> +                       urlString = urlString + "&" + c;
> +                       data.urlRequest = urlString;
> +               }
> +
> +               Iterator it = bodypara.getChildElements();
> +               while (it.hasNext()){
> +                       OMElement ele1 = (OMElement)it.next();
> +                       String parameter2;
> +                       parameter2 = ele1.getLocalName() + "=" + ele1.getText();
> +                       paraList.add(parameter2);
> +               }
> +
> +               String paraString= "";
> +               for(int j= 0; j<paraList.size() ; j++){
> +               String b = (String)paraList.get(j);
> +               paraString = paraString + "&" + b;
> +               data.bodyRequest = paraString;
> +               }
> +               return data;
> +       }
> +
>      public synchronized void invoke(MessageContext msgContext) throws AxisFault {
>          try {
>              String charSetEnc =
> @@ -154,14 +219,15 @@
>              // ######################################################
>
>              OMElement dataOut;
> -            /**
> +             /**
>               * Figuringout the REST properties/parameters
>               */
>              msgContext.setDoingREST(HTTPTransportUtils.isDoingREST(msgContext));
>              msgContext.setRestThroughPOST(HTTPTransportUtils.isDoingRESTThoughPost(msgContext));
> -
> -            if (msgContext.isDoingREST()) {
> -                dataOut = msgContext.getEnvelope().getBody().getFirstElement();
> +            boolean isRest = msgContext.isDoingREST();
> +
> +            if (isRest) {
> +               dataOut = msgContext.getEnvelope().getBody().getFirstElement();
>              } else {
>                  dataOut = msgContext.getEnvelope();
>              }
> @@ -180,7 +246,8 @@
>                      if (transportInfo != null) {
>                          omOutput.setSoap11(msgContext.isSOAP11());
>                          //this is the servlet2.3 way of setting encodings
> -                        String encoding = omOutput.getContentType() + "; charset=" + omOutput.getCharSetEncoding();
> +                        String contentType = findContentType(isRest,msgContext);
> +                                               String encoding = contentType + "; charset=" + omOutput.getCharSetEncoding();
>                          transportInfo.setContentType(encoding);
>                      } else {
>                          throw new AxisFault(HTTPConstants.HTTPOutTransportInfo + " does not set");
> @@ -204,7 +271,26 @@
>          }
>      }
>
> -    public void writeMessageWithToOutPutStream(
> +    /**
> +        * @return
> +        */
> +       private String findContentType(boolean isRest,MessageContext msgContext) {
> +               if (isRest){
> +                       if (msgContext.getProperty("content Type")!=null){
> +                               String contentType = (String)msgContext.getProperty("content Type");
> +                       //get the users setting from the axis2.xml parameters
> +                       //if present return that
> +                       //else return the default (application/xml)
> +                               return contentType;
> +                       }else{
> +                               return "application/xml";
> +                       }
> +               }else{
> +                       return omOutput.getContentType();
> +               }
> +       }
> +
> +       public void writeMessageWithToOutPutStream(
>              MessageContext msgContext,
>              OutputStream out) {
>
> @@ -235,16 +321,19 @@
>                          soapActionString);
>              }
>              if (msgContext.isDoingREST()) {
> -                if (msgContext.isRestThroughPOST()) {
> -                    this.transportConfigurationPOST(
> -                            msgContext,
> -                            dataout,
> -                            url,
> -                            soapActionString);
> -                } else {
> +               if (msgContext.isRestThroughPOST()) {
> +                       this.transportConfigurationPOST(
> +                                msgContext,
> +                                dataout,
> +                                url,
> +                                soapActionString);
> +               }
> +
> +                 else {
>                      this.transportConfigurationGET(msgContext, url);
>                  }
> -            }
> +        }
> +
>          } catch (MalformedURLException e) {
>              throw new AxisFault(e);
>          } catch (HttpException e) {
> @@ -254,7 +343,42 @@
>          }
>
>      }
> +    // POST application/x-www-form-urlencoded
> +
> +    public class PostAxisRequestEntity implements RequestEntity{
>
> +       private String charSetEnc;
> +       private String postRequestBody;
> +       private MessageContext msgCtxt;
> +       private String contentType;
> +
> +       public PostAxisRequestEntity(String postRequestBody, String charSetEnc,MessageContext msgCtxt, String contentType) {
> +               this.postRequestBody = postRequestBody;
> +               this.charSetEnc = charSetEnc;
> +               this.msgCtxt = msgCtxt;
> +               this.contentType = contentType;
> +       }
> +
> +               public boolean isRepeatable() {
> +                       return true;
> +               }
> +
> +
> +               public void writeRequest(OutputStream output) throws IOException {
> +                       output.write(postRequestBody.getBytes());
> +               }
> +
> +               public long getContentLength() {
> +                       return this.postRequestBody.getBytes().length;
> +               }
> +
> +
> +               public String getContentType() {
> +                       return this.contentType;
> +               }
> +
> +    }
> +
>      //get the contentLength...
>      public class AxisRequestEntity implements RequestEntity {
>
> @@ -304,6 +428,7 @@
>                      element.serializeAndConsume(output);
>                      output.flush();
>                      return bytesOut.toByteArray();
> +
>                  } else {
>                      omOutput.setCharSetEncoding(charSetEnc);
>                      omOutput.setOutputStream(bytesOut, true);  //changed...
> @@ -384,6 +509,7 @@
>          }
>
>          public String getContentType() {
> +
>              String encoding = omOutput.getCharSetEncoding();
>              String contentType = omOutput.getContentType();
>              if (encoding != null) {
> @@ -489,17 +615,27 @@
>          //todo giving proxy and NTLM support
>
>          PostMethod postMethod = new PostMethod(url.toString());
> -        postMethod.setPath(url.getPath());
> -
> +        String contentType = findContentType(true,msgContext);
> +
>          msgContext.setProperty(HTTP_METHOD, postMethod);
> -
>          String charEncoding =
>                  (String) msgContext.getProperty(
>                          MessageContext.CHARACTER_SET_ENCODING);
>          if (charEncoding == null) {
>              charEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
>          }
> -
> +
> +        //if POST as application/x-www-form-urlencoded
> +        RequestData reqData = null;
> +        if (contentType.equalsIgnoreCase(HTTPConstants.REST_CONTENTTYPE_URL_ENCODED)){
> +               reqData = createRequest(msgContext);
> +               postMethod.setPath(url.getPath()+ ((reqData.urlRequest) != null ? ("?" + reqData.urlRequest) : ""));
> +               postMethod.setRequestEntity(new PostAxisRequestEntity(reqData.bodyRequest,charEncoding,msgContext,contentType));
> +
> +        }else{
> +               postMethod.setPath(url.getPath());
> +
> +
>          postMethod.setRequestEntity(
>                  new AxisRequestEntity(
>                          dataout,
> @@ -507,12 +643,12 @@
>                          msgContext,
>                          charEncoding,
>                          soapActionString));
> +        }
>
> -
>          if (!httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)
>                  && chuncked) {
>              postMethod.setContentChunked(true);
> -        }
> +        }
>          postMethod.setRequestHeader(
>                  HTTPConstants.HEADER_USER_AGENT,
>                  "Axis/2.0");
> @@ -614,23 +750,51 @@
>                  in);
>      }
>
> +    //Method to return the parameter string to pass with the URL when using GET
> +
> +    public String getParam(MessageContext msgContext){
> +       OMElement dataOut;
> +       dataOut = msgContext.getEnvelope().getBody().getFirstElement();
> +               Iterator iter1 = dataOut.getChildElements();
> +       ArrayList paraList = new ArrayList();
> +
> +       while(iter1.hasNext()){
> +               OMElement ele = (OMElement)iter1.next();
> +               String parameter;
> +               parameter = ele.getLocalName() + "=" + ele.getText();
> +               paraList.add(parameter);
> +       }
> +
> +               String paraString= "";
> +               int count = paraList.size();
> +               for(int i= 0; i<count ; i++){
> +                       String c = (String)paraList.get(i);
> +                       paraString = paraString + "&" + c;
> +               }
> +       return paraString;
> +       }
> +
>      private void transportConfigurationGET(MessageContext msgContext, URL url)
>              throws MalformedURLException, AxisFault, IOException {
> -        GetMethod getMethod = new GetMethod();
> -        getMethod.setPath(url.getFile());
> +
> +       String param = getParam(msgContext);
> +           GetMethod getMethod = new GetMethod();
> +        getMethod.setPath(url.getFile()+"?"+param);
>
> -        String charEncoding =
> +        //Serialization as "application/x-www-form-urlencoded"
> +
> +       String charEncoding =
>                  (String) msgContext.getProperty(
>                          MessageContext.CHARACTER_SET_ENCODING);
>          if (charEncoding == null) //Default encoding scheme
>              getMethod.setRequestHeader(
>                      HTTPConstants.HEADER_CONTENT_TYPE,
> -                    "text/xml; charset="
> +                    HTTPConstants.REST_CONTENTTYPE_URL_ENCODED +"; charset="
>                              + MessageContext.DEFAULT_CHAR_SET_ENCODING);
>          else
>              getMethod.setRequestHeader(
>                      HTTPConstants.HEADER_CONTENT_TYPE,
> -                    "text/xml; charset=" + charEncoding);
> +                    HTTPConstants.REST_CONTENTTYPE_URL_ENCODED + "; charset=" + charEncoding);
>
>          this.httpClient = new HttpClient();
>
> @@ -843,6 +1007,14 @@
>          client.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
>          config.setProxy(proxyHostName, proxyPort);
>      }
> +
> +    //
> +    private class RequestData{
> +       String urlRequest;
> +               String bodyRequest;
> +               }
> +       String urlRequest;
> +       String bodyRequest;
> +       }
>
> -}
>
> Index: modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java
> ===================================================================
> --- modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java (revision 348719)
> +++ modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java (working copy)
> @@ -24,6 +24,8 @@
>   */
>  public class HTTPConstants {
>
> +       public static final String REST_CONTENTTYPE_URL_ENCODED="application/x-www-form-urlencoded" ;
> +
>      public static final String PROTOCOL_VERSION = "PROTOCOL";
>
>      /**
> Index: modules/integration/project.xml
> ===================================================================
> --- modules/integration/project.xml     (revision 348719)
> +++ modules/integration/project.xml     (working copy)
> @@ -272,6 +272,10 @@
>                  <exclude>**org/apache/axis2/mail/*.java</exclude>
>                  <exclude>**org/apache/axis2/soap12testing/soap12testsuite/*.java</exclude>
>                  <exclude>**/ScenarioST1Test.java</exclude>
> +                <!-- exclude the rest test cases -->
> +                <exclude>**/GetTest.java</exclude>
> +                <exclude>**/PostTest.java</exclude>
> +                <exclude>**/RESTGetTest.java</exclude>
>              </excludes>
>              <includes>
>                  <include>**/*Test.java</include>
> Index: modules/integration/test/org/apache/axis2/rest/GetTest.java
> ===================================================================
> --- modules/integration/test/org/apache/axis2/rest/GetTest.java (revision 0)
> +++ modules/integration/test/org/apache/axis2/rest/GetTest.java (revision 0)
> @@ -0,0 +1,73 @@
> +package org.apache.axis2.rest;
> +
> +import java.io.ByteArrayInputStream;
> +
> +import org.apache.axis2.Constants;
> +import org.apache.axis2.addressing.EndpointReference;
> +import org.apache.axis2.client.Call;
> +import org.apache.axis2.engine.util.TestConstants;
> +//import org.apache.axis2.clientapi.RESTCall;
> +import org.apache.axis2.om.OMAbstractFactory;
> +import org.apache.axis2.om.OMElement;
> +import org.apache.axis2.om.OMFactory;
> +import org.apache.axis2.om.OMNamespace;
> +import org.apache.axis2.om.impl.OMOutputImpl;
> +import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
> +
> +import javax.xml.stream.XMLInputFactory;
> +import javax.xml.stream.XMLOutputFactory;
> +import javax.xml.stream.XMLStreamException;
> +import javax.xml.stream.XMLStreamReader;
> +import javax.xml.stream.XMLStreamWriter;
> +
> +import junit.framework.TestCase;
> +
> +public class GetTest extends TestCase implements TestConstants {
> +
> +       public void testRESTGet() throws Exception{
> +
> +            String epr = "http://localhost:8080/axis2/services/MyService";
> +
> +            String  xml =
> +               "<echo>"+
> +                               "<Text>Hello</Text>"+
> +                               "</echo>";
> +
> +               byte arr[] = xml.getBytes();
> +               ByteArrayInputStream bais = new ByteArrayInputStream(arr);
> +
> +               XMLStreamReader reader = null;
> +               try {
> +                       XMLInputFactory xif= XMLInputFactory.newInstance();
> +                       reader= xif.createXMLStreamReader(bais);
> +               } catch (XMLStreamException e) {
> +                       e.printStackTrace();
> +               }
> +               StAXOMBuilder builder= new StAXOMBuilder(reader);
> +               OMElement data = builder.getDocumentElement();
> +
> +               OMFactory fac = OMAbstractFactory.getOMFactory();
> +            /*OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1", "example1");
> +            OMElement payload = fac.createOMElement("echo", omNs);
> +            OMElement value = fac.createOMElement("Text", omNs);
> +            value.addChild(fac.createText(value, "Hello"));
> +            payload.addChild(value);*/
> +
> +            //RESTCall call = new RESTCall();
> +               //OMElement val = fac.
> +            Call call = new Call();
> +            call.setTo(new EndpointReference(epr));
> +            call.setTransportInfo(Constants.TRANSPORT_HTTP,Constants.TRANSPORT_HTTP, false);
> +            call.set(Constants.Configuration.ENABLE_REST,Constants.VALUE_TRUE);
> +            call.set(Constants.Configuration.ENABLE_REST_THROUGH_GET,Constants.VALUE_TRUE);
> +
> +            //if post is through GET of HTTP
> +            //OMElement response = call.invokeBlocking("webSearch",data);
> +            OMElement response = call.invokeBlocking("webSearch",data);
> +            //OMElement response = call.invokeBlocking();
> +            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
> +            response.serialize(new OMOutputImpl(writer));
> +            writer.flush();
> +               }
> +}
> +
> Index: modules/integration/test/org/apache/axis2/rest/PostTest.java
> ===================================================================
> --- modules/integration/test/org/apache/axis2/rest/PostTest.java        (revision 0)
> +++ modules/integration/test/org/apache/axis2/rest/PostTest.java        (revision 0)
> @@ -0,0 +1,108 @@
> +/*
> + * Copyright 2004,2005 The Apache Software Foundation.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +
> +package org.apache.axis2.rest;
> +
> +import org.apache.axis2.AxisFault;
> +import org.apache.axis2.Constants;
> +import org.apache.axis2.addressing.EndpointReference;
> +import org.apache.axis2.client.Call;
> +import org.apache.axis2.engine.util.TestConstants;
> +import org.apache.axis2.om.OMAbstractFactory;
> +import org.apache.axis2.om.OMElement;
> +import org.apache.axis2.om.OMFactory;
> +import org.apache.axis2.om.OMNamespace;
> +import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
> +
> +import javax.xml.stream.XMLInputFactory;
> +import javax.xml.stream.XMLOutputFactory;
> +import javax.xml.stream.XMLStreamException;
> +import javax.xml.stream.XMLStreamReader;
> +
> +import java.io.ByteArrayInputStream;
> +import java.io.StringWriter;
> +
> +import junit.framework.TestCase;
> +
> +/**
> + * Sample for synchronous single channel blocking service invocation.
> + * Message Exchage Pattern IN-OUT
> + */
> +public class PostTest extends TestCase implements TestConstants{
> +    //private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService");
> +       //private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:80/axis2/services/MyService");
> +    private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8070/onca/xml");
> +
> +    public void testRESTPost() throws Exception{
> +        try {
> +           // OMElement payload = ClientUtil.getEchoOMElement();
> +               /*OMFactory fac = OMAbstractFactory.getOMFactory();
> +            OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1", "example1");
> +            OMElement payload = fac.createOMElement("echo", omNs);
> +            OMElement value = fac.createOMElement("Text", omNs);
> +            value.addChild(fac.createText(value, "Hello"));
> +            payload.addChild(value);*/
> +
> +               String  xml =
> +                       "<websearch>"+
> +                       "<Service>AWSECommerceService</Service>"+
> +                       "<SubscriptionId>03WM83XFMP0X52C7A9R2</SubscriptionId>"+
> +                       "<Operation>ItemSearch</Operation>"+
> +                       "<SearchIndex>Books</SearchIndex>"+
> +                       "<Keywords>Sanjiva,Web,Services</Keywords>"+
> +                       "<ResponseGroup>Request,Small</ResponseGroup>"+
> +                       "</websearch>";
> +
> +               byte arr[] = xml.getBytes();
> +               ByteArrayInputStream bais = new ByteArrayInputStream(arr);
> +
> +               XMLStreamReader reader = null;
> +               try {
> +                       XMLInputFactory xif= XMLInputFactory.newInstance();
> +                       reader= xif.createXMLStreamReader(bais);
> +               } catch (XMLStreamException e) {
> +                       e.printStackTrace();
> +               }
> +               StAXOMBuilder builder= new StAXOMBuilder(reader);
> +               OMElement websearch = builder.getDocumentElement();
> +
> +               Call call = new Call();
> +            call.setTo(targetEPR);
> +            call.setTransportInfo(Constants.TRANSPORT_HTTP,Constants.TRANSPORT_HTTP,false);
> +            call.set(Constants.Configuration.ENABLE_REST,Constants.VALUE_TRUE);
> +            //call.set(Constants.Configuration.ENABLE_REST_THROUGH_GET,Constants.VALUE_TRUE);
> +            call.set("content Type","application/x-www-form-urlencoded");
> +
> +            //Blocking invocation
> +            //OMElement result = call.invokeBlocking("echo",payload);
> +            OMElement result = call.invokeBlocking("echo",websearch);
> +
> +            StringWriter writer = new StringWriter();
> +            result.serialize(XMLOutputFactory.newInstance()
> +                    .createXMLStreamWriter(writer));
> +            writer.flush();
> +
> +            System.out.println(writer.toString());
> +
> +        } catch (AxisFault axisFault) {
> +            axisFault.printStackTrace();
> +        } catch (XMLStreamException e) {
> +            e.printStackTrace();
> +        }
> +    }
> +    }
> +
> Index: modules/integration/test/org/apache/axis2/rest/RESTGetTest.java
> ===================================================================
> --- modules/integration/test/org/apache/axis2/rest/RESTGetTest.java     (revision 0)
> +++ modules/integration/test/org/apache/axis2/rest/RESTGetTest.java     (revision 0)
> @@ -0,0 +1,92 @@
> +/*
> + * Created on Nov 24, 2005
> + *
> + * TODO To change the template for this generated file go to
> + * Window - Preferences - Java - Code Style - Code Templates
> + */
> +package org.apache.axis2.rest;
> +
> +import java.io.ByteArrayInputStream;
> +
> +import javax.xml.stream.FactoryConfigurationError;
> +import javax.xml.stream.XMLInputFactory;
> +import javax.xml.stream.XMLOutputFactory;
> +import javax.xml.stream.XMLStreamException;
> +import javax.xml.stream.XMLStreamReader;
> +import javax.xml.stream.XMLStreamWriter;
> +
> +import junit.framework.TestCase;
> +
> +import org.apache.axis2.AxisFault;
> +import org.apache.axis2.Constants;
> +import org.apache.axis2.addressing.EndpointReference;
> +import org.apache.axis2.client.Call;
> +//import org.apache.axis2.client.*;
> +import org.apache.axis2.engine.util.TestConstants;
> +import org.apache.axis2.om.OMElement;
> +import org.apache.axis2.om.impl.OMOutputImpl;
> +import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
> +
> +/**
> + * @author Thilini
> + *
> + * TODO To change the template for this generated type comment go to
> + * Window - Preferences - Java - Code Style - Code Templates
> + */
> +
> +//This Sample test Client is written for Yahoo Web Search
> +
> +public class RESTGetTest extends TestCase implements TestConstants{
> +
> +       public void testRESTGet(){
> +               //String epr = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=ApacheRestDemo&query=finances&format=pdf";
> +       //String epr = "http://127.0.0.1:8080/WebSearchService/V1/webSearch";
> +       //String epr = "http://webservices.amazon.com/onca/xml";
> +       String epr = "http://api.search.yahoo.com/WebSearchService/V1/webSearch";
> +
> +        String  xml =
> +               "<websearch>"+
> +                       "<appid>ApacheRestDemo</appid>"+
> +                       "<query>finances</query>"+
> +                       "<format>pdf</format>"+
> +                       "</websearch>";
> +
> +       byte arr[] = xml.getBytes();
> +       ByteArrayInputStream bais = new ByteArrayInputStream(arr);
> +
> +       XMLStreamReader reader = null;
> +       try {
> +               XMLInputFactory xif= XMLInputFactory.newInstance();
> +               reader= xif.createXMLStreamReader(bais);
> +       } catch (XMLStreamException e) {
> +               e.printStackTrace();
> +       }
> +       StAXOMBuilder builder= new StAXOMBuilder(reader);
> +       OMElement data = builder.getDocumentElement();
> +
> +        try {
> +                       //RESTCall call = new RESTCall();
> +                       Call call = new Call();
> +                       call.setTo(new EndpointReference(epr));
> +                       call.setTransportInfo(Constants.TRANSPORT_HTTP,Constants.TRANSPORT_HTTP, false);
> +                       call.set(Constants.Configuration.ENABLE_REST,Constants.VALUE_TRUE);
> +                       call.set(Constants.Configuration.ENABLE_REST_THROUGH_GET,Constants.VALUE_TRUE);
> +
> +                       //if post is through GET of HTTP
> +                       OMElement response = call.invokeBlocking("webSearch",data);
> +                       //OMElement response = call.invokeBlocking();
> +                       XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
> +                       response.serialize(new OMOutputImpl(writer));
> +                       writer.flush();
> +               } catch (AxisFault e1) {
> +                       // TODO Auto-generated catch block
> +                       e1.printStackTrace();
> +               } catch (XMLStreamException e1) {
> +                       // TODO Auto-generated catch block
> +                       e1.printStackTrace();
> +               } catch (FactoryConfigurationError e1) {
> +                       // TODO Auto-generated catch block
> +                       e1.printStackTrace();
> +               }
> +       }
> +}
>
>
>
>


--
Davanum Srinivas : http://wso2.com/blogs/