XML:

<!--   ~ Licensed to the Apache Software Foundation (ASF) under one   ~ or more contributor license agreements. See the NOTICE file   ~ distributed with this work for additional information   ~ regarding copyright ownership. The ASF licenses this file   ~ to you 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.   -->

<service name="ReiKingCallService" scope="application">     <description>         ReiKing Call Service     </description>     <operation name="callService">         <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>     </operation>     <parameter name="ServiceClass">umb.ws.service.ReiKingCallService</parameter> </service>

 

SERVER :

package umb.ws.service;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Hashtable; import java.util.Iterator;

import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace;

public class ReiKingCallService {

 private String namespace = "http://reiking.samples/xsd";  

private Hashtable values =  null;    

public OMElement callService(OMElement user) {   

user.build(); user.detach();

//  StringWriter writer = new StringWriter();

//  try {

//   user.serialize(XMLOutputFactory.newInstance()

//           .createXMLStreamWriter(writer));

//  } catch (XMLStreamException e1) {

  // TODO 自动生成 catch 块

//   e1.printStackTrace();

//  } catch (FactoryConfigurationError e1) {

// TODO 自动生成 catch 块

//   e1.printStackTrace();

//  }

//        writer.flush();

//        System.out.println(writer.toString());        

          values = new Hashtable();   

          Iterator list = user.getChildElements();  

          while(list.hasNext()) {   

           OMElement e = (OMElement) list.next();    

           System.out.println(e.getLocalName() + ": "+e.getText());   

           values.put(e.getLocalName(), e.getText());  

     }     

          String User = (String) values.get("User");                

          String password = (String) values.get("PWD");      

          String serverID = (String) values.get("ServerAppID");                

          String menuID = (String) values.get("MenuID");               

          String locator = (String) values.get("Locator");               

          String serviceType = (String) values.get("ServiceType");            

          String serviceID = (String) values.get("ServiceID");                

          String varID= (String) values.get("VarID");       

          String varValue= (String) values.get("VarValue");        

          String inputType = (String) values.get("InputType");        

          String inputContent = (String) values.get("InputContent");                    

          String res;        

        try        

        {          String tt="";         

              if ((varID !=null)&&(varID.length()>0)){            

                      varValue = URLEncoder.encode(new String(varValue.getBytes(), "iso-8859-1"), "iso-8859-1");   

                      tt += "&VarID=" + varID + "&VarValue=" + varValue;  

               }  

                if ((inputType !=null)&&(inputType.length()>0)) {       

                           inputContent = URLEncoder.encode(new String(inputContent.getBytes(), "iso-8859-1"), "iso-8859-1");     

                        tt += "&InputType=" + inputType + "&InputContent=" + inputContent;  

                }

                   String url = "http://localhost:7001/rkmanager/RKManager?";           

                   res = getXmlRtnContent(url                    

                         + "ProcessType=webservice.CallWebService&ServerAppID=" + serverID + "&MenuID=" + menuID + "&Locator=" + locator                          + "&ServiceType=" + serviceType + "&ServiceID=" + serviceID+ "&User=" + User + "&PWD=" + password      

                         + tt);   

          }       catch (Exception e)         {  

                  System.out.println(e.getMessage());            

                    res = "FAILED:" + e.getMessage();      

          }    

                  OMFactory fac = OMAbstractFactory.getOMFactory();       

                  OMNamespace omNs =   fac.createOMNamespace(namespace, "ns");       

                  OMElement method = fac.createOMElement("callServiceResponse", omNs);      

                  OMElement value = fac.createOMElement("return", omNs);    

                  value.addChild(fac.createOMText(value, res));    

                  method.addChild(value);      

                  return method;

       }    

 

                private String getXmlRtnContent(String url) throws Exception     {     

                     try   {

               //             System.out.println("URL:  " + url);      

                               URL pku = new URL(url);       

                               InputStreamReader inputStreamReader = new InputStreamReader(pku .openStream());        

                               BufferedReader in = new BufferedReader(inputStreamReader);

                               String inputLine;       

                               StringBuffer stringBuffer = new StringBuffer();   

                               while ((inputLine = in.readLine()) != null)             {                

                   //              System.out.println(inputLine);         

                                    stringBuffer = stringBuffer.append(inputLine);       

                                }            

                               in.close();     

                              return (stringBuffer.toString());      

                              }         catch (MalformedURLException ex)         {  

                                        System.out.println("exception in " + this.getClass().getName() + ": " + ex);         

                                        throw new Exception("error connect! ");  

                             }    catch (IOException ex)         {        

                              System.out.println("exception in " + this.getClass().getName() + ": " + ex);         

                              throw new Exception("error connect! ");    

                            }

                       }

                  }

 

 

