BUGZILLA4 XMLRPC Interface API call Implementation sharing: XMLRPC + HTTPS + cookies + httpclient +bugzilla + Java to implement encrypted communication under the XMLRPC interface call and resolve the login hold session function

Source: Internet
Author: User

XMLRPC, HTTPS, cookies, httpclient, Bugzilla, Java implementation of encrypted communication under the XMLRPC interface call and resolve the login to maintain the session function, the network for Bugzilla implementation of the very few, For XMLRPC have but basically are HTTP protocol, HTTPS authentication processing is more troublesome, and the session is still basic not too much sharing, so I decided to combine Xmlrpc\bugzilla official documents, netizens articles, combined with personal experience summed up, has been debugged on the window2007 64+jdk7 bit machine

Hands-on teaching you how to achieve:

The first step:

Create a MAVEN project in Eclipse, add RPC dependency packages between <dependencies> and </dependencies> in the Pom.xml file (if not MAVEN engineering, Go directly to the Download Jar package introduction):

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.xml.rpc</artifactId>
<version>3.2-b06</version>
</dependency>

The second step: Create a new base class Bugzillarpcutil.java, perform basic SSL authentication processing, parameter encapsulation, call encapsulation, etc.


Import java.io.IOException;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.net.URLConnection;
Import Java.security.cert.X509Certificate;
Import Java.util.HashMap;
Import Java.util.LinkedHashMap;
Import Java.util.Map;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;

Import Javax.net.ssl.HostnameVerifier;
Import javax.net.ssl.HttpsURLConnection;
Import Javax.net.ssl.SSLContext;
Import javax.net.ssl.SSLSession;
Import Javax.net.ssl.TrustManager;
Import Javax.net.ssl.X509TrustManager;

Import org.apache.xmlrpc.XmlRpcException;
Import Org.apache.xmlrpc.XmlRpcRequest;
Import org.apache.xmlrpc.client.XmlRpcClient;
Import Org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
Import org.apache.xmlrpc.client.XmlRpcClientException;
Import Org.apache.xmlrpc.client.XmlRpcSunHttpTransport;
Import Org.apache.xmlrpc.client.XmlRpcTransport;
Import Org.apache.xmlrpc.client.XmlRpcTransportFactory;


public class Bugzillarpcutil
{

private static xmlrpcclient client = NULL;

Very Simple Cookie Storage
Private final static linkedhashmap<string, string> cookies = new linkedhashmap<string, string> ();

Private hashmap<string, object> parameters = new hashmap<string, object> ();

Private String command;

Path to Bugzilla XML-RPC interface

Note https://bugzilla.tools.vipshop.com/bugzilla/This part of the URL to be replaced with your own domain address, as well as Bugzilla's account and password

private static final String ServerURL = "https://bugzilla.tools.vipshop.com/bugzilla/xmlrpc.cgi";

/**
* Creates a new instance of the Bugzilla xml-rpc command executor for a specific command
*
* @param command A remote method associated with the instance of RPC call executor
*/
Public bugzillarpcutil (String command)
{
Synchronized (This)
{
This.command = command;
if (client = = NULL)
{//assure the initialization is do only once
Client = new Xmlrpcclient ();
Xmlrpcclientconfigimpl config = new Xmlrpcclientconfigimpl ();
Try
{
Config.setserverurl (New URL (ServerURL));
}
catch (Malformedurlexception ex)
{
Logger.getlogger (BugzillaRPCUtil.class.getName ()). log (Level.severe, NULL, ex);
}
Xmlrpctransportfactory factory = new Xmlrpctransportfactory ()
{

Public Xmlrpctransport Gettransport ()
{
return new Xmlrpcsunhttptransport (client)
{

Private URLConnection Conn;

@Override
Protected URLConnection newurlconnection (URL PURL)
Throws IOException
{
conn = Super.newurlconnection (PURL);
Return conn;
}

@Override
protected void Inithttpheaders (Xmlrpcrequest prequest)
Throws Xmlrpcclientexception
{
Super.inithttpheaders (prequest);
Setcookies (conn);
}

@Override
protected void Close ()
Throws Xmlrpcclientexception
{
GetCookies (conn);
}

private void Setcookies (URLConnection pconn)
{
String cookiestring = "";
For (String CookieName:cookies.keySet ())
{
Cookiestring + = ";" + cookiename + "=" + Cookies.get (cookiename);
}
if (Cookiestring.length () > 2)
{
setRequestHeader ("Cookie", cookiestring.substring (2));
}
}

private void GetCookies (URLConnection pconn)
{
String headername = null;
for (int i = 1; (Headername = Pconn.getheaderfieldkey (i))! = NULL; i++)
{
if (Headername.equals ("Set-cookie"))
{
String cookie = Pconn.getheaderfield (i);
Cookie = cookie.substring (0, Cookie.indexof (";"));
String cookiename = cookie.substring (0, cookie.indexof ("="));
String cookievalue = cookie.substring (cookie.indexof ("=") + 1, cookie.length ());
Cookies.put (CookieName, cookievalue);
}
}
}
};
}
};
Client.settransportfactory (Factory);
Client.setconfig (config);
}
}
}

/**
* Get The parameters of this call, that were set using Setparameter method
*
* @return Array with a parameter HashMap
*/
Protected object[] GetParameters ()
{
return new object[] {parameters};
}

/**
* Set parameter to a given value
*
* @param name Name of the parameter to be set
* @param value A value of the parameter to is set
* @return Previous value of the parameter, if it was set already.
*/
Public Object Setparameter (String name, object value)
{
Return This.parameters.put (name, value);
}

/**
* Executes the XML-RPC call to Bugzilla instance and returns a map with result
*
* @return A Map with response
* @throws xmlrpcexception
*/
Public Map Execute ()
Throws Xmlrpcexception
{
Return (MAP) client.execute (command, This.getparameters ());
}

public static void Initssl ()
Throws Exception
{

Create a trust manager that does not validate certificate chains
trustmanager[] Trustallcerts = new trustmanager[] {new X509trustmanager ()
{
Public x509certificate[] Getacceptedissuers ()
{
return null;
}

public void checkclienttrusted (x509certificate[] certs, String authtype)
{
Trust always
}

public void checkservertrusted (x509certificate[] certs, String authtype)
{
Trust always
}
}};

Install the All-trusting trust manager
Sslcontext sc = sslcontext.getinstance ("SSL");
Create Empty Hostnameverifier
hostnameverifier HV = new Hostnameverifier ()
{
public boolean verify (String arg0, sslsession arg1)
{
return true;
}
};

Sc.init (NULL, Trustallcerts, New Java.security.SecureRandom ());
Httpsurlconnection.setdefaultsslsocketfactory (Sc.getsocketfactory ());
Httpsurlconnection.setdefaulthostnameverifier (HV);

}
}

