"WebService" based on Axis2 design WebService server with Soaphead security mechanism

Source: Internet
Author: User
Tags gettext

It's still written in Axis2. Is the first client, and then have this server, strictly according to the client's request message to write Oh. Whether to solve the "first chicken or Egg first" problem. Message, please refer to my previous post, "WebService" debugging Method (ii), then, my programming steps are like this.

1, New Java Project, import Axis2 package, dom4j kit.

2, according to the client request message above the SOAP namespace, for the engineering design package name, therefore, the project for the external call Class (server Class) where the package is cn.com.ultrapower.webservice.service.

3. Edit the configuration file Services.xml as follows:

<!--WebService configuration file   --><service name= "Prc_service" ><description>please Type your Service Description Here</description><messagereceivers><messagereceiver mep= "http://www.w3.org/2004/08/ Wsdl/in-only "class=" Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver "/><messagereceiver  mep=" Http://www.w3.org/2004/08/wsdl/in-out "  class=" Org.apache.axis2.rpc.receivers.RPCMessageReceiver "/></ Messagereceivers><parameter name= "ServiceClass" >cn.com.ultrapower.webservice.service.server</ Parameter></service>

4, according to the client request message above the SOAP request method, define the external method inside the server class is SendXML (String requestxml,string busikey). According to the message, it is known that the:<soapenv:header> parameters are username and password,<soapenv:body> The parameters are Busikey (requested by the business) and RequestXML (the requested business query parameters, XML format, need to be dom4j analyzed).

5, other tools design: With facilitates about the business code Busikey map of the Initbusikey.java,

6, other tool design: Used to load user's account password, interface permissions, interface traffic and other configuration information Inituser.java,

The following provides all the code for the server class:

Package Cn.com.ultrapower.webservice.service;import Java.io.unsupportedencodingexception;import Java.util.calendar;import Java.util.hashmap;import Java.util.iterator;import Java.util.map;import Org.apache.axiom.om.omelement;import Org.apache.axiom.soap.soapheader;import Org.apache.axis2.context.messagecontext;import Org.dom4j.documentexception;import Cn.com.ultrapower.webservice.service.conf.initbusikey;import Cn.com.ultrapower.webservice.service.conf.InitUser ; Import Cn.com.ultrapower.webservice.service.service.crbt_service;import Cn.com.ultrapower.webservice.service.service.isag_service;import Cn.com.ultrapower.webservice.service.service.mmsc_service;import Cn.com.ultrapower.webservice.service.service.wapgw_service;public class server{// Userflowmap is the cumulative number of user accesses to an interface in unit time private static map<string, long[]> Userflowmap = new hashmap<string, long[]  > ();//string user name username,long[0] is time, long[1] is the number of times string responsecode = "";  The query service returns the status code string responsexml = ""; Query the business data returned by the service */* @para Busikey: Business Platform Identity * @para requestxml: XML string for query criteria * Definition return code responsecode:001 input parameter Busikey Invalid 004 supplicant permission validation failed 003 Query interface maximum concurrency 00 2 input parameter RequestXML Invalid 005 request Query time timeout 999 other error 000 service interface processing succeeded * */public string SendXML (String requestxml,string busikey) {try{ String serviceName = Initbusikey.getinitbusikey (). Getservicename (Busikey); if (Servicename!=null &&! Servicename.equals ("") && Servicename.length () >0) {String Username = Checkauth (serviceName); if (username!= Null &&! Username.equals ("")) {if (Checkflow (Username)) {responsecode = "000";//000: Service interface processing succeeded Responsexml = Readrequestxml ( Servicename,requestxml); String returndata = Returndata (); return returndata;} Else{responsecode = "003";//003: Query interface reaches maximum concurrency Responsexml = ""; String returndata = Returndata (); return returndata;}} Else{responsecode = "004";//004: supplicant permission validation failed responsexml = ""; String returndata = Returndata (); return returndata;}} Else{responsecode = "001";//001: input parameter Busikey Invalid responsexml = ""; String returndata = Returndata (); return returndata;}}catch (NumberFormatException e) {//002: parameter string to int error (parameter RequestXML invalid) e.printstacktrace (); responsecode = "002"; Responsexml = ""; String returndata = Returndata (); return returndata;} catch (NullPointerException e) {//002: Error when parameter is empty ((invalid parameter requestxml)) E.printstacktrace (); responsecode = "002"; responsexml = ""; String returndata = Returndata (); return returndata;} catch (Exception e) {e.printstacktrace (); responsecode = "999";//999: Other Error responsexml = ""; String returndata = Returndata (); return returndata;}} Private String Returndata () {StringBuffer returndata = new StringBuffer ("<?xml version=\" 1.0\ "encoding=\" UTF-8\ "?) > "), Returndata.append (" <root> "); Returndata.append (" <responseCode> "); Returndata.append (Responsecode); Returndata.append ("</responseCode>"); Returndata.append ("<responseXML>"); Returndata.append (Responsexml); Returndata.append ("</responseXML>"); Returndata.append ("</root>"); return returndata.tostring ();} Detect user account and interface permissions (Soaphead security) Private String Checkauth (StRing ServiceName) {Boolean Pwisok = False;boolean Authisok = false; String username= "", password= "", address= "", province= "";  Messagecontext Msgcontext = Messagecontext.getcurrentmessagecontext ();      SoapHeader Header = Msgcontext.getenvelope (). GetHeader () Iterator list = Header.getchildren (); while (List.hasnext ()) {      omelement element = (omelement) list.next ();  if (Element.getlocalname (). Equals ("Username")) {Username = Element.gettext ();      } if (Element.getlocalname (). Equals ("Password")) {Password = Element.gettext (); }} if (Password.equals (Inituser.getinituser (). GetUser (Username). GetPassword ())) Pwisok=true;if ( Inituser.getinituser (). GetUser (Username). Getserviceauth (). Contains (("," +servicename+ ",")) Authisok=true; System.out.println ("Step1.checkauth >>>>>>>>> pwisok:" +pwisok+ "Authisok:" +authisok); if (Pwisok && Authisok) return username;else return "";} Control user Query frequency (interface Traffic) Private Boolean checkflow (String Username) {Boolean flowisok = trUe;long nowtime = Calendar.getinstance (). GetTime (). GetTime (); if (Userflowmap.get (Username) ==null) {long[] Userflow = { NOWTIME,1L};//USERFLOW[0] For the initial time, userflow[1] for the period of access Userflowmap.put (Username, userflow); Flowisok = true;} else if (nowtime> (Userflowmap.get (Username) [0]+60000)) {long[] Userflow = {Nowtime,1l};//userflow[0] is the initial time, USERFLOW[1] for period visits Userflowmap.put (Username, userflow); Flowisok = true;} else if (Userflowmap.get (Username) [0]<=nowtime && nowtime<= (Userflowmap.get (Username) [0]+60000)) {if ( Userflowmap.get (Username) [1]<inituser.getinituser (). GetUser (Username). Getflowin1min ()) {long[] Userflow = { Userflowmap.get (Username) [0],userflowmap.get (Username) [1]+1};//visits +1userflowmap.put (Username, Userflow); Flowisok = true;} Else{flowisok = false;}} System.out.println ("Step2.checkflow >>>>>>>>> Flowisok:" +flowisok); return Flowisok;} Executes the query service private string Readrequestxml (String servicename,string requestxml) throws Documentexception, NumberFormatException, NUllpointerexception, Unsupportedencodingexception{system.out.println ("Step3.readrequestxml >>>>> >>>> serviceName: "+servicename);//Select the corresponding Serviceif (servicename.equals (" WAPGW ")) return new Wapgw_service (). ReadXML (RequestXML); if (Servicename.equals ("MMSC")) return new Mmsc_service (). ReadXML (RequestXML); if ( Servicename.equals ("Isag")) return new Isag_service (). ReadXML (RequestXML); if (Servicename.equals ("CRBT")) return new Crbt_service (). ReadXML (RequestXML); else return "";}}


100 Dozens of lines of code, have you seen the halo? I hope you can understand this code patiently and hopefully this code will help you!

Using Axis2 for a long time, I also hope that some breakthroughs. Pre-array rewrite this segment of business, completely give up the AXIS2, directly made a servlet, that is, javaweb system, small, and directly thrown to Tomcat WebApps can run, with a post "WebService" debugging Method (ii) "above the source test , it's no problem. However, failed to contact the client manufacturer in time, because, I have applied for separation. Today is the last day in the company, snatch finishing the WebService series blog, I hope to bring you help. Not deep in the profession, and wish me a bright future ~

Reprint please indicate from whilejolly:http://blog.csdn.net/seedingly/article/details/39055107


"WebService" based on Axis2 design WebService server with Soaphead security mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.