CLIENT:

     

package umb.ws.client;

import java.io.StringWriter;

import javax.xml.stream.XMLOutputFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import umb.di.db.Row;

import umb.di.db.RowInfo;

import umb.di.db.RowSet;

import umb.di.value.ValueInfo;

import umb.di.value.ValueInterface;

import umb.di.value.ValueString;

public class AXIOMClient {  

    private static EndpointReference targetEPR =       

                   new EndpointReference(  "http://localhost:7001/rkwebservice/services/ReiKingCallService");

        public static void addRowInfo(RowSet rowSet) {  

                   RowInfo rowInfo = new RowInfo();  

                   ValueInfo vi;

                    for (int i=0; i<5; i++) {  

                           vi = new ValueInfo("No."+i, ValueInfo.VALUE_TYPE_STRING);

                               rowInfo.addValueInfo(vi);

                   }      rowSet.putRowInfo(rowInfo);  }

                   public static void addRows(RowSet rowSet) {  

                                 Row row = new Row();

                                 ValueInterface v;

                                 v = new ValueString("HQG");   

                                 row.addValue(v );  

                                v = new ValueString("北京"); 

                                row.addValue(v );

                                v = new ValueString("1000");  

                                row.addValue(v );

                                v = new ValueString("1970-07-31"); 

                                 row.addValue(v );

                                v = new ValueString("中科院"); 

                                row.addValue(v );  

                                rowSet.addRow(row);   

                                row = new Row();

                                v = new ValueString("wdx");

                                row.addValue(v );  

                                v = new ValueString("北京"); 

                                row.addValue(v );  

                                v = new ValueString("1000");  

                                row.addValue(v );

                                v = new ValueString("1971-01-31");

                                row.addValue(v );  

                                v = new ValueString("北京大学"); 

                                 row.addValue(v );  

                                rowSet.addRow(row);     

                                row = new Row();  

                               v = new ValueString("Hxy"); 

                               row.addValue(v );  

                                 v = new ValueString("北京");  

                                 row.addValue(v );  

                                 v = new ValueString("1000");  

                                  row.addValue(v );

                                 v = new ValueString("2004-11-11");

                                 row.addValue(v );   

                                  v = new ValueString("哈佛大学"); 

                                 row.addValue(v );  

                                  rowSet.addRow(row);

                        }

                         public static void main(String[] args) {        

                                   try {        

                                          Options options = new Options();          

                                           options.setTo(targetEPR);       

                                           options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

                                           ServiceClient sender = new ServiceClient();    

                                            sender.setOptions(options);          

                                            OMElement sendInfo;    

                                            OMElement result;     

                                            StringWriter writer = new StringWriter();     

                                             String response;          

                                             sendInfo = RKWebServiceClientUtil.getCallService("admin", "reiking", "yy/dxserver", "WebService测试", "Process",

                                                              "App1ETLProcess", "yy_ue_pppp");                                    

                                              sendInfo.serialize(XMLOutputFactory.newInstance()            

                                                .createXMLStreamWriter(writer));        

                                              writer.flush();       

                                              System.out.println(writer.toString());           

                                              result = sender.sendReceive(sendInfo);

                                              response = result.getFirstElement().getText();      

                                             System.err.println("Call Web Service : " + response);                 

                                             RowSet rowSet = new RowSet(10000);   

                                              addRowInfo(rowSet);   

                                              addRows(rowSet);       

                                              sendInfo = RKWebServiceClientUtil.getCallServiceHasRowSetInput("admin", "reiking", "yy/dxserver", "WebService测试

                                               RowSet",         "DataProcess", "DataProcessService2", "RowSetToFile", rowSet);  

                                              writer = new StringWriter();      

                                              sendInfo.serialize(XMLOutputFactory.newInstance()            

                                                  .createXMLStreamWriter(writer));     

                                              writer.flush();      

                                             System.out.println(writer.toString());            

                                             result = sender.sendReceive(sendInfo);

                                             response = result.getFirstElement().getText();       

                                             System.err.println("Call Web Service : " + response);              

                                        } catch (Exception e) {           

                                               e.printStackTrace();      

                                        }  

                                 }   

                           }

posted on 2014-02-20 21:59  孤狼啸月  阅读(224)  评论(0编辑  收藏  举报