Step three: Create a new interface call class Bugzillalogincall.java implement login, Inherit Bugzillarpcutil class


Import Java.util.Map;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;

Import org.apache.xmlrpc.XmlRpcException;

public class Bugzillalogincall extends Bugzillarpcutil
{

/**
* Create a Bugzilla login call instance and set parameters
*/
Public Bugzillalogincall (string Username, string password)
{
Super ("User.login");
Setparameter ("Login", username);
Setparameter ("password", password);
}

/**
* Perform the login action and set the login cookie
*
* @returns True If login is successful, false otherwise. The method sets Bugzilla login cookies.
*/
public static Boolean login (string username, string password)
{
Map result = null;
Try
{
The result should contain one item with an ID of logged in user
result = new Bugzillalogincall (username, password). Execute ();
}
catch (Xmlrpcexception ex)
{
Logger.getlogger (BugzillaLoginCall.class.getName ()). log (Level.severe, NULL, ex);
}
Generally, this is the Initialize model class from the result map
Return! (result = = NULL | | result.isempty ());
}

}

Third = Four step: Create a new interface call class Bugzillagetusercall.java implement query user information, inherit Bugzillarpcutil class


Import Java.util.Map;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;

Import org.apache.xmlrpc.XmlRpcException;

public class Bugzillagetusercall extends Bugzillarpcutil
{

/**
* Create a Bugzilla login call instance and set parameters
*/
Public Bugzillagetusercall (String IDs)
{
Super ("User.get");
Setparameter ("IDs", IDS);
Setparameter ("password", password);
}

/**
* Perform the login action and set the login cookie
*
* @returns True If login is successful, false otherwise. The method sets Bugzilla login cookies.
*/
public static Map GetUserInfo (String IDs)
{
Map result = null;
Try
{
The result should contain one item with an ID of logged in user
result = new Bugzillagetusercall (IDS). Execute ();

}
catch (Xmlrpcexception ex)
{
Logger.getlogger (BugzillaLoginCall.class.getName ()). log (Level.severe, NULL, ex);
}
Generally, this is the Initialize model class from the result map
return result;
}

}

Fifth step: Create a new run class Bugzillaapi.java and output the returned results

Import Java.util.HashMap;
Import Java.util.Map;

/**
* 20150608
*
* @author Sea.zeng
*
*/
public class Bugzillaapi
{
@SuppressWarnings ({"Rawtypes"})
public static void Main (string[] args)
Throws Exception
{
String username = new String ("Your Bugzilla account");
String password = new String ("Your Bugzilla password");

Bugzillarpcutil.initssl ();

Boolean r = Bugzillalogincall.login (username, password);

System.out.println ("r=" + R);

String ids = new String ("1603");

Map map = (HASHMAP) bugzillagetusercall.getuserinfo (IDS);
ID Real_name Email Name
System.out.println (Map.tostring ());



Object usersobject = Map.get ("users");
System.out.println (Usersobject.tostring ());

Object[] Usersobjectarray = (object[]) usersobject;
Map UserMap = (HASHMAP) usersobjectarray[0];
System.out.println (Usermap.tostring ());

Map r2 = UserMap;
System.out.println ("r2.id=" + r2.get ("id") + ", r2.real_name=" + r2.get ("real_name") + ", r2.email="
+ r2.get ("email") + ", r2.name=" + r2.get ("name"));
}
}

In the spirit of the principle of sharing resources, welcome all friends on this basis to improve and further share, let us achieve more elegant. If you have any questions and need further communication can add my QQ 1922003019 or directly send QQ mail to me to communicate

Sea 20150608 China: Guangzhou: VIP

BUGZILLA4 XMLRPC Interface API call Implementation sharing: XMLRPC + HTTPS + cookies + httpclient +bugzilla + Java to implement encrypted communication under the XMLRPC interface call and resolve the login hold session function

Related Article

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.