有没有 Java API 可以访问 Bugzilla?

7

是否有一个(独立的!)Java API可以将XML-RPC接口包装到bugzilla中?我不想为此编写自己的api,而且我实际上找不到只做这个的库。

更新:

我正在寻找类似于这个http://oss.dbc.dk/bugzproxy/的东西,只不过是用Java编写的。

7个回答

9

这是一个很好的概述,只不过很不幸大多数库都不够完整。 - Mauli

5

有一个名为Apache WS XML-RPC的完整XML-RPC实现,您可以使用它。我不太了解BugZilla,但假设其支持XML-RPC,则使用我刚刚提到的全称可能没有任何问题。


2

2

2

这个库/接口被称为JAX-WS(或JAXB),可以让你调用任何类型的Web服务。获取模式,生成bean和代理,然后调用它们即可。


1

对于您来说,Mylyn可能是一个不错的选择。

如果您需要更简单的设置或更好地控制事物发生的方式,则可以编写自己的XML-RPC调用以与Bugzilla Web服务接口通信。我在我的博客上概述了该过程: 使用Apache XML-RPC从Java聊天到Bugzilla

总之:

  • 获取Apache XML-RPC库
  • 获取commons中的Apache HTTP Client(旧版本)

然后使用以下类作为基类(它处理cookie等),并覆盖它:

/**
 * @author joshis_tweets
 */
public class BugzillaAbstractRPCCall {

    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
    private static final String BZ_PATH = "https://localhost/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 this instance of RPC call executor
     */
    public BugzillaAbstractRPCCall(String command) {
        synchronized (this) {
            this.command = command;
            if (client == null) { // assure the initialization is done only once
                client = new XmlRpcClient();
                XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
                try {
                    config.setServerURL(new URL(BZ_PATH));
                } catch (MalformedURLException ex) {
                    Logger.getLogger(BugzillaAbstractRPCCall.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 be 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 class BugzillaLoginCall extends BugzillaAbstractRPCCall {

    /**
     * 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 cookies
     * @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 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 place to initialize model class from the result map
        return !(result == null || result.isEmpty());
    }

}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接