/** * Copyright Promptu Systems Corporation 2016. All rights reserved. */ package bugzilla.rest; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Logger; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import com.google.gson.JsonElement; import com.google.gson.JsonParser; /** * A session that manages the credentials used while communicating with a Bugzilla instance. * * https://wiki.mozilla.org/Bugzilla:REST_API * * @author Warwick Hunter * @since 2016-11-26 */ public class BugzillaSession { private static boolean VERBOSE = false; private final HttpClient m_httpClient = new HttpClient(); private final String m_url; private String m_token; private final Logger m_logger = Logger.getLogger("p4bugzilla"); public BugzillaSession(String url) { m_url = url; } public void login(String username, String password) throws HttpException, IOException { if (username == null || username.length() < 1 || password == null || password.length() < 1) { throw new RuntimeException("Bugzilla username or password cannot be null"); } HttpMethod method = new GetMethod(m_url + "/login"); try { NameValuePair[] args = { new NameValuePair(BugzillaConst.LOGIN, username), new NameValuePair(BugzillaConst.PASSWORD, password) }; method.setQueryString(args); if (VERBOSE) { m_logger.info("request " + method.getURI().toString()); } int statusCode = m_httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new SecurityException("Login request failed: " + method.getStatusLine()); } JsonParser jsonParser = new JsonParser(); JsonElement jelt = jsonParser.parse(new InputStreamReader(method.getResponseBodyAsStream())); if (VERBOSE) { m_logger.info("response " + jelt); } if (jelt.getAsJsonObject() != null && jelt.getAsJsonObject().has(BugzillaConst.TOKEN)) { m_token = jelt.getAsJsonObject().get("token").getAsString(); if (VERBOSE) { m_logger.info("token " + m_token); } } else { throw new SecurityException("Login request failed, no token returned in response: " + jelt.toString()); } } finally { method.releaseConnection(); } } public HttpClient getHttpClient() { return m_httpClient; } public String getUrl() { return m_url; } @Override public String toString() { return String.format("BugzillaSession [m_httpClient=%s, m_url=%s, m_token=%s]", m_httpClient, m_url, m_token); } public String getToken() { return m_token; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 21161 | Warwick Hunter |
P4 Bugzilla v2.1 - Use the new Bugzilla REST API instead of the old XMLRPC API. - Use the latest P4 Java API. - Build with gradle and gradlew. - Integrate with the latest systemd Linux daemon startup environment found on Fedora 21+ systems. - Simplified the code to focus on just the job at hand. |