diff options
author | pikusa <pikusa@man.poznan.pl> | 2013-04-03 13:18:17 (GMT) |
---|---|---|
committer | pikusa <pikusa@man.poznan.pl> | 2013-04-03 13:18:17 (GMT) |
commit | 2f2a3a129c91de540e66c3bfbe30b0df1942cd4b (patch) | |
tree | 2d313cdf0068af368d4de6067d676be16f6a6464 /communications/src/main | |
parent | ff8aa232b071a9b54dff833714a870fd0aec0b30 (diff) | |
download | novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.zip novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.gz novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.bz2 |
project commit and dir tree change
Diffstat (limited to 'communications/src/main')
-rw-r--r-- | communications/src/main/java/eu/novi/connection/RESTCommunication.java | 84 | ||||
-rw-r--r-- | communications/src/main/java/eu/novi/connection/SSHConnection.java | 172 |
2 files changed, 256 insertions, 0 deletions
diff --git a/communications/src/main/java/eu/novi/connection/RESTCommunication.java b/communications/src/main/java/eu/novi/connection/RESTCommunication.java new file mode 100644 index 0000000..48d5868 --- /dev/null +++ b/communications/src/main/java/eu/novi/connection/RESTCommunication.java @@ -0,0 +1,84 @@ +package eu.novi.connection; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; + +public class RESTCommunication { + + private String response = ""; + + + /** + * Methods executes POST communication on the given address where REST service is running. + * @param restAddress URL address of the REST service to call. For example http://planetlab:8080/request/handler/delete + * @param requestParameters The pairs contains keys and values of the request parameters. For example for POST message in URL "http://planetlab:8080/request/handler/delete?sliceId=1234" the key is "sliceId" and + * @throws HttpException + * @throws IOException + */ + public void executePostMethod(String restAddress, Map<String, String> requestParameters) throws HttpException, IOException { + PostMethod postMethod = preparePostMethod(restAddress, requestParameters); + response = getResponseFromService(postMethod); + } + + /** + * Prepare POST method with the given parameters + * @param restAddress + * @param key + * @param value + * @return + */ + private PostMethod preparePostMethod(String restAddress, Map<String, String> requestParameters) { + Part[] parts = generateParts(requestParameters); + PostMethod postMethod = new PostMethod(restAddress); + postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); + + return postMethod; + } + + private Part[] generateParts(Map<String, String> keyValuePairs) { + Part[] parts = new Part[keyValuePairs.size()]; + int i = 0; + for(String key : keyValuePairs.keySet()){ + parts[i++] = new StringPart(key, keyValuePairs.get(key)); + } + return parts; + } + + private String getResponseFromService(PostMethod postMethod) throws HttpException, IOException { + String resp = ""; + final HttpClient client = new HttpClient(); + if (client.executeMethod(postMethod) != -1) { + resp = getResponseFromStream(postMethod.getResponseBodyAsStream()); + postMethod.releaseConnection(); + } + return resp; + } + + + public String getResponseFromStream(InputStream responseBodyAsStream) throws IOException{ + BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyAsStream)); + StringBuilder responseBuilder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + responseBuilder.append(line); + } + return responseBuilder.toString(); + } + + + public String getResponse() { + return response; + } + +} diff --git a/communications/src/main/java/eu/novi/connection/SSHConnection.java b/communications/src/main/java/eu/novi/connection/SSHConnection.java new file mode 100644 index 0000000..caf93a1 --- /dev/null +++ b/communications/src/main/java/eu/novi/connection/SSHConnection.java @@ -0,0 +1,172 @@ +package eu.novi.connection; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.jcraft.jsch.Channel; +import com.jcraft.jsch.ChannelExec; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; + + +public class SSHConnection { + + private static final Logger logger = LoggerFactory.getLogger(SSHConnection.class); + + private BufferedReader br; + private InputStream in; + private Session session; + private Channel channel; + private boolean connectedStatus = false; + private static final int SSHPORT = 22; + private static final int TIMEOUT = 30000; + private static final int AVAILABLE_BYTES = 1024; + + /** + * + * @return + */ + public boolean connected() { + return connectedStatus; + } + + /** + * + * @param user + * @param host + * @param password + * @param command + * @throws JSchException + * @throws Exception + */ + public void executeCommandOnHostWithPassword(String user, String host, String password, String command) + throws JSchException, Exception { + + JSch jsch = new JSch(); + session = jsch.getSession(user, host, SSHPORT); + session.setPassword(password); + session.setConfig("StrictHostKeyChecking", "no"); + session.connect(TIMEOUT); + executeCommandInSession(session, command); + } + + /** + * + * @param user + * @param host + * @param sshKeyLocation + * @param command + * @param + * @throws JSchException + * @throws Exception + */ + public void executeCommandOnHostWithKey(String user, String host, String sshKeyLocation, String command) + throws JSchException, Exception { + JSch jsch = new JSch(); + jsch.addIdentity(sshKeyLocation); + session = jsch.getSession(user, host, SSHPORT); + session.setConfig("StrictHostKeyChecking", "no"); + session.connect(TIMEOUT); + executeCommandInSession(session, command); + } + + private void executeCommandInSession(Session session, String command) throws JSchException, Exception { + channel = session.openChannel("exec"); + ((ChannelExec) channel).setCommand(command); + ((ChannelExec) channel).setErrStream(System.out); + in = channel.getInputStream(); + br = new BufferedReader(new InputStreamReader(in)); + channel.connect(); + connectedStatus = true; + } + + /** + * Reads the output stream that. It always reads the InputStream.available() amount of bytes. If there is expected more + * (for example when "tail -f" command is executed, than this method should be called many times. + * + * + * @param bytes + * = number of bytes to read. If bytes<0, then default value is + * InputStream.available() + * @return output If there is no mroe bytes that can be read the output="" + */ + public String readOutput(int bytes) throws IOException { + String result = ""; + if (in.available() > 0) { + byte[] tmp = new byte[AVAILABLE_BYTES]; + int i = in.read(tmp, 0, AVAILABLE_BYTES); + if (i > 0){ + result = (new String(tmp, 0, i)); + } + } + if (channel.isClosed() || !channel.isConnected()) { + disconnect(); + } + return result; + + } + + + public String readOutputLine() throws IOException { + String result = ""; + try{ + result = br.readLine(); + if (result == null) { + disconnect(); + return ""; + } + } catch (IOException e) { + //disconnect(); + throw e; + } + if (channel.isClosed()) { + logger.info("exit-status: " + channel.getExitStatus()); + disconnect(); + } + + return result; + } + + public String readOutputLineWhenBufferReady() throws IOException { + String result = ""; + try{ + if(!br.ready()){ + disconnect(); + return ""; + } + result = br.readLine(); + if (result == null) { + disconnect(); + return ""; + } + } catch (IOException e) { + disconnect(); + throw e; + } + if (channel.isClosed()) { + logger.info("exit-status: " + channel.getExitStatus()); + disconnect(); + } + + return result; + } + + /** + * Close the connection that is hosted by the object of this class. + * @throws IOException + */ + public void disconnect() throws IOException { + in.close(); + br.close(); + channel.disconnect(); + session.disconnect(); + connectedStatus = false; + } +} |