-
-
Save nikhilv/9e02bc7e2ef8c14680ee to your computer and use it in GitHub Desktop.
Java files demonstrating using java to communicate with ElectricCommander
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2012 Electric Cloud, Inc. | |
* | |
* Licensed 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. | |
*/ | |
import java.io.BufferedOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import com.electriccloud.commander.client.requests.CommanderRequest; | |
import com.electriccloud.commander.transport.XmlSerializer; | |
public class CommanderServiceImpl { | |
//~ Instance fields -------------------------------------------------------- | |
private XmlSerializer m_xmlSerializer = new XmlSerializer(); | |
private String m_sessionId = ""; | |
//~ Methods ---------------------------------------------------------------- | |
public byte[] createRequest(CommanderRequest<?> request) | |
throws UnsupportedEncodingException { | |
String actualRequest = createEnvelope(m_xmlSerializer.serialize( | |
request)); | |
return actualRequest.getBytes("UTF-8"); | |
} | |
// Given a string representation of a URL, sets up a connection and gets | |
// an input stream. | |
public InputStream createStream( | |
String urlString, | |
byte[] postData) { | |
URL url = null; | |
try { | |
url = new URL(urlString); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
HttpURLConnection conn; | |
InputStream stream = null; | |
try { | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setDoOutput(true); | |
conn.setRequestProperty("Content-Type", | |
"application/x-www-form-urlencoded"); | |
OutputStream out = new BufferedOutputStream(conn.getOutputStream()); | |
out.write(postData); | |
out.close(); | |
// Starts the query | |
conn.connect(); | |
stream = conn.getInputStream(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return stream; | |
} | |
private String createEnvelope(String xmlFragment) { | |
return | |
"<requests xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"commander.xsd\" version=\"2.2\" timeout=\"180\" sessionId=\"" | |
+ m_sessionId + "\">" + xmlFragment.toString() + "</requests>"; | |
} | |
public String getSessionId() { | |
return m_sessionId; | |
} | |
public void setSessionId(String m_sessionId) { | |
this.m_sessionId = m_sessionId; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.electriccloud.commander.client.CommanderRequestFactory; | |
import com.electriccloud.commander.client.domain.Job; | |
import com.electriccloud.commander.client.domain.ObjectType; | |
import com.electriccloud.commander.client.requests.FindObjectsFilter; | |
import com.electriccloud.commander.client.requests.FindObjectsRequest; | |
import com.electriccloud.commander.client.requests.LoginRequest; | |
import com.electriccloud.commander.client.requests.impl.CommanderRequestFactoryImpl; | |
import com.electriccloud.commander.client.responses.FindObjectsResponse; | |
import com.electriccloud.commander.client.responses.impl.FindObjectsResponseImpl; | |
import com.electriccloud.commander.transport.CommanderObjectImpl; | |
import org.dom4j.DocumentException; | |
import org.dom4j.DocumentHelper; | |
import org.dom4j.Element; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.xml.sax.SAXException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathExpressionException; | |
import javax.xml.xpath.XPathFactory; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
public class Main { | |
private static CommanderRequestFactory m_factory = | |
new CommanderRequestFactoryImpl(); | |
private static List<Job> m_jobs; | |
public static void main(String[] args) throws IOException { | |
InputStream stream = null; | |
String urlString = "http://10.168.11.14:8000"; | |
String m_userName = "admin"; | |
String m_password = "changeme"; | |
CommanderServiceImpl service = new CommanderServiceImpl(); | |
StringBuilder result = new StringBuilder(); | |
try { | |
LoginRequest loginRequest = m_factory.createLoginRequest(); | |
loginRequest.setUserName(m_userName); | |
loginRequest.setPassword(m_password); | |
stream = service.createStream(urlString, | |
service.createRequest(loginRequest)); | |
service.setSessionId(getSessionId(stream)); | |
// Makes sure that the InputStream is closed after the app is | |
// finished using it. | |
} | |
finally { | |
if (stream != null) { | |
stream.close(); | |
} | |
} | |
try { | |
FindObjectsRequest fObjects = m_factory.createFindObjectsRequest( | |
ObjectType.job); | |
fObjects.setMaxIds(20); | |
fObjects.addFilter(new FindObjectsFilter.EqualsFilter( | |
"launchedByUser", m_userName)); | |
stream = service.createStream(urlString, | |
service.createRequest(fObjects)); | |
parseFindObjectsResponse(stream); | |
for (Job job : m_jobs) { | |
result.append(job.getName()); | |
} | |
} | |
finally { | |
if (stream != null) { | |
stream.close(); | |
} | |
} | |
System.out.println(result.toString()); | |
} | |
public static void parseFindObjectsResponse(InputStream httpResponse) | |
{ | |
org.dom4j.Document doc = null; | |
try { | |
doc = DocumentHelper.parseText(parseResponse(httpResponse)); | |
} | |
catch (DocumentException e) { | |
e.printStackTrace(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
org.dom4j.Node n = doc.selectSingleNode("//response"); | |
CommanderObjectImpl co = new CommanderObjectImpl((Element) n); | |
FindObjectsResponse response = new FindObjectsResponseImpl( | |
ObjectType.job, co); | |
m_jobs = response.getJobs(); | |
} | |
private static String parseResponse(InputStream is) | |
throws IOException | |
{ | |
// read it with BufferedReader | |
BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line) | |
.append("\n"); | |
} | |
br.close(); | |
return sb.toString(); | |
} | |
public static String getSessionId(InputStream httpResponse) | |
{ | |
Document document = null; | |
Node node = null; | |
try { | |
DocumentBuilderFactory docFactory = DocumentBuilderFactory | |
.newInstance(); | |
DocumentBuilder builder = docFactory.newDocumentBuilder(); | |
document = builder.parse(httpResponse); | |
XPath xpath = XPathFactory.newInstance() | |
.newXPath(); | |
String expression = "//sessionId"; | |
node = (Node) xpath.evaluate(expression, document, | |
XPathConstants.NODE); | |
} | |
catch (XPathExpressionException e) { | |
e.printStackTrace(); | |
} | |
catch (SAXException e) { | |
e.printStackTrace(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
catch (ParserConfigurationException e) { | |
e.printStackTrace(); | |
} | |
if (node != null) { | |
return node.getTextContent(); | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment