List of usage examples for org.dom4j QName get
public static QName get(String qualifiedName, String uri)
From source file:com.xebialabs.overthere.cifs.winrm.soap.HeaderBuilder.java
License:Open Source License
public HeaderBuilder withOptionSet(List<KeyValuePair> options) { final Element optionSet = header.addElement(QName.get("OptionSet", NS_WSMAN_DMTF)); for (KeyValuePair p : options) { optionSet.addElement(QName.get("Option", NS_WSMAN_DMTF)).addAttribute("Name", p.getKey()) .addText(p.getValue());/*from ww w . ja v a2 s .com*/ } return this; }
From source file:com.xebialabs.overthere.cifs.winrm.soap.SoapMessageBuilder.java
License:Open Source License
public EnvelopeBuilder envelope() { Element envelope = doc.addElement(QName.get("Envelope", NS_SOAP_ENV)); return new EnvelopeBuilder(envelope); }
From source file:com.xebialabs.overthere.cifs.winrm.WinRmClient.java
License:Open Source License
private void cleanUp() { if (commandId == null) return;//ww w . j a v a 2s . c o m logger.debug("cleanUp shellId {} commandId {} ", shellId, commandId); final Element bodyContent = DocumentHelper.createElement(QName.get("Signal", Namespaces.NS_WIN_SHELL)) .addAttribute("CommandId", commandId); bodyContent.addElement(QName.get("Code", Namespaces.NS_WIN_SHELL)) .addText("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate"); final Document requestDocument = getRequestDocument(Action.WS_SIGNAL, ResourceURI.RESOURCE_URI_CMD, null, shellId, bodyContent); @SuppressWarnings("unused") Document responseDocument = sendMessage(requestDocument, SoapAction.SIGNAL); }
From source file:com.xebialabs.overthere.cifs.winrm.WinRmClient.java
License:Open Source License
private void getCommandOutput(OverthereProcessOutputHandler handler) { logger.debug("getCommandOutput shellId {} commandId {} ", shellId, commandId); final Element bodyContent = DocumentHelper.createElement(QName.get("Receive", Namespaces.NS_WIN_SHELL)); bodyContent.addElement(QName.get("DesiredStream", Namespaces.NS_WIN_SHELL)) .addAttribute("CommandId", commandId).addText("stdout stderr"); final Document requestDocument = getRequestDocument(Action.WS_RECEIVE, ResourceURI.RESOURCE_URI_CMD, null, shellId, bodyContent);//ww w . j a v a 2s . c om for (;;) { Document responseDocument = sendMessage(requestDocument, SoapAction.RECEIVE); String stdout = handleStream(responseDocument, ResponseExtractor.STDOUT); BufferedReader stdoutReader = new BufferedReader(new StringReader(stdout)); try { for (;;) { String line = stdoutReader.readLine(); if (line == null) { break; } handler.handleOutputLine(line); } } catch (IOException exc) { throw new RuntimeIOException("Unexpected I/O exception while reading stdout", exc); } String stderr = handleStream(responseDocument, ResponseExtractor.STDERR); BufferedReader stderrReader = new BufferedReader(new StringReader(stderr)); try { for (;;) { String line = stderrReader.readLine(); if (line == null) { break; } handler.handleErrorLine(line); } } catch (IOException exc) { throw new RuntimeIOException("Unexpected I/O exception while reading stderr", exc); } if (chunk == 0) { try { exitCode = getFirstElement(responseDocument, ResponseExtractor.EXIT_CODE); logger.info("exit code {}", exitCode); } catch (Exception e) { logger.debug("not found"); } } chunk++; /* We may need to get additional output if the stream has not finished. The CommandState will change from Running to Done like so: @example from... <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running"/> to... <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"> <rsp:ExitCode>0</rsp:ExitCode> </rsp:CommandState> */ final List<?> list = ResponseExtractor.STREAM_DONE.getXPath().selectNodes(responseDocument); if (!list.isEmpty()) { exitCode = getFirstElement(responseDocument, ResponseExtractor.EXIT_CODE); logger.info("exit code {}", exitCode); break; } } logger.debug("all the command output has been fetched (chunk={})", chunk); }
From source file:com.xebialabs.overthere.cifs.winrm.WinRmClient.java
License:Open Source License
private String runCommand(String command) { logger.debug("runCommand shellId {} command {}", shellId, command); final Element bodyContent = DocumentHelper.createElement(QName.get("CommandLine", Namespaces.NS_WIN_SHELL)); String encoded = command;/*from w w w .ja v a 2s.com*/ encoded = "\"" + encoded + "\""; logger.info("Encoded command is {}", encoded); bodyContent.addElement(QName.get("Command", Namespaces.NS_WIN_SHELL)).addText(encoded); final Document requestDocument = getRequestDocument(Action.WS_COMMAND, ResourceURI.RESOURCE_URI_CMD, OptionSet.RUN_COMMAND, shellId, bodyContent); Document responseDocument = sendMessage(requestDocument, SoapAction.COMMAND_LINE); return getFirstElement(responseDocument, ResponseExtractor.COMMAND_ID); }
From source file:com.xebialabs.overthere.cifs.winrm.WinRmClient.java
License:Open Source License
private String openShell() { logger.debug("openShell"); final Element bodyContent = DocumentHelper.createElement(QName.get("Shell", Namespaces.NS_WIN_SHELL)); bodyContent.addElement(QName.get("InputStreams", Namespaces.NS_WIN_SHELL)).addText("stdin"); bodyContent.addElement(QName.get("OutputStreams", Namespaces.NS_WIN_SHELL)).addText("stdout stderr"); final Document requestDocument = getRequestDocument(Action.WS_ACTION, ResourceURI.RESOURCE_URI_CMD, OptionSet.OPEN_SHELL, null, bodyContent); Document responseDocument = sendMessage(requestDocument, SoapAction.SHELL); return getFirstElement(responseDocument, ResponseExtractor.SHELL_ID); }
From source file:com.xebialabs.overthere.winrm.WinRmClient.java
License:Open Source License
public String createShell() { logger.debug("Sending WinRM Create Shell request"); final Element bodyContent = DocumentHelper.createElement(QName.get("Shell", Namespaces.NS_WIN_SHELL)); bodyContent.addElement(QName.get("InputStreams", Namespaces.NS_WIN_SHELL)).addText("stdin"); bodyContent.addElement(QName.get("OutputStreams", Namespaces.NS_WIN_SHELL)).addText("stdout stderr"); final Document requestDocument = getRequestDocument(Action.WS_ACTION, ResourceURI.RESOURCE_URI_CMD, OptionSet.OPEN_SHELL, bodyContent); Document responseDocument = sendRequest(requestDocument, SoapAction.SHELL); shellId = getFirstElement(responseDocument, ResponseExtractor.SHELL_ID); logger.debug("Received WinRM Create Shell response: shell with ID {} start created", shellId); return shellId; }
From source file:com.xebialabs.overthere.winrm.WinRmClient.java
License:Open Source License
public String executeCommand(String command) { logger.debug("Sending WinRM Execute Command request to shell {}", shellId); final Element bodyContent = DocumentHelper.createElement(QName.get("CommandLine", Namespaces.NS_WIN_SHELL)); String encoded = "\"" + command + "\""; bodyContent.addElement(QName.get("Command", Namespaces.NS_WIN_SHELL)).addText(encoded); final Document requestDocument = getRequestDocument(Action.WS_COMMAND, ResourceURI.RESOURCE_URI_CMD, OptionSet.RUN_COMMAND, bodyContent); Document responseDocument = sendRequest(requestDocument, SoapAction.COMMAND_LINE); commandId = getFirstElement(responseDocument, ResponseExtractor.COMMAND_ID); logger.debug("Received WinRM Execute Command response to shell {}: command with ID {} was started", shellId, commandId);//from www . j a va 2 s . c o m return commandId; }
From source file:com.xebialabs.overthere.winrm.WinRmClient.java
License:Open Source License
public boolean receiveOutput(OutputStream stdout, OutputStream stderr) throws IOException { logger.debug("Sending WinRM Receive Output request for command {} in shell {}", commandId, shellId); final Element bodyContent = DocumentHelper.createElement(QName.get("Receive", Namespaces.NS_WIN_SHELL)); bodyContent.addElement(QName.get("DesiredStream", Namespaces.NS_WIN_SHELL)) .addAttribute("CommandId", commandId).addText("stdout stderr"); final Document requestDocument = getRequestDocument(Action.WS_RECEIVE, ResourceURI.RESOURCE_URI_CMD, null, bodyContent);/*from w w w . j av a 2 s . c o m*/ Document responseDocument = sendRequest(requestDocument, SoapAction.RECEIVE); logger.debug("Received WinRM Receive Output response for command {} in shell {}", commandId, shellId); handleStream(responseDocument, ResponseExtractor.STDOUT, stdout); handleStream(responseDocument, ResponseExtractor.STDERR, stderr); if (chunk == 0) { parseExitCode(responseDocument); } chunk++; /* * We may need to get additional output if the stream has not finished. The CommandState will change from * Running to Done like so: * * @example * * from... <rsp:CommandState CommandId="..." * State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running"/> to... * <rsp:CommandState CommandId="..." * State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"> * <rsp:ExitCode>0</rsp:ExitCode> </rsp:CommandState> */ final List<?> list = ResponseExtractor.STREAM_DONE.getXPath().selectNodes(responseDocument); if (!list.isEmpty()) { logger.trace("Found CommandState element with State=Done, parsing exit code and returning false."); parseExitCode(responseDocument); return false; } else { logger.trace("Did not find CommandState element with State=Done, returning true."); return true; } }
From source file:com.xebialabs.overthere.winrm.WinRmClient.java
License:Open Source License
public void sendInput(byte[] buf) throws IOException { logger.debug("Sending WinRM Send Input request for command {} in shell {}", commandId, shellId); final Element bodyContent = DocumentHelper.createElement(QName.get("Send", Namespaces.NS_WIN_SHELL)); final Base64 base64 = new Base64(); bodyContent.addElement(QName.get("Stream", Namespaces.NS_WIN_SHELL)).addAttribute("Name", "stdin") .addAttribute("CommandId", commandId).addText(base64.encodeAsString(buf)); final Document requestDocument = getRequestDocument(Action.WS_SEND, ResourceURI.RESOURCE_URI_CMD, null, bodyContent);/* w ww .jav a2 s . c o m*/ sendRequest(requestDocument, SoapAction.SEND); logger.debug("Sent WinRM Send Input request for command {} in shell {}", commandId, shellId); }